From a8afb883c1c4b2c7ff2931d14dce4ce8b5f5500f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 11 Apr 2022 16:56:52 +0200 Subject: [PATCH] LibWeb: Implement CSS declaration block's "updating flag" This flag is used to prevent reparsing the style attribute after it is automatically updated after using the CSSOM API to mutate style. --- .../Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp | 12 ++++++++++++ Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 5 +++++ Userland/Libraries/LibWeb/DOM/Element.cpp | 8 ++++++++ Userland/Libraries/LibWeb/DOM/Element.h | 4 ++-- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index 3803e51a6c..d9807168c8 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -125,10 +125,22 @@ DOM::ExceptionOr PropertyOwningCSSStyleDeclaration::remove_property(Prop // https://drafts.csswg.org/cssom/#update-style-attribute-for void ElementInlineCSSStyleDeclaration::update_style_attribute() { + // 1. Assert: declaration block’s computed flag is unset. + // NOTE: Unnecessary, only relevant for ResolvedCSSStyleDeclaration. + + // 2. Let owner node be declaration block’s owner node. + // 3. If owner node is null, then return. if (!m_element) return; + // 4. Set declaration block’s updating flag. + m_updating = true; + + // 5. Set an attribute value for owner node using "style" and the result of serializing declaration block. m_element->set_attribute(HTML::AttributeNames::style, serialized()); + + // 6. Unset declaration block’s updating flag. + m_updating = false; } // https://drafts.csswg.org/cssom/#set-a-css-declaration diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 031c39b7e8..0e43db3eaf 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -101,12 +101,17 @@ public: DOM::Element* element() { return m_element.ptr(); } const DOM::Element* element() const { return m_element.ptr(); } + bool is_updating() const { return m_updating; } + private: explicit ElementInlineCSSStyleDeclaration(DOM::Element&, Vector properties, HashMap custom_properties); virtual void update_style_attribute() override; WeakPtr m_element; + + // https://drafts.csswg.org/cssom/#cssstyledeclaration-updating-flag + bool m_updating { false }; }; } diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index f5afe507c2..54cc4962ca 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -293,6 +293,11 @@ RefPtr Element::create_layout_node_for_display_type(DOM::Document& TODO(); } +CSS::CSSStyleDeclaration const* Element::inline_style() const +{ + return m_inline_style; +} + void Element::parse_attribute(FlyString const& name, String const& value) { if (name == HTML::AttributeNames::class_) { @@ -305,6 +310,9 @@ void Element::parse_attribute(FlyString const& name, String const& value) if (m_class_list) m_class_list->associated_attribute_changed(value); } else if (name == HTML::AttributeNames::style) { + // https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag + if (m_inline_style && m_inline_style->is_updating()) + return; m_inline_style = parse_css_style_attribute(CSS::ParsingContext(document()), value, *this); set_needs_style_update(true); } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 5608534806..d6b510ad6c 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -103,7 +103,7 @@ public: void set_computed_css_values(RefPtr style) { m_computed_css_values = move(style); } NonnullRefPtr resolved_css_values(); - const CSS::CSSStyleDeclaration* inline_style() const { return m_inline_style; } + CSS::CSSStyleDeclaration const* inline_style() const; NonnullRefPtr style_for_bindings(); @@ -152,7 +152,7 @@ private: String m_html_uppercased_qualified_name; NonnullRefPtr m_attributes; - RefPtr m_inline_style; + RefPtr m_inline_style; RefPtr m_computed_css_values; HashMap m_custom_properties;