Skip to content

Commit

Permalink
fix start/end positions for SelectorList, Selector and Nth
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 28, 2017
1 parent 2bf4c11 commit fb9b4cf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
27 changes: 22 additions & 5 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,10 @@ function getRule() {
}

function getSelectorList(nested, relative) {
readSC();

var start = scanner.tokenStart;
var end = start;
var children = new List();
var selector;
var lastComma = -2;
Expand Down Expand Up @@ -306,6 +309,10 @@ function getSelectorList(nested, relative) {
if (selector.children.isEmpty()) {
scanner.error('Simple selector expected');
}

if (needPositions) {
end = selector.children.last().loc.end.offset;
}
}
}

Expand All @@ -315,13 +322,16 @@ function getSelectorList(nested, relative) {

return {
type: 'SelectorList',
loc: getLocation(start, scanner.tokenStart),
loc: getLocation(start, end),
children: children
};
}

function getSelector(nested, relative) {
readSC();

var start = scanner.tokenStart;
var end = start;
var children = new List();
var combinator = null;
var combinatorOffset = -1;
Expand Down Expand Up @@ -419,6 +429,7 @@ function getSelector(nested, relative) {
}

children.appendData(child);
end = scanner.tokenStart;
}

if (combinator !== null && combinator !== DESCENDANT_COMBINATOR) {
Expand All @@ -427,7 +438,7 @@ function getSelector(nested, relative) {

return {
type: 'Selector',
loc: getLocation(start, scanner.tokenStart),
loc: getLocation(start, end),
children: children
};
}
Expand Down Expand Up @@ -1567,6 +1578,7 @@ function getNthSelector(allowOfClause) {
checkTokenIsInteger();
prefix = scanner.getTokenValue();
scanner.next();
nthEnd = scanner.tokenStart;
}

if (scanner.tokenType === IDENTIFIER) {
Expand Down Expand Up @@ -1610,16 +1622,19 @@ function getNthSelector(allowOfClause) {

b = '-' + scanner.getTokenValue();
scanner.next();
nthEnd = scanner.tokenStart;
}
} else {
prefix = '';
scanner.next();
nthEnd = scanner.tokenStart;
readSC();

if (scanner.tokenType === HYPHENMINUS ||
scanner.tokenType === PLUSSIGN) {
prefix = scanner.getTokenValue();
scanner.next();
nthEnd = scanner.tokenStart;
readSC();
}

Expand Down Expand Up @@ -1648,6 +1663,7 @@ function getNthSelector(allowOfClause) {
b = prefix + scanner.getTokenValue();

scanner.next();
nthEnd = scanner.tokenStart;
}
}
} else {
Expand All @@ -1658,10 +1674,9 @@ function getNthSelector(allowOfClause) {
b = prefix;
}

nthEnd = scanner.tokenStart;
query = {
type: 'An+B',
loc: getLocation(nthStart, scanner.tokenStart),
loc: getLocation(nthStart, nthEnd),
a: a,
b: b
};
Expand All @@ -1672,7 +1687,9 @@ function getNthSelector(allowOfClause) {
if (allowOfClause && scanner.lookupValue(0, 'of')) {
scanner.next();
selector = getSelectorList(NESTED);
nthEnd = scanner.tokenStart;
if (needPositions) {
nthEnd = selector.children.last().children.last().loc.end.offset;
}
}

scanner.eat(RIGHTPARENTHESIS);
Expand Down
52 changes: 26 additions & 26 deletions test/fixture/stringify.ast
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@
"column": 5
},
"end": {
"offset": 108,
"offset": 107,
"line": 7,
"column": 12
"column": 11
}
},
"children": [
Expand All @@ -412,9 +412,9 @@
"column": 5
},
"end": {
"offset": 108,
"offset": 107,
"line": 7,
"column": 12
"column": 11
}
},
"children": [
Expand Down Expand Up @@ -539,9 +539,9 @@
"column": 1
},
"end": {
"offset": 350,
"offset": 349,
"line": 14,
"column": 13
"column": 12
}
},
"children": [
Expand Down Expand Up @@ -930,9 +930,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 216,
"offset": 217,
"line": 12,
"column": 78
"column": 79
},
"end": {
"offset": 218,
Expand Down Expand Up @@ -1095,9 +1095,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 256,
"line": 12,
"column": 118
"offset": 257,
"line": 13,
"column": 1
},
"end": {
"offset": 336,
Expand Down Expand Up @@ -1285,9 +1285,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 304,
"offset": 305,
"line": 13,
"column": 48
"column": 49
},
"end": {
"offset": 310,
Expand All @@ -1301,9 +1301,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 304,
"offset": 305,
"line": 13,
"column": 48
"column": 49
},
"end": {
"offset": 306,
Expand Down Expand Up @@ -1336,9 +1336,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 307,
"offset": 308,
"line": 13,
"column": 51
"column": 52
},
"end": {
"offset": 310,
Expand Down Expand Up @@ -1427,9 +1427,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 331,
"offset": 332,
"line": 13,
"column": 75
"column": 76
},
"end": {
"offset": 335,
Expand All @@ -1443,9 +1443,9 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 331,
"offset": 332,
"line": 13,
"column": 75
"column": 76
},
"end": {
"offset": 335,
Expand Down Expand Up @@ -1504,14 +1504,14 @@
"loc": {
"source": "stringify.css",
"start": {
"offset": 337,
"line": 13,
"column": 81
"offset": 338,
"line": 14,
"column": 1
},
"end": {
"offset": 350,
"offset": 349,
"line": 14,
"column": 13
"column": 12
}
},
"children": [
Expand Down

0 comments on commit fb9b4cf

Please sign in to comment.