Skip to content

Commit 61b76c3

Browse files
committed
fix semantics of :nth-child regarding the root element
1 parent 8107052 commit 61b76c3

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

lib/match-node.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ function matchPseudos (rule, node, nodeIndex, parent) {
6565
return parent == null;
6666

6767
case 'nth-child':
68-
return pseudo.value(nodeIndex);
68+
return parent && pseudo.value(nodeIndex);
6969

7070
case 'first-child':
71-
return !parent || nodeIndex == 0;
71+
return parent && nodeIndex == 0;
7272

7373
case 'last-child':
74-
return !parent || nodeIndex == parent.children.length - 1;
74+
return parent && nodeIndex == parent.children.length - 1;
7575

7676
case 'not':
7777
return !matchNode(pseudo.value.rule, node, nodeIndex, parent);

test/select.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ test('structural pseudo-classes', function (t) {
140140
});
141141

142142
t.test(':nth-child', function (t) {
143+
// 6.6.5.2 explicitly states that matching element must have a parent.
143144
t.deepEqual(select(ast, ':root:nth-child(0)'), []);
144-
t.deepEqual(select(ast, ':root:nth-child(n)'), [ast]);
145+
t.deepEqual(select(ast, ':root:nth-child(n)'), []);
145146
t.deepEqual(select(ast, 'root > list:nth-child(2n+5)'),
146147
select(ast, 'root > list'));
147148
t.deepEqual(select(ast, 'heading:nth-child(even)'), [
@@ -155,7 +156,7 @@ test('structural pseudo-classes', function (t) {
155156
});
156157

157158
t.test(':first-child', function (t) {
158-
t.deepEqual(select(ast, ':root:first-child'), [ast]);
159+
t.deepEqual(select(ast, ':root:first-child'), []);
159160
t.deepEqual(select(ast, 'heading:first-child'), [path(ast, [0])]);
160161
t.deepEqual(select(ast, 'list listItem:first-child [value]:first-child'), [
161162
path(ast, [4, 0, 0, 0]),
@@ -167,7 +168,7 @@ test('structural pseudo-classes', function (t) {
167168
});
168169

169170
t.test(':last-child', function (t) {
170-
t.deepEqual(select(ast, ':root:last-child'), [ast]);
171+
t.deepEqual(select(ast, ':root:last-child'), []);
171172
t.deepEqual(select(ast, 'tableCell:last-child *')
172173
.map(function (node) { return node.value }),
173174
['mi', 'dolor', '15000']);

0 commit comments

Comments
 (0)