From 610ad255554bf5b81e63b5632624a865876e8bb7 Mon Sep 17 00:00:00 2001 From: Callum Law Date: Mon, 9 Jun 2025 16:42:53 +1200 Subject: [PATCH] LibWeb: Update m_inline_style when element style attribute is removed Previously we would just throw it away and construct a new (empty) one when required. This doesn't work as any existing references to the old instance will contain out of date information. Now we retain and update the existing instance instead. --- Libraries/LibWeb/DOM/Element.cpp | 21 +++++++------------ .../expected/DOM/removeAttribute-style.txt | 2 ++ .../Text/input/DOM/removeAttribute-style.html | 12 +++++++++++ 3 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/DOM/removeAttribute-style.txt create mode 100644 Tests/LibWeb/Text/input/DOM/removeAttribute-style.html diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp index 3d47ee7a3e8..9c8f914d067 100644 --- a/Libraries/LibWeb/DOM/Element.cpp +++ b/Libraries/LibWeb/DOM/Element.cpp @@ -3637,20 +3637,13 @@ void Element::attribute_changed(FlyString const& local_name, Optional co if (m_class_list) m_class_list->associated_attribute_changed(value_or_empty); } else if (local_name == HTML::AttributeNames::style) { - if (!value.has_value()) { - if (m_inline_style) { - m_inline_style = nullptr; - set_needs_style_update(true); - } - } else { - // https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag - if (m_inline_style && m_inline_style->is_updating()) - return; - if (!m_inline_style) - m_inline_style = CSS::CSSStyleProperties::create_element_inline_style({ *this }, {}, {}); - m_inline_style->set_declarations_from_text(*value); - set_needs_style_update(true); - } + // https://drafts.csswg.org/cssom/#ref-for-cssstyledeclaration-updating-flag + if (m_inline_style && m_inline_style->is_updating()) + return; + if (!m_inline_style) + m_inline_style = CSS::CSSStyleProperties::create_element_inline_style({ *this }, {}, {}); + m_inline_style->set_declarations_from_text(value.value_or(""_string)); + set_needs_style_update(true); } else if (local_name == HTML::AttributeNames::dir) { // https://html.spec.whatwg.org/multipage/dom.html#attr-dir if (value_or_empty.equals_ignoring_ascii_case("ltr"sv)) diff --git a/Tests/LibWeb/Text/expected/DOM/removeAttribute-style.txt b/Tests/LibWeb/Text/expected/DOM/removeAttribute-style.txt new file mode 100644 index 00000000000..e93194f0c10 --- /dev/null +++ b/Tests/LibWeb/Text/expected/DOM/removeAttribute-style.txt @@ -0,0 +1,2 @@ +Before: "color: red;" +After: "" diff --git a/Tests/LibWeb/Text/input/DOM/removeAttribute-style.html b/Tests/LibWeb/Text/input/DOM/removeAttribute-style.html new file mode 100644 index 00000000000..12a5eb9b226 --- /dev/null +++ b/Tests/LibWeb/Text/input/DOM/removeAttribute-style.html @@ -0,0 +1,12 @@ + + +