Files
browser/src/browser/tests/node/child_nodes.html
2025-10-28 19:12:47 +08:00

91 lines
2.7 KiB
HTML

<!DOCTYPE html>
<script src="../testing.js"></script>
<div id=d1><p id=p1></p><p id=p2></p></div>
<div id=empty></div>
<div id=one><p id=p10></p></div>
<script id=childNodes>
const div = $('#d1');
const children = div.childNodes;
testing.expectEqual(true, children instanceof NodeList);
testing.expectEqual(2, children.length);
testing.expectEqual('NodeList', children.constructor.name);
// we do some weird stuff with caching, so accessing these in different order
// needs to be tested
testing.expectEqual($('#p1'), children[0]);
testing.expectEqual($('#p1'), children[0]);
testing.expectEqual($('#p2'), children[1]);
testing.expectEqual($('#p1'), children[0]);
testing.expectEqual(undefined, children[4]);
testing.expectEqual(undefined, children[-1]);
testing.expectEqual(['p1', 'p2'], Array.from(children).map((n) => n.id));
</script>
<script id=values>
let acc = [];
for (let x of children.values()) {
acc.push(x.id);
}
testing.expectEqual(['p1', 'p2'], acc);
// same as .values()
acc = [];
for (let x of children) {
acc.push(x.id);
}
testing.expectEqual(['p1', 'p2'], acc);
</script>
<script id=keys>
acc = [];
for (let x of children.keys()) {
acc.push(x);
}
testing.expectEqual([0, 1], acc);
</script>
<script id=entries>
acc = [];
for (let x of children.entries()) {
acc.push([x[0], x[1].id]);
}
testing.expectEqual([[0, 'p1'], [1, 'p2']], acc);
</script>
<script id=empty>
const empty = $('#empty').childNodes;
testing.expectEqual(0, empty.length);
testing.expectEqual(undefined, empty[0]);
testing.expectEqual([], Array.from(empty.keys()));
testing.expectEqual([], Array.from(empty.values()));
testing.expectEqual([], Array.from(empty.entries()));
testing.expectEqual([], Array.from(empty));
</script>
<script id=one>
const one = $('#one').childNodes;
const p10 = $('#p10');
testing.expectEqual(1, one.length);
testing.expectEqual(p10, one[0]);
testing.expectEqual([0], Array.from(one.keys()));
testing.expectEqual([p10], Array.from(one.values()));
testing.expectEqual([[0, p10]], Array.from(one.entries()));
testing.expectEqual([p10], Array.from(one));
</script>
<script id=contains>
testing.expectEqual(true, document.contains(document));
testing.expectEqual(true, $('#d1').contains($('#d1')));
testing.expectEqual(true, document.contains($('#d1')));
testing.expectEqual(true, document.contains($('#p1')));
testing.expectEqual(true, document.contains($('#p2')));
testing.expectEqual(true, $('#d1').contains($('#p1')));
testing.expectEqual(true, $('#d1').contains($('#p2')));
testing.expectEqual(false, $('#d1').contains($('#empty')));
testing.expectEqual(false, $('#d1').contains($('#p10')));
</script>