more legacy test fixes

This commit is contained in:
Karl Seguin
2025-12-25 11:39:32 +08:00
parent 899567328e
commit 8be7a9f2bc
14 changed files with 111 additions and 334 deletions

View File

@@ -0,0 +1,19 @@
<!DOCTYPE html>
<a id="link" href="foo" class="ok">OK</a>
<script src="../../testing.js"></script>
<script id=text>
let t = new Text('foo');
testing.expectEqual('foo', t.data);
let emptyt = new Text();
testing.expectEqual('', emptyt.data);
let text = $('#link').firstChild;
testing.expectEqual('OK', text.wholeText);
text.data = 'OK modified';
let split = text.splitText('OK'.length);
testing.expectEqual(' modified', split.data);
testing.expectEqual('OK', text.data);
</script>

View File

@@ -23,6 +23,7 @@
<main>Main</main>
<script id=basic>
testing.expectEqual(24, document.getElementsByTagName('*').length);
testing.expectEqual(0, document.getElementsByTagName('a').length);
testing.expectEqual(0, document.getElementsByTagName('unknown').length);

View File

@@ -189,3 +189,13 @@
testing.expectEqual(1, df.childNodes.length);
}
</script>
<script id=isEqualNode>
testing.expectEqual('DocumentFragment', new DocumentFragment().constructor.name);
const dc1 = new DocumentFragment();
testing.expectEqual(true, dc1.isEqualNode(dc1))
const dc2 = new DocumentFragment();
testing.expectEqual(true, dc1.isEqualNode(dc2))
</script>

View File

@@ -41,6 +41,7 @@
{
const container = $('#container');
testing.expectEqual(5, container.getElementsByTagName('*').length);
testing.expectEqual(0, container.getElementsByTagName('a').length);
testing.expectEqual(0, container.getElementsByTagName('unknown').length);

View File

@@ -71,7 +71,7 @@
testing.expectEqual('bar', content.getAttribute('foo'));
testing.expectEqual(['id', 'dir', 'foo'], content.getAttributeNames());
testing.expectError('Error: Invalid Character', () => {
testing.expectError('InvalidCharacterError: Invalid Character', () => {
content.setAttribute('.foo', 'invalid')
});

View File

@@ -20,7 +20,7 @@
<script id=all>
let allTags = document.getElementsByTagName('*');
testing.expectEqual(13, allTags.length);
testing.expectEqual(11, allTags.length);
testing.expectEqual('html', allTags.item(0).localName);
testing.expectEqual('html', allTags.item(0).localName);
testing.expectEqual('head', allTags.item(1).localName);

View File

@@ -1,266 +0,0 @@
<!DOCTYPE html>
<body><div id="content">
<a id="link" href="foo" class="ok">OK</a>
<p id="para-empty" class="ok empty">
<span id="para-empty-child"></span>
</p>
<p id="para"> And</p>
<!--comment-->
</div>
<div id="rootNodeComposed"></div>
</body>
<script src="../testing.js"></script>
<script>
function trimAndReplace(str) {
str = str.replace(/(\r\n|\n|\r)/gm,'');
str = str.replace(/\s+/g, ' ');
str = str.trim();
return str;
}
let content = $('#content');
let link = $('#link');
let first_child = content.firstChild.nextSibling; // nextSibling because of line return \n
</script>
<script id=compareDocumentPosition>
testing.expectEqual(10, document.body.compareDocumentPosition(document.firstChild));
testing.expectEqual(10, $('#para-empty').compareDocumentPosition(content));
testing.expectEqual(20, content.compareDocumentPosition($('#para-empty')));
testing.expectEqual(0, link.compareDocumentPosition(link));
testing.expectEqual(2, $('#para-empty').compareDocumentPosition(link));
testing.expectEqual(4, link.compareDocumentPosition($('#para-empty')));
</script>
<script id=proto>
testing.expectEqual('HTMLDocument', content.getRootNode().__proto__.constructor.name);
</script>
<script id=getRootNodeComposed>
const testContainer = $('#rootNodeComposed');
const shadowHost = document.createElement('div');
testContainer.appendChild(shadowHost);
const shadowRoot = shadowHost.attachShadow({ mode: 'open' });
const shadowChild = document.createElement('span');
shadowRoot.appendChild(shadowChild);
testing.expectEqual('ShadowRoot', shadowChild.getRootNode().__proto__.constructor.name);
testing.expectEqual('ShadowRoot', shadowChild.getRootNode({ composed: false }).__proto__.constructor.name);
testing.expectEqual('HTMLDocument', shadowChild.getRootNode({ composed: true }).__proto__.constructor.name);
testing.expectEqual('HTMLDocument', shadowHost.getRootNode().__proto__.constructor.name);
const disconnected = document.createElement('div');
const disconnectedChild = document.createElement('span');
disconnected.appendChild(disconnectedChild);
testing.expectEqual('HTMLDivElement', disconnectedChild.getRootNode().__proto__.constructor.name);
testing.expectEqual('HTMLDivElement', disconnectedChild.getRootNode({ composed: true }).__proto__.constructor.name);
</script>
<script id=firstChild>
let body_first_child = document.body.firstChild;
testing.expectEqual('div', body_first_child.localName);
testing.expectEqual('HTMLDivElement', body_first_child.__proto__.constructor.name);
testing.expectEqual(null, $('#para-empty').firstChild.firstChild);
</script>
<script id=lastChild>
let last_child = content.lastChild.previousSibling; // previousSibling because of line return \n
testing.expectEqual('Comment', last_child.__proto__.constructor.name);
</script>
<script id=nextSibling>
let next_sibling = link.nextSibling.nextSibling;
testing.expectEqual('p', next_sibling.localName);
testing.expectEqual('HTMLParagraphElement', next_sibling.__proto__.constructor.name);
testing.expectEqual(null, $('#para-empty-child').nextSibling.nextSibling);
</script>
<script id=previousSibling>
let prev_sibling = $('#para-empty').previousSibling.previousSibling;
testing.expectEqual('a', prev_sibling.localName);
testing.expectEqual('HTMLAnchorElement', prev_sibling.__proto__.constructor.name);
testing.expectEqual(null, content.previousSibling);
</script>
<script id=parentElement>
let parent = $('#para').parentElement;
testing.expectEqual('div', parent.localName);
testing.expectEqual('HTMLDivElement', parent.__proto__.constructor.name)
let h = content.parentElement.parentElement;
testing.expectEqual(null, h.parentElement);
testing.expectEqual('HTMLDocument', h.parentNode.__proto__.constructor.name);
</script>
<script id=nodeName>
testing.expectEqual('A', first_child.nodeName);
testing.expectEqual('#text', link.firstChild.nodeName);
testing.expectEqual('#comment', last_child.nodeName);
testing.expectEqual('#document', document.nodeName);
</script>
<script id=nodeType>
testing.expectEqual(1, first_child.nodeType)
testing.expectEqual(3, link.firstChild.nodeType)
testing.expectEqual(8, last_child.nodeType)
testing.expectEqual(9, document.nodeType)
</script>
<script id=ownerDocument>
let owner = content.ownerDocument;
testing.expectEqual('HTMLDocument', owner.__proto__.constructor.name);
testing.expectEqual(null, document.ownerDocument)
let owner2 = document.createElement('div').ownerDocument;
testing.expectEqual('HTMLDocument', owner2.__proto__.constructor.name);
</script>
<script id=isConnected>
testing.expectEqual(true, content.isConnected);
testing.expectEqual(true, document.isConnected);
const connDiv = document.createElement('div');
testing.expectEqual(false, connDiv.isConnected);
const connParentDiv = document.createElement('div');
connParentDiv.appendChild(connDiv);
testing.expectEqual(false, connDiv.isConnected);
content.appendChild(connParentDiv);
testing.expectEqual(true, connDiv.isConnected);
</script>
<script id=nodeValue>
testing.expectEqual('comment', last_child.nodeValue);
testing.expectEqual(null, link.nodeValue);
let text = link.firstChild;
testing.expectEqual('OK', text.nodeValue);
text.nodeValue = 'OK modified';
testing.expectEqual('OK modified', text.nodeValue);
</script>
<script id=textContent>
testing.expectEqual('OK modified', text.textContent);
testing.expectEqual('OK modified And', trimAndReplace(content.textContent));
text.textContent = 'OK';
testing.expectEqual('OK', text.textContent);
testing.expectEqual('', trimAndReplace($('#para-empty').textContent));
$('#para-empty').textContent = 'OK';
testing.expectEqual('#text', $('#para-empty').firstChild.nodeName);
</script>
<script id=appendChild>
let append = document.createElement('h1');
testing.expectEqual('[object HTMLHeadingElement]', content.appendChild(append).toString());
testing.expectEqual('HTMLHeadingElement', content.lastChild.__proto__.constructor.name);
testing.expectEqual('[object HTMLAnchorElement]', content.appendChild(link).toString());
</script>
<script id=cloneNode>
let clone = link.cloneNode();
testing.expectEqual('[object HTMLAnchorElement]', clone.toString());
testing.expectEqual(null, clone.parentNode);
testing.expectEqual(null, clone.firstChild);
let clone_deep = link.cloneNode(true);
testing.expectEqual('#text', clone_deep.firstChild.nodeName);
</script>
<script id=contains>
testing.expectEqual(true, link.contains(text));
testing.expectEqual(false, text.contains(link));
</script>
<script id=hasChildNodes>
testing.expectEqual(true, link.hasChildNodes());
testing.expectEqual(false, text.hasChildNodes());
</script>
<script id=childNodesLength>
testing.expectEqual(1, link.childNodes.length);
testing.expectEqual(0, text.childNodes.length);
</script>
<script id=insertBefore>
let insertBefore = document.createElement('a');
testing.expectEqual(true, link.insertBefore(insertBefore, text) !== undefined);
testing.expectEqual('a', link.firstChild.localName);
let insertBefore2 = document.createElement('b');
testing.expectEqual('b', link.insertBefore(insertBefore2, null).localName);
testing.expectEqual('b', link.childNodes[link.childNodes.length - 1].localName);
</script>
<script id=isDefaultNamespace>
// TODO: does not seems to work
// link.isDefaultNamespace('')", "true" },
testing.expectEqual(false, link.isDefaultNamespace('false'));
</script>
<script id=isEqualNode>
let equal1 = document.createElement('a');
let equal2 = document.createElement('a');
equal1.textContent = 'is equal';
equal2.textContent = 'is equal';
// TODO: does not seems to work
// testing.expectEqual(true, equal1.isEqualNode(equal2));
testing.skip();
</script>
<script id=isSameNode>
testing.expectEqual(true, document.body.isSameNode(document.body));
testing.expectEqual(false, document.body.isSameNode(content));
</script>
<script id=baseURI>
testing.expectEqual('http://localhost:9589/dom/node.html', link.baseURI);
</script>
<script id=removeChild>
testing.expectEqual(true, content.removeChild(append) !== undefined);
testing.expectEqual(true, last_child.__proto__.constructor.name !== 'HTMLHeadingElement');
</script>
<script id=replaceChild>
let replace = document.createElement('div');
testing.expectEqual(true, link.replaceChild(replace, insertBefore) !== undefined);
</script>
<script id=NODE_TYPE>
testing.expectEqual(1, Node.ELEMENT_NODE);
testing.expectEqual(2, Node.ATTRIBUTE_NODE);
testing.expectEqual(3, Node.TEXT_NODE);
testing.expectEqual(4, Node.CDATA_SECTION_NODE);
testing.expectEqual(7, Node.PROCESSING_INSTRUCTION_NODE);
testing.expectEqual(8, Node.COMMENT_NODE);
testing.expectEqual(9, Node.DOCUMENT_NODE);
testing.expectEqual(10, Node.DOCUMENT_TYPE_NODE);
testing.expectEqual(11, Node.DOCUMENT_FRAGMENT_NODE);
testing.expectEqual(5, Node.ENTITY_REFERENCE_NODE);
testing.expectEqual(6, Node.ENTITY_NODE);
testing.expectEqual(12, Node.NOTATION_NODE);
</script>
<span id=token class="token" style="color:#ce9178">&quot;puppeteer &quot;</span>
<h3 id=name>Leto
<!-- -->
<!-- -->
Atreides</h3>
<script id=normalize>
const token = $('#token');
testing.expectEqual('"puppeteer "', token.firstChild.nodeValue);
const name = $('#name');
testing.expectEqual([
"Leto\n ",
" ",
"\n ",
" ",
"\n Atreides"
], Array.from(name.childNodes).map((n) => n.nodeValue));
</script>

View File

@@ -1,62 +0,0 @@
<!DOCTYPE html>
<script src="../testing.js"></script>
<script id=nodeIterator>
const nodeIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ELEMENT,
{
acceptNode(node) {
return NodeFilter.FILTER_ACCEPT;
},
},
);
testing.expectEqual('BODY', nodeIterator.nextNode().nodeName);
testing.expectEqual('DIV', nodeIterator.nextNode().nodeName);
testing.expectEqual('A', nodeIterator.nextNode().nodeName);
testing.expectEqual('A', nodeIterator.previousNode().nodeName); // pointer_before_current flips
testing.expectEqual('A', nodeIterator.nextNode().nodeName); // pointer_before_current flips
testing.expectEqual('A', nodeIterator.previousNode().nodeName); // pointer_before_current flips
testing.expectEqual('DIV', nodeIterator.previousNode().nodeName);
testing.expectEqual('BODY', nodeIterator.previousNode().nodeName);
testing.expectEqual(null, nodeIterator.previousNode()); // Not HEAD since body is root
testing.expectEqual(null, nodeIterator.previousNode()); // Keeps returning null
testing.expectEqual('BODY', nodeIterator.nextNode().nodeName);
nodeIterator.nextNode();
nodeIterator.nextNode();
nodeIterator.nextNode();
testing.expectEqual('SPAN', nodeIterator.nextNode().nodeName);
testing.expectEqual('P', nodeIterator.nextNode().nodeName);
testing.expectEqual(null, nodeIterator.nextNode()); // Just the last one
testing.expectEqual(null ,nodeIterator.nextNode()); // Keeps returning null
testing.expectEqual('P', nodeIterator.previousNode().nodeName);
const notationIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_NOTATION,
);
testing.expectEqual(null, notationIterator.nextNode());
testing.expectEqual(null, notationIterator.previousNode());
testing.expectEqual(1, nodeIterator.filter.acceptNode(document.body));
testing.expectEqual(null, notationIterator.filter);
const rejectIterator = document.createNodeIterator(
document.body,
NodeFilter.SHOW_ALL,
(e => { return NodeFilter.FILTER_REJECT}),
);
testing.expectEqual(2, rejectIterator.filter(document.body));
</script>
<body>
<div id="content">
<a id="link" href="foo" class="ok">OK</a>
<p id="para-empty" class="ok empty">
<span id="para-empty-child"></span>
</p>
<p id="para"> And</p>
<!--comment-->
</div>
</body>

View File

@@ -163,7 +163,6 @@ pub fn getElementsByTagName(self: *Document, tag_name: []const u8, page: *Page)
return error.InvalidTagName;
}
// Handle wildcard '*' - return all elements
if (std.mem.eql(u8, tag_name, "*")) {
return .{
.all_elements = collections.NodeLive(.all_elements).init(self.asNode(), {}, page),

View File

@@ -199,6 +199,29 @@ pub fn cloneFragment(self: *DocumentFragment, deep: bool, page: *Page) !*Node {
return fragment_node;
}
pub fn isEqualNode(self: *DocumentFragment, other: *DocumentFragment) bool {
var self_iter = self.asNode().childrenIterator();
var other_iter = other.asNode().childrenIterator();
while (true) {
const self_child = self_iter.next();
const other_child = other_iter.next();
if ((self_child == null) != (other_child == null)) {
return false;
}
if (self_child == null) {
// We've reached the end
return true;
}
if (!self_child.?.isEqualNode(other_child.?)) {
return false;
}
}
}
pub const JsApi = struct {
pub const bridge = js.Bridge(DocumentFragment);

View File

@@ -53,6 +53,12 @@ pub fn className(_: *const DocumentType) []const u8 {
return "[object DocumentType]";
}
pub fn isEqualNode(self: *const DocumentType, other: *const DocumentType) bool {
return std.mem.eql(u8, self._name, other._name) and
std.mem.eql(u8, self._public_id, other._public_id) and
std.mem.eql(u8, self._system_id, other._system_id);
}
pub const JsApi = struct {
pub const bridge = js.Bridge(DocumentType);

View File

@@ -944,6 +944,7 @@ fn calculateSiblingPosition(node: *Node) f64 {
const GetElementsByTagNameResult = union(enum) {
tag: collections.NodeLive(.tag),
tag_name: collections.NodeLive(.tag_name),
all_elements: collections.NodeLive(.all_elements),
};
pub fn getElementsByTagName(self: *Element, tag_name: []const u8, page: *Page) !GetElementsByTagNameResult {
if (tag_name.len > 256) {
@@ -951,6 +952,12 @@ pub fn getElementsByTagName(self: *Element, tag_name: []const u8, page: *Page) !
return error.InvalidTagName;
}
if (std.mem.eql(u8, tag_name, "*")) {
return .{
.all_elements = collections.NodeLive(.all_elements).init(self.asNode(), {}, page),
};
}
const lower = std.ascii.lowerString(&page.buf, tag_name);
if (Tag.parseForMatch(lower)) |known| {
// optimized for known tag names

View File

@@ -331,7 +331,10 @@ pub fn isEqualNode(self: *Node, other: *Node) bool {
.element => self.as(Element).isEqualNode(other.as(Element)),
.attribute => self.as(Element.Attribute).isEqualNode(other.as(Element.Attribute)),
.cdata => self.as(CData).isEqualNode(other.as(CData)),
else => {
.document_fragment => self.as(DocumentFragment).isEqualNode(other.as(DocumentFragment)),
.document_type => self.as(DocumentType).isEqualNode(other.as(DocumentType)),
.document => {
// Document comparison is complex and rarely used in practice
log.warn(.browser, "not implemented", .{
.type = self._type,
.feature = "Node.isEqualNode",

View File

@@ -16,19 +16,53 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const js = @import("../../js/js.zig");
const Page = @import("../../Page.zig");
const CData = @import("../CData.zig");
const Text = @This();
_proto: *CData,
pub fn init(str: ?[]const u8, page: *Page) !*Text {
const node = try page.createTextNode(str orelse "");
return node.as(Text);
}
pub fn getWholeText(self: *Text) []const u8 {
return self._proto._data;
}
pub fn splitText(self: *Text, offset: usize, page: *Page) !*Text {
const data = self._proto._data;
if (offset > data.len) {
return error.IndexSizeError;
}
const new_data = data[offset..];
const new_node = try page.createTextNode(new_data);
const new_text = new_node.as(Text);
const old_data = data[0..offset];
try self._proto.setData(old_data, page);
// If this node has a parent, insert the new node right after this one
const node = self._proto.asNode();
if (node.parentNode()) |parent| {
const next_sibling = node.nextSibling();
_ = try parent.insertBefore(new_node, next_sibling, page);
}
return new_text;
}
const testing = @import("../../../testing.zig");
test "WebApi: CData.Text" {
try testing.htmlRunner("cdata/text", .{});
}
pub const JsApi = struct {
const js = @import("../../js/js.zig");
pub const bridge = js.Bridge(Text);
pub const Meta = struct {
@@ -37,5 +71,7 @@ pub const JsApi = struct {
pub var class_id: bridge.ClassId = undefined;
};
pub const constructor = bridge.constructor(Text.init, .{});
pub const wholeText = bridge.accessor(Text.getWholeText, null, .{});
pub const splitText = bridge.function(Text.splitText, .{ .dom_exception = true });
};