From b9f0ea2178b9d930a1aac9aedb06dc7abaf2e430 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 28 Apr 2024 16:39:26 +0100 Subject: [PATCH] LibWeb: Evaluate media rules for adopted style sheets Previously, media rules were not evaluated for adopted style sheets. --- .../Text/expected/css/constructed-style-sheets.txt | 1 + .../Text/input/css/constructed-style-sheets.html | 1 + Userland/Libraries/LibWeb/DOM/Document.cpp | 14 ++++++++------ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Tests/LibWeb/Text/expected/css/constructed-style-sheets.txt b/Tests/LibWeb/Text/expected/css/constructed-style-sheets.txt index 9359dbb869..5a54b480ca 100644 --- a/Tests/LibWeb/Text/expected/css/constructed-style-sheets.txt +++ b/Tests/LibWeb/Text/expected/css/constructed-style-sheets.txt @@ -1 +1,2 @@ Disabled constructed style sheet applies to document: false +Constructed style sheet with media rules applies to document: true diff --git a/Tests/LibWeb/Text/input/css/constructed-style-sheets.html b/Tests/LibWeb/Text/input/css/constructed-style-sheets.html index 64de3f5f02..86a7190434 100644 --- a/Tests/LibWeb/Text/input/css/constructed-style-sheets.html +++ b/Tests/LibWeb/Text/input/css/constructed-style-sheets.html @@ -16,5 +16,6 @@ } println(`Disabled constructed style sheet applies to document: ${constructedStyleSheetAppliesToDocument({ disabled: true })}`); + println(`Constructed style sheet with media rules applies to document: ${constructedStyleSheetAppliesToDocument({ media: "screen, print" })}`); }); diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 80528f7494..fa802a3402 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -2562,10 +2562,10 @@ void Document::evaluate_media_rules() return; bool any_media_queries_changed_match_state = false; - for (auto& style_sheet : style_sheets().sheets()) { - if (style_sheet->evaluate_media_queries(*window)) + for_each_css_style_sheet([&](CSS::CSSStyleSheet& style_sheet) { + if (style_sheet.evaluate_media_queries(*window)) any_media_queries_changed_match_state = true; - } + }); if (any_media_queries_changed_match_state) { style_computer().invalidate_rule_cache(); @@ -4897,9 +4897,11 @@ WebIDL::ExceptionOr Document::set_adopted_style_sheets(JS::Value new_value void Document::for_each_css_style_sheet(Function&& callback) const { - for (auto& style_sheet : m_style_sheets->sheets()) { - if (!(style_sheet->is_alternate() && style_sheet->disabled())) - callback(*style_sheet); + if (m_style_sheets) { + for (auto& style_sheet : m_style_sheets->sheets()) { + if (!(style_sheet->is_alternate() && style_sheet->disabled())) + callback(*style_sheet); + } } if (m_adopted_style_sheets) {