add HTMLStyleElement.get_sheet

This commit is contained in:
Karl Seguin
2025-07-15 10:59:59 +08:00
parent d35a3eab6c
commit 1bd430598d
2 changed files with 33 additions and 4 deletions

View File

@@ -30,6 +30,7 @@ const Env = @import("env.zig").Env;
const parser = @import("netsurf.zig"); const parser = @import("netsurf.zig");
const DataSet = @import("html/DataSet.zig"); const DataSet = @import("html/DataSet.zig");
const ShadowRoot = @import("dom/shadow_root.zig").ShadowRoot; const ShadowRoot = @import("dom/shadow_root.zig").ShadowRoot;
const StyleSheet = @import("cssom/stylesheet.zig").StyleSheet;
const CSSStyleDeclaration = @import("cssom/css_style_declaration.zig").CSSStyleDeclaration; const CSSStyleDeclaration = @import("cssom/css_style_declaration.zig").CSSStyleDeclaration;
// for HTMLScript (but probably needs to be added to more) // for HTMLScript (but probably needs to be added to more)
@@ -39,10 +40,17 @@ onerror: ?Env.Function = null,
// for HTMLElement // for HTMLElement
style: CSSStyleDeclaration = .empty, style: CSSStyleDeclaration = .empty,
dataset: ?DataSet = null, dataset: ?DataSet = null,
template_content: ?*parser.DocumentFragment = null,
// For dom/element
shadow_root: ?*ShadowRoot = null,
// for html/document // for html/document
ready_state: ReadyState = .loading, ready_state: ReadyState = .loading,
// for html/HTMLStyleElement
style_sheet: ?*StyleSheet = null,
// for dom/document // for dom/document
active_element: ?*parser.Element = null, active_element: ?*parser.Element = null,
@@ -61,10 +69,6 @@ active_element: ?*parser.Element = null,
// default (by returning selectedIndex == 0). // default (by returning selectedIndex == 0).
explicit_index_set: bool = false, explicit_index_set: bool = false,
template_content: ?*parser.DocumentFragment = null,
shadow_root: ?*ShadowRoot = null,
const ReadyState = enum { const ReadyState = enum {
loading, loading,
interactive, interactive,

View File

@@ -29,6 +29,7 @@ const Node = @import("../dom/node.zig").Node;
const Element = @import("../dom/element.zig").Element; const Element = @import("../dom/element.zig").Element;
const DataSet = @import("DataSet.zig"); const DataSet = @import("DataSet.zig");
const StyleSheet = @import("../cssom/stylesheet.zig").StyleSheet;
const CSSStyleDeclaration = @import("../cssom/css_style_declaration.zig").CSSStyleDeclaration; const CSSStyleDeclaration = @import("../cssom/css_style_declaration.zig").CSSStyleDeclaration;
// HTMLElement interfaces // HTMLElement interfaces
@@ -1012,6 +1013,18 @@ pub const HTMLStyleElement = struct {
pub const Self = parser.Style; pub const Self = parser.Style;
pub const prototype = *HTMLElement; pub const prototype = *HTMLElement;
pub const subtype = .node; pub const subtype = .node;
pub fn get_sheet(self: *parser.Style, page: *Page) !*StyleSheet {
const state = try page.getOrCreateNodeState(@alignCast(@ptrCast(self)));
if (state.style_sheet) |ss| {
return ss;
}
const ss = try page.arena.create(StyleSheet);
ss.* = .{};
state.style_sheet = ss;
return ss;
}
}; };
pub const HTMLTableElement = struct { pub const HTMLTableElement = struct {
@@ -1453,6 +1466,18 @@ test "Browser.HTML.HTMLTemplateElement" {
}, .{}); }, .{});
} }
test "Browser.HTML.HTMLStyleElement" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "" });
defer runner.deinit();
try runner.testCases(&.{
.{ "let s = document.createElement('style')", null },
.{ "s.sheet.type", "text/css" },
.{ "s.sheet == s.sheet", "true" },
.{ "document.createElement('style').sheet == s.sheet", "false" },
}, .{});
}
const Check = struct { const Check = struct {
input: []const u8, input: []const u8,
expected: ?[]const u8 = null, // Needed when input != expected expected: ?[]const u8 = null, // Needed when input != expected