css: make CSSStyleSheet.insertRule index optional

This commit is contained in:
Adrià Arrufat
2026-03-14 19:58:28 +09:00
parent e362a9cbc3
commit 351e44343d
2 changed files with 23 additions and 1 deletions

View File

@@ -447,6 +447,27 @@
} }
</script> </script>
<script id="CSSStyleSheet_insertRule_default_index">
{
const style = document.createElement('style');
document.head.appendChild(style);
const sheet = style.sheet;
testing.expectEqual(0, sheet.cssRules.length);
// Call without index, should default to 0
sheet.insertRule('.test-default { color: blue; }');
testing.expectEqual(1, sheet.cssRules.length);
testing.expectEqual('.test-default', sheet.cssRules[0].selectorText);
// Insert another rule without index, should default to 0 and push the first one to index 1
sheet.insertRule('.test-at-0 { color: red; }');
testing.expectEqual(2, sheet.cssRules.length);
testing.expectEqual('.test-at-0', sheet.cssRules[0].selectorText);
testing.expectEqual('.test-default', sheet.cssRules[1].selectorText);
}
</script>
<script id="CSSStyleSheet_replaceSync"> <script id="CSSStyleSheet_replaceSync">
{ {
const sheet = new CSSStyleSheet(); const sheet = new CSSStyleSheet();

View File

@@ -55,7 +55,8 @@ pub fn getOwnerRule(self: *const CSSStyleSheet) ?*CSSRule {
return self._owner_rule; return self._owner_rule;
} }
pub fn insertRule(self: *CSSStyleSheet, rule: []const u8, index: u32, page: *Page) !u32 { pub fn insertRule(self: *CSSStyleSheet, rule: []const u8, maybe_index: ?u32, page: *Page) !u32 {
const index = maybe_index orelse 0;
var it = Parser.parseStylesheet(rule); var it = Parser.parseStylesheet(rule);
const parsed_rule = it.next() orelse return error.SyntaxError; const parsed_rule = it.next() orelse return error.SyntaxError;