Skip to content

Commit 7524537

Browse files
committed
Use escaped values for id, namespace, pseudo, and root nodes. Fix issues in lossy parse mode.
1 parent e88cb66 commit 7524537

9 files changed

Lines changed: 38 additions & 53 deletions

File tree

‎src/__tests__/escapes.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {test} from './util/helpers';
22

3-
test('escaped semicolon', '.\\;', (t, tree) => {
3+
test('escaped semicolon in class', '.\\;', (t, tree) => {
44
t.deepEqual(tree.nodes[0].nodes[0].value, ';');
55
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\;');
66
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
77
});
88

9-
test('escaped semicolon', '#\\;', (t, tree) => {
10-
t.deepEqual(tree.nodes[0].nodes[0].value, '\\;');
9+
test('escaped semicolon in id', '#\\;', (t, tree) => {
10+
t.deepEqual(tree.nodes[0].nodes[0].value, ';');
11+
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\;');
1112
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
1213
});

‎src/__tests__/guards.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('className guard', '.foo', (t, tree) => {
1818
t.false(parser.isClassName(undefined));
1919
t.true(parser.isClassName(n));
2020
t.false(parser.isContainer(n));
21-
t.true(parser.isNamespace(n));
21+
t.false(parser.isNamespace(n));
2222
});
2323

2424
test('combinator guard', '.foo > .bar', (t, tree) => {

‎src/__tests__/lossy.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ ava('tag - extraneous whitespace', testLossy, ' h1 , h2 ', 'h1,h2');
5252
ava('tag - trailing comma', testLossy, 'h1, ', 'h1,');
5353
ava('tag - trailing comma (1)', testLossy, 'h1,', 'h1,');
5454
ava('tag - trailing comma (2)', testLossy, 'h1', 'h1');
55-
ava('tag - trailing slash (1)', testLossy, 'h1\\ ', 'h1\\');
55+
ava('tag - trailing slash (1)', testLossy, 'h1\\ ', 'h1\\ ');
5656
ava('tag - trailing slash (2)', testLossy, 'h1\\ h2\\', 'h1\\ h2\\');
5757

5858
ava('universal - combinator', testLossy, ' * + * ', '*+*');

‎src/__tests__/pseudos.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ test('extra whitespace inside parentheses', 'a:not( h2 )', (t, tree) => {
6161

6262
test('escaped numbers in class name with pseudo', 'a:before.\\31\\ 0', (t, tree) => {
6363
t.deepEqual(tree.nodes[0].nodes[2].type, 'class');
64-
t.deepEqual(tree.nodes[0].nodes[2].value, '\\31\\ 0');
64+
t.deepEqual(tree.nodes[0].nodes[2].value, '1 0');
65+
t.deepEqual(tree.nodes[0].nodes[2].raws.value, '\\31\\ 0');
6566
});
6667

6768
test('nested pseudo', '.btn-group>.btn:last-child:not(:first-child)', (t, tree) => {

‎src/parser.js‎

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ export default class Parser {
7373

7474
this.css = typeof this.rule === 'string' ? this.rule : this.rule.selector;
7575

76-
if (this.options.lossy) {
77-
this.css = this.css.trim();
78-
}
7976
this.tokens = tokenize({
8077
css: this.css,
8178
error: this._errorGenerator(),
@@ -346,15 +343,15 @@ export default class Parser {
346343
this.currToken[TOKEN.TYPE] === tokens.combinator)) {
347344
const content = this.content();
348345
if (this.nextToken && this.nextToken[TOKEN.TYPE] === tokens.combinator) {
349-
node.spaces.before = this.parseSpace(content);
346+
node.spaces.before = this.optionalSpace(content);
350347
node.source = getTokenSource(this.nextToken);
351348
node.sourceIndex = this.nextToken[TOKEN.START_POS];
352349
} else if (this.prevToken && this.prevToken[TOKEN.TYPE] === tokens.combinator) {
353-
node.spaces.after = this.parseSpace(content);
350+
node.spaces.after = this.optionalSpace(content);
354351
} else if (this.currToken[TOKEN.TYPE] === tokens.combinator) {
355352
node.value = content;
356353
} else if (this.currToken[TOKEN.TYPE] === tokens.space) {
357-
node.value = this.parseSpace(content, ' ');
354+
node.value = this.requiredSpace(content);
358355
}
359356
this.position ++;
360357
}
@@ -512,14 +509,14 @@ export default class Parser {
512509
this.prevToken[TOKEN.TYPE] === tokens.comma ||
513510
this.prevToken[TOKEN.TYPE] === tokens.openParenthesis
514511
) {
515-
this.spaces = this.parseSpace(content);
512+
this.spaces = this.optionalSpace(content);
516513
this.position ++;
517514
} else if (
518515
this.position === (this.tokens.length - 1) ||
519516
this.nextToken[TOKEN.TYPE] === tokens.comma ||
520517
this.nextToken[TOKEN.TYPE] === tokens.closeParenthesis
521518
) {
522-
this.current.last.spaces.after = this.parseSpace(content);
519+
this.current.last.spaces.after = this.optionalSpace(content);
523520
this.position ++;
524521
} else {
525522
this.combinator();
@@ -564,7 +561,7 @@ export default class Parser {
564561
if (current.lastIndexOf('\\') === current.length - 1) {
565562
let next = this.nextToken;
566563
if (next && next[TOKEN.TYPE] === tokens.space) {
567-
word += this.parseSpace(this.content(next), ' ');
564+
word += this.requiredSpace(this.content(next));
568565
this.position ++;
569566
}
570567
}
@@ -608,11 +605,12 @@ export default class Parser {
608605
};
609606
node = new ID(unescapeProp(idOpts, "value"));
610607
} else {
611-
node = new Tag({
608+
let tagOpts = {
612609
value,
613610
source,
614611
sourceIndex,
615-
});
612+
};
613+
node = new Tag(unescapeProp(tagOpts, "value"));
616614
}
617615
this.newNode(node, namespace);
618616
// Ensure that the namespace is used only once
@@ -710,46 +708,32 @@ export default class Parser {
710708
);
711709
}
712710

713-
parseNamespace (namespace) {
714-
if (this.options.lossy && typeof namespace === 'string') {
715-
const trimmed = namespace.trim();
716-
if (!trimmed.length) {
717-
return true;
718-
}
719-
720-
return trimmed;
721-
}
722-
723-
return namespace;
711+
requiredSpace (space) {
712+
return this.options.lossy ? ' ' : space;
724713
}
725714

726-
parseSpace (space, replacement = '') {
727-
return this.options.lossy ? replacement : space;
728-
}
729-
730-
parseValue (value) {
731-
if (!this.options.lossy || !value || typeof value !== 'string') {
732-
return value;
733-
}
734-
return value.trim();
715+
optionalSpace (space) {
716+
return this.options.lossy ? '' : space;
735717
}
736718

737719
parseParenthesisToken (token) {
738720
const content = this.content(token);
739721
if (token[TOKEN.TYPE] === tokens.space) {
740-
return content;
741-
}
742-
743-
if (token[0] === tokens.space) {
744-
return this.parseSpace(content, ' ');
722+
return this.requiredSpace(content);
745723
}
746724

747-
return this.parseValue(content);
725+
return content;
748726
}
749727

750728
newNode (node, namespace) {
751729
if (namespace) {
752-
node.namespace = this.parseNamespace(namespace);
730+
if (/^ +$/.test(namespace)) {
731+
if (!this.options.lossy) {
732+
this.spaces = (this.spaces || '') + namespace;
733+
}
734+
namespace = true;
735+
}
736+
node.namespace = namespace;
753737
}
754738
if (this.spaces) {
755739
node.spaces.before = this.spaces;

‎src/selectors/id.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import Namespace from './namespace';
1+
import Node from './node';
22
import {ID as IDType} from './types';
33

4-
export default class ID extends Namespace {
4+
export default class ID extends Node {
55
constructor (opts) {
66
super(opts);
77
this.type = IDType;
@@ -10,8 +10,7 @@ export default class ID extends Namespace {
1010
toString () {
1111
return [
1212
this.spaces.before,
13-
this.ns,
14-
String('#' + this.value),
13+
String('#' + this.stringifyProperty("value")),
1514
this.spaces.after,
1615
].join('');
1716
}

‎src/selectors/namespace.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default class Namespace extends Node {
4444
toString () {
4545
return [
4646
this.spaces.before,
47-
this.qualifiedName(this.value),
47+
this.qualifiedName(this.stringifyProperty("value")),
4848
this.spaces.after,
4949
].join('');
5050
}

‎src/selectors/pseudo.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Pseudo extends Container {
1111
let params = this.length ? '(' + this.map(String).join(',') + ')' : '';
1212
return [
1313
this.spaces.before,
14-
String(this.value),
14+
this.stringifyProperty("value"),
1515
params,
1616
this.spaces.after,
1717
].join('');

‎src/selectors/root.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export default class Root extends Container {
99

1010
toString () {
1111
let str = this.reduce((memo, selector) => {
12-
let sel = String(selector);
13-
return sel ? memo + sel + ',' : '';
14-
}, '').slice(0, -1);
12+
memo.push(String(selector));
13+
return memo;
14+
}, []).join(',');
1515
return this.trailingComma ? str + ',' : str;
1616
}
1717

0 commit comments

Comments
 (0)