1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:47:35 +00:00

LibWeb: Add actual document loading for the CSS (at)import rule

This commit is contained in:
Sviatoslav Peleshko 2021-02-21 18:44:17 +02:00 committed by Andreas Kling
parent 54617e1a91
commit 183ebaee91
11 changed files with 229 additions and 56 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -35,7 +36,11 @@ namespace Web::HTML {
HTMLLinkElement::HTMLLinkElement(DOM::Document& document, QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
, m_css_loader(document)
{
m_css_loader.on_load = [&] {
document.update_style();
};
}
HTMLLinkElement::~HTMLLinkElement()
@ -46,44 +51,10 @@ void HTMLLinkElement::inserted_into(Node& node)
{
HTMLElement::inserted_into(node);
if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate))
load_stylesheet(document().complete_url(href()));
}
void HTMLLinkElement::resource_did_fail()
{
}
void HTMLLinkElement::resource_did_load()
{
VERIFY(resource());
if (!resource()->has_encoded_data())
return;
dbgln("HTMLLinkElement: Resource did load, looks good! {}", href());
auto sheet = parse_css(CSS::ParsingContext(document()), resource()->encoded_data());
if (!sheet) {
dbgln("HTMLLinkElement: Failed to parse stylesheet: {}", href());
return;
if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
m_css_loader.load_from_url(document().complete_url(href()));
document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull());
}
// Transfer the rules from the successfully parsed sheet into the sheet we've already inserted.
m_style_sheet->rules() = sheet->rules();
document().update_style();
}
void HTMLLinkElement::load_stylesheet(const URL& url)
{
// First insert an empty style sheet in the document sheet list.
// There's probably a nicer way to do this, but this ensures that sheets are in document order.
m_style_sheet = CSS::StyleSheet::create({});
document().style_sheets().add_sheet(*m_style_sheet);
LoadRequest request;
request.set_url(url);
set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
}
void HTMLLinkElement::parse_attribute(const FlyString& name, const String& value)

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,13 +28,11 @@
#pragma once
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Loader/Resource.h>
#include <LibWeb/Loader/CSSLoader.h>
namespace Web::HTML {
class HTMLLinkElement final
: public HTMLElement
, public ResourceClient {
class HTMLLinkElement final : public HTMLElement {
public:
using WrapperType = Bindings::HTMLLinkElementWrapper;
@ -47,14 +46,8 @@ public:
String href() const { return attribute(HTML::AttributeNames::href); }
private:
// ^ResourceClient
virtual void resource_did_fail() override;
virtual void resource_did_load() override;
void parse_attribute(const FlyString&, const String&) override;
void load_stylesheet(const URL&);
struct Relationship {
enum {
Alternate = 1 << 0,
@ -63,7 +56,7 @@ private:
};
unsigned m_relationship { 0 };
RefPtr<CSS::StyleSheet> m_style_sheet;
CSSLoader m_css_loader;
};
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -25,7 +26,6 @@
*/
#include <AK/StringBuilder.h>
#include <LibWeb/CSS/Parser/CSSParser.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Text.h>
#include <LibWeb/HTML/HTMLStyleElement.h>
@ -34,7 +34,11 @@ namespace Web::HTML {
HTMLStyleElement::HTMLStyleElement(DOM::Document& document, QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
, m_css_loader(document)
{
m_css_loader.on_load = [&] {
document.update_style();
};
}
HTMLStyleElement::~HTMLStyleElement()
@ -48,17 +52,15 @@ void HTMLStyleElement::children_changed()
if (is<DOM::Text>(child))
builder.append(downcast<DOM::Text>(child).text_content());
});
m_stylesheet = parse_css(CSS::ParsingContext(document()), builder.to_string());
if (m_stylesheet)
document().style_sheets().add_sheet(*m_stylesheet);
else
document().style_sheets().add_sheet(CSS::StyleSheet::create({}));
m_css_loader.load_from_text(builder.to_string());
document().style_sheets().add_sheet(m_css_loader.style_sheet().release_nonnull());
HTMLElement::children_changed();
}
void HTMLStyleElement::removed_from(Node& old_parent)
{
if (m_stylesheet) {
if (m_css_loader.style_sheet()) {
// FIXME: Remove the sheet from the document
}
return HTMLElement::removed_from(old_parent);

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -27,6 +28,7 @@
#pragma once
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/Loader/CSSLoader.h>
namespace Web::HTML {
@ -41,7 +43,7 @@ public:
virtual void removed_from(Node&) override;
private:
RefPtr<CSS::StyleSheet> m_stylesheet;
CSSLoader m_css_loader;
};
}