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.
This commit is contained in:
Milo van der Tier 2024-11-25 14:35:56 +01:00 committed by Andreas Kling
commit aa33acf3a2
Notes: github-actions[bot] 2024-11-25 16:18:19 +00:00
4 changed files with 25 additions and 9 deletions

View file

@ -513,6 +513,19 @@ void PropertyOwningCSSStyleDeclaration::set_the_declarations(Vector<StylePropert
m_custom_properties = move(custom_properties); m_custom_properties = move(custom_properties);
} }
void ElementInlineCSSStyleDeclaration::set_declarations_from_text(StringView css_text)
{
// FIXME: What do we do if the element is null?
if (!m_element) {
dbgln("FIXME: Returning from ElementInlineCSSStyleDeclaration::declarations_from_text as m_element is null.");
return;
}
empty_the_declarations();
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());
}
// https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext // https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-csstext
WebIDL::ExceptionOr<void> ElementInlineCSSStyleDeclaration::set_css_text(StringView css_text) WebIDL::ExceptionOr<void> ElementInlineCSSStyleDeclaration::set_css_text(StringView css_text)
{ {
@ -526,11 +539,8 @@ WebIDL::ExceptionOr<void> ElementInlineCSSStyleDeclaration::set_css_text(StringV
// NOTE: See ResolvedCSSStyleDeclaration. // NOTE: See ResolvedCSSStyleDeclaration.
// 2. Empty the declarations. // 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. // 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_declarations_from_text(css_text);
set_the_declarations(style->properties(), style->custom_properties());
// 4. Update style attribute for the CSS declaration block. // 4. Update style attribute for the CSS declaration block.
update_style_attribute(); update_style_attribute();

View file

@ -123,6 +123,8 @@ public:
bool is_updating() const { return m_updating; } bool is_updating() const { return m_updating; }
void set_declarations_from_text(StringView);
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override; virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
private: private:

View file

@ -2740,7 +2740,12 @@ void Element::attribute_changed(FlyString const& local_name, Optional<String> co
// https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag // https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag
if (m_inline_style && m_inline_style->is_updating()) if (m_inline_style && m_inline_style->is_updating())
return; 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); set_needs_style_update(true);
} }
} else if (local_name == HTML::AttributeNames::dir) { } else if (local_name == HTML::AttributeNames::dir) {

View file

@ -6,10 +6,9 @@ Rerun
Found 4 tests Found 4 tests
2 Pass 4 Pass
2 Fail
Details Details
Result Test Name MessagePass Parsing of initial style attribute Result Test Name MessagePass Parsing of initial style attribute
Fail Parsing of invalid style attribute Pass Parsing of invalid style attribute
Fail Parsing of style attribute Pass Parsing of style attribute
Pass Update style.backgroundColor Pass Update style.backgroundColor