blob: 19ba9b6257441bf205a1a2d697b4ba4b4c3886c3 [file] [log] [blame]
<!doctype html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<style>
#test-root * {
background-color: red;
}
</style>
<style id="style">
</style>
</head>
<body>
<div style="display:none" id="test-root">
<testcase id="testcase1"></testcase>
<testcase id="testcase2"></testcase>
<testcase id="testcase3"></testcase>
<testcase id="testcase4"></testcase>
<testcase id="testcase5"></testcase>
</div>
</body>
<script>
description('Test cases of :nth-last-child() matching only the last child.');
function testQuerySelector(selector, expectedIds) {
shouldBe("document.querySelectorAll('" + selector + "').length", '' + expectedIds.length);
for (var i = 0; i < expectedIds.length; ++i)
shouldBeEqualToString("document.querySelectorAll('" + selector + "')[" + i + "].id", expectedIds[i]);
}
function testStyling(selector, expectedIds) {
var stylingElement = document.getElementById("style");
stylingElement.innerHTML = '' + selector + ' { background-color: rgb(10, 100, 200); }';
var allTestCases = document.querySelectorAll("#test-root *");
for (var i = 0; i < allTestCases.length; ++i) {
var expectMatch = expectedIds.indexOf(allTestCases[i].id) >= 0;
shouldBeEqualToString('getComputedStyle(document.querySelectorAll("#test-root *")[' + i + ']).backgroundColor', expectMatch ? 'rgb(10, 100, 200)' : 'rgb(255, 0, 0)');
}
stylingElement.innerHTML = '';
}
function testSelector(selector, expectedIds) {
debug("Testing \"" + selector + "\"");
testQuerySelector("#test-root " + selector, expectedIds);
testStyling("#test-root " + selector, expectedIds);
debug("");
}
// All of thoses only match the last child, they are synonym with :last-child.
testSelector(":nth-last-child(1)", ["testcase5"]);
testSelector(":nth-last-child(+1)", ["testcase5"]);
testSelector(":nth-last-child(-n+1)", ["testcase5"]);
testSelector(":nth-last-child(-2n+1)", ["testcase5"]);
testSelector(":nth-last-child(-3n+1)", ["testcase5"]);
// The above combined with other :nth-last-child().
testSelector(":nth-last-child(odd):nth-last-child(1):nth-last-child(2n+1)", ["testcase5"]);
testSelector(":nth-last-child(odd):nth-last-child(+1):nth-last-child(2n+1)", ["testcase5"]);
testSelector(":nth-last-child(odd):nth-last-child(-n+1):nth-last-child(2n+1)", ["testcase5"]);
testSelector(":nth-last-child(odd):nth-last-child(-2n+1):nth-last-child(2n+1)", ["testcase5"]);
testSelector(":nth-last-child(odd):nth-last-child(-3n+1):nth-last-child(2n+1)", ["testcase5"]);
// Some related counter examples.
testSelector(":nth-last-child(-n-1)", []);
testSelector(":nth-last-child(n+1)", ["testcase1", "testcase2", "testcase3", "testcase4", "testcase5"]);
testSelector(":nth-last-child(n-1)", ["testcase1", "testcase2", "testcase3", "testcase4", "testcase5"]);
</script>
<script src="../../resources/js-test-post.js"></script>
</html>