LibWeb: Move style sheet parsing into create_a_css_style_sheet()

The spec is unclear about when exactly we should parse the style sheet.
Previously we'd do so before calling this algorithm, which was
error-prone, as seen by the bug fixed by the previous commit. The spec
for step 1 of "create a CSS style sheet" says:

1. Create a new CSS style sheet object and set its properties as
   specified.

The definitions linked are UA-defined enough that it seems reasonable to
put the parsing here. That simplifies the user code a little and makes
it harder to mess up. It does raise the question of what to do if
parsing fails. I've matched our previous behaviour by just logging and
returning in that case.

While I'm modifying this method, I've also converted the bool params to
enums so they're a little clearer to read.
This commit is contained in:
Sam Atkins 2025-04-14 13:53:11 +01:00
commit 2a96a81e68
Notes: github-actions[bot] 2025-04-15 08:41:41 +00:00
4 changed files with 74 additions and 50 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2023, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -19,9 +20,17 @@ class StyleSheetList final : public Bindings::PlatformObject {
public:
[[nodiscard]] static GC::Ref<StyleSheetList> create(GC::Ref<DOM::Node> document_or_shadow_root);
void add_a_css_style_sheet(CSS::CSSStyleSheet&);
void remove_a_css_style_sheet(CSS::CSSStyleSheet&);
void create_a_css_style_sheet(String type, DOM::Element* owner_node, String media, String title, bool alternate, bool origin_clean, Optional<::URL::URL> location, CSS::CSSStyleSheet* parent_style_sheet, CSS::CSSRule* owner_rule, CSS::CSSStyleSheet&);
void add_a_css_style_sheet(CSSStyleSheet&);
void remove_a_css_style_sheet(CSSStyleSheet&);
enum class Alternate : u8 {
No,
Yes,
};
enum class OriginClean : u8 {
No,
Yes,
};
GC::Ptr<CSSStyleSheet> create_a_css_style_sheet(String const& css_text, String type, DOM::Element* owner_node, String media, String title, Alternate, OriginClean, Optional<::URL::URL> location, CSSStyleSheet* parent_style_sheet, CSSRule* owner_rule);
Vector<GC::Ref<CSSStyleSheet>> const& sheets() const { return m_sheets; }
Vector<GC::Ref<CSSStyleSheet>>& sheets() { return m_sheets; }