Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/browser/State.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const Env = @import("env.zig").Env;
const parser = @import("netsurf.zig");
const DataSet = @import("html/DataSet.zig");
const ShadowRoot = @import("dom/shadow_root.zig").ShadowRoot;
const StyleSheet = @import("cssom/stylesheet.zig").StyleSheet;
const CSSStyleDeclaration = @import("cssom/css_style_declaration.zig").CSSStyleDeclaration;

// for HTMLScript (but probably needs to be added to more)
Expand All @@ -39,10 +40,17 @@ onerror: ?Env.Function = null,
// for HTMLElement
style: CSSStyleDeclaration = .empty,
dataset: ?DataSet = null,
template_content: ?*parser.DocumentFragment = null,

// For dom/element
shadow_root: ?*ShadowRoot = null,

// for html/document
ready_state: ReadyState = .loading,

// for html/HTMLStyleElement
style_sheet: ?*StyleSheet = null,

// for dom/document
active_element: ?*parser.Element = null,

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

template_content: ?*parser.DocumentFragment = null,

shadow_root: ?*ShadowRoot = null,

const ReadyState = enum {
loading,
interactive,
Expand Down
25 changes: 25 additions & 0 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const Node = @import("../dom/node.zig").Node;
const Element = @import("../dom/element.zig").Element;
const DataSet = @import("DataSet.zig");

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

// HTMLElement interfaces
Expand Down Expand Up @@ -1012,6 +1013,18 @@ pub const HTMLStyleElement = struct {
pub const Self = parser.Style;
pub const prototype = *HTMLElement;
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 {
Expand Down Expand Up @@ -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 {
input: []const u8,
expected: ?[]const u8 = null, // Needed when input != expected
Expand Down