Merge pull request #659 from lightpanda-io/dedup-document

Deduplicate document
This commit is contained in:
Karl Seguin
2025-05-19 19:07:16 +08:00
committed by GitHub
10 changed files with 9 additions and 15 deletions

View File

@@ -30,7 +30,7 @@ pub const Comment = struct {
pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Comment { pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Comment {
return parser.documentCreateComment( return parser.documentCreateComment(
parser.documentHTMLToDocument(state.document.?), parser.documentHTMLToDocument(state.window.document.?),
data orelse "", data orelse "",
); );
} }

View File

@@ -41,12 +41,12 @@ pub const Document = struct {
pub fn constructor(state: *const SessionState) !*parser.DocumentHTML { pub fn constructor(state: *const SessionState) !*parser.DocumentHTML {
const doc = try parser.documentCreateDocument( const doc = try parser.documentCreateDocument(
try parser.documentHTMLGetTitle(state.document.?), try parser.documentHTMLGetTitle(state.window.document.?),
); );
// we have to work w/ document instead of html document. // we have to work w/ document instead of html document.
const ddoc = parser.documentHTMLToDocument(doc); const ddoc = parser.documentHTMLToDocument(doc);
const ccur = parser.documentHTMLToDocument(state.document.?); const ccur = parser.documentHTMLToDocument(state.window.document.?);
try parser.documentSetDocumentURI(ddoc, try parser.documentGetDocumentURI(ccur)); try parser.documentSetDocumentURI(ddoc, try parser.documentGetDocumentURI(ccur));
try parser.documentSetInputEncoding(ddoc, try parser.documentGetInputEncoding(ccur)); try parser.documentSetInputEncoding(ddoc, try parser.documentGetInputEncoding(ccur));

View File

@@ -29,7 +29,7 @@ pub const DocumentFragment = struct {
pub fn constructor(state: *const SessionState) !*parser.DocumentFragment { pub fn constructor(state: *const SessionState) !*parser.DocumentFragment {
return parser.documentCreateDocumentFragment( return parser.documentCreateDocumentFragment(
parser.documentHTMLToDocument(state.document.?), parser.documentHTMLToDocument(state.window.document.?),
); );
} }

View File

@@ -50,7 +50,7 @@ pub const IntersectionObserver = struct {
// new IntersectionObserver(callback, options) [not supported yet] // new IntersectionObserver(callback, options) [not supported yet]
pub fn constructor(callback: Env.Callback, options_: ?IntersectionObserverOptions, state: *SessionState) !IntersectionObserver { pub fn constructor(callback: Env.Callback, options_: ?IntersectionObserverOptions, state: *SessionState) !IntersectionObserver {
var options = IntersectionObserverOptions{ var options = IntersectionObserverOptions{
.root = parser.documentToNode(parser.documentHTMLToDocument(state.document.?)), .root = parser.documentToNode(parser.documentHTMLToDocument(state.window.document.?)),
.rootMargin = "0px 0px 0px 0px", .rootMargin = "0px 0px 0px 0px",
.threshold = &.{0.0}, .threshold = &.{0.0},
}; };
@@ -142,7 +142,7 @@ pub const IntersectionObserverEntry = struct {
// Returns a DOMRectReadOnly for the intersection observer's root. // Returns a DOMRectReadOnly for the intersection observer's root.
pub fn get_rootBounds(self: *const IntersectionObserverEntry) !Element.DOMRect { pub fn get_rootBounds(self: *const IntersectionObserverEntry) !Element.DOMRect {
const root = self.options.root.?; const root = self.options.root.?;
if (@intFromPtr(root) == @intFromPtr(self.state.document.?)) { if (@intFromPtr(root) == @intFromPtr(self.state.window.document.?)) {
return self.state.renderer.boundingRect(); return self.state.renderer.boundingRect();
} }

View File

@@ -41,7 +41,7 @@ pub const ProcessingInstruction = struct {
// a simple workaround. // a simple workaround.
pub fn _cloneNode(self: *parser.ProcessingInstruction, _: ?bool, state: *SessionState) !*parser.ProcessingInstruction { pub fn _cloneNode(self: *parser.ProcessingInstruction, _: ?bool, state: *SessionState) !*parser.ProcessingInstruction {
return try parser.documentCreateProcessingInstruction( return try parser.documentCreateProcessingInstruction(
@ptrCast(state.document), @ptrCast(state.window.document.?),
try get_target(self), try get_target(self),
(try get_data(self)) orelse "", (try get_data(self)) orelse "",
); );

View File

@@ -34,7 +34,7 @@ pub const Text = struct {
pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Text { pub fn constructor(data: ?[]const u8, state: *const SessionState) !*parser.Text {
return parser.documentCreateTextNode( return parser.documentCreateTextNode(
parser.documentHTMLToDocument(state.document.?), parser.documentHTMLToDocument(state.window.document.?),
data orelse "", data orelse "",
); );
} }

View File

@@ -56,7 +56,6 @@ pub const SessionState = struct {
arena: std.mem.Allocator, arena: std.mem.Allocator,
http_client: *HttpClient, http_client: *HttpClient,
cookie_jar: *storage.CookieJar, cookie_jar: *storage.CookieJar,
document: ?*parser.DocumentHTML,
// dangerous, but set by the JS framework // dangerous, but set by the JS framework
// shorter-lived than the arena above, which // shorter-lived than the arena above, which

View File

@@ -95,7 +95,6 @@ pub const Page = struct {
.window_clicked_event_node = .{ .func = windowClicked }, .window_clicked_event_node = .{ .func = windowClicked },
.state = .{ .state = .{
.arena = arena, .arena = arena,
.document = null,
.url = &self.url, .url = &self.url,
.window = &self.window, .window = &self.window,
.renderer = &self.renderer, .renderer = &self.renderer,
@@ -277,9 +276,6 @@ pub const Page = struct {
// https://html.spec.whatwg.org/#read-html // https://html.spec.whatwg.org/#read-html
// update the sessions state
self.state.document = html_doc;
// browse the DOM tree to retrieve scripts // browse the DOM tree to retrieve scripts
// TODO execute the synchronous scripts during the HTL parsing. // TODO execute the synchronous scripts during the HTL parsing.
// TODO fetch the script resources concurrently but execute them in the // TODO fetch the script resources concurrently but execute them in the

View File

@@ -116,7 +116,7 @@ fn run(arena: Allocator, test_file: []const u8, loader: *FileLoader, err_out: *?
try polyfill.load(arena, runner.scope); try polyfill.load(arena, runner.scope);
// loop over the scripts. // loop over the scripts.
const doc = parser.documentHTMLToDocument(runner.state.document.?); const doc = parser.documentHTMLToDocument(runner.state.window.document.?);
const scripts = try parser.documentGetElementsByTagName(doc, "script"); const scripts = try parser.documentGetElementsByTagName(doc, "script");
const script_count = try parser.nodeListLength(scripts); const script_count = try parser.nodeListLength(scripts);
for (0..script_count) |i| { for (0..script_count) |i| {

View File

@@ -421,7 +421,6 @@ pub const JsRunner = struct {
self.state = .{ self.state = .{
.arena = arena, .arena = arena,
.loop = &self.loop, .loop = &self.loop,
.document = document,
.url = &self.url, .url = &self.url,
.window = &self.window, .window = &self.window,
.renderer = &self.renderer, .renderer = &self.renderer,