Skip to content

Commit

Permalink
simplify selector parsing
Browse files Browse the repository at this point in the history
- don't convert spaces into descendant combinator
- don't validate structure on parsing (selectors may be checked by lexer
later)
- remove unnecessary arguments for SelectorList and Selector
  • Loading branch information
lahmatiy committed Feb 6, 2017
1 parent e5b87e8 commit a77504b
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 158 deletions.
2 changes: 1 addition & 1 deletion lib/parser/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Parser.prototype = {
'nth-last-child': sequence.nthWithOfClause,
'nth-of-type': sequence.nth,
'nth-last-of-type': sequence.nth,
'slotted': sequence.compoundSelectorList
'slotted': sequence.compoundSelector
},
context: {
stylesheet: getStyleSheet,
Expand Down
13 changes: 5 additions & 8 deletions lib/parser/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ var ASTERISK = TYPE.Asterisk;
var PERCENTSIGN = TYPE.PercentSign;
var U = 117; // 'u'.charCodeAt(0)

var ABSOLUTE = false;
var RELATIVE = true;
var ALLOW_OF_CLAUSE = true;
var DISALLOW_OF_CLAUSE = false;
var DISALLOW_VAR = false;
var DISALLOW_COMBINATORS = true;

function singleIdentifier() {
return new List().appendData(
Expand All @@ -33,19 +30,19 @@ function singleIdentifier() {

function selectorList() {
return new List().appendData(
this.SelectorList(ABSOLUTE)
this.SelectorList()
);
}

function relativeSelectorList() {
return new List().appendData(
this.SelectorList(RELATIVE)
this.SelectorList()
);
}

function compoundSelectorList() {
function compoundSelector() {
return new List().appendData(
this.Selector(ABSOLUTE, DISALLOW_COMBINATORS)
this.Selector()
);
}

Expand Down Expand Up @@ -181,7 +178,7 @@ module.exports = {
singleIdentifier: singleIdentifier,
selectorList: selectorList,
relativeSelectorList: relativeSelectorList,
compoundSelectorList: compoundSelectorList,
compoundSelector: compoundSelector,
nth: nth,
nthWithOfClause: nthWithOfClause,
default: defaultSequence
Expand Down
55 changes: 10 additions & 45 deletions lib/parser/type/Selector.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var List = require('../../utils/list');
var TYPE = require('../../tokenizer').TYPE;
var DESCENDANT_COMBINATOR = {};

var WHITESPACE = TYPE.Whitespace;
var IDENTIFIER = TYPE.Identifier;
Expand All @@ -17,40 +16,25 @@ var LEFTSQUAREBRACKET = TYPE.LeftSquareBracket;
var VERTICALLINE = TYPE.VerticalLine;
var TILDE = TYPE.Tilde;

module.exports = function Selector(relative, disallowCombinators) {
module.exports = function Selector() {
this.readSC();

var start = this.scanner.tokenStart;
var end = start;
var children = new List();
var combinator = null;
var combinatorOffset = -1;
var space = null;
var child = null;

relative = relative || false;
disallowCombinators = disallowCombinators || false;

scan:
while (!this.scanner.eof) {
switch (this.scanner.tokenType) {
case COMMENT:
if (disallowCombinators) {
this.readSC();
break scan;
}

this.scanner.next();
continue;

case WHITESPACE:
if (disallowCombinators) {
this.readSC();
break scan;
}

if (combinator === null && children.head !== null) {
combinatorOffset = this.scanner.tokenStart;
combinator = DESCENDANT_COMBINATOR;
if (child === null || child.type !== 'Combinator') {
space = this.Space();
} else {
this.scanner.next();
}
Expand All @@ -60,15 +44,9 @@ module.exports = function Selector(relative, disallowCombinators) {
case GREATERTHANSIGN:
case TILDE:
case SOLIDUS:
if (disallowCombinators ||
(children.head === null && !relative) || // combinator in the beginning
(combinator !== null && combinator !== DESCENDANT_COMBINATOR)) {
this.scanner.error('Unexpected combinator');
}

combinatorOffset = this.scanner.tokenStart;
combinator = this.Combinator();
continue;
space = null;
child = this.Combinator();
break;

case FULLSTOP:
child = this.Class();
Expand Down Expand Up @@ -123,18 +101,9 @@ module.exports = function Selector(relative, disallowCombinators) {
break scan;
}

if (combinator !== null) {
// create descendant combinator on demand to avoid garbage
if (combinator === DESCENDANT_COMBINATOR) {
combinator = {
type: 'Combinator',
loc: this.getLocation(combinatorOffset, combinatorOffset + 1),
name: ' '
};
}

children.appendData(combinator);
combinator = null;
if (space !== null) {
children.appendData(space);
space = null;
}

children.appendData(child);
Expand All @@ -146,10 +115,6 @@ module.exports = function Selector(relative, disallowCombinators) {
this.scanner.error('Selector is expected');
}

if (combinator !== null && combinator !== DESCENDANT_COMBINATOR) {
this.scanner.error('Unexpected combinator', combinatorOffset);
}

return {
type: 'Selector',
loc: this.getLocation(start, end),
Expand Down
4 changes: 2 additions & 2 deletions lib/parser/type/SelectorList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ var COMMA = TYPE.Comma;
var LEFTCURLYBRACKET = TYPE.LeftCurlyBracket;
var BALANCED = true;

module.exports = function SelectorList(relative) {
module.exports = function SelectorList() {
this.readSC();

var children = new List();
var selector = null;

while (!this.scanner.eof) {
selector = this.parseSelector ? this.Selector(relative) : this.Raw(BALANCED, COMMA, LEFTCURLYBRACKET);
selector = this.parseSelector ? this.Selector() : this.Raw(BALANCED, COMMA, LEFTCURLYBRACKET);
children.appendData(selector);

if (this.scanner.tokenType === COMMA) {
Expand Down
4 changes: 2 additions & 2 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ describe('Common', function() {
filename: path.basename(css),
positions: true
});
});

// fs.writeFileSync(__dirname + '/fixture/stringify.ast', stringify(ast, true) + '\n', 'utf-8');
// fs.writeFileSync(__dirname + '/fixture/stringify.ast', stringify(ast, true) + '\n', 'utf-8');
});

it('utils.strigify()', function() {
assert.equal(
Expand Down
38 changes: 6 additions & 32 deletions test/fixture/parse/selector/Combinator.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"name": "a"
},
{
"type": "Combinator",
"name": " "
"type": "Space"
},
{
"type": "Type",
Expand Down Expand Up @@ -101,7 +100,7 @@
}
},
"all selectors": {
"source": "a b + c > d ~ e",
"source": "a b+c>d~e",
"translate": "a b+c>d~e",
"ast": {
"type": "Selector",
Expand All @@ -111,8 +110,7 @@
"name": "a"
},
{
"type": "Combinator",
"name": " "
"type": "Space"
},
{
"type": "Type",
Expand Down Expand Up @@ -156,8 +154,7 @@
"name": "a"
},
{
"type": "Combinator",
"name": " "
"type": "Space"
},
{
"type": "Type",
Expand Down Expand Up @@ -261,8 +258,7 @@
"name": "a"
},
{
"type": "Combinator",
"name": " "
"type": "Space"
},
{
"type": "Type",
Expand Down Expand Up @@ -354,27 +350,5 @@
}
]
}
},
"error": [
{
"source": "> b",
"offset": "^ ",
"error": "Unexpected combinator"
},
{
"source": " > b",
"offset": " ^ ",
"error": "Unexpected combinator"
},
{
"source": "b >",
"offset": " ^",
"error": "Unexpected combinator"
},
{
"source": "a + ~ b",
"offset": " ^ ",
"error": "Unexpected combinator"
}
]
}
}
3 changes: 1 addition & 2 deletions test/fixture/parse/selector/Selector.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@
"name": "\\31 "
},
{
"type": "Combinator",
"name": " "
"type": "Space"
},
{
"type": "Type",
Expand Down
12 changes: 1 addition & 11 deletions test/fixture/parse/selector/functional-pseudo/has.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
}
},
"should support relative selectors": {
"source": ":has(> .a,+.b)",
"source": ":has(>.a,+.b)",
"translate": ":has(>.a,+.b)",
"ast": {
"type": "PseudoClass",
Expand Down Expand Up @@ -199,16 +199,6 @@
"source": ":has(var(--test))",
"offset": " ^",
"error": "RightParenthesis is expected"
},
{
"source": ":has(>+a)",
"offset": " ^",
"error": "Unexpected combinator"
},
{
"source": ":has(~foo, >+a)",
"offset": " ^",
"error": "Unexpected combinator"
}
]
}
10 changes: 0 additions & 10 deletions test/fixture/parse/selector/functional-pseudo/matches.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@
"source": ":matches(var(--test))",
"offset": " ^",
"error": "RightParenthesis is expected"
},
{
"source": ":matches(> test)",
"offset": " ^",
"error": "Unexpected combinator"
},
{
"source": ":matches(foo,> bar)",
"offset": " ^",
"error": "Unexpected combinator"
}
]
}
10 changes: 0 additions & 10 deletions test/fixture/parse/selector/functional-pseudo/not.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@
"source": ":not(var(--test))",
"offset": " ^",
"error": "RightParenthesis is expected"
},
{
"source": ":not(> test)",
"offset": " ^",
"error": "Unexpected combinator"
},
{
"source": ":not(foo,> bar)",
"offset": " ^",
"error": "Unexpected combinator"
}
]
}
5 changes: 0 additions & 5 deletions test/fixture/parse/selector/functional-pseudo/slotted.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@
"offset": " ^",
"error": "RightParenthesis is expected"
},
{
"source": "::slotted(> test)",
"offset": " ^",
"error": "Unexpected combinator"
},
{
"source": "::slotted(foo,bar)",
"offset": " ^",
Expand Down
32 changes: 2 additions & 30 deletions test/fixture/stringify.ast
Original file line number Diff line number Diff line change
Expand Up @@ -588,21 +588,7 @@
"name": "class"
},
{
"type": "Combinator",
"loc": {
"source": "stringify.css",
"start": {
"offset": 146,
"line": 12,
"column": 8
},
"end": {
"offset": 147,
"line": 12,
"column": 9
}
},
"name": " "
"type": "Space"
},
{
"type": "Type",
Expand Down Expand Up @@ -675,21 +661,7 @@
"flags": null
},
{
"type": "Combinator",
"loc": {
"source": "stringify.css",
"start": {
"offset": 163,
"line": 12,
"column": 25
},
"end": {
"offset": 164,
"line": 12,
"column": 26
}
},
"name": " "
"type": "Space"
},
{
"type": "Attribute",
Expand Down

0 comments on commit a77504b

Please sign in to comment.