Merge pull request #124 from lightpanda-io/dom-parentnode

Dom parentnode accessors mixin
This commit is contained in:
Pierre Tachoire
2023-12-15 16:52:27 +01:00
committed by GitHub
38 changed files with 1069 additions and 81 deletions

View File

@@ -34,9 +34,10 @@ pub const Document = struct {
return DOMImplementation{};
}
pub fn get_documentElement(self: *parser.Document) !ElementUnion {
pub fn get_documentElement(self: *parser.Document) !?ElementUnion {
const e = try parser.documentGetDocumentElement(self);
return try Element.toInterface(e);
if (e == null) return null;
return try Element.toInterface(e.?);
}
pub fn get_documentURI(self: *parser.Document) ![]const u8 {
@@ -105,12 +106,12 @@ pub const Document = struct {
alloc: std.mem.Allocator,
tag_name: []const u8,
) !collection.HTMLCollection {
const root = try parser.documentGetDocumentElement(self);
return try collection.HTMLCollectionByTagName(
alloc,
parser.elementToNode(root),
tag_name,
);
var elt: ?*parser.Node = null;
if (try parser.documentGetDocumentElement(self)) |root| {
elt = parser.elementToNode(root);
}
return try collection.HTMLCollectionByTagName(alloc, elt, tag_name, true);
}
pub fn _getElementsByClassName(
@@ -118,12 +119,12 @@ pub const Document = struct {
alloc: std.mem.Allocator,
classNames: []const u8,
) !collection.HTMLCollection {
const root = try parser.documentGetDocumentElement(self);
return try collection.HTMLCollectionByClassName(
alloc,
parser.elementToNode(root),
classNames,
);
var elt: ?*parser.Node = null;
if (try parser.documentGetDocumentElement(self)) |root| {
elt = parser.elementToNode(root);
}
return try collection.HTMLCollectionByClassName(alloc, elt, classNames, true);
}
pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment {
@@ -164,6 +165,31 @@ pub const Document = struct {
return try parser.documentCreateAttributeNS(self, ns, qname);
}
// ParentNode
// https://dom.spec.whatwg.org/#parentnode
pub fn get_children(self: *parser.Document) !collection.HTMLCollection {
var elt: ?*parser.Node = null;
if (try parser.documentGetDocumentElement(self)) |root| {
elt = parser.elementToNode(root);
}
return try collection.HTMLCollectionChildren(elt, true);
}
pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion {
const elt = try parser.documentGetDocumentElement(self) orelse return null;
return try Element.toInterface(elt);
}
pub fn get_lastElementChild(self: *parser.Document) !?ElementUnion {
const elt = try parser.documentGetDocumentElement(self) orelse return null;
return try Element.toInterface(elt);
}
pub fn get_childElementCount(self: *parser.Document) !u32 {
_ = try parser.documentGetDocumentElement(self) orelse return 0;
return 1;
}
pub fn deinit(_: *parser.Document, _: std.mem.Allocator) void {}
};
@@ -179,6 +205,12 @@ pub fn testExecFn(
.{ .src = "document.__proto__.__proto__.constructor.name", .ex = "Document" },
.{ .src = "document.__proto__.__proto__.__proto__.constructor.name", .ex = "Node" },
.{ .src = "document.__proto__.__proto__.__proto__.__proto__.constructor.name", .ex = "EventTarget" },
.{ .src = "let newdoc = new Document()", .ex = "undefined" },
.{ .src = "newdoc.documentElement", .ex = "null" },
.{ .src = "newdoc.children.length", .ex = "0" },
.{ .src = "newdoc.getElementsByTagName('*').length", .ex = "0" },
.{ .src = "newdoc.getElementsByTagName('*').item(0)", .ex = "null" },
};
try checkCases(js_env, &constructor);
@@ -306,6 +338,22 @@ pub fn testExecFn(
};
try checkCases(js_env, &createAttr);
var parentNode = [_]Case{
.{ .src = "document.children.length", .ex = "1" },
.{ .src = "document.children.item(0).nodeName", .ex = "HTML" },
.{ .src = "document.firstElementChild.nodeName", .ex = "HTML" },
.{ .src = "document.lastElementChild.nodeName", .ex = "HTML" },
.{ .src = "document.childElementCount", .ex = "1" },
.{ .src = "let nd = new Document()", .ex = "undefined" },
.{ .src = "nd.children.length", .ex = "0" },
.{ .src = "nd.children.item(0)", .ex = "null" },
.{ .src = "nd.firstElementChild", .ex = "null" },
.{ .src = "nd.lastElementChild", .ex = "null" },
.{ .src = "nd.childElementCount", .ex = "0" },
};
try checkCases(js_env, &parentNode);
const tags = comptime parser.Tag.all();
comptime var createElements: [(tags.len) * 2]Case = undefined;
inline for (tags, 0..) |tag, i| {

View File

@@ -6,6 +6,8 @@ const jsruntime = @import("jsruntime");
const Case = jsruntime.test_utils.Case;
const checkCases = jsruntime.test_utils.checkCases;
const collection = @import("html_collection.zig");
const Node = @import("node.zig").Node;
const HTMLElem = @import("../html/elements.zig");
pub const Union = @import("../html/elements.zig").Union;
@@ -122,6 +124,60 @@ pub const Element = struct {
// Return true.
return true;
}
pub fn _getElementsByTagName(
self: *parser.Element,
alloc: std.mem.Allocator,
tag_name: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(
alloc,
parser.elementToNode(self),
tag_name,
false,
);
}
pub fn _getElementsByClassName(
self: *parser.Element,
alloc: std.mem.Allocator,
classNames: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByClassName(
alloc,
parser.elementToNode(self),
classNames,
false,
);
}
// ParentNode
// https://dom.spec.whatwg.org/#parentnode
pub fn get_children(self: *parser.Element) !collection.HTMLCollection {
return try collection.HTMLCollectionChildren(parser.elementToNode(self), false);
}
pub fn get_firstElementChild(self: *parser.Element) !?Union {
var children = try get_children(self);
return try children._item(0);
}
pub fn get_lastElementChild(self: *parser.Element) !?Union {
// TODO we could check the last child node first, if it's an element,
// we can return it directly instead of looping twice over the
// children.
var children = try get_children(self);
const ln = try children.get_length();
if (ln == 0) return null;
return try children._item(ln - 1);
}
pub fn get_childElementCount(self: *parser.Element) !u32 {
var children = try get_children(self);
return try children.get_length();
}
pub fn deinit(_: *parser.Element, _: std.mem.Allocator) void {}
};
// Tests
@@ -192,4 +248,13 @@ pub fn testExecFn(
.{ .src = "b.hasAttribute('foo')", .ex = "false" },
};
try checkCases(js_env, &toggleAttr);
var parentNode = [_]Case{
.{ .src = "let c = document.getElementById('content')", .ex = "undefined" },
.{ .src = "c.children.length", .ex = "3" },
.{ .src = "c.firstElementChild.nodeName", .ex = "A" },
.{ .src = "c.lastElementChild.nodeName", .ex = "P" },
.{ .src = "c.childElementCount", .ex = "3" },
};
try checkCases(js_env, &parentNode);
}

View File

@@ -14,16 +14,21 @@ const Union = @import("element.zig").Union;
const Matcher = union(enum) {
matchByTagName: MatchByTagName,
matchByClassName: MatchByClassName,
matchTrue: struct {},
pub fn match(self: Matcher, node: *parser.Node) !bool {
switch (self) {
inline else => |case| return case.match(node),
inline .matchTrue => return true,
inline .matchByTagName => |case| return case.match(node),
inline .matchByClassName => |case| return case.match(node),
}
}
pub fn deinit(self: Matcher, alloc: std.mem.Allocator) void {
switch (self) {
inline else => |case| return case.deinit(alloc),
inline .matchTrue => return,
inline .matchByTagName => |case| return case.deinit(alloc),
inline .matchByClassName => |case| return case.deinit(alloc),
}
}
};
@@ -54,14 +59,17 @@ pub const MatchByTagName = struct {
pub fn HTMLCollectionByTagName(
alloc: std.mem.Allocator,
root: *parser.Node,
root: ?*parser.Node,
tag_name: []const u8,
include_root: bool,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = Walker{ .walkerDepthFirst = .{} },
.matcher = Matcher{
.matchByTagName = try MatchByTagName.init(alloc, tag_name),
},
.include_root = include_root,
};
}
@@ -95,17 +103,108 @@ pub const MatchByClassName = struct {
pub fn HTMLCollectionByClassName(
alloc: std.mem.Allocator,
root: *parser.Node,
root: ?*parser.Node,
classNames: []const u8,
include_root: bool,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = Walker{ .walkerDepthFirst = .{} },
.matcher = Matcher{
.matchByClassName = try MatchByClassName.init(alloc, classNames),
},
.include_root = include_root,
};
}
pub fn HTMLCollectionChildren(
root: ?*parser.Node,
include_root: bool,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = Walker{ .walkerChildren = .{} },
.matcher = Matcher{ .matchTrue = .{} },
.include_root = include_root,
};
}
const Walker = union(enum) {
walkerDepthFirst: WalkerDepthFirst,
walkerChildren: WalkerChildren,
pub fn get_next(self: Walker, root: *parser.Node, cur: ?*parser.Node) !?*parser.Node {
switch (self) {
inline else => |case| return case.get_next(root, cur),
}
}
};
// WalkerDepthFirst iterates over the DOM tree to return the next following
// node or null at the end.
//
// This implementation is a zig version of Netsurf code.
// http://source.netsurf-browser.org/libdom.git/tree/src/html/html_collection.c#n177
//
// The iteration is a depth first as required by the specification.
// https://dom.spec.whatwg.org/#htmlcollection
// https://dom.spec.whatwg.org/#concept-tree-order
pub const WalkerDepthFirst = struct {
pub fn get_next(_: WalkerDepthFirst, root: *parser.Node, cur: ?*parser.Node) !?*parser.Node {
var n = cur orelse root;
// TODO deinit next
if (try parser.nodeFirstChild(n)) |next| {
return next;
}
// TODO deinit next
if (try parser.nodeNextSibling(n)) |next| {
return next;
}
// TODO deinit parent
// Back to the parent of cur.
// If cur has no parent, then the iteration is over.
var parent = try parser.nodeParentNode(n) orelse return null;
// TODO deinit lastchild
var lastchild = try parser.nodeLastChild(parent);
while (n != root and n == lastchild) {
n = parent;
// TODO deinit parent
// Back to the prev's parent.
// If prev has no parent, then the loop must stop.
parent = try parser.nodeParentNode(n) orelse break;
// TODO deinit lastchild
lastchild = try parser.nodeLastChild(parent);
}
if (n == root) {
return null;
}
return try parser.nodeNextSibling(n);
}
};
// WalkerChildren iterates over the root's children only.
pub const WalkerChildren = struct {
pub fn get_next(_: WalkerChildren, root: *parser.Node, cur: ?*parser.Node) !?*parser.Node {
// On walk start, we return the first root's child.
if (cur == null) return try parser.nodeFirstChild(root);
// If cur is root, then return null.
// This is a special case, if the root is included in the walk, we
// don't want to go further to find children.
if (root == cur.?) return null;
return try parser.nodeNextSibling(cur.?);
}
};
// WEB IDL https://dom.spec.whatwg.org/#htmlcollection
// HTMLCollection is re implemented in zig here because libdom
// dom_html_collection expects a comparison function callback as arguement.
@@ -114,96 +213,68 @@ pub const HTMLCollection = struct {
pub const mem_guarantied = true;
matcher: Matcher,
walker: Walker,
root: *parser.Node,
root: ?*parser.Node,
// By default the HTMLCollection walk on the root's descendant only.
// But on somes cases, like for dom document, we want to walk over the root
// itself.
include_root: bool = false,
// save a state for the collection to improve the _item speed.
cur_idx: ?u32 = undefined,
cur_node: ?*parser.Node = undefined,
// get_next iterates over the DOM tree to return the next following node or
// null at the end.
//
// This implementation is a zig version of Netsurf code.
// http://source.netsurf-browser.org/libdom.git/tree/src/html/html_collection.c#n177
//
// The iteration is a depth first as required by the specification.
// https://dom.spec.whatwg.org/#htmlcollection
// https://dom.spec.whatwg.org/#concept-tree-order
fn get_next(root: *parser.Node, cur: *parser.Node) !?*parser.Node {
// TODO deinit next
if (try parser.nodeFirstChild(cur)) |next| {
return next;
// start returns the first node to walk on.
fn start(self: HTMLCollection) !?*parser.Node {
if (self.root == null) return null;
if (self.include_root) {
return self.root.?;
}
// TODO deinit next
if (try parser.nodeNextSibling(cur)) |next| {
return next;
}
// TODO deinit parent
// Back to the parent of cur.
// If cur has no parent, then the iteration is over.
var parent = try parser.nodeParentNode(cur) orelse return null;
// TODO deinit lastchild
var lastchild = try parser.nodeLastChild(parent);
var prev = cur;
while (prev != root and prev == lastchild) {
prev = parent;
// TODO deinit parent
// Back to the prev's parent.
// If prev has no parent, then the loop must stop.
parent = try parser.nodeParentNode(prev) orelse break;
// TODO deinit lastchild
lastchild = try parser.nodeLastChild(parent);
}
if (prev == root) {
return null;
}
return try parser.nodeNextSibling(prev);
return try self.walker.get_next(self.root.?, null);
}
/// get_length computes the collection's length dynamically according to
/// the current root structure.
// TODO: nodes retrieved must be de-referenced.
pub fn get_length(self: *HTMLCollection) !u32 {
if (self.root == null) return 0;
var len: u32 = 0;
var node: *parser.Node = self.root;
var ntype: parser.NodeType = undefined;
var node = try self.start() orelse return 0;
while (true) {
ntype = try parser.nodeType(node);
if (ntype == .element) {
if (try parser.nodeType(node) == .element) {
if (try self.matcher.match(node)) {
len += 1;
}
}
node = try get_next(self.root, node) orelse break;
node = try self.walker.get_next(self.root.?, node) orelse break;
}
return len;
}
pub fn _item(self: *HTMLCollection, index: u32) !?Union {
if (self.root == null) return null;
var i: u32 = 0;
var node: *parser.Node = self.root;
var ntype: parser.NodeType = undefined;
var node: *parser.Node = undefined;
// Use the current state to improve speed if possible.
if (self.cur_idx != null and index >= self.cur_idx.?) {
i = self.cur_idx.?;
node = self.cur_node.?;
} else {
node = try self.start() orelse return null;
}
while (true) {
ntype = try parser.nodeType(node);
if (ntype == .element) {
if (try parser.nodeType(node) == .element) {
if (try self.matcher.match(node)) {
// check if we found the searched element.
if (i == index) {
@@ -219,23 +290,20 @@ pub const HTMLCollection = struct {
}
}
node = try get_next(self.root, node) orelse break;
node = try self.walker.get_next(self.root.?, node) orelse break;
}
return null;
}
pub fn _namedItem(self: *HTMLCollection, name: []const u8) !?Union {
if (name.len == 0) {
return null;
}
if (self.root == null) return null;
if (name.len == 0) return null;
var node: *parser.Node = self.root;
var ntype: parser.NodeType = undefined;
var node = try self.start() orelse return null;
while (true) {
ntype = try parser.nodeType(node);
if (ntype == .element) {
if (try parser.nodeType(node) == .element) {
if (try self.matcher.match(node)) {
const elem = @as(*parser.Element, @ptrCast(node));
@@ -253,7 +321,7 @@ pub const HTMLCollection = struct {
}
}
node = try get_next(self.root, node) orelse break;
node = try self.walker.get_next(self.root.?, node) orelse break;
}
return null;
@@ -290,6 +358,13 @@ pub fn testExecFn(
.{ .src = "getElementsByTagNameAll.item(7).localName", .ex = "p" },
.{ .src = "getElementsByTagNameAll.namedItem('para-empty-child').localName", .ex = "span" },
.{ .src = "document.getElementById('content').getElementsByTagName('*').length", .ex = "4" },
.{ .src = "document.getElementById('content').getElementsByTagName('p').length", .ex = "2" },
.{ .src = "document.getElementById('content').getElementsByTagName('div').length", .ex = "0" },
.{ .src = "document.children.length", .ex = "1" },
.{ .src = "document.getElementById('content').children.length", .ex = "3" },
// check liveness
.{ .src = "let content = document.getElementById('content')", .ex = "undefined" },
.{ .src = "let pe = document.getElementById('para-empty')", .ex = "undefined" },

View File

@@ -1150,10 +1150,11 @@ pub inline fn documentGetElementsByTagName(doc: *Document, tagname: []const u8)
}
// documentGetDocumentElement returns the root document element.
pub inline fn documentGetDocumentElement(doc: *Document) !*Element {
pub inline fn documentGetDocumentElement(doc: *Document) !?*Element {
var elem: ?*Element = undefined;
const err = documentVtable(doc).dom_document_get_document_element.?(doc, &elem);
try DOMErr(err);
if (elem == null) return null;
return elem.?;
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>Null test</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of firstElementChild and lastChildElement returning null</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle" font-weight="bold">Test</text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
assert_equals(parentEl.firstElementChild, null)
assert_equals(parentEl.lastElementChild, null)
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 787 B

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Null Test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of firstElementChild and lastChildElement returning null</h1>
<div id="log"></div>
<p id="parentEl" style="font-weight:bold;">Test.</p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
assert_equals(parentEl.firstElementChild, null)
assert_equals(parentEl.lastElementChild, null)
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,15 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Null test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of firstElementChild and lastChildElement returning null</h1>
<div id="log"></div>
<p id="parentEl" style="font-weight:bold;">Test.</p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl")
assert_equals(parentEl.firstElementChild, null)
assert_equals(parentEl.lastElementChild, null)
})
</script>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>Dynamic Adding of Elements</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of Dynamic Adding of Elements</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle">The result of this test is
<tspan id="first_element_child" font-weight="bold">unknown.</tspan></text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var newChild = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
parentEl.appendChild(newChild);
assert_equals(parentEl.childElementCount, 2)
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 913 B

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Adding of Elements</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of Dynamic Adding of Elements</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">logged above.</span></p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var newChild = document.createElement("span");
parentEl.appendChild(newChild);
assert_equals(parentEl.childElementCount, 2)
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Dynamic Adding of Elements</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of Dynamic Adding of Elements</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">logged above.</span></p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl");
var newChild = document.createElement("span");
parentEl.appendChild(newChild);
assert_equals(parentEl.childElementCount, 2)
})
</script>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>Dynamic Removal of Elements</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of Dynamic Removal of Elements</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle">The result of this test is
<tspan id="first_element_child" font-weight="bold">unknown.</tspan><tspan id="last_element_child"> </tspan></text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var lec = parentEl.lastElementChild;
parentEl.removeChild(lec);
assert_equals(parentEl.childElementCount, 1)
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 907 B

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dynamic Removal of Elements</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of Removal Adding of Elements</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">logged above.</span><span id="last_element_child"> </span></p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var lec = parentEl.lastElementChild;
parentEl.removeChild(lec);
assert_equals(parentEl.childElementCount, 1)
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>Dynamic Removal of Elements</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of Dynamic Removal of Elements</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">unknown.</span><span id="last_element_child"> </span></p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl");
var lec = parentEl.lastElementChild;
parentEl.removeChild(lec);
assert_equals(parentEl.childElementCount, 1)
})
</script>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>childElementCount</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of childElementCount with No Child Element Nodes</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle" font-weight="bold">Test</text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
assert_equals(parentEl.childElementCount, 0)
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 735 B

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>childElementCount without Child Element Nodes</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of childElementCount with No Child Element Nodes</h1>
<div id="log"></div>
<p id="parentEl" style="font-weight:bold;">Test.</p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
assert_equals(parentEl.childElementCount, 0)
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,14 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>childElementCount without Child Element Nodes</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of childElementCount with No Child Element Nodes</h1>
<div id="log"></div>
<p id="parentEl" style="font-weight:bold;">Test.</p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl")
assert_equals(parentEl.childElementCount, 0)
})
</script>

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>childElementCount</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of childElementCount</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle">The result of <tspan id="first_element_child"><tspan>this</tspan> <tspan>test</tspan></tspan> is
<tspan id="middle_element_child" font-weight="bold">unknown.</tspan>
<tspan id="last_element_child" style="display:none;">fnord</tspan> </text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
assert_true("childElementCount" in parentEl)
assert_equals(parentEl.childElementCount, 3)
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 967 B

View File

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>childElementCount</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of childElementCount</h1>
<div id="log"></div>
<p id="parentEl">The result of <span id="first_element_child"><span>this</span> <span>test</span></span> is
<span id="middle_element_child" style="font-weight:bold;">unknown.</span>
<span id="last_element_child" style="display:none;">fnord</span> </p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
assert_true("childElementCount" in parentEl)
assert_equals(parentEl.childElementCount, 3)
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,20 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>childElementCount</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of childElementCount</h1>
<div id="log"></div>
<p id="parentEl">The result of <span id="first_element_child"><span>this</span> <span>test</span></span> is
<span id="middle_element_child" style="font-weight:bold;">given above.</span>
<span id="last_element_child" style="display:none;">fnord</span> </p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl")
assert_true("childElementCount" in parentEl)
assert_equals(parentEl.childElementCount, 3)
})
</script>

View File

@@ -0,0 +1,58 @@
<!DOCTYPE html>
<title>HTMLCollection edge cases</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<div id="test"><img><img id=foo><img id=foo><img name="bar"></div>
<script>
setup(function() {
// Add some non-HTML elements in there to test what happens with those.
var container = document.getElementById("test");
var child = document.createElementNS("", "img");
child.setAttribute("id", "baz");
container.appendChild(child);
child = document.createElementNS("", "img");
child.setAttribute("name", "qux");
container.appendChild(child);
});
test(function() {
var container = document.getElementById("test");
var result = container.children.item("foo");
assert_true(result instanceof Element, "Expected an Element.");
assert_false(result.hasAttribute("id"), "Expected the IDless Element.")
})
test(function() {
var container = document.getElementById("test");
var list = container.children;
var result = [];
for (var p in list) {
if (list.hasOwnProperty(p)) {
result.push(p);
}
}
assert_array_equals(result, ['0', '1', '2', '3', '4', '5']);
result = Object.getOwnPropertyNames(list);
assert_array_equals(result, ['0', '1', '2', '3', '4', '5', 'foo', 'bar', 'baz']);
// Mapping of exposed names to their indices in the list.
var exposedNames = { 'foo': 1, 'bar': 3, 'baz': 4 };
for (var exposedName in exposedNames) {
assert_true(exposedName in list);
assert_true(list.hasOwnProperty(exposedName));
assert_equals(list[exposedName], list.namedItem(exposedName));
assert_equals(list[exposedName], list.item(exposedNames[exposedName]));
assert_true(list[exposedName] instanceof Element);
}
var unexposedNames = ['qux'];
for (var unexposedName of unexposedNames) {
assert_false(unexposedName in list);
assert_false(list.hasOwnProperty(unexposedName));
assert_equals(list[unexposedName], undefined);
assert_equals(list.namedItem(unexposedName), null);
}
});
</script>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
[
<!ENTITY tree "<span id='first_element_child' style='font-weight:bold;'>unknown.</span>">
]>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Entity References</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of Entity References</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is &tree;</p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"
[
<!ENTITY tree "<tspan id='first_element_child' font-weight='bold'>unknown.</tspan>">
]>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>Entity References</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of Entity References</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle">The result of this test is &tree;</text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl")
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 1015 B

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
xmlns:pickle="http://ns.example.org/pickle"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>firstElementChild with namespaces</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of firstElementChild with namespaces</text>
<g id="parentEl">
<pickle:dill id="first_element_child"/>
</g>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
assert_equals(fec.localName, "dill")
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 888 B

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:pickle="http://ns.example.org/pickle">
<head>
<title>firstElementChild with namespaces</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of firstElementChild with namespaces</h1>
<div id="parentEl">
<pickle:dill id="first_element_child"/>
</div>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">logged above.</span></p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
assert_equals(fec.localName, "dill")
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>firstElementChild with namespaces</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of firstElementChild with namespaces</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is a unknown.</p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl")
var el = document.createElementNS("http://ns.example.org/pickle", "pickle:dill")
el.setAttribute("id", "first_element_child")
parentEl.appendChild(el)
var fec = parentEl.firstElementChild
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
assert_equals(fec.localName, "dill")
})
</script>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>firstElementChild</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of firstElementChild</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle">The result of this test is
<tspan id="first_element_child" font-weight="bold">unknown.</tspan></text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 889 B

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>firstElementChild</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of firstElementChild</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">logged above.</span></p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>firstElementChild</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of firstElementChild</h1>
<div id="log"></div>
<p id="parentEl">The result of this test is
<span id="first_element_child" style="font-weight:bold;">logged above.</span></p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl");
var fec = parentEl.firstElementChild;
assert_true(!!fec)
assert_equals(fec.nodeType, 1)
assert_equals(fec.getAttribute("id"), "first_element_child")
})
</script>

View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<title>Element.getElementsByClassName</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
test(function() {
var a = document.createElement("a"), b = document.createElement("b")
b.className = "foo"
a.appendChild(b)
var list = a.getElementsByClassName("foo")
assert_array_equals(list, [b])
var secondList = a.getElementsByClassName("foo")
assert_true(list === secondList || list !== secondList, "Caching is allowed.")
}, "getElementsByClassName should work on disconnected subtrees.")
test(function() {
var list = document.getElementsByClassName("foo")
assert_false(list instanceof NodeList, "NodeList")
assert_true(list instanceof HTMLCollection, "HTMLCollection")
}, "Interface should be correct.")
test(function() {
var a = document.createElement("a");
var b = document.createElement("b");
var c = document.createElement("c");
b.className = "foo";
document.body.appendChild(a);
this.add_cleanup(function() {document.body.removeChild(a)});
a.appendChild(b);
var l = a.getElementsByClassName("foo");
assert_true(l instanceof HTMLCollection);
assert_equals(l.length, 1);
c.className = "foo";
a.appendChild(c);
assert_equals(l.length, 2);
a.removeChild(c);
assert_equals(l.length, 1);
}, "getElementsByClassName() should be a live collection");
</script>

View File

@@ -0,0 +1,51 @@
<!doctype html>
<meta charset=utf-8>
<title></title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<iframe src="Element-getElementsByTagName-change-document-HTMLNess-iframe.xml"></iframe>
<script>
setup({ single_test: true });
onload = function() {
var parent = document.createElement("div");
var child1 = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
child1.textContent = "xhtml:a";
var child2 = document.createElementNS("http://www.w3.org/1999/xhtml", "A");
child2.textContent = "xhtml:A";
var child3 = document.createElementNS("", "a");
child3.textContent = "a";
var child4 = document.createElementNS("", "A");
child4.textContent = "A";
parent.appendChild(child1);
parent.appendChild(child2);
parent.appendChild(child3);
parent.appendChild(child4);
var list = parent.getElementsByTagName("A");
assert_array_equals(list, [child1, child4],
"In an HTML document, should lowercase the tagname passed in for HTML " +
"elements only");
frames[0].document.documentElement.appendChild(parent);
assert_array_equals(list, [child1, child4],
"After changing document, should still be lowercasing for HTML");
assert_array_equals(parent.getElementsByTagName("A"),
[child2, child4],
"New list with same root and argument should not be lowercasing now");
// Now reinsert all those nodes into the parent, to blow away caches.
parent.appendChild(child1);
parent.appendChild(child2);
parent.appendChild(child3);
parent.appendChild(child4);
assert_array_equals(list, [child1, child4],
"After blowing away caches, should still have the same list");
assert_array_equals(parent.getElementsByTagName("A"),
[child2, child4],
"New list with same root and argument should still not be lowercasing");
done();
}
</script>

View File

@@ -0,0 +1,30 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Element.getElementsByTagName</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-element-getelementsbytagname">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="Document-Element-getElementsByTagName.js"></script>
<div id="log"></div>
<script>
var element;
setup(function() {
element = document.createElement("div");
element.appendChild(document.createTextNode("text"));
var p = element.appendChild(document.createElement("p"));
p.appendChild(document.createElement("a"))
.appendChild(document.createTextNode("link"));
p.appendChild(document.createElement("b"))
.appendChild(document.createTextNode("bold"));
p.appendChild(document.createElement("em"))
.appendChild(document.createElement("u"))
.appendChild(document.createTextNode("emphasized"));
element.appendChild(document.createComment("comment"));
});
test_getElementsByTagName(element, element);
test(function() {
assert_array_equals(element.getElementsByTagName(element.localName), []);
}, "Matching the context object");
</script>

View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>Element.getElementsByTagNameNS</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-element-getelementsbytagnamens">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="Document-Element-getElementsByTagNameNS.js"></script>
<div id="log"></div>
<script>
var element;
setup(function() {
element = document.createElement("div");
element.appendChild(document.createTextNode("text"));
var p = element.appendChild(document.createElement("p"));
p.appendChild(document.createElement("a"))
.appendChild(document.createTextNode("link"));
p.appendChild(document.createElement("b"))
.appendChild(document.createTextNode("bold"));
p.appendChild(document.createElement("em"))
.appendChild(document.createElement("u"))
.appendChild(document.createTextNode("emphasized"));
element.appendChild(document.createComment("comment"));
});
test_getElementsByTagNameNS(element, element);
test(function() {
assert_array_equals(element.getElementsByTagNameNS("*", element.localName), []);
}, "Matching the context object (wildcard namespace)");
test(function() {
assert_array_equals(
element.getElementsByTagNameNS("http://www.w3.org/1999/xhtml",
element.localName),
[]);
}, "Matching the context object (specific namespace)");
</script>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:h="http://www.w3.org/1999/xhtml"
version="1.1"
width="100%" height="100%" viewBox="0 0 400 400">
<title>lastElementChild</title>
<h:script src="/resources/testharness.js"/>
<h:script src="/resources/testharnessreport.js"/>
<text x="200" y="40" font-size="25" fill="black" text-anchor="middle">Test of lastElementChild</text>
<text id="parentEl" x="200" y="70" font-size="20" fill="black" text-anchor="middle">The result of <tspan id="first_element_child">this test</tspan> is <tspan id="last_element_child" font-weight="bold">not</tspan> known.</text>
<h:script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var lec = parentEl.lastElementChild;
assert_true(!!lec)
assert_equals(lec.nodeType, 1)
assert_equals(lec.getAttribute("id"), "last_element_child")
})
]]></h:script>
</svg>

After

Width:  |  Height:  |  Size: 926 B

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>firstElementChild</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<h1>Test of firstElementChild</h1>
<div id="log"></div>
<p id="parentEl">The result of <span id="first_element_child">this test</span> is <span id="last_element_child" style="font-weight:bold;">logged</span> above.</p>
<script><![CDATA[
test(function() {
var parentEl = document.getElementById("parentEl");
var lec = parentEl.lastElementChild;
assert_true(!!lec)
assert_equals(lec.nodeType, 1)
assert_equals(lec.getAttribute("id"), "last_element_child")
})
]]></script>
</body>
</html>

View File

@@ -0,0 +1,17 @@
<!DOCTYPE HTML>
<meta charset=utf-8>
<title>lastElementChild</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<h1>Test of lastElementChild</h1>
<div id="log"></div>
<p id="parentEl">The result of <span id="first_element_child">this test</span> is <span id="last_element_child" style="font-weight:bold;">logged</span> above.</p>
<script>
test(function() {
var parentEl = document.getElementById("parentEl");
var lec = parentEl.lastElementChild;
assert_true(!!lec)
assert_equals(lec.nodeType, 1)
assert_equals(lec.getAttribute("id"), "last_element_child")
})
</script>

View File

@@ -0,0 +1,27 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>ParentNode.children</title>
<link rel=help href="https://dom.spec.whatwg.org/#dom-parentnode-children">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<div style="display: none">
<ul><li id='test'>1</li><li>2</li><li>3</li><li>4</li></ul>
</div>
<script>
test(() => {
var node = document.getElementById("test");
var parentNode = node.parentNode;
var children = parentNode.children;
assert_true(children instanceof HTMLCollection);
var li = document.createElement("li");
assert_equals(children.length, 4);
parentNode.appendChild(li);
assert_equals(children.length, 5);
parentNode.removeChild(li);
assert_equals(children.length, 4);
}, "ParentNode.children should be a live collection");
</script>
</html>