Skip to content

Commit

Permalink
[scss] Implement numberOrInterpolation for percentage check
Browse files Browse the repository at this point in the history
  • Loading branch information
bgriffith authored and tonyganch committed Jun 21, 2016
1 parent 89e4904 commit f69420a
Show file tree
Hide file tree
Showing 4 changed files with 570 additions and 57 deletions.
76 changes: 19 additions & 57 deletions src/scss/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2201,9 +2201,6 @@ function checkKeyframesSelector(i) {
} else if (l = checkPercentage(i)) {
i += l;
tokens[start].keyframesSelectorType = 2;
} else if (l = checkInterpolatedPercentage(i)) {
i += l;
tokens[start].keyframesSelectorType = 4;
} else if (l = checkInterpolation(i)) {
i += l;
tokens[start].keyframesSelectorType = 3;
Expand Down Expand Up @@ -2231,9 +2228,7 @@ function getKeyframesSelector() {
content.push(getIdent());
} else if (token.keyframesSelectorType === 2) {
content.push(getPercentage());
} else if (token.keyframesSelectorType === 4) {
content.push(getInterpolatedPercentage());
} else {
} else if (token.keyframesSelectorType === 3) {
content.push(getInterpolation());
}

Expand Down Expand Up @@ -2692,38 +2687,42 @@ function getParentSelectorWithExtension() {
}

/**
* Check if token is part of a number with percent sign (e.g. `10%`)
* Check if token is part of a number or an interpolation with a percent sign (e.g. `10%`)
* @param {Number} i Token's index number
* @returns {Number}
*/
function checkPercentage(i) {
var x;
let start = i;
let l;

if (i >= tokensLength) return 0;

x = checkNumber(i);
if (l = checkNumberOrInterpolation(i)) i += l;
else return 0;

if (i >= tokensLength) return 0;

if (!x || i + x >= tokensLength) return 0;
if (tokens[i].type !== TokenType.PercentSign) return 0;

return tokens[i + x].type === TokenType.PercentSign ? x + 1 : 0;
return i - start + 1;
}

/**
* Get node of number with percent sign
* @returns {Array} `['percentage', ['number', x]]` where `x` is a number
* (without percent sign) converted to string.
* Get a percentage node that contains either a number or an interpolation
* @returns {Object} The percentage node
*/
function getPercentage() {
let startPos = pos;
let x = [getNumber()];
var token = tokens[startPos];
var line = token.ln;
var column = token.col;
let token = tokens[startPos];
let line = token.ln;
let column = token.col;
let content = getNumberOrInterpolation();
let end = getLastPosition(content, line, column, 1);

var end = getLastPosition(x, line, column, 1);
// Skip %
pos++;

return newNode(NodeType.PercentageType, x, token.ln, token.col, end);
return newNode(NodeType.PercentageType, content, token.ln, token.col, end);
}

/**
Expand Down Expand Up @@ -2759,43 +2758,6 @@ function getNumberOrInterpolation() {
return content;
}

/**
* Check if token is part of an interpolation with percent sign (e.g. `#{$num}%`)
* @param {Number} i Token's index number
* @returns {Number}
*/
function checkInterpolatedPercentage(i) {
let start = i;
let l;

if (i >= tokensLength) return 0;

if (l = checkInterpolation(i)) i += l;
else return 0;

if (i >= tokensLength) return 0;

return tokens[i].type === TokenType.PercentSign ? i - start + 1 : 0;
}

/**
* Get node of interpolation with percent sign
* @returns {Array} `['percentage', ['interpolation', x]]` where `x` is an
* interpolation (without percent sign)
*/
function getInterpolatedPercentage() {
let startPos = pos;
let x = [getInterpolation()];
var token = tokens[startPos];
var line = token.ln;
var column = token.col;

var end = getLastPosition(x, line, column, 1);
pos++;

return newNode(NodeType.PercentageType, x, token.ln, token.col, end);
}

/**
* Check if token is part of a placeholder selector (e.g. `%abc`).
* @param {Number} i Token's index number
Expand Down
Loading

0 comments on commit f69420a

Please sign in to comment.