Skip to content

Commit 33476e5

Browse files
committed
Add string attribute operators
1 parent c7e14fb commit 33476e5

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ select(ast, 'paragraph emphasis > text')
2929
- [x] Adjacent sibling selectors: `paragraph + text`
3030
- [x] Group selectors: `paragraph, text`
3131
- [x] Universal selector: `*`
32-
- [ ] Attribute selectors: `text[value*="substr"]`
32+
- [x] Attribute selectors: `text[value*="substr"]`
3333
- [x] Existence: `[value]`
3434
- [x] Equality: `[value="foo"]`
35-
- [ ] Begins with: `[value^="prefix"]`
36-
- [ ] Containment: `[value*="substr"]`
37-
- [ ] Ends with: `[value$="suffix"]`
35+
- [x] Begins with: `[value^="prefix"]`
36+
- [x] Containment: `[value*="substr"]`
37+
- [x] Ends with: `[value$="suffix"]`
3838

3939
## API
4040

lib/select.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ function matches (rule, node) {
106106
}
107107
return node[attr.name] == attr.value;
108108

109+
case '^=':
110+
return typeof node[attr.name] == 'string' &&
111+
node[attr.name].slice(0, attr.value.length) == attr.value;
112+
113+
case '*=':
114+
return typeof node[attr.name] == 'string' &&
115+
node[attr.name].indexOf(attr.value) >= 0;
116+
117+
case '$=':
118+
return typeof node[attr.name] == 'string' &&
119+
node[attr.name].slice(-attr.value.length) == attr.value;
120+
109121
default:
110122
throw Error('Undefined attribute operator: ' + attr.operator);
111123
}

lib/selector.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ var Parser = require('css-selector-parser').CssSelectorParser;
66
module.exports = function SelectorParser () {
77
var parser = new Parser;
88
parser.registerNestingOperators('>', '+', '~');
9+
parser.registerAttrEqualityMods('^', '*', '$');
910
return parser.parse.bind(parser);
1011
};

test/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,11 @@ test('attribute selectors', function (t) {
117117
t.deepEqual(select(ast, '[ordered=true]'), [ast.children[6]]);
118118
t.deepEqual(select(ast, 'list[loose=false]'), select(ast, 'list'));
119119

120+
t.comment('string operators');
121+
t.deepEqual(select(ast, '[link^="http://"]'), select(ast, 'definition'));
122+
t.deepEqual(select(ast, '[value*=reduce]'),
123+
select(ast, 'root > code[lang=js]'));
124+
t.deepEqual(select(ast, '[type$=Cell]'), select(ast, 'tableCell'));
125+
120126
t.end();
121127
});

0 commit comments

Comments
 (0)