From aa33acf3a253b498a8c9b8813a90176f0ddbeb74 Mon Sep 17 00:00:00 2001 From: Milo van der Tier Date: Mon, 25 Nov 2024 14:35:56 +0100 Subject: [PATCH] LibWeb: Update existing style object when setting style attribute Previously any existing ElementInlineCSSStyleDeclaration would get overwritten by e.setAttribute("style", ...), while it should be updated instead. This fixes 2 WPT subtests. --- Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp | 18 ++++++++++++++---- Libraries/LibWeb/CSS/CSSStyleDeclaration.h | 2 ++ Libraries/LibWeb/DOM/Element.cpp | 7 ++++++- .../domparsing/style_attribute_html.txt | 7 +++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp index fef865c27f3..5d7b1511dd7 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp @@ -513,6 +513,19 @@ void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vectordocument()), css_text, *m_element.ptr()); + set_the_declarations(style->properties(), style->custom_properties()); +} + // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext WebIDL::ExceptionOr ElementInlineCSSStyleDeclaration::set_css_text(StringView css_text) { @@ -526,11 +539,8 @@ WebIDL::ExceptionOr ElementInlineCSSStyleDeclaration::set_css_text(StringV // NOTE: See ResolvedCSSStyleDeclaration. // 2. Empty the declarations. - empty_the_declarations(); - // 3. Parse the given value and, if the return value is not the empty list, insert the items in the list into the declarations, in specified order. - auto style = parse_css_style_attribute(CSS::Parser::ParsingContext(m_element->document()), css_text, *m_element.ptr()); - set_the_declarations(style->properties(), style->custom_properties()); + set_declarations_from_text(css_text); // 4. Update style attribute for the CSS declaration block. update_style_attribute(); diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h index 32a898d50dc..8ebc5bca0d2 100644 --- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.h +++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.h @@ -123,6 +123,8 @@ public: bool is_updating() const { return m_updating; } + void set_declarations_from_text(StringView); + virtual WebIDL::ExceptionOr set_css_text(StringView) override; private: diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index fea2ff0449f..a7851cdeedc 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -2740,7 +2740,12 @@ void Element::attribute_changed(FlyString const& local_name, Optional co // 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::Parser::ParsingContext(document()), *value, *this); + if (!m_inline_style) { + m_inline_style = parse_css_style_attribute(CSS::Parser::ParsingContext(document()), *value, *this); + } else { + // NOTE: ElementInlineCSSStyleDeclaration::set_css_text should never throw an exception. + m_inline_style->set_declarations_from_text(*value); + } set_needs_style_update(true); } } else if (local_name == HTML::AttributeNames::dir) { diff --git a/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt b/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt index 349d7ed6d4d..60841df97db 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/domparsing/style_attribute_html.txt @@ -6,10 +6,9 @@ Rerun Found 4 tests -2 Pass -2 Fail +4 Pass Details Result Test Name MessagePass Parsing of initial style attribute -Fail Parsing of invalid style attribute -Fail Parsing of style attribute +Pass Parsing of invalid style attribute +Pass Parsing of style attribute Pass Update style.backgroundColor \ No newline at end of file