From 51fc87bc1b051c45d8a93ea5098e7b8ce10551ad Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 30 Oct 2024 14:33:46 +0000 Subject: [PATCH] LibWeb/CSS: Return 0 from CSSRule.type for non-spec types We use the CSSRule::Type enum for identifying the type of a CSSRule, but the spec requires that only some of these types are exposed via the `type` attribute. For the rest, we're required to return 0, so let's do so. :^) --- .../LibWeb/Text/expected/css/CSSRule-type.txt | 9 +++++++ Tests/LibWeb/Text/input/css/CSSRule-type.html | 25 +++++++++++++++++++ Userland/Libraries/LibWeb/CSS/CSSRule.cpp | 11 ++++++++ Userland/Libraries/LibWeb/CSS/CSSRule.h | 6 +++-- Userland/Libraries/LibWeb/CSS/CSSRule.idl | 2 +- 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/css/CSSRule-type.txt create mode 100644 Tests/LibWeb/Text/input/css/CSSRule-type.html diff --git a/Tests/LibWeb/Text/expected/css/CSSRule-type.txt b/Tests/LibWeb/Text/expected/css/CSSRule-type.txt new file mode 100644 index 00000000000..a23632c8203 --- /dev/null +++ b/Tests/LibWeb/Text/expected/css/CSSRule-type.txt @@ -0,0 +1,9 @@ +CSSImportRule type = 3 +CSSNamespaceRule type = 10 +CSSStyleRule type = 1 +CSSMediaRule type = 4 +CSSKeyframesRule type = 7 +CSSSupportsRule type = 12 +CSSLayerStatementRule type = 0 +CSSLayerStatementRule type = 0 +CSSPropertyRule type = 0 diff --git a/Tests/LibWeb/Text/input/css/CSSRule-type.html b/Tests/LibWeb/Text/input/css/CSSRule-type.html new file mode 100644 index 00000000000..c2d2580bf1e --- /dev/null +++ b/Tests/LibWeb/Text/input/css/CSSRule-type.html @@ -0,0 +1,25 @@ + + + + diff --git a/Userland/Libraries/LibWeb/CSS/CSSRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSRule.cpp index 9c7f65b08d4..d2856a95d92 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSRule.cpp @@ -26,6 +26,17 @@ void CSSRule::visit_edges(Cell::Visitor& visitor) visitor.visit(m_parent_rule); } +// https://www.w3.org/TR/cssom/#dom-cssrule-type +WebIDL::UnsignedShort CSSRule::type_for_bindings() const +{ + // NOTE: Types that aren't defined in the spec must return 0. + // To do this, we arbitrarily make non-spec ones start at 100. + auto type = to_underlying(m_type); + if (type >= 100) + return 0; + return type; +} + // https://www.w3.org/TR/cssom/#dom-cssrule-csstext String CSSRule::css_text() const { diff --git a/Userland/Libraries/LibWeb/CSS/CSSRule.h b/Userland/Libraries/LibWeb/CSS/CSSRule.h index 4da219419f4..15a491037af 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSRule.h @@ -12,6 +12,7 @@ #include #include #include +#include namespace Web::CSS { @@ -22,7 +23,7 @@ public: virtual ~CSSRule() = default; // https://drafts.csswg.org/cssom/#dom-cssrule-type - enum class Type : u16 { + enum class Type : WebIDL::UnsignedShort { Style = 1, Import = 3, Media = 4, @@ -35,10 +36,11 @@ public: LayerBlock = 100, LayerStatement = 101, NestedDeclarations = 102, - Property = 103, // FIXME: This should return `0` as a type, but type is used for a lot of dispatching + Property = 103, }; Type type() const { return m_type; } + WebIDL::UnsignedShort type_for_bindings() const; String css_text() const; void set_css_text(StringView); diff --git a/Userland/Libraries/LibWeb/CSS/CSSRule.idl b/Userland/Libraries/LibWeb/CSS/CSSRule.idl index c6bb86ea872..03001f556f3 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRule.idl +++ b/Userland/Libraries/LibWeb/CSS/CSSRule.idl @@ -9,7 +9,7 @@ interface CSSRule { readonly attribute CSSStyleSheet? parentStyleSheet; // the following attribute and constants are historical - readonly attribute unsigned short type; + [ImplementedAs=type_for_bindings] readonly attribute unsigned short type; const unsigned short STYLE_RULE = 1; const unsigned short CHARSET_RULE = 2; const unsigned short IMPORT_RULE = 3;