fallback unknown rule to new unknown type

This commit is contained in:
Karl Seguin
2026-05-15 06:59:21 +08:00
parent 6d1740b40f
commit b7a0ca2bca
3 changed files with 16 additions and 7 deletions

View File

@@ -668,3 +668,13 @@
testing.expectTrue(exists);
}
</script>
<script id="CSSStyleSheet_insertRule_unknown_at_rule_typed_as_zero">
{
const sheet = new CSSStyleSheet();
sheet.insertRule('@layer utilities { .x { color: red; } }');
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual(0, sheet.cssRules[0].type);
testing.expectTrue(sheet.cssRules[0].cssText.includes('@layer'));
}
</script>

View File

@@ -23,6 +23,7 @@ pub const Type = union(enum) {
font_feature_values: void,
viewport: void,
region_style: void,
unknown: void,
};
_type: Type,
@@ -62,6 +63,9 @@ pub fn initAtRule(rule_type: Type, text: []const u8, frame: *Frame) !*CSSRule {
}
pub fn getType(self: *const CSSRule) u16 {
if (self._type == .unknown) {
return 0;
}
return @as(u16, @intFromEnum(std.meta.activeTag(self._type))) + 1;
}

View File

@@ -124,12 +124,7 @@ pub fn insertRule(self: *CSSStyleSheet, rule: []const u8, maybe_index: ?u32, fra
/// Map an at-rule keyword (without `@`) to the matching `CSSRule.Type`
/// variant. Vendor prefixes are stripped before matching
/// (`-webkit-keyframes` -> `keyframes`). Unrecognized keywords fall back
/// to `.media`: this is a deliberate choice to avoid changing the
/// `CSSRule.Type` enum's numeric layout (which is exposed as the spec
/// `CSSRule.type` constant) just to add an `unknown` variant. The CSS
/// engine doesn't use the type for anything; the value matters only for
/// JS-side `rule.type` checks, and CSS-in-JS dedup paths key on `length`
/// or `cssText` rather than `type`.
/// to `.unknown`: which maps to 0 for rule.type.
fn atRuleTypeFor(keyword_with_prefix: []const u8) CSSRule.Type {
var keyword = keyword_with_prefix;
inline for (.{ "-webkit-", "-moz-", "-ms-", "-o-" }) |prefix| {
@@ -152,7 +147,7 @@ fn atRuleTypeFor(keyword_with_prefix: []const u8) CSSRule.Type {
if (eql(keyword, "font-feature-values")) return .font_feature_values;
if (eql(keyword, "viewport")) return .viewport;
if (eql(keyword, "document")) return .document;
return .media;
return .unknown;
}
pub fn deleteRule(self: *CSSStyleSheet, index: u32, frame: *Frame) !void {