return CSSStyleSheet from HTMLStyleElement.sheet

Per spec, connected style elements with type text/css should
return a CSSStyleSheet object. Previously always returned null.
The sheet is lazily created and cached on first access.
This commit is contained in:
egrs
2026-02-18 08:11:08 +01:00
parent db4a97743f
commit ea4eebd2d6
2 changed files with 26 additions and 3 deletions

View File

@@ -3,7 +3,22 @@
<script id="sheet">
{
// Disconnected style element should have no sheet
testing.expectEqual(null, document.createElement('style').sheet);
// Connected style element should have a CSSStyleSheet
const style = document.createElement('style');
document.head.appendChild(style);
testing.expectEqual(true, style.sheet instanceof CSSStyleSheet);
// Same sheet instance on repeated access
testing.expectEqual(true, style.sheet === style.sheet);
// Non-CSS type should have no sheet
const lessStyle = document.createElement('style');
lessStyle.type = 'text/less';
document.head.appendChild(lessStyle);
testing.expectEqual(null, lessStyle.sheet);
}
</script>

View File

@@ -16,6 +16,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const js = @import("../../../js/js.zig");
const Page = @import("../../../Page.zig");
@@ -25,6 +26,7 @@ const HtmlElement = @import("../Html.zig");
const Style = @This();
_proto: *HtmlElement,
_sheet: ?*CSSStyleSheet = null,
pub fn asElement(self: *Style) *Element {
return self._proto._proto;
@@ -75,9 +77,15 @@ pub fn setDisabled(self: *Style, disabled: bool, page: *Page) !void {
}
const CSSStyleSheet = @import("../../css/CSSStyleSheet.zig");
pub fn getSheet(_: *const Style) ?*CSSStyleSheet {
// TODO?
return null;
pub fn getSheet(self: *Style, page: *Page) !?*CSSStyleSheet {
// Per spec, sheet is null for disconnected elements or non-CSS types.
if (!self.asNode().isConnected()) return null;
if (!std.mem.eql(u8, self.getType(), "text/css")) return null;
if (self._sheet) |sheet| return sheet;
const sheet = try CSSStyleSheet.init(page);
self._sheet = sheet;
return sheet;
}
pub const JsApi = struct {