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. :^)
This commit is contained in:
Sam Atkins 2024-10-30 14:33:46 +00:00 committed by Andreas Kling
parent 4998385c7a
commit 51fc87bc1b
Notes: github-actions[bot] 2024-10-30 17:46:59 +00:00
5 changed files with 50 additions and 3 deletions

View file

@ -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

View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<style>
@import "http://something.invalid/style.css";
@namespace "wheee";
.foo {}
@media (min-width: 100px) {}
@font-face {}
@keyframes foo {}
@supports (foo: bar) {}
@layer foo;
@layer bar {}
@property --foo {
syntax: "*";
inherits: false;
}
</style>
<script src="../include.js"></script>
<script>
test(() => {
let styleSheet = document.styleSheets[0];
for (let rule of styleSheet.cssRules) {
println(`${rule.constructor.name} type = ${rule.type}`);
}
});
</script>

View file

@ -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
{

View file

@ -12,6 +12,7 @@
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/CSS/CSSStyleDeclaration.h>
#include <LibWeb/CSS/Selector.h>
#include <LibWeb/WebIDL/Types.h>
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);

View file

@ -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;