Skip to content

Commit 30679d1

Browse files
Merge pull request lightpanda-io#271 from lightpanda-io/currentscript
implement DOM document.currentscript
2 parents 4b5668f + 95c0ff6 commit 30679d1

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/browser/browser.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ pub const Page = struct {
411411
// > immediately before the browser continues to parse the
412412
// > page.
413413
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#notes
414+
try parser.documentHTMLSetCurrentScript(html_doc, @ptrCast(e));
414415
self.evalScript(e) catch |err| log.warn("evaljs: {any}", .{err});
416+
try parser.documentHTMLSetCurrentScript(html_doc, null);
415417
}
416418

417419
// TODO wait for deferred scripts
@@ -428,7 +430,9 @@ pub const Page = struct {
428430

429431
// eval async scripts.
430432
for (sasync.items) |e| {
433+
try parser.documentHTMLSetCurrentScript(html_doc, @ptrCast(e));
431434
self.evalScript(e) catch |err| log.warn("evaljs: {any}", .{err});
435+
try parser.documentHTMLSetCurrentScript(html_doc, null);
432436
}
433437

434438
// TODO wait for async scripts

src/html/document.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ pub const HTMLDocument = struct {
153153
return try collection.HTMLCollectionAll(parser.documentHTMLToNode(self), true);
154154
}
155155

156-
pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element {
157-
return null;
156+
pub fn get_currentScript(self: *parser.DocumentHTML) !?*parser.Script {
157+
return try parser.documentHTMLGetCurrentScript(self);
158158
}
159159

160160
pub fn get_designMode(_: *parser.DocumentHTML) []const u8 {

src/netsurf/netsurf.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,3 +2249,18 @@ pub inline fn documentHTMLSetTitle(doc: *DocumentHTML, v: []const u8) !void {
22492249
const err = documentHTMLVtable(doc).set_title.?(doc, try strFromData(v));
22502250
try DOMErr(err);
22512251
}
2252+
2253+
pub fn documentHTMLSetCurrentScript(doc: *DocumentHTML, script: ?*Script) !void {
2254+
var s: ?*ElementHTML = null;
2255+
if (script != null) s = @ptrCast(script.?);
2256+
const err = documentHTMLVtable(doc).set_current_script.?(doc, s);
2257+
try DOMErr(err);
2258+
}
2259+
2260+
pub fn documentHTMLGetCurrentScript(doc: *DocumentHTML) !?*Script {
2261+
var elem: ?*ElementHTML = undefined;
2262+
const err = documentHTMLVtable(doc).get_current_script.?(doc, &elem);
2263+
try DOMErr(err);
2264+
if (elem == null) return null;
2265+
return @ptrCast(elem.?);
2266+
}

0 commit comments

Comments
 (0)