Lightpanda: the headless browser designed for AI and automation
Register a detached node's frame
Karl Seguin0cca01c0e865
diff --git a/src/browser/tests/document/document.html b/src/browser/tests/document/document.htmlindex 44587ae7d..741a652a0 100644--- a/src/browser/tests/document/document.html+++ b/src/browser/tests/document/document.html@@ -307,10 +307,13 @@testing.expectEqual("InvalidCharacterError", err.name);}, () => document.createAttribute(''));+ // a leading '.' is fine: browsers don't apply the XML Name production here+ testing.expectEqual('.over', document.createAttribute('.over').name);+testing.withError((err) => {testing.expectEqual(5, err.code);testing.expectEqual("InvalidCharacterError", err.name);- }, () => document.createAttribute('.over'));+ }, () => document.createAttribute('over there'));}</script>diff --git a/src/browser/tests/element/attributes.html b/src/browser/tests/element/attributes.htmlindex 0192c9347..7e021f661 100644--- a/src/browser/tests/element/attributes.html+++ b/src/browser/tests/element/attributes.html@@ -166,54 +166,19 @@}</script>-<script id=invalidAttributeNames>+<script id=validAttributeNames>{+ // Neither Chrome nor Firefox checks the XML Name production here, so names+ // it rejects still go through. '\v' is deliberate: it is not HTML+ // whitespace, and both browsers take it. Kept in its own script block: a+ // setAttribute that wrongly throws kills the block, and the runner only+ // catches that while the block has recorded no assertion of its own.const div = document.createElement('div');-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('0abc', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('123', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('-invalid', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('.foo', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('my attr', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('my@attr', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('attr!', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('~', 'value'));-- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.setAttribute('', 'value'));+ for (const name of ['0abc', '123', '-invalid', '.foo', 'my@attr', 'attr!', '~', '<x', '::x', 'a\vb', 'é']) {+ div.setAttribute(name, 'value');+ testing.expectEqual('value', div.getAttribute(name));+ testing.expectEqual(name, document.createAttribute(name).name);+ }div.setAttribute('valid-name', 'value1');testing.expectEqual('value1', div.getAttribute('valid-name'));@@ -229,21 +194,30 @@div.setAttribute('data-test-123', 'value5');testing.expectEqual('value5', div.getAttribute('data-test-123'));+}+</script>- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.toggleAttribute('.invalid'));+<script id=invalidAttributeNames>+{+ // What both browsers do reject: an empty name, and the characters that+ // would break attribute serialization.+ const div = document.createElement('div');+ for (const name of ['', 'my attr', 'a\tb', 'a\nb', 'a\fb', 'a\rb', 'a\0b', 'a=b', 'a/b', 'a>b']) {+ testing.withError((err) => {+ testing.expectEqual(5, err.code);+ testing.expectEqual("InvalidCharacterError", err.name);+ }, () => div.setAttribute(name, 'value'));- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.toggleAttribute('0invalid'));+ testing.withError((err) => {+ testing.expectEqual(5, err.code);+ testing.expectEqual("InvalidCharacterError", err.name);+ }, () => div.toggleAttribute(name));- testing.withError((err) => {- testing.expectEqual(5, err.code);- testing.expectEqual("InvalidCharacterError", err.name);- }, () => div.toggleAttribute('-invalid'));+ testing.withError((err) => {+ testing.expectEqual(5, err.code);+ testing.expectEqual("InvalidCharacterError", err.name);+ }, () => document.createAttribute(name));+ }}</script>@@ -301,6 +275,65 @@}</script>+<div id=set-named-src class=c data-y=1></div>+<div id=set-named-dst></div>+<script id=setNamedItemInUse>+ {+ const src = $('#set-named-src');+ const dst = $('#set-named-dst');++ // setNamedItem runs the same "set an attribute" steps as setAttributeNode+ testing.withError((err) => {+ testing.expectEqual(10, err.code);+ testing.expectEqual('InUseAttributeError', err.name);+ }, () => dst.attributes.setNamedItem(src.getAttributeNode('data-y')));+ testing.expectEqual(src, src.getAttributeNode('data-y').ownerElement);+ testing.expectEqual('1', src.getAttribute('data-y'));+ testing.expectEqual(false, dst.hasAttribute('data-y'));++ const moved = src.attributes.removeNamedItem('data-y');+ testing.expectEqual(null, moved.ownerElement);+ testing.expectEqual(null, dst.attributes.setNamedItem(moved));+ testing.expectEqual(dst, moved.ownerElement);+ testing.expectEqual('1', dst.getAttribute('data-y'));+ }+</script>++<div id=detach-owner data-z=1></div>+<script id=removeAttributeDetaches>+ {+ const el = $('#detach-owner');+ const other = document.createElement('div');++ const attr = el.getAttributeNode('data-z');+ el.removeAttribute('data-z');+ testing.expectEqual(null, attr.ownerElement);++ // free to move now, and to come back to the element it left+ testing.expectEqual(null, other.setAttributeNode(attr));+ testing.expectEqual(other, attr.ownerElement);+ testing.expectEqual('1', other.getAttribute('data-z'));++ other.removeAttribute('data-z');+ testing.expectEqual(null, el.setAttributeNode(attr));+ testing.expectEqual(el, attr.ownerElement);+ testing.expectEqual('1', el.getAttribute('data-z'));+ }+</script>++<script id=attributeNodeCloneOwnerDocument>+ {+ const doc = new DOMParser().parseFromString('<p id=x data-q=7>hi</p>', 'text/html');+ const attr = doc.getElementById('x').getAttributeNode('data-q');+ testing.expectEqual(doc, attr.ownerDocument);++ // the copy has no owner element, but keeps the node document+ const clone = attr.cloneNode();+ testing.expectEqual(null, clone.ownerElement);+ testing.expectEqual(doc, clone.ownerDocument);+ }+</script>+<div id="nsa"></div><script id=non-string-attr>{diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zigindex 914fe0123..ed9f4a9e2 100644--- a/src/browser/webapi/element/Attribute.zig+++ b/src/browser/webapi/element/Attribute.zig@@ -89,11 +89,19 @@ pub fn isEqualNode(self: *const Attribute, other: *const Attribute) bool {}pub fn clone(self: *const Attribute, frame: *Frame) !*Attribute {- return frame._factory.node(Attribute{+ const cloned = try frame._factory.node(Attribute{._element = null,._name = self._name,._value = self._value,});++ if (self._element) |el| {+ // cloned has no element, we need to store its document+ if (el.asNode().ownerDocument(frame)) |doc| {+ try frame.setNodeOwnerDocument(cloned.asNode(), doc);+ }+ }+ return cloned;}pub const JsApi = struct {@@ -341,7 +349,10 @@ pub const List = struct {// remove this BEFORE triggering anything, incase that re-enters delete// or some other callback.- _ = owner._attribute_lookup.remove(.{ .list = self, .name = entry._name_ptr });+ if (owner._attribute_lookup.fetchRemove(.{ .list = self, .name = entry._name_ptr })) |kv| {+ // The attribute can still be alive+ kv.value._element = null;+ }const index = (@intFromPtr(entry) - @intFromPtr(self._entries)) / @sizeOf(Entry);const list_entries = self._entries[0..self._len];std.mem.copyForwards(Entry, list_entries[index .. list_entries.len - 1], list_entries[index + 1 ..]);@@ -489,6 +500,9 @@ fn shouldAddToIdMap(normalized_name: String, element: *Element) bool {return node.isConnected();}+// names and things that would break serialization aren't allowed.+const invalid_name_chars = "\x00\t\n\x0C\r />=";+pub fn validateAttributeName(name: String) !void {const name_str = name.str();@@ -496,23 +510,9 @@ pub fn validateAttributeName(name: String) !void {return error.InvalidCharacterError;}- const first = name_str[0];- if ((first >= '0' and first <= '9') or first == '-' or first == '.') {+ if (std.mem.indexOfAny(u8, name_str, invalid_name_chars) != null) {return error.InvalidCharacterError;}-- for (name_str) |c| {- if (c == 0 or c == '/' or c == '=' or c == '>' or std.ascii.isWhitespace(c)) {- return error.InvalidCharacterError;- }-- const is_valid = std.ascii.isAlphanumeric(c) or- c == '_' or c == '-' or c == '.' or c == ':';-- if (!is_valid) {- return error.InvalidCharacterError;- }- }}// Every stored entry name either comes from the static String.intern or from@@ -594,8 +594,7 @@ pub const NamedNodeMap = struct {}pub fn set(self: *const NamedNodeMap, attribute: *Attribute, frame: *Frame) !?*Attribute {- attribute._element = null; // just a requirement of list.putAttribute, it'll re-set it.- return self.list().putAttribute(attribute, self._element, frame);+ return self._element.setAttributeNode(attribute, frame);}pub fn removeByName(self: *const NamedNodeMap, name: String, frame: *Frame) !?*Attribute {