css: throw IndexSizeError in deleteRule and insertRule

This commit is contained in:
Adrià Arrufat
2026-03-12 22:40:01 +09:00
parent 2aef4ab677
commit de2b1cc6fe
4 changed files with 15 additions and 7 deletions

View File

@@ -436,6 +436,15 @@
sheet.deleteRule(0); sheet.deleteRule(0);
testing.expectEqual(0, sheet.cssRules.length); testing.expectEqual(0, sheet.cssRules.length);
let caught = false;
try {
sheet.deleteRule(5);
} catch (e) {
caught = true;
testing.expectEqual('IndexSizeError', e.name);
}
testing.expectTrue(caught);
} }
</script> </script>

View File

@@ -24,14 +24,14 @@ pub fn item(self: *const CSSRuleList, index: usize) ?*CSSRule {
pub fn insert(self: *CSSRuleList, index: u32, rule: *CSSRule, page: *Page) !void { pub fn insert(self: *CSSRuleList, index: u32, rule: *CSSRule, page: *Page) !void {
if (index > self._rules.items.len) { if (index > self._rules.items.len) {
return error.IndexSizeError; // Or standard DOMException mapped error return error.IndexSizeError;
} }
try self._rules.insert(page.arena, index, rule); try self._rules.insert(page.arena, index, rule);
} }
pub fn remove(self: *CSSRuleList, index: u32) void { pub fn remove(self: *CSSRuleList, index: u32) !void {
if (index >= self._rules.items.len) { if (index >= self._rules.items.len) {
return; // Ignore or throw? Standard says IndexSizeError DOMException, but we might just no-op or return an error depending on the caller. return error.IndexSizeError;
} }
_ = self._rules.orderedRemove(index); _ = self._rules.orderedRemove(index);
} }

View File

@@ -193,7 +193,6 @@ pub fn setCssText(self: *CSSStyleDeclaration, text: []const u8, page: *Page) !vo
while (it.next()) |declaration| { while (it.next()) |declaration| {
try self.setPropertyImpl(declaration.name, declaration.value, declaration.important, page); try self.setPropertyImpl(declaration.name, declaration.value, declaration.important, page);
} }
try self.syncStyleAttribute(page); try self.syncStyleAttribute(page);
} }

View File

@@ -73,7 +73,7 @@ pub fn insertRule(self: *CSSStyleSheet, rule: []const u8, index: u32, page: *Pag
pub fn deleteRule(self: *CSSStyleSheet, index: u32, page: *Page) !void { pub fn deleteRule(self: *CSSStyleSheet, index: u32, page: *Page) !void {
const rules = try self.getCssRules(page); const rules = try self.getCssRules(page);
rules.remove(index); try rules.remove(index);
} }
pub fn replace(self: *CSSStyleSheet, text: []const u8, page: *Page) !js.Promise { pub fn replace(self: *CSSStyleSheet, text: []const u8, page: *Page) !js.Promise {
@@ -116,8 +116,8 @@ pub const JsApi = struct {
pub const disabled = bridge.accessor(CSSStyleSheet.getDisabled, CSSStyleSheet.setDisabled, .{}); pub const disabled = bridge.accessor(CSSStyleSheet.getDisabled, CSSStyleSheet.setDisabled, .{});
pub const cssRules = bridge.accessor(CSSStyleSheet.getCssRules, null, .{}); pub const cssRules = bridge.accessor(CSSStyleSheet.getCssRules, null, .{});
pub const ownerRule = bridge.accessor(CSSStyleSheet.getOwnerRule, null, .{}); pub const ownerRule = bridge.accessor(CSSStyleSheet.getOwnerRule, null, .{});
pub const insertRule = bridge.function(CSSStyleSheet.insertRule, .{}); pub const insertRule = bridge.function(CSSStyleSheet.insertRule, .{ .dom_exception = true });
pub const deleteRule = bridge.function(CSSStyleSheet.deleteRule, .{}); pub const deleteRule = bridge.function(CSSStyleSheet.deleteRule, .{ .dom_exception = true });
pub const replace = bridge.function(CSSStyleSheet.replace, .{}); pub const replace = bridge.function(CSSStyleSheet.replace, .{});
pub const replaceSync = bridge.function(CSSStyleSheet.replaceSync, .{}); pub const replaceSync = bridge.function(CSSStyleSheet.replaceSync, .{});
}; };