Skip to content

Commit a0226dc

Browse files
committed
Add Script get/set nonce
When we toInterface(node), if node is a DocumentFragment with a host, then return the host. Both changes for reddit.
1 parent 6d3065c commit a0226dc

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/browser/dom/node.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,19 @@ pub const Node = struct {
8686
},
8787
.document_type => .{ .DocumentType = @as(*parser.DocumentType, @ptrCast(node)) },
8888
.attribute => .{ .Attr = @as(*parser.Attribute, @ptrCast(node)) },
89-
.document_fragment => .{ .DocumentFragment = @as(*parser.DocumentFragment, @ptrCast(node)) },
89+
.document_fragment => blk: {
90+
const df: *parser.DocumentFragment = @ptrCast(node);
91+
if (parser.documentFragmentGetHost(df)) |host| {
92+
// If the DocumentFragment has a "host", then it's 100%
93+
// acting as the root of the ShadowDOM. And I *think*, in
94+
// those cases, if we end up with that DocumentFragment, we
95+
// should actually return the host. For example, if there's
96+
// A MutationObservation on the DocumentFragment, it's the
97+
// Host which we should treat as the target. I think.
98+
break :blk toInterface(host);
99+
}
100+
break :blk .{ .DocumentFragment = df };
101+
},
90102
else => @panic("node type not handled"), // TODO
91103
};
92104
}

src/browser/html/elements.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,21 @@ pub const HTMLScriptElement = struct {
977977
return try parser.elementRemoveAttribute(parser.scriptToElt(self), "nomodule");
978978
}
979979

980+
pub fn get_nonce(self: *parser.Script) !?[]const u8 {
981+
return try parser.elementGetAttribute(
982+
parser.scriptToElt(self),
983+
"nonce",
984+
) orelse "";
985+
}
986+
987+
pub fn set_nonce(self: *parser.Script, v: bool) !void {
988+
if (v) {
989+
return try parser.elementSetAttribute(parser.scriptToElt(self), "nonce", "");
990+
}
991+
992+
return try parser.elementRemoveAttribute(parser.scriptToElt(self), "nonce");
993+
}
994+
980995
pub fn get_onload(self: *parser.Script, page: *Page) !?Env.Function {
981996
const state = page.getNodeState(@ptrCast(@alignCast(self))) orelse return null;
982997
return state.onload;

src/tests/html/script/script.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111

1212
script.defer = true;
1313
testing.expectEqual(true, script.defer);
14+
15+
testing.expectEqual('', script.nonce);
16+
script.nonce = 'hello';
17+
testing.expectEqual('hello', script.nonce);
1418
</script>

0 commit comments

Comments
 (0)