Skip to content

Commit 9f1cc09

Browse files
committed
add HTML Template's content attribute
1 parent 5dcc3db commit 9f1cc09

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/browser/State.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ active_element: ?*parser.Element = null,
5858
// default (by returning selectedIndex == 0).
5959
explicit_index_set: bool = false,
6060

61+
template_content: ?*parser.DocumentFragment = null,
62+
6163
const ReadyState = enum {
6264
loading,
6365
interactive,

src/browser/dom/document_fragment.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,17 @@ test "Browser.DOM.DocumentFragment" {
7171
.{ "dc1.isEqualNode(dc1)", "true" },
7272
.{ "dc1.isEqualNode(dc2)", "true" },
7373
}, .{});
74+
75+
try runner.testCases(&.{
76+
.{ "let f = document.createDocumentFragment()", null },
77+
.{ "let d = document.createElement('div');", null },
78+
.{ "d.id = 'x';", null },
79+
.{ "document.getElementById('x') == null;", "true" },
80+
81+
.{ "f.append(d);", null },
82+
.{ "document.getElementById('x') == null;", "true" },
83+
84+
.{ "document.getElementsByTagName('body')[0].append(f.cloneNode(true));", null },
85+
.{ "document.getElementById('x') != null;", "true" },
86+
}, .{});
7487
}

src/browser/html/elements.zig

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,16 @@ pub const HTMLTemplateElement = struct {
12651265
pub fn constructor(page: *Page, js_this: Env.JsThis) !*parser.Element {
12661266
return constructHtmlElement(page, js_this);
12671267
}
1268+
1269+
pub fn get_content(self: *parser.Template, page: *Page) !*parser.DocumentFragment {
1270+
const state = try page.getOrCreateNodeState(@alignCast(@ptrCast(self)));
1271+
if (state.template_content) |tc| {
1272+
return tc;
1273+
}
1274+
const tc = try parser.documentCreateDocumentFragment(@ptrCast(page.window.document));
1275+
state.template_content = tc;
1276+
return tc;
1277+
}
12681278
};
12691279

12701280
pub const HTMLTextAreaElement = struct {
@@ -1550,7 +1560,8 @@ test "Browser.HTML.Element" {
15501560
.{ "document.activeElement === focused", "true" },
15511561
}, .{});
15521562
}
1553-
test "Browser.HTML.HtmlInputElement.propeties" {
1563+
1564+
test "Browser.HTML.HtmlInputElement.properties" {
15541565
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io/noslashattheend" });
15551566
defer runner.deinit();
15561567
var alloc = std.heap.ArenaAllocator.init(runner.app.allocator);
@@ -1634,7 +1645,8 @@ test "Browser.HTML.HtmlInputElement.propeties" {
16341645
.{ "input_value.value", "mango" }, // Still mango
16351646
}, .{});
16361647
}
1637-
test "Browser.HTML.HtmlInputElement.propeties.form" {
1648+
1649+
test "Browser.HTML.HtmlInputElement.properties.form" {
16381650
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html =
16391651
\\ <form action="test.php" target="_blank">
16401652
\\ <p>
@@ -1652,6 +1664,21 @@ test "Browser.HTML.HtmlInputElement.propeties.form" {
16521664
}, .{});
16531665
}
16541666

1667+
test "Browser.HTML.HTMLTemplateElement" {
1668+
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "<div id=c></div>" });
1669+
defer runner.deinit();
1670+
1671+
try runner.testCases(&.{
1672+
.{ "let t = document.createElement('template')", null },
1673+
.{ "let d = document.createElement('div')", null },
1674+
.{ "d.id = 'abc'", null },
1675+
.{ "t.content.append(d)", null },
1676+
.{ "document.getElementById('abc')", "null" },
1677+
.{ "document.getElementById('c').appendChild(t.content.cloneNode(true))", null },
1678+
.{ "document.getElementById('abc').id", "abc" },
1679+
}, .{});
1680+
}
1681+
16551682
const Check = struct {
16561683
input: []const u8,
16571684
expected: ?[]const u8 = null, // Needed when input != expected

0 commit comments

Comments
 (0)