Skip to content

Commit

Permalink
[scss] Add trailing comma support for selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
bgriffith committed Sep 2, 2017
1 parent 87d90c7 commit 179017b
Show file tree
Hide file tree
Showing 12 changed files with 1,145 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/scss/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5215,38 +5215,68 @@ function checkSelectorsGroup(i) {

const start = i;
let l;
let selectorCounter = 0;
let delimCounter = 0;

if (l = checkSelector(i)) i += l;
else return 0;
if (l = checkSelector(i)) {
i += l;
selectorCounter++;
} else return 0;

while (i < tokensLength) {
const spaceBefore = checkSC(i);
const comma = checkDelim(i + spaceBefore);
if (!comma) break;
const tempStart = i;
let tempIndex = i;
let tempLength;

const spaceAfter = checkSC(i + spaceBefore + comma);
if (l = checkSelector(i + spaceBefore + comma + spaceAfter)) {
i += spaceBefore + comma + spaceAfter + l;
} else break;
let spaceBefore = checkSC(tempIndex);

if (tempLength = checkDelim(tempIndex + spaceBefore)) {
tempIndex += spaceBefore + tempLength;
delimCounter++;

if (tempLength = checkSC(tempIndex)) tempIndex += tempLength;
if (tempLength = checkSelector(tempIndex)) {
tempIndex += tempLength;
selectorCounter++;
}
}
else break;

i += tempIndex - tempStart;
}

tokens[start].selectorsGroupEnd = i;
tokens[start].selectorsGroupSelectorCount = selectorCounter;
tokens[start].selectorsGroupDelimCount = delimCounter;

return i - start;
}

function getSelectorsGroup() {
let selectorsGroup = [];
let selectorCounter = 0;
let delimCounter = 0;

const selectorsGroupEnd = tokens[pos].selectorsGroupEnd;
const selectorCount = tokens[pos].selectorsGroupSelectorCount;
const delimCount = tokens[pos].selectorsGroupDelimCount;

selectorsGroup.push(getSelector());
selectorCounter++;

while (pos < selectorsGroupEnd) {
selectorsGroup = selectorsGroup.concat(
getSC(),
getDelim(),
getSC(),
getSelector()
);
if (delimCounter < delimCount) {
selectorsGroup = selectorsGroup.concat(getSC());
selectorsGroup = selectorsGroup.concat(getDelim());
delimCounter++;

selectorsGroup = selectorsGroup.concat(getSC());

if (selectorCounter < selectorCount) {
selectorsGroup = selectorsGroup.concat(getSelector());
selectorCounter++;
}
}
}

return selectorsGroup;
Expand Down
163 changes: 163 additions & 0 deletions test/scss/ruleset/6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
{
"type": "ruleset",
"content": [
{
"type": "selector",
"content": [
{
"type": "typeSelector",
"content": [
{
"type": "ident",
"content": "s",
"syntax": "scss",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 1
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 1
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 1
}
},
{
"type": "delimiter",
"content": ",",
"syntax": "scss",
"start": {
"line": 1,
"column": 2
},
"end": {
"line": 1,
"column": 2
}
},
{
"type": "block",
"content": [
{
"type": "declaration",
"content": [
{
"type": "property",
"content": [
{
"type": "ident",
"content": "p",
"syntax": "scss",
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 4
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 4
}
},
{
"type": "propertyDelimiter",
"content": ":",
"syntax": "scss",
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 5
}
},
{
"type": "value",
"content": [
{
"type": "ident",
"content": "v",
"syntax": "scss",
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 6
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 6
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 6
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 7
}
}
],
"syntax": "scss",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 7
}
}
1 change: 1 addition & 0 deletions test/scss/ruleset/6.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s,{p:v}
Loading

0 comments on commit 179017b

Please sign in to comment.