1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:27:45 +00:00

LibWeb: Process style sheets in document order

Until now we would simply apply stylesheets in the order they finished
loading. This patch adds a StyleSheetList object that hangs off of each
Document and contains all the style sheets in document order.

There's still a lot of work to do for a proper cascade, but at least
this makes us consistently wrong every time. :^)
This commit is contained in:
Andreas Kling 2020-06-04 16:06:32 +02:00
parent ec1891837f
commit 959de19418
11 changed files with 120 additions and 7 deletions

View file

@ -63,6 +63,7 @@ namespace Web {
Document::Document(const URL& url)
: ParentNode(*this, NodeType::DOCUMENT_NODE)
, m_style_resolver(make<StyleResolver>(*this))
, m_style_sheets(CSS::StyleSheetList::create(*this))
, m_url(url)
, m_window(Window::create_with_document(*this))
{

View file

@ -37,6 +37,7 @@
#include <LibJS/Forward.h>
#include <LibWeb/CSS/StyleResolver.h>
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/DOM/NonElementParentNode.h>
#include <LibWeb/DOM/ParentNode.h>
@ -63,8 +64,8 @@ public:
StyleResolver& style_resolver() { return *m_style_resolver; }
const StyleResolver& style_resolver() const { return *m_style_resolver; }
void add_sheet(const StyleSheet& sheet) { m_sheets.append(sheet); }
const NonnullRefPtrVector<StyleSheet>& stylesheets() const { return m_sheets; }
CSS::StyleSheetList& style_sheets() { return *m_style_sheets; }
const CSS::StyleSheetList& style_sheets() const { return *m_style_sheets; }
virtual FlyString tag_name() const override { return "#document"; }
@ -146,7 +147,7 @@ private:
virtual RefPtr<LayoutNode> create_layout_node(const StyleProperties* parent_style) const override;
OwnPtr<StyleResolver> m_style_resolver;
NonnullRefPtrVector<StyleSheet> m_sheets;
RefPtr<CSS::StyleSheetList> m_style_sheets;
RefPtr<Node> m_hovered_node;
RefPtr<Node> m_inspected_node;
WeakPtr<Frame> m_frame;

View file

@ -68,12 +68,20 @@ void HTMLLinkElement::resource_did_load()
dbg() << "HTMLLinkElement: Failed to parse stylesheet: " << href();
return;
}
document().add_sheet(*sheet);
// 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 = 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));

View file

@ -50,6 +50,8 @@ private:
virtual void resource_did_load() override;
void load_stylesheet(const URL&);
RefPtr<StyleSheet> m_style_sheet;
};
template<>

View file

@ -50,7 +50,9 @@ void HTMLStyleElement::children_changed()
});
m_stylesheet = parse_css(builder.to_string());
if (m_stylesheet)
document().add_sheet(*m_stylesheet);
document().style_sheets().add_sheet(*m_stylesheet);
else
document().style_sheets().add_sheet(StyleSheet::create({}));
HTMLElement::children_changed();
}