mirror of
https://github.com/thangisme/notes.git
synced 2024-11-01 02:27:20 -04:00
274 lines
31 KiB
JavaScript
274 lines
31 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports.default = tokenize;
|
||
|
var SINGLE_QUOTE = 39;
|
||
|
var DOUBLE_QUOTE = 34;
|
||
|
var BACKSLASH = 92;
|
||
|
var SLASH = 47;
|
||
|
var NEWLINE = 10;
|
||
|
var SPACE = 32;
|
||
|
var FEED = 12;
|
||
|
var TAB = 9;
|
||
|
var CR = 13;
|
||
|
var OPEN_SQUARE = 91;
|
||
|
var CLOSE_SQUARE = 93;
|
||
|
var OPEN_PARENTHESES = 40;
|
||
|
var CLOSE_PARENTHESES = 41;
|
||
|
var OPEN_CURLY = 123;
|
||
|
var CLOSE_CURLY = 125;
|
||
|
var SEMICOLON = 59;
|
||
|
var ASTERISK = 42;
|
||
|
var COLON = 58;
|
||
|
var AT = 64;
|
||
|
|
||
|
var RE_AT_END = /[ \n\t\r\f\{\(\)'"\\;/\[\]#]/g;
|
||
|
var RE_WORD_END = /[ \n\t\r\f\(\)\{\}:;@!'"\\\]\[#]|\/(?=\*)/g;
|
||
|
var RE_BAD_BRACKET = /.[\\\/\("'\n]/;
|
||
|
|
||
|
function tokenize(input) {
|
||
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||
|
|
||
|
var tokens = [];
|
||
|
var css = input.css.valueOf();
|
||
|
|
||
|
var ignore = options.ignoreErrors;
|
||
|
|
||
|
var code = void 0,
|
||
|
next = void 0,
|
||
|
quote = void 0,
|
||
|
lines = void 0,
|
||
|
last = void 0,
|
||
|
content = void 0,
|
||
|
escape = void 0,
|
||
|
nextLine = void 0,
|
||
|
nextOffset = void 0,
|
||
|
escaped = void 0,
|
||
|
escapePos = void 0,
|
||
|
prev = void 0,
|
||
|
n = void 0;
|
||
|
|
||
|
var length = css.length;
|
||
|
var offset = -1;
|
||
|
var line = 1;
|
||
|
var pos = 0;
|
||
|
|
||
|
function unclosed(what) {
|
||
|
throw input.error('Unclosed ' + what, line, pos - offset);
|
||
|
}
|
||
|
|
||
|
while (pos < length) {
|
||
|
code = css.charCodeAt(pos);
|
||
|
|
||
|
if (code === NEWLINE || code === FEED || code === CR && css.charCodeAt(pos + 1) !== NEWLINE) {
|
||
|
offset = pos;
|
||
|
line += 1;
|
||
|
}
|
||
|
|
||
|
switch (code) {
|
||
|
case NEWLINE:
|
||
|
case SPACE:
|
||
|
case TAB:
|
||
|
case CR:
|
||
|
case FEED:
|
||
|
next = pos;
|
||
|
do {
|
||
|
next += 1;
|
||
|
code = css.charCodeAt(next);
|
||
|
if (code === NEWLINE) {
|
||
|
offset = next;
|
||
|
line += 1;
|
||
|
}
|
||
|
} while (code === SPACE || code === NEWLINE || code === TAB || code === CR || code === FEED);
|
||
|
|
||
|
tokens.push(['space', css.slice(pos, next)]);
|
||
|
pos = next - 1;
|
||
|
break;
|
||
|
|
||
|
case OPEN_SQUARE:
|
||
|
tokens.push(['[', '[', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case CLOSE_SQUARE:
|
||
|
tokens.push([']', ']', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case OPEN_CURLY:
|
||
|
tokens.push(['{', '{', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case CLOSE_CURLY:
|
||
|
tokens.push(['}', '}', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case COLON:
|
||
|
tokens.push([':', ':', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case SEMICOLON:
|
||
|
tokens.push([';', ';', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case OPEN_PARENTHESES:
|
||
|
prev = tokens.length ? tokens[tokens.length - 1][1] : '';
|
||
|
n = css.charCodeAt(pos + 1);
|
||
|
if (prev === 'url' && n !== SINGLE_QUOTE && n !== DOUBLE_QUOTE && n !== SPACE && n !== NEWLINE && n !== TAB && n !== FEED && n !== CR) {
|
||
|
next = pos;
|
||
|
do {
|
||
|
escaped = false;
|
||
|
next = css.indexOf(')', next + 1);
|
||
|
if (next === -1) {
|
||
|
if (ignore) {
|
||
|
next = pos;
|
||
|
break;
|
||
|
} else {
|
||
|
unclosed('bracket');
|
||
|
}
|
||
|
}
|
||
|
escapePos = next;
|
||
|
while (css.charCodeAt(escapePos - 1) === BACKSLASH) {
|
||
|
escapePos -= 1;
|
||
|
escaped = !escaped;
|
||
|
}
|
||
|
} while (escaped);
|
||
|
|
||
|
tokens.push(['brackets', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
|
||
|
pos = next;
|
||
|
} else {
|
||
|
next = css.indexOf(')', pos + 1);
|
||
|
content = css.slice(pos, next + 1);
|
||
|
|
||
|
if (next === -1 || RE_BAD_BRACKET.test(content)) {
|
||
|
tokens.push(['(', '(', line, pos - offset]);
|
||
|
} else {
|
||
|
tokens.push(['brackets', content, line, pos - offset, line, next - offset]);
|
||
|
pos = next;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
break;
|
||
|
|
||
|
case CLOSE_PARENTHESES:
|
||
|
tokens.push([')', ')', line, pos - offset]);
|
||
|
break;
|
||
|
|
||
|
case SINGLE_QUOTE:
|
||
|
case DOUBLE_QUOTE:
|
||
|
quote = code === SINGLE_QUOTE ? '\'' : '"';
|
||
|
next = pos;
|
||
|
do {
|
||
|
escaped = false;
|
||
|
next = css.indexOf(quote, next + 1);
|
||
|
if (next === -1) {
|
||
|
if (ignore) {
|
||
|
next = pos + 1;
|
||
|
break;
|
||
|
} else {
|
||
|
unclosed('string');
|
||
|
}
|
||
|
}
|
||
|
escapePos = next;
|
||
|
while (css.charCodeAt(escapePos - 1) === BACKSLASH) {
|
||
|
escapePos -= 1;
|
||
|
escaped = !escaped;
|
||
|
}
|
||
|
} while (escaped);
|
||
|
|
||
|
content = css.slice(pos, next + 1);
|
||
|
lines = content.split('\n');
|
||
|
last = lines.length - 1;
|
||
|
|
||
|
if (last > 0) {
|
||
|
nextLine = line + last;
|
||
|
nextOffset = next - lines[last].length;
|
||
|
} else {
|
||
|
nextLine = line;
|
||
|
nextOffset = offset;
|
||
|
}
|
||
|
|
||
|
tokens.push(['string', css.slice(pos, next + 1), line, pos - offset, nextLine, next - nextOffset]);
|
||
|
|
||
|
offset = nextOffset;
|
||
|
line = nextLine;
|
||
|
pos = next;
|
||
|
break;
|
||
|
|
||
|
case AT:
|
||
|
RE_AT_END.lastIndex = pos + 1;
|
||
|
RE_AT_END.test(css);
|
||
|
if (RE_AT_END.lastIndex === 0) {
|
||
|
next = css.length - 1;
|
||
|
} else {
|
||
|
next = RE_AT_END.lastIndex - 2;
|
||
|
}
|
||
|
tokens.push(['at-word', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
|
||
|
pos = next;
|
||
|
break;
|
||
|
|
||
|
case BACKSLASH:
|
||
|
next = pos;
|
||
|
escape = true;
|
||
|
while (css.charCodeAt(next + 1) === BACKSLASH) {
|
||
|
next += 1;
|
||
|
escape = !escape;
|
||
|
}
|
||
|
code = css.charCodeAt(next + 1);
|
||
|
if (escape && code !== SLASH && code !== SPACE && code !== NEWLINE && code !== TAB && code !== CR && code !== FEED) {
|
||
|
next += 1;
|
||
|
}
|
||
|
tokens.push(['word', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
|
||
|
pos = next;
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
if (code === SLASH && css.charCodeAt(pos + 1) === ASTERISK) {
|
||
|
next = css.indexOf('*/', pos + 2) + 1;
|
||
|
if (next === 0) {
|
||
|
if (ignore) {
|
||
|
next = css.length;
|
||
|
} else {
|
||
|
unclosed('comment');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
content = css.slice(pos, next + 1);
|
||
|
lines = content.split('\n');
|
||
|
last = lines.length - 1;
|
||
|
|
||
|
if (last > 0) {
|
||
|
nextLine = line + last;
|
||
|
nextOffset = next - lines[last].length;
|
||
|
} else {
|
||
|
nextLine = line;
|
||
|
nextOffset = offset;
|
||
|
}
|
||
|
|
||
|
tokens.push(['comment', content, line, pos - offset, nextLine, next - nextOffset]);
|
||
|
|
||
|
offset = nextOffset;
|
||
|
line = nextLine;
|
||
|
pos = next;
|
||
|
} else {
|
||
|
RE_WORD_END.lastIndex = pos + 1;
|
||
|
RE_WORD_END.test(css);
|
||
|
if (RE_WORD_END.lastIndex === 0) {
|
||
|
next = css.length - 1;
|
||
|
} else {
|
||
|
next = RE_WORD_END.lastIndex - 2;
|
||
|
}
|
||
|
|
||
|
tokens.push(['word', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
|
||
|
pos = next;
|
||
|
}
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
pos++;
|
||
|
}
|
||
|
|
||
|
return tokens;
|
||
|
}
|
||
|
module.exports = exports['default'];
|
||
|
//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRva2VuaXplLmVzNiJdLCJuYW1lcyI6WyJ0b2tlbml6ZSIsIlNJTkdMRV9RVU9URSIsIkRPVUJMRV9RVU9URSIsIkJBQ0tTTEFTSCIsIlNMQVNIIiwiTkVXTElORSIsIlNQQUNFIiwiRkVFRCIsIlRBQiIsIkNSIiwiT1BFTl9TUVVBUkUiLCJDTE9TRV9TUVVBUkUiLCJPUEVOX1BBUkVOVEhFU0VTIiwiQ0xPU0VfUEFSRU5USEVTRVMiLCJPUEVOX0NVUkxZIiwiQ0xPU0VfQ1VSTFkiLCJTRU1JQ09MT04iLCJBU1RFUklTSyIsIkNPTE9OIiwiQVQiLCJSRV9BVF9FTkQiLCJSRV9XT1JEX0VORCIsIlJFX0JBRF9CUkFDS0VUIiwiaW5wdXQiLCJvcHRpb25zIiwidG9rZW5zIiwiY3NzIiwidmFsdWVPZiIsImlnbm9yZSIsImlnbm9yZUVycm9ycyIsImNvZGUiLCJuZXh0IiwicXVvdGUiLCJsaW5lcyIsImxhc3QiLCJjb250ZW50IiwiZXNjYXBlIiwibmV4dExpbmUiLCJuZXh0T2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