Skip to content

Commit

Permalink
implement Brackets node
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 14, 2017
1 parent 5255538 commit 6ba79d3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
58 changes: 56 additions & 2 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ function getValue(nested, property) {
child = getParentheses(SCOPE_VALUE);
break;

case LEFTSQUAREBRACKET:
child = getBrackets();
break;

case STRING:
child = getString();
break;
Expand Down Expand Up @@ -716,7 +720,7 @@ function getParentheses(scope) {
var wasSpace = false;
var child;

// left brace
// left parenthesis
scanner.eat(LEFTPARENTHESIS);
readSC();

Expand Down Expand Up @@ -766,7 +770,7 @@ function getParentheses(scope) {
children.appendData(child);
}

// right brace
// right parenthesis
scanner.eat(RIGHTPARENTHESIS);

return {
Expand All @@ -776,6 +780,56 @@ function getParentheses(scope) {
};
}

// currently only Grid Layout uses square brackets
// https://drafts.csswg.org/css-grid/#track-sizing
// [ ident* ]
function getBrackets() {
var start = scanner.tokenStart;
var children = new List();
var wasSpace = false;
var child;

// left bracket
scanner.eat(LEFTSQUAREBRACKET);
readSC();

scan:
while (!scanner.eof) {
switch (scanner.tokenType) {
case RIGHTSQUAREBRACKET:
break scan;

case WHITESPACE:
wasSpace = true;
scanner.next();
continue;

case COMMENT: // ignore comments
scanner.next();
continue;

default:
child = getIdentifier(false);
}

if (wasSpace) {
wasSpace = false;
children.appendData(SPACE_NODE);
}

children.appendData(child);
}

// right bracket
scanner.eat(RIGHTSQUAREBRACKET);

return {
type: 'Brackets',
info: getInfo(start),
children: children
};
}

// '.' ident
function getClass() {
var start = scanner.tokenStart;
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ function translate(node) {
case 'Parentheses':
return '(' + each(node.children) + ')';

case 'Brackets':
return '[' + each(node.children) + ']';

case 'AtruleExpression':
return each(node.children);

Expand Down
3 changes: 3 additions & 0 deletions lib/utils/translateWithSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ function translate(node) {
case 'Parentheses':
return '(' + each(node.children) + ')';

case 'Brackets':
return '[' + each(node.children) + ']';

case 'AtruleExpression':
return each(node.children);

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ function walk(walk, node) {
break;

case 'Value':
case 'Argument':
case 'SimpleSelector':
case 'Parentheses':
case 'Brackets':
case 'Negation':
node.children.each(walk, this);
break;
Expand Down
40 changes: 40 additions & 0 deletions test/fixture/parse/value/Brackets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"empty": {
"source": "[]",
"ast": {
"type": "Brackets",
"children": []
}
},
"single identifier": {
"source": "[foo]",
"ast": {
"type": "Brackets",
"children": [
{
"type": "Identifier",
"name": "foo"
}
]
}
},
"multiple identifier": {
"source": "[foo bar]",
"ast": {
"type": "Brackets",
"children": [
{
"type": "Identifier",
"name": "foo"
},
{
"type": "Space"
},
{
"type": "Identifier",
"name": "bar"
}
]
}
}
}

0 comments on commit 6ba79d3

Please sign in to comment.