fix type check: case-insensitive, accept empty string, clear on disconnect

Per spec, valid type values are absent, empty string, or a
case-insensitive match for "text/css". Also clear cached sheet
when the element is disconnected or type becomes invalid.
This commit is contained in:
egrs
2026-02-18 10:40:42 +01:00
parent ea4eebd2d6
commit 54d6eed740
2 changed files with 30 additions and 2 deletions

View File

@@ -19,6 +19,25 @@
lessStyle.type = 'text/less'; lessStyle.type = 'text/less';
document.head.appendChild(lessStyle); document.head.appendChild(lessStyle);
testing.expectEqual(null, lessStyle.sheet); testing.expectEqual(null, lessStyle.sheet);
// Empty type attribute is valid (defaults to text/css per spec)
const emptyType = document.createElement('style');
emptyType.setAttribute('type', '');
document.head.appendChild(emptyType);
testing.expectEqual(true, emptyType.sheet instanceof CSSStyleSheet);
// Case-insensitive type check
const upperType = document.createElement('style');
upperType.type = 'TEXT/CSS';
document.head.appendChild(upperType);
testing.expectEqual(true, upperType.sheet instanceof CSSStyleSheet);
// Disconnection clears sheet
const tempStyle = document.createElement('style');
document.head.appendChild(tempStyle);
testing.expectEqual(true, tempStyle.sheet instanceof CSSStyleSheet);
document.head.removeChild(tempStyle);
testing.expectEqual(null, tempStyle.sheet);
} }
</script> </script>

View File

@@ -79,8 +79,17 @@ pub fn setDisabled(self: *Style, disabled: bool, page: *Page) !void {
const CSSStyleSheet = @import("../../css/CSSStyleSheet.zig"); const CSSStyleSheet = @import("../../css/CSSStyleSheet.zig");
pub fn getSheet(self: *Style, page: *Page) !?*CSSStyleSheet { pub fn getSheet(self: *Style, page: *Page) !?*CSSStyleSheet {
// Per spec, sheet is null for disconnected elements or non-CSS types. // Per spec, sheet is null for disconnected elements or non-CSS types.
if (!self.asNode().isConnected()) return null; // Valid types: absent (defaults to "text/css"), empty string, or
if (!std.mem.eql(u8, self.getType(), "text/css")) return null; // case-insensitive match for "text/css".
if (!self.asNode().isConnected()) {
self._sheet = null;
return null;
}
const t = self.getType();
if (t.len != 0 and !std.ascii.eqlIgnoreCase(t, "text/css")) {
self._sheet = null;
return null;
}
if (self._sheet) |sheet| return sheet; if (self._sheet) |sheet| return sheet;
const sheet = try CSSStyleSheet.init(page); const sheet = try CSSStyleSheet.init(page);