Lightpanda: the headless browser designed for AI and automation
Test cross validate
Karl Seguin5758bf6f5147
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex c4d4b579e..48da92d5f 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -1630,9 +1630,9 @@ fn _documentIsComplete(self: *Frame) !void {try self.dispatchQueuedEvents();if (self.loadEventsAborted()) return;- // A close() from within our own load handler: the outer dispatch- // finishes the load steps.- if (self._load_event_in_progress) return;+ // A close() from within our own load handler, or from the iframe+ // element's (the tail of our load steps): no nested load (as in Chrome).+ if (self._load_event_in_progress or self.document._iframe_load_in_progress) return;// Dispatch window.load event.const window_target = self.window.asEventTarget();diff --git a/src/browser/tests/document/all_collection.html b/src/browser/tests/document/all_collection.htmlindex 9711e5a28..c92e2acb5 100644--- a/src/browser/tests/document/all_collection.html+++ b/src/browser/tests/document/all_collection.html@@ -1,6 +1,7 @@<!DOCTYPE html><html><head>+ <meta name=lp-mode content=test><title>Test Page</title></head><body>@@ -17,14 +18,14 @@testing.expectEqual(false, document.all === undefined);testing.expectEqual(true, !document.all);testing.expectEqual(false, !!document.all);-- testing.expectEqual(10, document.all.length);+ testing.expectEqual(11, document.all.length);testing.expectEqual('HTML', document.all[0].tagName);testing.expectEqual('HEAD', document.all[1].tagName);- testing.expectEqual('TITLE', document.all[2].tagName);- testing.expectEqual('BODY', document.all[3].tagName);- testing.expectEqual('DIV', document.all[4].tagName);+ testing.expectEqual('META', document.all[2].tagName);+ testing.expectEqual('TITLE', document.all[3].tagName);+ testing.expectEqual('BODY', document.all[4].tagName);+ testing.expectEqual('DIV', document.all[5].tagName);testing.expectEqual('DIV', document.all.first.tagName);testing.expectEqual('First', document.all.first.textContent);@@ -36,7 +37,7 @@testing.expectEqual('A', document.all.link.tagName);testing.expectEqual(undefined, document.all.second);- testing.expectEqual('SPAN', document.all.item(5).tagName);+ testing.expectEqual('SPAN', document.all.item(6).tagName);testing.expectEqual(null, document.all.item(999));testing.expectEqual(null, document.all.item(-1));@@ -48,19 +49,19 @@// Test callable functionality: document.all(index) and document.all(name)testing.expectEqual('HTML', document.all(0).tagName);testing.expectEqual('HEAD', document.all(1).tagName);- testing.expectEqual('DIV', document.all(4).tagName);+ testing.expectEqual('DIV', document.all(5).tagName);testing.expectEqual('DIV', document.all('first').tagName);testing.expectEqual('First', document.all('first').textContent);- testing.expectEqual(undefined, document.all('second'));+ testing.expectEqual(null, document.all('second'));testing.expectEqual('P', document.all('third').tagName);- testing.expectEqual(undefined, document.all(999));- testing.expectEqual(undefined, document.all('nonexistent'));+ testing.expectEqual(null, document.all(999));+ testing.expectEqual(null, document.all('nonexistent'));let count = 0;for (const el of document.all) {count++;}- testing.expectEqual(10, count);+ testing.expectEqual(11, count);const plainDoc = new Document();testing.expectEqual('undefined', typeof plainDoc.all);diff --git a/src/browser/tests/document/dir_lang.html b/src/browser/tests/document/dir_lang.htmlindex c5e5415b0..836641cda 100644--- a/src/browser/tests/document/dir_lang.html+++ b/src/browser/tests/document/dir_lang.html@@ -1,5 +1,6 @@<html lang="en" dir="ltr"><head>+ <meta name=lp-mode content=test><script src="../testing.js"></script></head><body>@@ -8,9 +9,12 @@testing.expectEqual('ltr', document.documentElement.dir);testing.expectEqual('en', document.documentElement.lang);- // Document.dir and Document.lang delegate to documentElement+ // Document.dir delegates to documentElementtesting.expectEqual('ltr', document.dir);- testing.expectEqual('en', document.lang);++ // Document has no lang property, setting one is a plain expando+ testing.expectEqual(undefined, document.lang);+ testing.expectEqual(false, 'lang' in document);// Setting via document updates the documentElement attributedocument.dir = 'rtl';@@ -18,16 +22,13 @@testing.expectEqual('rtl', document.documentElement.getAttribute('dir'));document.lang = 'fr';- testing.expectEqual('fr', document.documentElement.lang);- testing.expectEqual('fr', document.documentElement.getAttribute('lang'));+ testing.expectEqual('en', document.documentElement.lang);+ testing.expectEqual('en', document.documentElement.getAttribute('lang'));// Setting via element is reflected in documentdocument.documentElement.dir = 'ltr';testing.expectEqual('ltr', document.dir);- document.documentElement.lang = 'de';- testing.expectEqual('de', document.lang);-// div elements also have dir and langconst div = document.createElement('div');testing.expectEqual('', div.dir);diff --git a/src/browser/tests/document/get_elements_by_tag_name.html b/src/browser/tests/document/get_elements_by_tag_name.htmlindex e440db6d1..9b2ad79b4 100644--- a/src/browser/tests/document/get_elements_by_tag_name.html+++ b/src/browser/tests/document/get_elements_by_tag_name.html@@ -1,4 +1,5 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script><div>0</div><div>1</div>@@ -25,7 +26,7 @@<script id=basic>{const all = document.getElementsByTagName('*');- testing.expectEqual(24, all.length);+ testing.expectEqual(25, all.length);testing.expectEqual(false, "values" in all);testing.expectEqual(false, "entries" in all);testing.expectEqual(false, "forEach" in all);diff --git a/src/browser/tests/document/named_properties.html b/src/browser/tests/document/named_properties.htmlindex b808e925e..3dd51d478 100644--- a/src/browser/tests/document/named_properties.html+++ b/src/browser/tests/document/named_properties.html@@ -1,4 +1,5 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script><form name="form1" id="f1">@@ -20,7 +21,20 @@testing.expectEqual(form, document['form1']);testing.expectEqual('hello', document.form1.field.value);testing.expectTrue('form1' in document);- testing.expectFalse(Object.keys(document).includes('form1'));+}+</script>++<script id=enumerable>+{+ // Document named properties are enumerable (no [LegacyUnenumerableNamedProperties]),+ // and location is an own property ([LegacyUnforgeable]). An img/object+ // contributes its id before its name, duplicates are dropped.+ testing.expectEqual(+ ['location', 'form1', 'dup', 'dup2', 'img_by_id', 'pic', 'obj_by_id', 'getElementById', 'frame1'],+ Object.keys(document)+ );+ testing.expectTrue(Object.getOwnPropertyDescriptor(document, 'form1').enumerable);+ testing.expectEqual(undefined, Object.getOwnPropertyDescriptor(Document.prototype, 'location'));}</script>@@ -39,7 +53,9 @@</script><script id=builtins>- testing.expectEqual('function', typeof document.getElementById);+ testing.diverges('Chrome correctly allows elements to clobber document functions [LegacyOverrideBuiltIns]', () => {+ testing.expectEqual('function', typeof document.getElementById);+ });</script><script id=multiple>diff --git a/src/browser/tests/document/open_load_events.html b/src/browser/tests/document/open_load_events.htmlindex 39249a4a1..f388f3fd5 100644--- a/src/browser/tests/document/open_load_events.html+++ b/src/browser/tests/document/open_load_events.html@@ -1,4 +1,5 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script><iframe id=loaded src="about:blank"></iframe>@@ -162,7 +163,9 @@<!--https://html.spec.whatwg.org/multipage/iframe-embed-object.html#iframe-load-event-steps- open() from within the iframe element's load event mutes its later ones.+ open() from within the iframe element's load event mutes its later ones. That+ event is the tail of the iframe document's load, so, as from a window load+ handler, close() fires DOMContentLoaded but no nested window load (as in Chrome).--><script id=open_during_iframe_load_mutes type=module>{@@ -171,24 +174,25 @@const iframe = document.createElement('iframe');let loads = 0;let window_loads = 0;+ let dcls = 0;iframe.addEventListener('load', () => {loads += 1;if (loads > 1) return;const doc = iframe.contentDocument;doc.open();- iframe.contentWindow.addEventListener('load', () => {- window_loads += 1;- // a (wrongly) unmuted element load fires right after this one- setTimeout(() => state.resolve(), 0);- });+ doc.addEventListener('DOMContentLoaded', () => dcls += 1);+ iframe.contentWindow.addEventListener('load', () => window_loads += 1);doc.write('<p>muted</p>');doc.close();+ setTimeout(() => state.resolve(), 20);});document.body.appendChild(iframe);await state.done(() => {testing.expectEqual(1, loads);- testing.expectEqual(1, window_loads);+ testing.expectEqual(1, dcls);+ testing.expectEqual(0, window_loads);+ testing.expectEqual('muted', iframe.contentDocument.body.textContent);});}</script>diff --git a/src/browser/tests/document/query_selector_attributes.html b/src/browser/tests/document/query_selector_attributes.htmlindex 7caad780d..e12933260 100644--- a/src/browser/tests/document/query_selector_attributes.html+++ b/src/browser/tests/document/query_selector_attributes.html@@ -1,4 +1,5 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script><div id="container">@@ -137,11 +138,15 @@testing.expectEqual('caseLink', document.querySelector('a[target=_blank][rel~=nofollow][hreflang|=en]')?.id);// An explicit `s` flag keeps them case-sensitive- testing.expectEqual(null, document.querySelector('form[method=post s]'));- testing.expectEqual(null, document.querySelector('a[rel~=nofollow s]'));+ testing.diverges("Chrome doesn't support the s flag", () => {+ testing.expectEqual(null, document.querySelector('form[method=post s]'));+ testing.expectEqual(null, document.querySelector('a[rel~=nofollow s]'));+ });// Other attributes and foreign elements stay case-sensitivetesting.expectEqual(null, document.querySelector('[name=q]'));- testing.expectEqual(null, document.querySelector('#caseSvg[type=matrix]'));+ testing.diverges("Chrome doesn't enforce this case sensitivity", () => {+ testing.expectEqual(null, document.querySelector('#caseSvg[type=matrix]'));+ });testing.expectEqual('caseSvg', document.querySelector('#caseSvg[type=matrix i]')?.id);</script>diff --git a/src/browser/tests/document/set_body.html b/src/browser/tests/document/set_body.htmlindex 09e268479..f6ce1bca5 100644--- a/src/browser/tests/document/set_body.html+++ b/src/browser/tests/document/set_body.html@@ -1,80 +1,137 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script>+<!-- the iframes replace their body while the parser is still inside it --><iframe id=isimple src=set_body/simple.html></iframe><script id=simple>- {- testing.onload(() => {- testing.expectEqual(- '<head></head><body><p>replaced</p></body>',- isimple.contentDocument.documentElement.innerHTML,- );- // The original body element is gone; a fresh one is in place.- testing.expectEqual(null, isimple.contentDocument.getElementById('orig'));- testing.expectEqual(true, isimple.contentDocument.body instanceof isimple.contentWindow.HTMLBodyElement);- });- }+ testing.onload(() => {+ const doc = isimple.contentDocument;+ testing.expectEqual('<head></head><body><p>replaced</p></body>', doc.documentElement.innerHTML);+ testing.expectEqual(null, doc.getElementById('orig'));+ testing.expectEqual(true, doc.body instanceof isimple.contentWindow.HTMLBodyElement);+ });</script><iframe id=inone src=set_body/no_body.html></iframe><script id=no_body>- {- testing.onload(() => {- // Document only had a <head>; setting body should append a fresh body.- testing.expectEqual(- '<head><title>no_body</title></head>\n<body><p>appended</p></body>',- inone.contentDocument.documentElement.innerHTML,- );- });- }+ testing.onload(() => {+ testing.expectEqual(+ '<head><title>no_body</title></head>\n<body><p>appended</p></body>',+ inone.contentDocument.documentElement.innerHTML,+ );+ });</script><iframe id=iempty src=set_body/empty.html></iframe><script id=empty>- {- testing.onload(() => {- // Setting body to the empty string yields an empty <body>.- testing.expectEqual(- '<head></head><body></body>',- iempty.contentDocument.documentElement.innerHTML,- );- });- }+ testing.onload(() => {+ testing.expectEqual('<head></head><body></body>', iempty.contentDocument.documentElement.innerHTML);+ });</script><iframe id=icomplex src=set_body/complex.html></iframe><script id=complex>+ testing.onload(() => {+ const doc = icomplex.contentDocument;+ testing.expectEqual(+ '<body><h1>title</h1>text node<div id="d" class="x"><span>nested</span></div></body>',+ doc.documentElement.querySelector('body').outerHTML,+ );+ testing.expectEqual('d', doc.body.querySelector('div').id);+ });+</script>++<iframe id=iframeset src=set_body/frameset.html></iframe>+<script id=frameset>+ testing.onload(() => {+ const doc = iframeset.contentDocument;+ testing.expectEqual('<head></head><frameset rows="50%,50%"></frameset>', doc.documentElement.innerHTML);+ testing.expectEqual('FRAMESET', doc.body.tagName);+ // the legacy color accessors only read a <body>+ testing.expectEqual('', doc.bgColor);+ });+</script>++<script id=replace>{- testing.onload(() => {- const doc = icomplex.contentDocument;- // Multiple siblings + text node + attributes survive as body children.- testing.expectEqual(- '<body><h1>title</h1>text node<div id="d" class="x"><span>nested</span></div></body>',- doc.documentElement.querySelector('body').outerHTML,- );- // The replacement body can still be located via document.body.- testing.expectEqual('d', doc.body.querySelector('div').id);- });+ const doc = document.implementation.createHTMLDocument('x');+ const original = doc.body;+ const body = doc.createElement('body');+ body.innerHTML = '<span>new</span>';+ doc.body = body;++ testing.expectEqual(body, doc.body);+ testing.expectEqual(null, original.parentNode);+ testing.expectEqual('<span>new</span>', doc.body.innerHTML);+ // head is untouched+ testing.expectEqual('<title>x</title>', doc.head.innerHTML);++ // setting the current body is a no-op+ doc.body = body;+ testing.expectEqual(body, doc.body);}</script>-<iframe id=iframeset src=set_body/frameset.html></iframe>-<script id=html_wrapper_stripped>+<script id=append_when_missing>+ {+ const doc = document.implementation.createHTMLDocument('x');+ doc.body.remove();+ testing.expectEqual(null, doc.body);++ const body = doc.createElement('body');+ doc.body = body;+ testing.expectEqual(body, doc.body);+ testing.expectEqual(doc.documentElement, body.parentNode);+ testing.expectEqual(body, doc.documentElement.lastChild);+ }+</script>++<script id=moves_existing_body>+ {+ // a body from another document is adopted+ const doc = document.implementation.createHTMLDocument('x');+ const other = document.implementation.createHTMLDocument('y');+ const body = other.body;+ doc.body = body;+ testing.expectEqual(body, doc.body);+ testing.expectEqual(doc, body.ownerDocument);+ testing.expectEqual(null, other.body);+ }+</script>++<script id=type_error>+ {+ const doc = document.implementation.createHTMLDocument('x');+ for (const value of ['<p>hi</p>', 1, {}, doc.createTextNode('t')]) {+ let err = null;+ try {+ doc.body = value;+ } catch (e) {+ err = e;+ }+ testing.expectEqual(true, err instanceof TypeError);+ }+ }+</script>++<script id=hierarchy_request_error>{- testing.onload(() => {- // Author wrapped content in <html><body>…</body></html>; the parser- // should unwrap those so the new body contains just the <p>.- testing.expectEqual(- '<body><p>unwrapped</p></body>',- iframeset.contentDocument.body.outerHTML,- );- });+ const doc = document.implementation.createHTMLDocument('x');+ for (const value of [null, doc.createElement('div')]) {+ let err = null;+ try {+ doc.body = value;+ } catch (e) {+ err = e;+ }+ testing.expectEqual(true, err instanceof DOMException);+ testing.expectEqual('HierarchyRequestError', err.name);+ }}</script><script id=no_document_element>- // Build a detached HTMLDocument, strip its documentElement, and verify- // setting body throws HierarchyRequestError.{const doc = document.implementation.createHTMLDocument('x');doc.documentElement.remove();@@ -82,7 +139,7 @@let err = null;try {- doc.body = '<p>hi</p>';+ doc.body = doc.createElement('body');} catch (e) {err = e;}@@ -91,17 +148,16 @@}</script>-<script id=detached_html_document>- // Fresh HTMLDocument with both head and body: replacing body should leave- // head untouched and install a new body.+<body>+<script id=live_document>{- const doc = document.implementation.createHTMLDocument('x');- const originalBody = doc.body;- doc.body = '<span>new</span>';-- testing.expectEqual(true, doc.body !== originalBody);- testing.expectEqual('<span>new</span>', doc.body.innerHTML);- // Head (including the auto-created <title>) is preserved.- testing.expectEqual('<title>x</title>', doc.head.innerHTML);+ const original = document.body;+ testing.expectEqual(true, original instanceof HTMLBodyElement);+ const body = document.createElement('body');+ body.id = 'replacement';+ document.body = body;+ testing.expectEqual('replacement', document.body.id);+ document.body = original;+ testing.expectEqual(original, document.body);}</script>diff --git a/src/browser/tests/document/set_body/complex.html b/src/browser/tests/document/set_body/complex.htmlindex 0f3823ece..93db094b6 100644--- a/src/browser/tests/document/set_body/complex.html+++ b/src/browser/tests/document/set_body/complex.html@@ -1,5 +1,7 @@<html><body><div>original</div></body><script>- document.body = '<h1>title</h1>text node<div id="d" class="x"><span>nested</span></div>';+ const body = document.createElement('body');+ body.innerHTML = '<h1>title</h1>text node<div id="d" class="x"><span>nested</span></div>';+ document.body = body;</script>diff --git a/src/browser/tests/document/set_body/empty.html b/src/browser/tests/document/set_body/empty.htmlindex f946a756f..f755e983e 100644--- a/src/browser/tests/document/set_body/empty.html+++ b/src/browser/tests/document/set_body/empty.html@@ -1,5 +1,5 @@<html><body><div>to be wiped</div></body><script>- document.body = '';+ document.body = document.createElement('body');</script>diff --git a/src/browser/tests/document/set_body/frameset.html b/src/browser/tests/document/set_body/frameset.htmlindex 4e6948016..e48a5ef1d 100644--- a/src/browser/tests/document/set_body/frameset.html+++ b/src/browser/tests/document/set_body/frameset.html@@ -1,7 +1,8 @@<html><body><div>original</div></body><script>- // Author wraps contents in their own <html><body>. Fragment parsing- // should unwrap these so the resulting body holds just the <p>.- document.body = '<html><body><p>unwrapped</p></body></html>';+ // A frameset is also a valid body, and document.body returns it.+ const frameset = document.createElement('frameset');+ frameset.setAttribute('rows', '50%,50%');+ document.body = frameset;</script>diff --git a/src/browser/tests/document/set_body/no_body.html b/src/browser/tests/document/set_body/no_body.htmlindex a50bddcdf..2405d8027 100644--- a/src/browser/tests/document/set_body/no_body.html+++ b/src/browser/tests/document/set_body/no_body.html@@ -4,6 +4,8 @@<body><!-- this body is removed before setBody runs --></body><script>document.body.remove();- document.body = '<p>appended</p>';+ const body = document.createElement('body');+ body.innerHTML = '<p>appended</p>';+ document.body = body;</script></html>diff --git a/src/browser/tests/document/set_body/simple.html b/src/browser/tests/document/set_body/simple.htmlindex 7bdc2db42..6bc372c56 100644--- a/src/browser/tests/document/set_body/simple.html+++ b/src/browser/tests/document/set_body/simple.html@@ -1,5 +1,7 @@<html><body id=orig><div>original</div></body><script>- document.body = '<p>replaced</p>';+ const body = document.createElement('body');+ body.innerHTML = '<p>replaced</p>';+ document.body = body;</script>diff --git a/src/browser/tests/testing.js b/src/browser/tests/testing.jsindex d9003f781..de454e586 100644--- a/src/browser/tests/testing.js+++ b/src/browser/tests/testing.js@@ -351,8 +351,15 @@HOST: '127.0.0.1',ORIGIN: 'http://127.0.0.1:9582',BASE_URL: 'http://127.0.0.1:9582/src/browser/tests/',+ skipAsync: skipAsync};+ async function skipAsync() {+ const state = await async();+ state.resolve();+ state.done(() => { expectTrue(true); });+ }+if (IS_TEST_RUNNER === false) {// The page is running in a different browser. Probably a developer making sure// a test is correct. There are a few tweaks we need to do to make this adiff --git a/src/browser/tests/window/location_external_scheme.html b/src/browser/tests/window/location_external_scheme.htmlindex 6fee7680b..418d1e4f0 100644--- a/src/browser/tests/window/location_external_scheme.html+++ b/src/browser/tests/window/location_external_scheme.html@@ -10,7 +10,13 @@<a id=mailto href="mailto:[email protected]">mail</a><a id=tel href="TEL:+15555550100">tel</a><script id=link type=module>-{+(async() => {+ if (testing.IS_TEST_RUNNER === false) {+ // Even if it's headless, chrome will still try to open these link in+ // whatever application is registered. Annoying. Easier to just skip.+ return testing.skipAsync();+ }+const state = await testing.async();window.__marker = 'kept';document.getElementById('mailto').click();@@ -21,5 +27,5 @@testing.expectEqual('kept', window.__marker);testing.expectEqual(testing.BASE_URL + 'window/location_external_scheme.html', location.href);});-}+})();</script>diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 6ee637fff..5db572481 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -1832,7 +1832,7 @@ pub const JsApi = struct {pub const onreadystatechange = EventTarget.handlerAccessor(Document, "readystatechange");pub const onvisibilitychange = EventTarget.handlerAccessor(Document, "visibilitychange");pub const URL = bridge.accessor(Document.getURL, null, .{});- pub const location = bridge.accessor(Document.getLocation, Document.setLocation, .{});+ pub const location = bridge.accessor(Document.getLocation, Document.setLocation, .{ .unforgeable = true, .deletable = false });pub const documentURI = bridge.accessor(Document.getURL, null, .{});pub const documentElement = bridge.accessor(Document.getDocumentElement, null, .{});pub const scrollingElement = bridge.accessor(Document.getDocumentElement, null, .{});diff --git a/src/browser/webapi/HTMLDocument.zig b/src/browser/webapi/HTMLDocument.zigindex b3e19bc16..ab2d30b0b 100644--- a/src/browser/webapi/HTMLDocument.zig+++ b/src/browser/webapi/HTMLDocument.zig@@ -65,40 +65,48 @@ pub fn getHead(self: *HTMLDocument) ?*Element {return null;}-pub fn getBody(self: *HTMLDocument) ?*Element.Html.Body {+// The body element: the first <body> or <frameset> child of <html>.+pub fn getBody(self: *HTMLDocument) ?*Element.Html {const document_element = self._proto.getDocumentElement() orelse return null;return findBodyForDoc(document_element);}-pub fn setBody(self: *HTMLDocument, html: []const u8, frame: *Frame) !void {- const document_element = self._proto.getDocumentElement() orelse return error.HierarchyError;-- // Build a fresh <body> holding the parsed HTML as its children. Fragment- // parsing strips any <html>/<body>/<head> wrappers the author included.- const new_body_node = try Frame.node_factory.createElementNS(self._proto, .html, "body", null);- if (html.len > 0) {- try Frame.parse.htmlAsChildren(frame, new_body_node, html);+// https://html.spec.whatwg.org/multipage/dom.html#dom-document-body+fn setBody(self: *HTMLDocument, value: ?*Element.Html, frame: *Frame) !void {+ const new_body = value orelse return error.HierarchyError;+ if (isBodyElement(new_body) == false) {+ return error.HierarchyError;}+ const document_element = self._proto.getDocumentElement() orelse return error.HierarchyError;const document_node = document_element.asNode();if (findBodyForDoc(document_element)) |current| {- _ = try document_node.replaceChild(new_body_node, current.asNode(), frame);+ if (current == new_body) {+ return;+ }+ _ = try document_node.replaceChild(new_body.asNode(), current.asNode(), frame);} else {- _ = try document_node.appendChild(new_body_node, frame);+ _ = try document_node.appendChild(new_body.asNode(), frame);}}-fn findBodyForDoc(document_element: *Element) ?*Element.Html.Body {+fn findBodyForDoc(document_element: *Element) ?*Element.Html {var child = document_element.asNode().firstChild();while (child) |node| {- if (node.is(Element.Html.Body)) |body| {- return body;+ if (node.is(Element.Html)) |html| {+ if (isBodyElement(html)) {+ return html;+ }}child = node.nextSibling();}return null;}+fn isBodyElement(html: *Element.Html) bool {+ return html.is(Element.Html.Body) != null or html.asElement().getTag() == .frameset;+}+pub fn getTitle(self: *HTMLDocument, frame: *Frame) ![]const u8 {// Search the entire document for the first <title> elementconst root = self._proto.getDocumentElement() orelse return "";@@ -210,18 +218,6 @@ fn setDir(self: *HTMLDocument, value: []const u8, frame: *Frame) !void {try html.asElement().setAttributeSafe(comptime .wrap("dir"), .wrap(value), frame);}-fn getLang(self: *HTMLDocument) []const u8 {- const el = self._proto.getDocumentElement() orelse return "";- const html = el.is(Element.Html) orelse return "";- return html.getLang();-}--fn setLang(self: *HTMLDocument, value: []const u8, frame: *Frame) !void {- const el = self._proto.getDocumentElement() orelse return;- const html = el.is(Element.Html) orelse return;- try html.setLang(value, frame);-}-pub fn getAll(self: *HTMLDocument, frame: *Frame) !*collections.HTMLAllCollection {return frame._factory.create(collections.HTMLAllCollection.init(self.asNode(), frame));}@@ -272,6 +268,46 @@ fn namedItem(self: *HTMLDocument, name: []const u8, frame: *Frame) !?NamedItemRereturn .{ .element = first };}+// Not the only thing that applies this type of rule. The short of it is:+// 1 - document order+// 2 - id is prefered over name, but for X (img) only if there's no name+// (which is different than am empty name)+// 3 - Drop duplicates+// https://html.spec.whatwg.org/multipage/dom.html#dom-document-nameditem+fn getNames(self: *HTMLDocument, frame: *Frame) !js.Array {+ const arena = frame.local_arena;+ var names: std.ArrayList([]const u8) = .empty;+ var seen: std.StringHashMapUnmanaged(void) = .empty;++ var tw = TreeWalker.FullExcludeSelf.Elements.init(self.asNode(), .{});+ while (tw.next()) |el| {+ const html = el.is(Element.Html) orelse continue;+ const name = el.getName() orelse "";+ const id: []const u8 = switch (html._type) {+ .embed, .form, .iframe => "",+ .img => if (name.len == 0) "" else el.getId() orelse "",+ .object => el.getId() orelse "",+ else => continue,+ };++ for ([_][]const u8{ id, name }) |n| {+ if (n.len == 0) {+ continue;+ }+ const gop = try seen.getOrPut(arena, n);+ if (gop.found_existing == false) {+ try names.append(arena, n);+ }+ }+ }++ var arr = frame.js.local.?.newArray(@intCast(names.items.len));+ for (names.items, 0..) |n, i| {+ _ = try arr.set(@intCast(i), n, .{});+ }+ return arr;+}+fn findNamed(tw: *TreeWalker.FullExcludeSelf.Elements, name: []const u8) ?*Element {while (tw.next()) |el| {if (isNamed(el, name)) {@@ -340,10 +376,12 @@ pub const JsApi = struct {const R = struct {fn get(self: *HTMLDocument) []const u8 {const b = self.getBody() orelse return "";+ if (b.is(Element.Html.Body) == null) return "";return b.asElement().getAttributeSafe(comptime .wrap(attr)) orelse "";}fn set(self: *HTMLDocument, value: js.Value, frame: *Frame) !void {const b = self.getBody() orelse return;+ if (b.is(Element.Html.Body) == null) return;const str = if (value.isNull()) "" else try value.toStringSlice();try b.asElement().setAttributeSafe(comptime .wrap(attr), .wrap(str), frame);}@@ -352,7 +390,6 @@ pub const JsApi = struct {}pub const head = bridge.accessor(HTMLDocument.getHead, null, .{});pub const body = bridge.accessor(HTMLDocument.getBody, HTMLDocument.setBody, .{ .ce_reactions = true });- pub const lang = bridge.accessor(HTMLDocument.getLang, HTMLDocument.setLang, .{});pub const title = bridge.accessor(HTMLDocument.getTitle, HTMLDocument.setTitle, .{ .ce_reactions = true });pub const images = bridge.accessor(HTMLDocument.getImages, null, .{});pub const scripts = bridge.accessor(HTMLDocument.getScripts, null, .{});@@ -366,7 +403,7 @@ pub const JsApi = struct {pub const all = bridge.accessor(HTMLDocument.getAll, null, .{});pub const doctype = bridge.accessor(HTMLDocument.getDocType, null, .{});- pub const @"[str]" = bridge.namedIndexed(HTMLDocument.namedItem, null, null, null, struct {+ pub const @"[str]" = bridge.namedIndexed(HTMLDocument.namedItem, null, null, HTMLDocument.getNames, struct {fn query(self: *HTMLDocument, name: []const u8) !u32 {if (name.len == 0) {return error.NotHandled;diff --git a/src/browser/webapi/collections/HTMLAllCollection.zig b/src/browser/webapi/collections/HTMLAllCollection.zigindex f84d21650..66e02b904 100644--- a/src/browser/webapi/collections/HTMLAllCollection.zig+++ b/src/browser/webapi/collections/HTMLAllCollection.zig@@ -146,7 +146,7 @@ pub const JsApi = struct {pub const namedItem = bridge.function(HTMLAllCollection.getByName, .{});pub const symbol_iterator = bridge.iterator(HTMLAllCollection.iterator, .{});- pub const callable = bridge.callable(HTMLAllCollection.callable, .{ .null_as_undefined = true });+ pub const callable = bridge.callable(HTMLAllCollection.callable, .{});};const testing = @import("../../../testing.zig");