|
'use strict'; |
|
|
|
const types = require('./types.cjs'); |
|
const charCodeDefinitions = require('./char-code-definitions.cjs'); |
|
const utils = require('./utils.cjs'); |
|
const names = require('./names.cjs'); |
|
const OffsetToLocation = require('./OffsetToLocation.cjs'); |
|
const TokenStream = require('./TokenStream.cjs'); |
|
|
|
function tokenize(source, onToken) { |
|
function getCharCode(offset) { |
|
return offset < sourceLength ? source.charCodeAt(offset) : 0; |
|
} |
|
|
|
|
|
function consumeNumericToken() { |
|
|
|
offset = utils.consumeNumber(source, offset); |
|
|
|
|
|
if (charCodeDefinitions.isIdentifierStart(getCharCode(offset), getCharCode(offset + 1), getCharCode(offset + 2))) { |
|
|
|
|
|
|
|
type = types.Dimension; |
|
offset = utils.consumeName(source, offset); |
|
return; |
|
} |
|
|
|
|
|
if (getCharCode(offset) === 0x0025) { |
|
|
|
type = types.Percentage; |
|
offset++; |
|
return; |
|
} |
|
|
|
|
|
type = types.Number; |
|
} |
|
|
|
|
|
function consumeIdentLikeToken() { |
|
const nameStartOffset = offset; |
|
|
|
|
|
offset = utils.consumeName(source, offset); |
|
|
|
|
|
|
|
if (utils.cmpStr(source, nameStartOffset, offset, 'url') && getCharCode(offset) === 0x0028) { |
|
|
|
offset = utils.findWhiteSpaceEnd(source, offset + 1); |
|
|
|
|
|
|
|
|
|
if (getCharCode(offset) === 0x0022 || |
|
getCharCode(offset) === 0x0027) { |
|
type = types.Function; |
|
offset = nameStartOffset + 4; |
|
return; |
|
} |
|
|
|
|
|
consumeUrlToken(); |
|
return; |
|
} |
|
|
|
|
|
|
|
if (getCharCode(offset) === 0x0028) { |
|
type = types.Function; |
|
offset++; |
|
return; |
|
} |
|
|
|
|
|
type = types.Ident; |
|
} |
|
|
|
|
|
function consumeStringToken(endingCodePoint) { |
|
|
|
|
|
|
|
if (!endingCodePoint) { |
|
endingCodePoint = getCharCode(offset++); |
|
} |
|
|
|
|
|
type = types.String; |
|
|
|
|
|
for (; offset < source.length; offset++) { |
|
const code = source.charCodeAt(offset); |
|
|
|
switch (charCodeDefinitions.charCodeCategory(code)) { |
|
|
|
case endingCodePoint: |
|
|
|
offset++; |
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case charCodeDefinitions.WhiteSpaceCategory: |
|
if (charCodeDefinitions.isNewline(code)) { |
|
|
|
|
|
offset += utils.getNewlineLength(source, offset, code); |
|
type = types.BadString; |
|
return; |
|
} |
|
break; |
|
|
|
|
|
case 0x005C: |
|
|
|
if (offset === source.length - 1) { |
|
break; |
|
} |
|
|
|
const nextCode = getCharCode(offset + 1); |
|
|
|
|
|
if (charCodeDefinitions.isNewline(nextCode)) { |
|
offset += utils.getNewlineLength(source, offset + 1, nextCode); |
|
} else if (charCodeDefinitions.isValidEscape(code, nextCode)) { |
|
|
|
|
|
|
|
offset = utils.consumeEscaped(source, offset) - 1; |
|
} |
|
break; |
|
|
|
|
|
|
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function consumeUrlToken() { |
|
|
|
type = types.Url; |
|
|
|
|
|
offset = utils.findWhiteSpaceEnd(source, offset); |
|
|
|
|
|
for (; offset < source.length; offset++) { |
|
const code = source.charCodeAt(offset); |
|
|
|
switch (charCodeDefinitions.charCodeCategory(code)) { |
|
|
|
case 0x0029: |
|
|
|
offset++; |
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case charCodeDefinitions.WhiteSpaceCategory: |
|
|
|
offset = utils.findWhiteSpaceEnd(source, offset); |
|
|
|
|
|
|
|
|
|
if (getCharCode(offset) === 0x0029 || offset >= source.length) { |
|
if (offset < source.length) { |
|
offset++; |
|
} |
|
return; |
|
} |
|
|
|
|
|
|
|
offset = utils.consumeBadUrlRemnants(source, offset); |
|
type = types.BadUrl; |
|
return; |
|
|
|
|
|
|
|
|
|
|
|
case 0x0022: |
|
case 0x0027: |
|
case 0x0028: |
|
case charCodeDefinitions.NonPrintableCategory: |
|
|
|
|
|
offset = utils.consumeBadUrlRemnants(source, offset); |
|
type = types.BadUrl; |
|
return; |
|
|
|
|
|
case 0x005C: |
|
|
|
|
|
if (charCodeDefinitions.isValidEscape(code, getCharCode(offset + 1))) { |
|
offset = utils.consumeEscaped(source, offset) - 1; |
|
break; |
|
} |
|
|
|
|
|
|
|
offset = utils.consumeBadUrlRemnants(source, offset); |
|
type = types.BadUrl; |
|
return; |
|
|
|
|
|
|
|
} |
|
} |
|
} |
|
|
|
|
|
source = String(source || ''); |
|
|
|
const sourceLength = source.length; |
|
let start = charCodeDefinitions.isBOM(getCharCode(0)); |
|
let offset = start; |
|
let type; |
|
|
|
|
|
|
|
while (offset < sourceLength) { |
|
const code = source.charCodeAt(offset); |
|
|
|
switch (charCodeDefinitions.charCodeCategory(code)) { |
|
|
|
case charCodeDefinitions.WhiteSpaceCategory: |
|
|
|
type = types.WhiteSpace; |
|
offset = utils.findWhiteSpaceEnd(source, offset + 1); |
|
break; |
|
|
|
|
|
case 0x0022: |
|
|
|
consumeStringToken(); |
|
break; |
|
|
|
|
|
case 0x0023: |
|
|
|
if (charCodeDefinitions.isName(getCharCode(offset + 1)) || charCodeDefinitions.isValidEscape(getCharCode(offset + 1), getCharCode(offset + 2))) { |
|
|
|
type = types.Hash; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
offset = utils.consumeName(source, offset + 1); |
|
|
|
|
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
|
|
break; |
|
|
|
|
|
case 0x0027: |
|
|
|
consumeStringToken(); |
|
break; |
|
|
|
|
|
case 0x0028: |
|
|
|
type = types.LeftParenthesis; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x0029: |
|
|
|
type = types.RightParenthesis; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x002B: |
|
|
|
if (charCodeDefinitions.isNumberStart(code, getCharCode(offset + 1), getCharCode(offset + 2))) { |
|
|
|
consumeNumericToken(); |
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
break; |
|
|
|
|
|
case 0x002C: |
|
|
|
type = types.Comma; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x002D: |
|
|
|
if (charCodeDefinitions.isNumberStart(code, getCharCode(offset + 1), getCharCode(offset + 2))) { |
|
consumeNumericToken(); |
|
} else { |
|
|
|
if (getCharCode(offset + 1) === 0x002D && |
|
getCharCode(offset + 2) === 0x003E) { |
|
type = types.CDC; |
|
offset = offset + 3; |
|
} else { |
|
|
|
if (charCodeDefinitions.isIdentifierStart(code, getCharCode(offset + 1), getCharCode(offset + 2))) { |
|
|
|
consumeIdentLikeToken(); |
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
} |
|
} |
|
break; |
|
|
|
|
|
case 0x002E: |
|
|
|
if (charCodeDefinitions.isNumberStart(code, getCharCode(offset + 1), getCharCode(offset + 2))) { |
|
|
|
consumeNumericToken(); |
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
|
|
break; |
|
|
|
|
|
case 0x002F: |
|
|
|
if (getCharCode(offset + 1) === 0x002A) { |
|
|
|
|
|
type = types.Comment; |
|
offset = source.indexOf('*/', offset + 2); |
|
offset = offset === -1 ? source.length : offset + 2; |
|
} else { |
|
type = types.Delim; |
|
offset++; |
|
} |
|
break; |
|
|
|
|
|
case 0x003A: |
|
|
|
type = types.Colon; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x003B: |
|
|
|
type = types.Semicolon; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x003C: |
|
|
|
if (getCharCode(offset + 1) === 0x0021 && |
|
getCharCode(offset + 2) === 0x002D && |
|
getCharCode(offset + 3) === 0x002D) { |
|
|
|
type = types.CDO; |
|
offset = offset + 4; |
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
|
|
break; |
|
|
|
|
|
case 0x0040: |
|
|
|
if (charCodeDefinitions.isIdentifierStart(getCharCode(offset + 1), getCharCode(offset + 2), getCharCode(offset + 3))) { |
|
|
|
type = types.AtKeyword; |
|
offset = utils.consumeName(source, offset + 1); |
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
|
|
break; |
|
|
|
|
|
case 0x005B: |
|
|
|
type = types.LeftSquareBracket; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x005C: |
|
|
|
if (charCodeDefinitions.isValidEscape(code, getCharCode(offset + 1))) { |
|
|
|
consumeIdentLikeToken(); |
|
} else { |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
break; |
|
|
|
|
|
case 0x005D: |
|
|
|
type = types.RightSquareBracket; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x007B: |
|
|
|
type = types.LeftCurlyBracket; |
|
offset++; |
|
break; |
|
|
|
|
|
case 0x007D: |
|
|
|
type = types.RightCurlyBracket; |
|
offset++; |
|
break; |
|
|
|
|
|
case charCodeDefinitions.DigitCategory: |
|
|
|
consumeNumericToken(); |
|
break; |
|
|
|
|
|
case charCodeDefinitions.NameStartCategory: |
|
|
|
consumeIdentLikeToken(); |
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
type = types.Delim; |
|
offset++; |
|
} |
|
|
|
|
|
onToken(type, start, start = offset); |
|
} |
|
} |
|
|
|
exports.AtKeyword = types.AtKeyword; |
|
exports.BadString = types.BadString; |
|
exports.BadUrl = types.BadUrl; |
|
exports.CDC = types.CDC; |
|
exports.CDO = types.CDO; |
|
exports.Colon = types.Colon; |
|
exports.Comma = types.Comma; |
|
exports.Comment = types.Comment; |
|
exports.Delim = types.Delim; |
|
exports.Dimension = types.Dimension; |
|
exports.EOF = types.EOF; |
|
exports.Function = types.Function; |
|
exports.Hash = types.Hash; |
|
exports.Ident = types.Ident; |
|
exports.LeftCurlyBracket = types.LeftCurlyBracket; |
|
exports.LeftParenthesis = types.LeftParenthesis; |
|
exports.LeftSquareBracket = types.LeftSquareBracket; |
|
exports.Number = types.Number; |
|
exports.Percentage = types.Percentage; |
|
exports.RightCurlyBracket = types.RightCurlyBracket; |
|
exports.RightParenthesis = types.RightParenthesis; |
|
exports.RightSquareBracket = types.RightSquareBracket; |
|
exports.Semicolon = types.Semicolon; |
|
exports.String = types.String; |
|
exports.Url = types.Url; |
|
exports.WhiteSpace = types.WhiteSpace; |
|
exports.tokenTypes = types; |
|
exports.DigitCategory = charCodeDefinitions.DigitCategory; |
|
exports.EofCategory = charCodeDefinitions.EofCategory; |
|
exports.NameStartCategory = charCodeDefinitions.NameStartCategory; |
|
exports.NonPrintableCategory = charCodeDefinitions.NonPrintableCategory; |
|
exports.WhiteSpaceCategory = charCodeDefinitions.WhiteSpaceCategory; |
|
exports.charCodeCategory = charCodeDefinitions.charCodeCategory; |
|
exports.isBOM = charCodeDefinitions.isBOM; |
|
exports.isDigit = charCodeDefinitions.isDigit; |
|
exports.isHexDigit = charCodeDefinitions.isHexDigit; |
|
exports.isIdentifierStart = charCodeDefinitions.isIdentifierStart; |
|
exports.isLetter = charCodeDefinitions.isLetter; |
|
exports.isLowercaseLetter = charCodeDefinitions.isLowercaseLetter; |
|
exports.isName = charCodeDefinitions.isName; |
|
exports.isNameStart = charCodeDefinitions.isNameStart; |
|
exports.isNewline = charCodeDefinitions.isNewline; |
|
exports.isNonAscii = charCodeDefinitions.isNonAscii; |
|
exports.isNonPrintable = charCodeDefinitions.isNonPrintable; |
|
exports.isNumberStart = charCodeDefinitions.isNumberStart; |
|
exports.isUppercaseLetter = charCodeDefinitions.isUppercaseLetter; |
|
exports.isValidEscape = charCodeDefinitions.isValidEscape; |
|
exports.isWhiteSpace = charCodeDefinitions.isWhiteSpace; |
|
exports.cmpChar = utils.cmpChar; |
|
exports.cmpStr = utils.cmpStr; |
|
exports.consumeBadUrlRemnants = utils.consumeBadUrlRemnants; |
|
exports.consumeEscaped = utils.consumeEscaped; |
|
exports.consumeName = utils.consumeName; |
|
exports.consumeNumber = utils.consumeNumber; |
|
exports.decodeEscaped = utils.decodeEscaped; |
|
exports.findDecimalNumberEnd = utils.findDecimalNumberEnd; |
|
exports.findWhiteSpaceEnd = utils.findWhiteSpaceEnd; |
|
exports.findWhiteSpaceStart = utils.findWhiteSpaceStart; |
|
exports.getNewlineLength = utils.getNewlineLength; |
|
exports.tokenNames = names; |
|
exports.OffsetToLocation = OffsetToLocation.OffsetToLocation; |
|
exports.TokenStream = TokenStream.TokenStream; |
|
exports.tokenize = tokenize; |
|
|