Skip to content

Commit 59adca0

Browse files
committed
Add universal selector
1 parent 9b0d2d3 commit 59adca0

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ select(ast, 'paragraph emphasis > text')
2828
- [x] Sibling selectors: `paragraph ~ text`
2929
- [x] Adjacent sibling selectors: `paragraph + text`
3030
- [x] Group selectors: `paragraph, text`
31+
- [x] Universal selector: `*`
3132
- [ ] Attribute selectors: `text[value*="substr"]`
32-
- [ ] Universal selectors: `*`
3333

3434
## API
3535

lib/select.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,24 @@ select.rule = function (selector, ast) {
4646
return result;
4747

4848
function walk (node, parent) {
49-
if (node.type == selector.tagName) {
49+
if (selector.tagName == '*' || node.type == selector.tagName) {
5050
if (!selector.rule) {
5151
append(result, [node]);
5252
}
5353
else if (!selector.rule.nestingOperator ||
5454
selector.rule.nestingOperator == '>') {
55-
if (!node.children) return;
56-
node.children.forEach(function (childNode) {
57-
append(result, select.rule(selector.rule, childNode));
58-
});
55+
if (node.children) {
56+
node.children.forEach(function (childNode) {
57+
append(result, select.rule(selector.rule, childNode));
58+
});
59+
}
5960
}
6061
else {
61-
if (!parent) return;
62-
append(result, select.rule(selector.rule, {
63-
children: parent.children.slice(parent.children.indexOf(node) + 1)
64-
}));
62+
if (parent) {
63+
append(result, select.rule(selector.rule, {
64+
children: parent.children.slice(parent.children.indexOf(node) + 1)
65+
}));
66+
}
6567
}
6668
}
6769

test/test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,19 @@ test('grouping', function (t) {
8383
]);
8484
t.end();
8585
});
86+
87+
88+
test('universal selector', function (t) {
89+
t.equal(select(ast, '*').length, totalNodes(ast));
90+
t.deepEqual(select(ast, '* ~ heading'), select(ast, 'heading ~ heading'));
91+
t.true(select(ast, 'list > *').every(function (listItem) {
92+
return listItem.type == 'listItem';
93+
}));
94+
t.end();
95+
96+
function totalNodes (ast) {
97+
return 1 + (ast.children || []).map(totalNodes).reduce(function (a, b) {
98+
return a + b;
99+
}, 0);
100+
}
101+
});

0 commit comments

Comments
 (0)