From f29a95e2fed085762e29da6ffb735db6f0a7a005 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 14 Apr 2025 17:44:17 +0100 Subject: [PATCH] LibWeb: Assign linked style sheet location when parsing it Before this change, we assigned the style sheet's location *after* its content rules were parsed and added to it. This meant any `@import`s would try to fetch their style sheet before they knew the URL they should fetch it relative to. --- Libraries/LibWeb/HTML/HTMLLinkElement.cpp | 8 +++----- Tests/LibWeb/Layout/data/import-a.css | 5 +++++ Tests/LibWeb/Layout/data/import-b.css | 3 +++ .../expected/import-from-linked-style-sheet.txt | 16 ++++++++++++++++ .../input/import-from-linked-style-sheet.html | 3 +++ 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 Tests/LibWeb/Layout/data/import-a.css create mode 100644 Tests/LibWeb/Layout/data/import-b.css create mode 100644 Tests/LibWeb/Layout/expected/import-from-linked-style-sheet.txt create mode 100644 Tests/LibWeb/Layout/input/import-from-linked-style-sheet.html diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp index 0adacae602e..2d59eb9fc41 100644 --- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp @@ -487,13 +487,11 @@ void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastru dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error)); } else { auto const decoded_string = maybe_decoded_string.release_value(); - m_loaded_style_sheet = parse_css_stylesheet(CSS::Parser::ParsingParams(document(), *response.url()), decoded_string); + VERIFY(!response.url_list().is_empty()); + auto location = response.url_list().first(); + m_loaded_style_sheet = parse_css_stylesheet(CSS::Parser::ParsingParams(document(), location), decoded_string, location); if (m_loaded_style_sheet) { - Optional<::URL::URL> location; - if (!response.url_list().is_empty()) - location = response.url_list().first(); - document_or_shadow_root_style_sheets().create_a_css_style_sheet( "text/css"_string, this, diff --git a/Tests/LibWeb/Layout/data/import-a.css b/Tests/LibWeb/Layout/data/import-a.css new file mode 100644 index 00000000000..ad942cacb5e --- /dev/null +++ b/Tests/LibWeb/Layout/data/import-a.css @@ -0,0 +1,5 @@ +@import "import-b.css"; + +#target { + width: 50px; +} diff --git a/Tests/LibWeb/Layout/data/import-b.css b/Tests/LibWeb/Layout/data/import-b.css new file mode 100644 index 00000000000..ebe0467b83d --- /dev/null +++ b/Tests/LibWeb/Layout/data/import-b.css @@ -0,0 +1,3 @@ +#target { + width: 200px !important; +} diff --git a/Tests/LibWeb/Layout/expected/import-from-linked-style-sheet.txt b/Tests/LibWeb/Layout/expected/import-from-linked-style-sheet.txt new file mode 100644 index 00000000000..e72c44d809d --- /dev/null +++ b/Tests/LibWeb/Layout/expected/import-from-linked-style-sheet.txt @@ -0,0 +1,16 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x33 [BFC] children: not-inline + BlockContainer at (8,8) content-size 784x17 children: not-inline + BlockContainer at (8,8) content-size 200x17 children: inline + frag 0 from TextNode start: 0, length: 5, rect: [8,8 39.78125x17] baseline: 13.296875 + "Hello" + TextNode <#text> + BlockContainer <(anonymous)> at (8,25) content-size 784x0 children: inline + TextNode <#text> + +ViewportPaintable (Viewport<#document>) [0,0 800x600] + PaintableWithLines (BlockContainer) [0,0 800x33] + PaintableWithLines (BlockContainer) [8,8 784x17] + PaintableWithLines (BlockContainer
#target) [8,8 200x17] + TextPaintable (TextNode<#text>) + PaintableWithLines (BlockContainer(anonymous)) [8,25 784x0] diff --git a/Tests/LibWeb/Layout/input/import-from-linked-style-sheet.html b/Tests/LibWeb/Layout/input/import-from-linked-style-sheet.html new file mode 100644 index 00000000000..4ed721d9eaf --- /dev/null +++ b/Tests/LibWeb/Layout/input/import-from-linked-style-sheet.html @@ -0,0 +1,3 @@ + + +
Hello