Skip to content

Commit b7fd4e9

Browse files
authored
Merge pull request #894 from lightpanda-io/HTMLStyleElement_get_sheet
add HTMLStyleElement.get_sheet
2 parents b6341c1 + 1bd4305 commit b7fd4e9

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/browser/State.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const Env = @import("env.zig").Env;
3030
const parser = @import("netsurf.zig");
3131
const DataSet = @import("html/DataSet.zig");
3232
const ShadowRoot = @import("dom/shadow_root.zig").ShadowRoot;
33+
const StyleSheet = @import("cssom/stylesheet.zig").StyleSheet;
3334
const CSSStyleDeclaration = @import("cssom/css_style_declaration.zig").CSSStyleDeclaration;
3435

3536
// for HTMLScript (but probably needs to be added to more)
@@ -39,10 +40,17 @@ onerror: ?Env.Function = null,
3940
// for HTMLElement
4041
style: CSSStyleDeclaration = .empty,
4142
dataset: ?DataSet = null,
43+
template_content: ?*parser.DocumentFragment = null,
44+
45+
// For dom/element
46+
shadow_root: ?*ShadowRoot = null,
4247

4348
// for html/document
4449
ready_state: ReadyState = .loading,
4550

51+
// for html/HTMLStyleElement
52+
style_sheet: ?*StyleSheet = null,
53+
4654
// for dom/document
4755
active_element: ?*parser.Element = null,
4856

@@ -61,10 +69,6 @@ active_element: ?*parser.Element = null,
6169
// default (by returning selectedIndex == 0).
6270
explicit_index_set: bool = false,
6371

64-
template_content: ?*parser.DocumentFragment = null,
65-
66-
shadow_root: ?*ShadowRoot = null,
67-
6872
const ReadyState = enum {
6973
loading,
7074
interactive,

src/browser/html/elements.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const Node = @import("../dom/node.zig").Node;
2929
const Element = @import("../dom/element.zig").Element;
3030
const DataSet = @import("DataSet.zig");
3131

32+
const StyleSheet = @import("../cssom/stylesheet.zig").StyleSheet;
3233
const CSSStyleDeclaration = @import("../cssom/css_style_declaration.zig").CSSStyleDeclaration;
3334

3435
// HTMLElement interfaces
@@ -1012,6 +1013,18 @@ pub const HTMLStyleElement = struct {
10121013
pub const Self = parser.Style;
10131014
pub const prototype = *HTMLElement;
10141015
pub const subtype = .node;
1016+
1017+
pub fn get_sheet(self: *parser.Style, page: *Page) !*StyleSheet {
1018+
const state = try page.getOrCreateNodeState(@alignCast(@ptrCast(self)));
1019+
if (state.style_sheet) |ss| {
1020+
return ss;
1021+
}
1022+
1023+
const ss = try page.arena.create(StyleSheet);
1024+
ss.* = .{};
1025+
state.style_sheet = ss;
1026+
return ss;
1027+
}
10151028
};
10161029

10171030
pub const HTMLTableElement = struct {
@@ -1453,6 +1466,18 @@ test "Browser.HTML.HTMLTemplateElement" {
14531466
}, .{});
14541467
}
14551468

1469+
test "Browser.HTML.HTMLStyleElement" {
1470+
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "" });
1471+
defer runner.deinit();
1472+
1473+
try runner.testCases(&.{
1474+
.{ "let s = document.createElement('style')", null },
1475+
.{ "s.sheet.type", "text/css" },
1476+
.{ "s.sheet == s.sheet", "true" },
1477+
.{ "document.createElement('style').sheet == s.sheet", "false" },
1478+
}, .{});
1479+
}
1480+
14561481
const Check = struct {
14571482
input: []const u8,
14581483
expected: ?[]const u8 = null, // Needed when input != expected

0 commit comments

Comments
 (0)