diff --git a/Libraries/LibWeb/DOM/StyleElementBase.cpp b/Libraries/LibWeb/DOM/StyleElementBase.cpp index 69f02bc915..61a9fc99d3 100644 --- a/Libraries/LibWeb/DOM/StyleElementBase.cpp +++ b/Libraries/LibWeb/DOM/StyleElementBase.cpp @@ -14,11 +14,27 @@ #include #include #include +#include #include #include namespace Web::DOM { +void StyleElementBase::update_a_style_block_for_dynamic_change() +{ + update_a_style_block(); +} + +void StyleElementBase::style_element_attribute_changed(FlyString const& name, Optional const& value) +{ + if (name == HTML::AttributeNames::media) { + if (auto* sheet = this->sheet()) + sheet->set_media(value.value_or({})); + } else if (name == HTML::AttributeNames::type) { + update_a_style_block_for_dynamic_change(); + } +} + // The user agent must run the "update a style block" algorithm whenever one of the following conditions occur: // FIXME: The element is popped off the stack of open elements of an HTML parser or XML parser. // @@ -152,6 +168,29 @@ void StyleElementBase::finished_loading_critical_subresources(AnyFailed any_fail m_document_load_event_delayer.clear(); } +// https://html.spec.whatwg.org/multipage/semantics.html#contributes-a-script-blocking-style-sheet +bool StyleElementBase::style_element_contributes_a_script_blocking_style_sheet() const +{ + // An element el in the context of a Document of an HTML parser or XML parser + // contributes a script-blocking style sheet if all of the following are true: + + // FIXME: el was created by that Document's parser. + + // el is either a style element or a link element that was an external resource link that contributes to the styling processing model when the el was created by the parser. + // NOTE: This is a style element, so all good! + + // FIXME: el's media attribute's value matches the environment. + + // FIXME: el's style sheet was enabled when the element was created by the parser. + + // FIXME: The last time the event loop reached step 1, el's root was that Document. + + // FIXME: The user agent hasn't given up on loading that particular style sheet yet. + // A user agent may give up on loading a style sheet at any time. + + return false; +} + // https://www.w3.org/TR/cssom/#dom-linkstyle-sheet CSS::CSSStyleSheet* StyleElementBase::sheet() { diff --git a/Libraries/LibWeb/DOM/StyleElementBase.h b/Libraries/LibWeb/DOM/StyleElementBase.h index a6a9a970a3..6e914571c6 100644 --- a/Libraries/LibWeb/DOM/StyleElementBase.h +++ b/Libraries/LibWeb/DOM/StyleElementBase.h @@ -18,6 +18,8 @@ public: virtual ~StyleElementBase() = default; void update_a_style_block(); + void update_a_style_block_for_dynamic_change(); + void style_element_attribute_changed(FlyString const&, Optional const& value); CSS::CSSStyleSheet* sheet(); CSS::CSSStyleSheet const* sheet() const; @@ -34,6 +36,10 @@ public: void visit_style_element_edges(JS::Cell::Visitor&); virtual Element& as_element() = 0; + virtual Element const& as_element() const = 0; + +protected: + bool style_element_contributes_a_script_blocking_style_sheet() const; private: // https://www.w3.org/TR/cssom/#associated-css-style-sheet diff --git a/Libraries/LibWeb/HTML/HTMLStyleElement.cpp b/Libraries/LibWeb/HTML/HTMLStyleElement.cpp index be09ee499d..2600594bc7 100644 --- a/Libraries/LibWeb/HTML/HTMLStyleElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLStyleElement.cpp @@ -7,8 +7,6 @@ */ #include -#include -#include #include namespace Web::HTML { @@ -37,31 +35,25 @@ void HTMLStyleElement::visit_edges(Cell::Visitor& visitor) void HTMLStyleElement::children_changed(ChildrenChangedMetadata const& metadata) { Base::children_changed(metadata); - update_a_style_block(); + update_a_style_block_for_dynamic_change(); } void HTMLStyleElement::inserted() { Base::inserted(); - update_a_style_block(); + update_a_style_block_for_dynamic_change(); } void HTMLStyleElement::removed_from(IsSubtreeRoot is_subtree_root, Node* old_ancestor, Node& old_root) { Base::removed_from(is_subtree_root, old_ancestor, old_root); - update_a_style_block(); + update_a_style_block_for_dynamic_change(); } void HTMLStyleElement::attribute_changed(FlyString const& name, Optional const& old_value, Optional const& value, Optional const& namespace_) { Base::attribute_changed(name, old_value, value, namespace_); - - if (name == HTML::AttributeNames::media) { - if (auto* sheet = this->sheet()) - sheet->set_media(value.value_or({})); - } else if (name == HTML::AttributeNames::type) { - update_a_style_block(); - } + style_element_attribute_changed(name, value); } // https://html.spec.whatwg.org/multipage/semantics.html#dom-style-disabled @@ -94,24 +86,7 @@ void HTMLStyleElement::set_disabled(bool disabled) // https://html.spec.whatwg.org/multipage/semantics.html#contributes-a-script-blocking-style-sheet bool HTMLStyleElement::contributes_a_script_blocking_style_sheet() const { - // An element el in the context of a Document of an HTML parser or XML parser - // contributes a script-blocking style sheet if all of the following are true: - - // FIXME: el was created by that Document's parser. - - // el is either a style element or a link element that was an external resource link that contributes to the styling processing model when the el was created by the parser. - // NOTE: This is a style element, so all good! - - // FIXME: el's media attribute's value matches the environment. - - // FIXME: el's style sheet was enabled when the element was created by the parser. - - // FIXME: The last time the event loop reached step 1, el's root was that Document. - - // FIXME: The user agent hasn't given up on loading that particular style sheet yet. - // A user agent may give up on loading a style sheet at any time. - - return false; + return style_element_contributes_a_script_blocking_style_sheet(); } } diff --git a/Libraries/LibWeb/HTML/HTMLStyleElement.h b/Libraries/LibWeb/HTML/HTMLStyleElement.h index 11b58f5f62..c6f10445bd 100644 --- a/Libraries/LibWeb/HTML/HTMLStyleElement.h +++ b/Libraries/LibWeb/HTML/HTMLStyleElement.h @@ -39,6 +39,7 @@ private: // ^DOM::StyleElementBase virtual Element& as_element() override { return *this; } + virtual Element const& as_element() const override { return *this; } virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; diff --git a/Libraries/LibWeb/SVG/SVGStyleElement.h b/Libraries/LibWeb/SVG/SVGStyleElement.h index bb29803736..e0583d0884 100644 --- a/Libraries/LibWeb/SVG/SVGStyleElement.h +++ b/Libraries/LibWeb/SVG/SVGStyleElement.h @@ -32,6 +32,7 @@ private: // ^DOM::StyleElementBase virtual Element& as_element() override { return *this; } + virtual Element const& as_element() const override { return *this; } virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override;