Skip to content

Commit 313a9a0

Browse files
committed
Handle parenthesis in selector parsing for non-standard syntaxes.
Closes #124.
1 parent 73ff5e6 commit 313a9a0

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎src/__tests__/escapes.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ test('escaped semicolon in id', '#\\;', (t, tree) => {
1111
t.deepEqual(tree.nodes[0].nodes[0].raws.value, '\\;');
1212
t.deepEqual(tree.nodes[0].nodes[0].type, 'id');
1313
});
14+
15+
// This is a side-effect of allowing media queries to be parsed. Not sure it shouldn't just be an error.
16+
test('bare parens capture contents as a string', '(h1)', (t, tree) => {
17+
t.deepEqual(tree.nodes[0].nodes[0].value, '(h1)');
18+
t.deepEqual(tree.nodes[0].nodes[0].type, 'string');
19+
});

‎src/__tests__/lossy.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,4 @@ ava('@words - space before', testLossy, ' @media', '@media');
7777
ava('@words - space after', testLossy, '@media ', '@media');
7878
ava('@words - maintains space between', testLossy, '@media (min-width: 700px) and (orientation: landscape)', '@media (min-width: 700px) and (orientation: landscape)');
7979
ava('@words - extraneous space between', testLossy, '@media (min-width: 700px) and (orientation: landscape)', '@media (min-width: 700px) and (orientation: landscape)');
80+
ava('@words - multiple', testLossy, '@media (min-width: 700px), (min-height: 400px)', '@media (min-width: 700px),(min-height: 400px)');

‎src/__tests__/util/helpers.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export function test (spec, input, callback, only = false) {
2929

3030
test.only = (spec, input, callback) => test(spec, input, callback, true);
3131

32-
export const throws = (spec, input) => {
32+
export const throws = (spec, input, validator) => {
3333
ava(`${spec} (throws)`, t => {
34-
t.throws(() => parser().processSync(input));
34+
t.throws(() => parser().processSync(input), validator);
3535
});
3636
};

‎src/parser.js‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ export default class Parser {
427427
}
428428

429429
parentheses () {
430-
const last = this.current.last;
430+
let last = this.current.last;
431431
let balanced = 1;
432432
this.position ++;
433433
if (last && last.type === types.PSEUDO) {
@@ -452,18 +452,36 @@ export default class Parser {
452452
}
453453
this.current = cache;
454454
} else {
455-
last.appendToPropertyAndEscape("value", '(', '(');
455+
// I think this case should be an error. It's used to implement a basic parse of media queries
456+
// but I don't think it's a good idea.
457+
let parenStart = this.currToken;
458+
let parenValue = "(";
459+
let parenEnd;
456460
while (this.position < this.tokens.length && balanced) {
457461
if (this.currToken[TOKEN.TYPE] === tokens.openParenthesis) {
458462
balanced ++;
459463
}
460464
if (this.currToken[TOKEN.TYPE] === tokens.closeParenthesis) {
461465
balanced --;
462466
}
463-
let additionalValue = this.parseParenthesisToken(this.currToken);
464-
last.appendToPropertyAndEscape("value", additionalValue, additionalValue);
467+
parenEnd = this.currToken;
468+
parenValue += this.parseParenthesisToken(this.currToken);
465469
this.position ++;
466470
}
471+
if (last) {
472+
last.appendToPropertyAndEscape("value", parenValue, parenValue);
473+
} else {
474+
this.newNode(new Str({
475+
value: parenValue,
476+
source: getSource(
477+
parenStart[TOKEN.START_LINE],
478+
parenStart[TOKEN.START_COL],
479+
parenEnd[TOKEN.END_LINE],
480+
parenEnd[TOKEN.END_COL],
481+
),
482+
sourceIndex: parenStart[TOKEN.START_POS],
483+
}));
484+
}
467485
}
468486
if (balanced) {
469487
return this.expected('closing parenthesis', this.currToken[TOKEN.START_POS]);
@@ -727,9 +745,9 @@ export default class Parser {
727745
const content = this.content(token);
728746
if (token[TOKEN.TYPE] === tokens.space) {
729747
return this.requiredSpace(content);
748+
} else {
749+
return content;
730750
}
731-
732-
return content;
733751
}
734752

735753
newNode (node, namespace) {

0 commit comments

Comments
 (0)