mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:38:10 +00:00
LibHTML: Start adding support for <link rel="stylesheet">
This patch adds basic support for external stylesheets. It currently only works with file:// URLs. We do a synchronous full relayout after loading a stylesheet, which is definitely on the aggressive side, but it gives us something to work on improving. :^)
This commit is contained in:
parent
c136fd3fe2
commit
71e8ddcd1c
8 changed files with 89 additions and 1 deletions
41
Libraries/LibHTML/DOM/HTMLLinkElement.cpp
Normal file
41
Libraries/LibHTML/DOM/HTMLLinkElement.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include <AK/URL.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/DOM/HTMLLinkElement.h>
|
||||
#include <LibHTML/Parser/CSSParser.h>
|
||||
|
||||
HTMLLinkElement::HTMLLinkElement(Document& document, const String& tag_name)
|
||||
: HTMLElement(document, tag_name)
|
||||
{
|
||||
}
|
||||
|
||||
HTMLLinkElement::~HTMLLinkElement()
|
||||
{
|
||||
}
|
||||
|
||||
void HTMLLinkElement::inserted_into(Node&)
|
||||
{
|
||||
if (rel() == "stylesheet") {
|
||||
URL url = document().complete_url(href());
|
||||
if (url.protocol() != "file") {
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
auto file = CFile::construct(url.path());
|
||||
if (!file->open(CIODevice::ReadOnly)) {
|
||||
dbg() << "Failed to open " << url.to_string();
|
||||
ASSERT_NOT_REACHED();
|
||||
return;
|
||||
}
|
||||
auto data = file->read_all();
|
||||
auto sheet = parse_css(String::copy(data));
|
||||
|
||||
if (!sheet) {
|
||||
dbg() << "Failed to parse " << url.to_string();
|
||||
ASSERT_NOT_REACHED();
|
||||
return;
|
||||
}
|
||||
|
||||
document().add_sheet(*sheet);
|
||||
document().invalidate_layout();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue