Skip to content

Commit

Permalink
change format for Nth, implement An+B node
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 14, 2017
1 parent ccb097d commit 5255538
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 99 deletions.
34 changes: 17 additions & 17 deletions lib/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1401,29 +1401,28 @@ function checkTokenIsInteger() {
// https://drafts.csswg.org/css-syntax-3/#the-anb-type
function getNthSelector(allowOfClause) {
var start = scanner.tokenStart;
var children = new List();
var selector = null;
var query;
var name;

scanner.eat(COLON);
name = readIdent(false);
scanner.eat(LEFTPARENTHESIS);
readSC();

if (scanner.lookupValue(0, 'odd') || scanner.lookupValue(0, 'even')) {
var start = scanner.tokenStart;
var info = getInfo();
var start = scanner.tokenStart;
var info = getInfo();

if (scanner.lookupValue(0, 'odd') || scanner.lookupValue(0, 'even')) {
scanner.next();
children.appendData({
query = {
type: 'Identifier',
info: info,
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;

Expand Down Expand Up @@ -1524,22 +1523,19 @@ function getNthSelector(allowOfClause) {
b = prefix;
}

children.appendData({
type: 'Nth',
query = {
type: 'An+B',
info: info,
a: a,
b: b
});
};
}

readSC();

if (allowOfClause && scanner.lookupValue(0, 'of')) {
scanner.next();

var selector = getSelector(NESTED);
selector.type = 'OfClauseSelector';

children.appendData(selector);
selector = getSelector(NESTED);
}

scanner.eat(RIGHTPARENTHESIS);
Expand All @@ -1548,7 +1544,11 @@ function getNthSelector(allowOfClause) {
type: 'PseudoClass',
info: getInfo(start),
name: name,
children: children
children: new List().appendData({
type: 'Nth',
nth: query,
selector: selector
})
};
}

Expand Down
10 changes: 7 additions & 3 deletions lib/utils/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ function translate(node) {
case 'Selector':
return eachDelim(node.children, ',');

case 'OfClauseSelector':
return ' of ' + eachDelim(node.children, ',');
case 'Nth':
var result = translate(node.nth);
if (node.selector !== null) {
result += ' of ' + translate(node.selector);
}
return result;

case 'SimpleSelector':
return translateSimpleSelector(node.children);
Expand Down Expand Up @@ -194,7 +198,7 @@ function translate(node) {
case 'Dimension':
return node.value + node.unit;

case 'Nth':
case 'An+B':
var result = '';
var a = node.a !== null && node.a !== undefined;
var b = node.b !== null && node.b !== undefined;
Expand Down
13 changes: 7 additions & 6 deletions lib/utils/translateWithSourceMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,12 @@ function translate(node) {
case 'Selector':
return createAnonymousSourceNode(node.children.map(translate)).join(',');

case 'OfClauseSelector':
return createAnonymousSourceNode([
' of ',
createAnonymousSourceNode(node.children.map(translate)).join(',')
]);
case 'Nth':
var nodes = [translate(node.nth)];
if (node.selector !== null) {
nodes.push(' of ', translate(node.selector));
}
return createAnonymousSourceNode(nodes);

case 'SimpleSelector':
var nodes = node.children.map(function(node) {
Expand Down Expand Up @@ -271,7 +272,7 @@ function translate(node) {
case 'Dimension':
return node.value + node.unit;

case 'Nth':
case 'An+B':
var result = '';
var a = node.a !== null && node.a !== undefined;
var b = node.b !== null && node.b !== undefined;
Expand Down
10 changes: 8 additions & 2 deletions lib/utils/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ function walk(walk, node) {
break;

case 'Selector':
case 'OfClauseSelector':
var oldSelector = this.selector;
this.selector = node;

Expand All @@ -131,6 +130,13 @@ function walk(walk, node) {
this.selector = oldSelector;
break;

case 'Nth':
walk.call(this, node.nth);
if (node.selector !== null) {
walk.call(this, node.selector);
}
break;

case 'Block':
node.children.each(walk, this);
break;
Expand Down Expand Up @@ -198,7 +204,7 @@ function walk(walk, node) {
// case 'Universal':
// case 'Identifier':
// case 'UnicodeRange':
// case 'Nth':
// case 'An+B':
// case 'Class':
// case 'Id':
// case 'Percentage':
Expand Down
Loading

0 comments on commit 5255538

Please sign in to comment.