Skip to content

Commit

Permalink
rework Nth
Browse files Browse the repository at this point in the history
- change format of An+B
- fix edge cases when a or b is not an integer
- add more tests
  • Loading branch information
lahmatiy committed Jan 13, 2017
1 parent 9cf3b7d commit 81498e0
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 151 deletions.
100 changes: 47 additions & 53 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,21 @@ function getImportant() {
return true;
}

function checkTokenIsInteger() {
var pos = scanner.tokenStart;

if (scanner.source.charCodeAt(pos) === PLUSSIGN ||
scanner.source.charCodeAt(pos) === HYPHENMINUS) {
pos++;
}

for (; pos < scanner.tokenEnd; pos++) {
if (!isNumber(scanner.source.charCodeAt(pos))) {
scanner.error('Unexpected input', pos);
}
}
}

// https://drafts.csswg.org/css-syntax-3/#the-anb-type
function getNthSelector() {
var start = scanner.tokenStart;
Expand All @@ -1391,18 +1406,22 @@ function getNthSelector() {

scanner.next();
sequence.appendData({
type: 'Nth',
type: 'Identifier',
info: info,
value: scanner.substrToCursor(start)
name: scanner.substrToCursor(start)
});
} else {
// scan An+B microsyntax https://www.w3.org/TR/css-syntax-3/#anb
var prefix = '';
var start = scanner.tokenStart;
var info = getInfo();
var a = null;
var b = null;

if (scanner.tokenType === HYPHENMINUS ||
scanner.tokenType === PLUSSIGN ||
scanner.tokenType === NUMBER) {
checkTokenIsInteger();
prefix = scanner.getTokenValue();
scanner.next();
}
Expand All @@ -1412,11 +1431,9 @@ function getNthSelector() {
scanner.error();
}

sequence.appendData({
type: 'Nth',
info: info,
value: prefix + scanner.source.charAt(scanner.tokenStart)
});
a = prefix === '' || prefix === '+' ? '1' :
prefix === '-' ? '-1' :
prefix;

var len = scanner.tokenEnd - scanner.tokenStart;
if (len > 1) {
Expand All @@ -1427,11 +1444,6 @@ function getNthSelector() {
}

scanner.tokenStart = start + 1;
sequence.appendData({
type: 'Operator',
info: getInfo(),
value: scanner.source.charAt(start + 1)
});

// ..n-{number}..
if (len > 2) {
Expand All @@ -1441,16 +1453,8 @@ function getNthSelector() {
}
}

scanner.tokenStart = start + 2;
var info = getInfo();
scanner.next();

sequence.appendData({
type: 'Nth',
info: info,
value: scanner.substrToCursor(start + 2)
});

b = '-' + scanner.substrToCursor(start + 2);
} else {
scanner.next();
readSC();
Expand All @@ -1461,12 +1465,7 @@ function getNthSelector() {
scanner.error();
}

sequence.appendData({
type: 'Nth',
info: getInfo(),
value: scanner.getTokenValue()
});

b = '-' + scanner.getTokenValue();
scanner.next();
}
} else {
Expand All @@ -1476,41 +1475,34 @@ function getNthSelector() {

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

if (scanner.tokenType === NUMBER) {
var sign = '';
checkTokenIsInteger();

if (cmpChar(scanner.source, scanner.tokenStart, PLUSSIGN) ||
cmpChar(scanner.source, scanner.tokenStart, HYPHENMINUS)) {
info = getInfo();
sign = scanner.source.charAt(scanner.tokenStart);
}

// prefix or sign should be specified but not both
if (!(prefix === '' ^ sign === '')) {
scanner.error();
}
// prefix or sign should be specified but not both
if (prefix !== '') {
scanner.error();
}

if (sign) {
prefix = scanner.source.charAt(scanner.tokenStart);
scanner.tokenStart++;
}

sequence.appendData({
type: 'Operator',
info: info,
value: prefix || sign
});
if (prefix === '') {
// should be an operator before number
scanner.error();
} else if (prefix === '+') {
// plus is using by default
prefix = '';
}

sequence.appendData({
type: 'Nth',
info: getInfo(),
value: scanner.getTokenValue()
});
b = prefix + scanner.getTokenValue();

scanner.next();
}
Expand All @@ -1520,12 +1512,14 @@ function getNthSelector() {
scanner.error('Number or identifier is expected');
}

sequence.appendData({
type: 'Nth',
info: info,
value: prefix
});
b = prefix;
}

sequence.appendData({
type: 'Nth',
a: a,
b: b
});
}

readSC();
Expand Down
23 changes: 22 additions & 1 deletion lib/utils/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,28 @@ function translate(node) {
return node.value + node.unit;

case 'Nth':
return node.value;
var result = '';
var a = node.a !== null && node.a !== undefined;
var b = node.b !== null && node.b !== undefined;

if (a) {
result += node.a === '+1' || node.a === '1' ? 'n' :
node.a === '-1' ? '-n' :
node.a + 'n';
}

if (a && b) {
if (String(node.b).charAt(0) !== '-' &&
String(node.b).charAt(0) !== '+') {
result += '+';
}
}

if (b) {
result += node.b;
}

return result;

case 'Number':
return node.value;
Expand Down
23 changes: 22 additions & 1 deletion lib/utils/translateWithSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,28 @@ function translate(node) {
return node.value + node.unit;

case 'Nth':
return node.value;
var result = '';
var a = node.a !== null && node.a !== undefined;
var b = node.b !== null && node.b !== undefined;

if (a) {
result += node.a === '+1' || node.a === '1' ? 'n' :
node.a === '-1' ? '-n' :
node.a + 'n';
}

if (a && b) {
if (String(node.b).charAt(0) !== '-' &&
String(node.b).charAt(0) !== '+') {
result += '+';
}
}

if (b) {
result += node.b;
}

return result;

case 'Number':
return node.value;
Expand Down

0 comments on commit 81498e0

Please sign in to comment.