|
'use strict'; |
|
|
|
const genericConst = require('./generic-const.cjs'); |
|
const genericAnPlusB = require('./generic-an-plus-b.cjs'); |
|
const genericUrange = require('./generic-urange.cjs'); |
|
const types = require('../tokenizer/types.cjs'); |
|
const charCodeDefinitions = require('../tokenizer/char-code-definitions.cjs'); |
|
const utils = require('../tokenizer/utils.cjs'); |
|
|
|
const calcFunctionNames = ['calc(', '-moz-calc(', '-webkit-calc(']; |
|
const balancePair = new Map([ |
|
[types.Function, types.RightParenthesis], |
|
[types.LeftParenthesis, types.RightParenthesis], |
|
[types.LeftSquareBracket, types.RightSquareBracket], |
|
[types.LeftCurlyBracket, types.RightCurlyBracket] |
|
]); |
|
|
|
|
|
function charCodeAt(str, index) { |
|
return index < str.length ? str.charCodeAt(index) : 0; |
|
} |
|
|
|
function eqStr(actual, expected) { |
|
return utils.cmpStr(actual, 0, actual.length, expected); |
|
} |
|
|
|
function eqStrAny(actual, expected) { |
|
for (let i = 0; i < expected.length; i++) { |
|
if (eqStr(actual, expected[i])) { |
|
return true; |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
|
|
function isPostfixIeHack(str, offset) { |
|
if (offset !== str.length - 2) { |
|
return false; |
|
} |
|
|
|
return ( |
|
charCodeAt(str, offset) === 0x005C && |
|
charCodeDefinitions.isDigit(charCodeAt(str, offset + 1)) |
|
); |
|
} |
|
|
|
function outOfRange(opts, value, numEnd) { |
|
if (opts && opts.type === 'Range') { |
|
const num = Number( |
|
numEnd !== undefined && numEnd !== value.length |
|
? value.substr(0, numEnd) |
|
: value |
|
); |
|
|
|
if (isNaN(num)) { |
|
return true; |
|
} |
|
|
|
|
|
|
|
if (opts.min !== null && num < opts.min && typeof opts.min !== 'string') { |
|
return true; |
|
} |
|
|
|
|
|
|
|
if (opts.max !== null && num > opts.max && typeof opts.max !== 'string') { |
|
return true; |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
function consumeFunction(token, getNextToken) { |
|
let balanceCloseType = 0; |
|
let balanceStash = []; |
|
let length = 0; |
|
|
|
|
|
scan: |
|
do { |
|
switch (token.type) { |
|
case types.RightCurlyBracket: |
|
case types.RightParenthesis: |
|
case types.RightSquareBracket: |
|
if (token.type !== balanceCloseType) { |
|
break scan; |
|
} |
|
|
|
balanceCloseType = balanceStash.pop(); |
|
|
|
if (balanceStash.length === 0) { |
|
length++; |
|
break scan; |
|
} |
|
|
|
break; |
|
|
|
case types.Function: |
|
case types.LeftParenthesis: |
|
case types.LeftSquareBracket: |
|
case types.LeftCurlyBracket: |
|
balanceStash.push(balanceCloseType); |
|
balanceCloseType = balancePair.get(token.type); |
|
break; |
|
} |
|
|
|
length++; |
|
} while (token = getNextToken(length)); |
|
|
|
return length; |
|
} |
|
|
|
|
|
|
|
|
|
function calc(next) { |
|
return function(token, getNextToken, opts) { |
|
if (token === null) { |
|
return 0; |
|
} |
|
|
|
if (token.type === types.Function && eqStrAny(token.value, calcFunctionNames)) { |
|
return consumeFunction(token, getNextToken); |
|
} |
|
|
|
return next(token, getNextToken, opts); |
|
}; |
|
} |
|
|
|
function tokenType(expectedTokenType) { |
|
return function(token) { |
|
if (token === null || token.type !== expectedTokenType) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function customIdent(token) { |
|
if (token === null || token.type !== types.Ident) { |
|
return 0; |
|
} |
|
|
|
const name = token.value.toLowerCase(); |
|
|
|
|
|
if (eqStrAny(name, genericConst.cssWideKeywords)) { |
|
return 0; |
|
} |
|
|
|
|
|
if (eqStr(name, 'default')) { |
|
return 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function customPropertyName(token) { |
|
|
|
if (token === null || token.type !== types.Ident) { |
|
return 0; |
|
} |
|
|
|
|
|
if (charCodeAt(token.value, 0) !== 0x002D || charCodeAt(token.value, 1) !== 0x002D) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function hexColor(token) { |
|
if (token === null || token.type !== types.Hash) { |
|
return 0; |
|
} |
|
|
|
const length = token.value.length; |
|
|
|
|
|
if (length !== 4 && length !== 5 && length !== 7 && length !== 9) { |
|
return 0; |
|
} |
|
|
|
for (let i = 1; i < length; i++) { |
|
if (!charCodeDefinitions.isHexDigit(charCodeAt(token.value, i))) { |
|
return 0; |
|
} |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
function idSelector(token) { |
|
if (token === null || token.type !== types.Hash) { |
|
return 0; |
|
} |
|
|
|
if (!charCodeDefinitions.isIdentifierStart(charCodeAt(token.value, 1), charCodeAt(token.value, 2), charCodeAt(token.value, 3))) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
|
|
|
|
function declarationValue(token, getNextToken) { |
|
if (!token) { |
|
return 0; |
|
} |
|
|
|
let balanceCloseType = 0; |
|
let balanceStash = []; |
|
let length = 0; |
|
|
|
|
|
|
|
scan: |
|
do { |
|
switch (token.type) { |
|
|
|
case types.BadString: |
|
case types.BadUrl: |
|
break scan; |
|
|
|
|
|
case types.RightCurlyBracket: |
|
case types.RightParenthesis: |
|
case types.RightSquareBracket: |
|
if (token.type !== balanceCloseType) { |
|
break scan; |
|
} |
|
|
|
balanceCloseType = balanceStash.pop(); |
|
break; |
|
|
|
|
|
case types.Semicolon: |
|
if (balanceCloseType === 0) { |
|
break scan; |
|
} |
|
|
|
break; |
|
|
|
|
|
case types.Delim: |
|
if (balanceCloseType === 0 && token.value === '!') { |
|
break scan; |
|
} |
|
|
|
break; |
|
|
|
case types.Function: |
|
case types.LeftParenthesis: |
|
case types.LeftSquareBracket: |
|
case types.LeftCurlyBracket: |
|
balanceStash.push(balanceCloseType); |
|
balanceCloseType = balancePair.get(token.type); |
|
break; |
|
} |
|
|
|
length++; |
|
} while (token = getNextToken(length)); |
|
|
|
return length; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function anyValue(token, getNextToken) { |
|
if (!token) { |
|
return 0; |
|
} |
|
|
|
let balanceCloseType = 0; |
|
let balanceStash = []; |
|
let length = 0; |
|
|
|
|
|
|
|
scan: |
|
do { |
|
switch (token.type) { |
|
|
|
case types.BadString: |
|
case types.BadUrl: |
|
break scan; |
|
|
|
|
|
case types.RightCurlyBracket: |
|
case types.RightParenthesis: |
|
case types.RightSquareBracket: |
|
if (token.type !== balanceCloseType) { |
|
break scan; |
|
} |
|
|
|
balanceCloseType = balanceStash.pop(); |
|
break; |
|
|
|
case types.Function: |
|
case types.LeftParenthesis: |
|
case types.LeftSquareBracket: |
|
case types.LeftCurlyBracket: |
|
balanceStash.push(balanceCloseType); |
|
balanceCloseType = balancePair.get(token.type); |
|
break; |
|
} |
|
|
|
length++; |
|
} while (token = getNextToken(length)); |
|
|
|
return length; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function dimension(type) { |
|
if (type) { |
|
type = new Set(type); |
|
} |
|
|
|
return function(token, getNextToken, opts) { |
|
if (token === null || token.type !== types.Dimension) { |
|
return 0; |
|
} |
|
|
|
const numberEnd = utils.consumeNumber(token.value, 0); |
|
|
|
|
|
if (type !== null) { |
|
|
|
const reverseSolidusOffset = token.value.indexOf('\\', numberEnd); |
|
const unit = reverseSolidusOffset === -1 || !isPostfixIeHack(token.value, reverseSolidusOffset) |
|
? token.value.substr(numberEnd) |
|
: token.value.substring(numberEnd, reverseSolidusOffset); |
|
|
|
if (type.has(unit.toLowerCase()) === false) { |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
if (outOfRange(opts, token.value, numberEnd)) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function percentage(token, getNextToken, opts) { |
|
|
|
if (token === null || token.type !== types.Percentage) { |
|
return 0; |
|
} |
|
|
|
|
|
if (outOfRange(opts, token.value, token.value.length - 1)) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function zero(next) { |
|
if (typeof next !== 'function') { |
|
next = function() { |
|
return 0; |
|
}; |
|
} |
|
|
|
return function(token, getNextToken, opts) { |
|
if (token !== null && token.type === types.Number) { |
|
if (Number(token.value) === 0) { |
|
return 1; |
|
} |
|
} |
|
|
|
return next(token, getNextToken, opts); |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function number(token, getNextToken, opts) { |
|
if (token === null) { |
|
return 0; |
|
} |
|
|
|
const numberEnd = utils.consumeNumber(token.value, 0); |
|
const isNumber = numberEnd === token.value.length; |
|
if (!isNumber && !isPostfixIeHack(token.value, numberEnd)) { |
|
return 0; |
|
} |
|
|
|
|
|
if (outOfRange(opts, token.value, numberEnd)) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
|
|
|
|
function integer(token, getNextToken, opts) { |
|
|
|
if (token === null || token.type !== types.Number) { |
|
return 0; |
|
} |
|
|
|
|
|
let i = charCodeAt(token.value, 0) === 0x002B || |
|
charCodeAt(token.value, 0) === 0x002D ? 1 : 0; |
|
|
|
|
|
for (; i < token.value.length; i++) { |
|
if (!charCodeDefinitions.isDigit(charCodeAt(token.value, i))) { |
|
return 0; |
|
} |
|
} |
|
|
|
|
|
if (outOfRange(opts, token.value, i)) { |
|
return 0; |
|
} |
|
|
|
return 1; |
|
} |
|
|
|
|
|
const tokenTypes = { |
|
'ident-token': tokenType(types.Ident), |
|
'function-token': tokenType(types.Function), |
|
'at-keyword-token': tokenType(types.AtKeyword), |
|
'hash-token': tokenType(types.Hash), |
|
'string-token': tokenType(types.String), |
|
'bad-string-token': tokenType(types.BadString), |
|
'url-token': tokenType(types.Url), |
|
'bad-url-token': tokenType(types.BadUrl), |
|
'delim-token': tokenType(types.Delim), |
|
'number-token': tokenType(types.Number), |
|
'percentage-token': tokenType(types.Percentage), |
|
'dimension-token': tokenType(types.Dimension), |
|
'whitespace-token': tokenType(types.WhiteSpace), |
|
'CDO-token': tokenType(types.CDO), |
|
'CDC-token': tokenType(types.CDC), |
|
'colon-token': tokenType(types.Colon), |
|
'semicolon-token': tokenType(types.Semicolon), |
|
'comma-token': tokenType(types.Comma), |
|
'[-token': tokenType(types.LeftSquareBracket), |
|
']-token': tokenType(types.RightSquareBracket), |
|
'(-token': tokenType(types.LeftParenthesis), |
|
')-token': tokenType(types.RightParenthesis), |
|
'{-token': tokenType(types.LeftCurlyBracket), |
|
'}-token': tokenType(types.RightCurlyBracket) |
|
}; |
|
|
|
|
|
const productionTypes = { |
|
|
|
'string': tokenType(types.String), |
|
'ident': tokenType(types.Ident), |
|
|
|
|
|
'percentage': calc(percentage), |
|
|
|
|
|
'zero': zero(), |
|
'number': calc(number), |
|
'integer': calc(integer), |
|
|
|
|
|
'custom-ident': customIdent, |
|
'custom-property-name': customPropertyName, |
|
'hex-color': hexColor, |
|
'id-selector': idSelector, |
|
'an-plus-b': genericAnPlusB, |
|
'urange': genericUrange, |
|
'declaration-value': declarationValue, |
|
'any-value': anyValue |
|
}; |
|
|
|
|
|
function createDemensionTypes(units) { |
|
const { |
|
angle, |
|
decibel, |
|
frequency, |
|
flex, |
|
length, |
|
resolution, |
|
semitones, |
|
time |
|
} = units || {}; |
|
|
|
return { |
|
'dimension': calc(dimension(null)), |
|
'angle': calc(dimension(angle)), |
|
'decibel': calc(dimension(decibel)), |
|
'frequency': calc(dimension(frequency)), |
|
'flex': calc(dimension(flex)), |
|
'length': calc(zero(dimension(length))), |
|
'resolution': calc(dimension(resolution)), |
|
'semitones': calc(dimension(semitones)), |
|
'time': calc(dimension(time)) |
|
}; |
|
} |
|
|
|
function createGenericTypes(units) { |
|
return { |
|
...tokenTypes, |
|
...productionTypes, |
|
...createDemensionTypes(units) |
|
}; |
|
} |
|
|
|
exports.createDemensionTypes = createDemensionTypes; |
|
exports.createGenericTypes = createGenericTypes; |
|
exports.productionTypes = productionTypes; |
|
exports.tokenTypes = tokenTypes; |
|
|