Skip to content

Commit aa35329

Browse files
committed
Add string node type. Fixes #25.
1 parent d2cfb82 commit aa35329

File tree

7 files changed

+75
-5
lines changed

7 files changed

+75
-5
lines changed

API.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ Arguments:
132132

133133
* `props (object)`: The new node's properties.
134134

135+
### `parser.string([props])`
136+
137+
Creates a new string node.
138+
139+
```js
140+
parser.string();
141+
// => (empty)
142+
```
143+
144+
Arguments:
145+
146+
* `props (object)`: The new node's properties.
147+
135148
### `parser.tag([props])`
136149

137150
Creates a new tag selector.
@@ -164,7 +177,7 @@ Arguments:
164177

165178
A string representation of the selector type. It can be one of the following;
166179
`attribute`, `class`, `combinator`, `comment`, `id`, `pseudo`, `root`,
167-
`selector`, `tag`, or `universal`.
180+
`selector`, `string`, `tag`, or `universal`.
168181

169182
```js
170183
parser.attribute({attribute: 'href'}).type;

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.3.0
2+
3+
* Adds a new node type, `String`, to fix a crash on selectors such as
4+
`foo:bar("test")`.
5+
16
# 1.2.1
27

38
* Fixes a crash when the parser encountered a trailing combinator.

src/__tests__/parser.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
import test from 'tape';
24
import parser from '../index';
35

@@ -37,13 +39,19 @@ test('parser#pseudo', (t) => {
3739
t.equal(String(node), '::before');
3840
});
3941

42+
test('parser#string', (t) => {
43+
let node = parser.string({value: '"wow"'});
44+
t.plan(1);
45+
t.equal(String(node), '"wow"');
46+
});
47+
4048
test('parser#tag', (t) => {
4149
let node = parser.tag({value: 'button'});
4250
t.plan(1);
4351
t.equal(String(node), 'button');
4452
});
4553

46-
test('parser#tag', (t) => {
54+
test('parser#universal', (t) => {
4755
let node = parser.universal();
4856
t.plan(1);
4957
t.equal(String(node), '*');

src/__tests__/pseudos.js

+6
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ test('extraneous non-combinating whitespace', ' h1:after , h2:after ', (t,
9090
t.equal(tree.nodes[1].nodes[1].value, ':after');
9191
t.equal(tree.nodes[1].nodes[1].spaces.after, ' ');
9292
});
93+
94+
test('negation pseudo element with quotes', 'h1:not(".heading")', (t, tree) => {
95+
t.plan(2);
96+
t.equal(tree.nodes[0].nodes[1].value, ':not');
97+
t.equal(tree.nodes[0].nodes[1].nodes[0].nodes[0].value, '".heading"');
98+
});

src/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import Id from './selectors/id';
99
import Pseudo from './selectors/pseudo';
1010
import Root from './selectors/root';
1111
import Selector from './selectors/selector';
12+
import Str from './selectors/string';
1213
import Tag from './selectors/tag';
1314
import Universal from './selectors/universal';
1415

1516
let parser = function (processor) {
1617
return new Processor(processor);
17-
}
18+
};
1819

1920
parser.attribute = function (opts) {
2021
return new Attribute(opts);
@@ -30,7 +31,7 @@ parser.combinator = function (opts) {
3031

3132
parser.comment = function (opts) {
3233
return new Comment(opts);
33-
}
34+
};
3435

3536
parser.id = function (opts) {
3637
return new Id(opts);
@@ -48,6 +49,10 @@ parser.selector = function (opts) {
4849
return new Selector(opts);
4950
};
5051

52+
parser.string = function (opts) {
53+
return new Str(opts);
54+
};
55+
5156
parser.tag = function (opts) {
5257
return new Tag(opts);
5358
};

src/parser.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ClassName from './selectors/className';
66
import Comment from './selectors/comment';
77
import ID from './selectors/id';
88
import Tag from './selectors/tag';
9+
import Str from './selectors/string';
910
import Pseudo from './selectors/pseudo';
1011
import Attribute from './selectors/attribute';
1112
import Universal from './selectors/universal';
@@ -69,7 +70,7 @@ export default class Parser {
6970
} else {
7071
attributeProps.attribute = parts[0];
7172
}
72-
attr = new Attribute(attributeProps)
73+
attr = new Attribute(attributeProps);
7374

7475
if (parts[2]) {
7576
let insensitive = parts[2].split(/(\s+i\s*?)$/);
@@ -259,6 +260,25 @@ export default class Parser {
259260
}
260261
}
261262

263+
string () {
264+
let token = this.currToken;
265+
this.newNode(new Str({
266+
value: this.currToken[1],
267+
source: {
268+
start: {
269+
line: token[2],
270+
column: token[3]
271+
},
272+
end: {
273+
line: token[4],
274+
column: token[5]
275+
}
276+
},
277+
sourceIndex: token[6]
278+
}));
279+
this.position++;
280+
}
281+
262282
universal (namespace) {
263283
let nextToken = this.nextToken;
264284
if (nextToken && nextToken[1] === '|') {
@@ -405,6 +425,9 @@ export default class Parser {
405425
case 'combinator':
406426
this.combinator();
407427
break;
428+
case 'string':
429+
this.string();
430+
break;
408431
}
409432
}
410433

src/selectors/string.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
import Node from './node';
4+
5+
export default class String extends Node {
6+
constructor (opts) {
7+
super(opts);
8+
this.type = 'string';
9+
}
10+
}

0 commit comments

Comments
 (0)