mirror of
https://github.com/thangisme/notes.git
synced 2024-11-01 00:37:33 -04:00
287 lines
32 KiB
JavaScript
287 lines
32 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_PARENTHESES = 40;
|
||
|
var CLOSE_PARENTHESES = 41;
|
||
|
var OPEN_CURLY = 123;
|
||
|
var CLOSE_CURLY = 125;
|
||
|
var SEMICOLON = 59;
|
||
|
var ASTERICK = 42;
|
||
|
var COLON = 58;
|
||
|
var AT = 64;
|
||
|
var COMMA = 44;
|
||
|
|
||
|
var RE_AT_END = /[ \n\t\r\f\{\(\)'"\\;/]/g;
|
||
|
var RE_NEW_LINE = /[\r\f\n]/g;
|
||
|
var RE_WORD_END = /[ \n\t\r\f\(\)\{\}:;@!'"\\,]|\/(?=\*)/g;
|
||
|
var RE_BAD_BRACKET = /.[\\\/\("'\n]/;
|
||
|
|
||
|
function tokenize(input) {
|
||
|
var tokens = [];
|
||
|
var css = input.css.valueOf();
|
||
|
|
||
|
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 CR:
|
||
|
if (css.charCodeAt(pos + 1) === NEWLINE) {
|
||
|
offset = pos;
|
||
|
line += 1;
|
||
|
pos += 1;
|
||
|
tokens.push(['newline', '\r\n', line - 1]);
|
||
|
} else {
|
||
|
tokens.push(['newline', '\r', line - 1]);
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case FEED:
|
||
|
case NEWLINE:
|
||
|
tokens.push(['newline', css.slice(pos, pos + 1), line - 1]);
|
||
|
break;
|
||
|
|
||
|
case SPACE:
|
||
|
case TAB:
|
||
|
next = pos;
|
||
|
do {
|
||
|
next += 1;
|
||
|
code = css.charCodeAt(next);
|
||
|
} while (code === SPACE || code === TAB);
|
||
|
|
||
|
tokens.push(['space', css.slice(pos, next)]);
|
||
|
pos = next - 1;
|
||
|
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 COMMA:
|
||
|
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) 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) unclosed('quote');
|
||
|
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;
|
||
|
|
||
|
nextLine = line;
|
||
|
|
||
|
while (css.charCodeAt(next + 1) === BACKSLASH) {
|
||
|
next += 1;
|
||
|
escape = !escape;
|
||
|
}
|
||
|
code = css.charCodeAt(next + 1);
|
||
|
if (escape) {
|
||
|
if (code === CR && css.charCodeAt(next + 2) === NEWLINE) {
|
||
|
next += 2;
|
||
|
nextLine += 1;
|
||
|
nextOffset = next;
|
||
|
} else if (code === CR || code === NEWLINE || code === FEED) {
|
||
|
next += 1;
|
||
|
nextLine += 1;
|
||
|
nextOffset = next;
|
||
|
} else {
|
||
|
next += 1;
|
||
|
}
|
||
|
}
|
||
|
tokens.push(['word', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
|
||
|
if (nextLine !== line) {
|
||
|
line = nextLine;
|
||
|
offset = nextOffset;
|
||
|
}
|
||
|
pos = next;
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
n = css.charCodeAt(pos + 1);
|
||
|
|
||
|
if (code === SLASH && n === ASTERICK) {
|
||
|
next = css.indexOf('*/', pos + 2) + 1;
|
||
|
if (next === 0) 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 if (code === SLASH && n === SLASH) {
|
||
|
RE_NEW_LINE.lastIndex = pos + 1;
|
||
|
RE_NEW_LINE.test(css);
|
||
|
if (RE_NEW_LINE.lastIndex === 0) {
|
||
|
next = css.length - 1;
|
||
|
} else {
|
||
|
next = RE_NEW_LINE.lastIndex - 2;
|
||
|
}
|
||
|
|
||
|
content = css.slice(pos, next + 1);
|
||
|
|
||
|
tokens.push(['comment', content, line, pos - offset, line, next - offset, 'inline']);
|
||
|
|
||
|
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;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRva2VuaXplLmVzNiJdLCJuYW1lcyI6WyJ0b2tlbml6ZSIsIlNJTkdMRV9RVU9URSIsIkRPVUJMRV9RVU9URSIsIkJBQ0tTTEFTSCIsIlNMQVNIIiwiTkVXTElORSIsIlNQQUNFIiwiRkVFRCIsIlRBQiIsIkNSIiwiT1BFTl9QQVJFTlRIRVNFUyIsIkNMT1NFX1BBUkVOVEhFU0VTIiwiT1BFTl9DVVJMWSIsIkNMT1NFX0NVUkxZIiwiU0VNSUNPTE9OIiwiQVNURVJJQ0siLCJDT0xPTiIsIkFUIiwiQ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