Truly independent web browser
<!DOCTYPE html>
<script src="../include.js"></script>
<table><tbody id="rows"></tbody></table>
<script>
test(() => {
const rows = document.getElementById("rows");
for (let i = 0; i < 10000; ++i) {
const row = document.createElement("tr");
if (i % 3 === 0)
row.className = "marked";
rows.append(row);
}
const all = Array.from(rows.children);
const marked = all.filter(row => row.className === "marked");
function check(selector, expected) {
const found = Array.from(rows.querySelectorAll(selector));
const same = found.length === expected.length && found.every((row, i) => row === expected[i]);
println(`${selector}: ${found.length} ${same ? "PASS" : "FAIL"}`);
}
check("tr:nth-child(odd)", all.filter((_, i) => i % 2 === 0));
check("tr:nth-last-child(3n)", all.filter((_, i) => (all.length - i) % 3 === 0));
check("tr:nth-of-type(2n)", all.filter((_, i) => i % 2 === 1));
check("tr:nth-last-of-type(-n+5)", all.slice(-5));
check("tr:nth-child(even of .marked)", marked.filter((_, i) => i % 2 === 1));
check("tr:nth-last-child(1 of .marked)", marked.slice(-1));
let matching = 0;
for (let i = all.length - 1; i >= 0; --i)
matching += all[i].matches(":nth-child(4n+1)") ? 1 : 0;
println(`matches() backwards: ${matching}`);
});
</script>