From 99b2eff9887946f0424cce73324d0cc8b5bfbc45 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 14 Apr 2024 23:05:05 +0100 Subject: [PATCH] LibWeb: Invalidate style when CSSStyleRule selectorText changes Previously, any change to the selectorText of a CSSStyleRule was not reflected in the document style. --- .../css/CSSStyleRule-set-selectorText.txt | 10 ++++++ .../css/CSSStyleRule-set-selectorText.html | 36 +++++++++++++++++++ .../Libraries/LibWeb/CSS/CSSStyleRule.cpp | 11 +++++- Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Tests/LibWeb/Text/expected/css/CSSStyleRule-set-selectorText.txt create mode 100644 Tests/LibWeb/Text/input/css/CSSStyleRule-set-selectorText.html diff --git a/Tests/LibWeb/Text/expected/css/CSSStyleRule-set-selectorText.txt b/Tests/LibWeb/Text/expected/css/CSSStyleRule-set-selectorText.txt new file mode 100644 index 00000000000..2b4dd85c3e5 --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/CSSStyleRule-set-selectorText.txt @@ -0,0 +1,10 @@ + Testing window.document: +selectorText initial value: .green-background +container element backgroundColor initial value: rgb(255, 0, 0) +selectorText after setting selectorText value to #container: #container +container element backgroundColor after setting selectorText value to #container: rgb(0, 255, 0) +Testing iframe.contentDocument: +selectorText initial value: .green-background +container element backgroundColor initial value: rgb(255, 0, 0) +selectorText after setting selectorText value to #container: #container +container element backgroundColor after setting selectorText value to #container: rgb(0, 255, 0) diff --git a/Tests/LibWeb/Text/input/css/CSSStyleRule-set-selectorText.html b/Tests/LibWeb/Text/input/css/CSSStyleRule-set-selectorText.html new file mode 100644 index 00000000000..776f3f9cf30 --- /dev/null +++ b/Tests/LibWeb/Text/input/css/CSSStyleRule-set-selectorText.html @@ -0,0 +1,36 @@ + + + + + diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp index a6e9b61aeb6..97ce852febd 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include namespace Web::CSS { @@ -102,8 +103,16 @@ void CSSStyleRule::set_selector_text(StringView selector_text) auto parsed_selectors = parse_selector(Parser::ParsingContext { realm() }, selector_text); // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value. - if (parsed_selectors.has_value()) + if (parsed_selectors.has_value()) { m_selectors = parsed_selectors.release_value(); + if (auto* sheet = parent_style_sheet()) { + if (auto style_sheet_list = sheet->style_sheet_list()) { + auto& document = style_sheet_list->document(); + document.style_computer().invalidate_rule_cache(); + document.invalidate_style(); + } + } + } // 3. Otherwise, if the algorithm returns a null value, do nothing. } diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h index 7d67d7dd552..71c78c39b15 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleSheet.h @@ -62,6 +62,7 @@ public: bool evaluate_media_queries(HTML::Window const&); void for_each_effective_keyframes_at_rule(Function const& callback) const; + JS::GCPtr style_sheet_list() const { return m_style_sheet_list; } void set_style_sheet_list(Badge, StyleSheetList*); Optional default_namespace() const;