mirror of
https://github.com/thangisme/notes.git
synced 2024-11-01 02:27:20 -04:00
345 lines
39 KiB
JavaScript
345 lines
39 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
exports.__esModule = true;
|
||
|
exports.default = scssTokenize;
|
||
|
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 ASTERICK = 42;
|
||
|
var COLON = 58;
|
||
|
var AT = 64;
|
||
|
|
||
|
// SCSS PATCH {
|
||
|
var COMMA = 44;
|
||
|
var HASH = 35;
|
||
|
// } SCSS PATCH
|
||
|
|
||
|
var RE_AT_END = /[ \n\t\r\f\{\(\)'"\\;/\[\]#]/g;
|
||
|
var RE_WORD_END = /[ \n\t\r\f\(\)\{\}:;@!'"\\\]\[#]|\/(?=\*)/g;
|
||
|
var RE_BAD_BRACKET = /.[\\\/\("'\n]/;
|
||
|
|
||
|
var RE_NEW_LINE = /[\r\f\n]/g; // SCSS PATCH
|
||
|
|
||
|
// SCSS PATCH function name was changed
|
||
|
function scssTokenize(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 brackets = void 0; // SCSS PATCH
|
||
|
|
||
|
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;
|
||
|
|
||
|
// SCSS PATCH {
|
||
|
case COMMA:
|
||
|
tokens.push(['word', ',', line, pos - offset, line, pos - offset + 1]);
|
||
|
break;
|
||
|
// } SCSS PATCH
|
||
|
|
||
|
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) {
|
||
|
// SCSS PATCH {
|
||
|
brackets = 1;
|
||
|
escaped = false;
|
||
|
next = pos + 1;
|
||
|
while (next <= css.length - 1) {
|
||
|
n = css.charCodeAt(next);
|
||
|
if (n === BACKSLASH) {
|
||
|
escaped = !escaped;
|
||
|
} else if (n === OPEN_PARENTHESES) {
|
||
|
brackets += 1;
|
||
|
} else if (n === CLOSE_PARENTHESES) {
|
||
|
brackets -= 1;
|
||
|
if (brackets === 0) break;
|
||
|
}
|
||
|
next += 1;
|
||
|
}
|
||
|
// } SCSS PATCH
|
||
|
|
||
|
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('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;
|
||
|
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:
|
||
|
// SCSS PATCH {
|
||
|
n = css.charCodeAt(pos + 1);
|
||
|
|
||
|
if (code === HASH && n === OPEN_CURLY) {
|
||
|
var deep = 1;
|
||
|
next = pos;
|
||
|
while (deep > 0) {
|
||
|
next += 1;
|
||
|
if (css.length <= next) unclosed('interpolation');
|
||
|
|
||
|
code = css.charCodeAt(next);
|
||
|
n = css.charCodeAt(next + 1);
|
||
|
|
||
|
if (code === CLOSE_CURLY) {
|
||
|
deep -= 1;
|
||
|
} else if (code === HASH && n === OPEN_CURLY) {
|
||
|
deep += 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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(['word', content, line, pos - offset, nextLine, next - nextOffset]);
|
||
|
|
||
|
offset = nextOffset;
|
||
|
line = nextLine;
|
||
|
pos = next;
|
||
|
} else if (code === SLASH && n === ASTERICK) {
|
||
|
// } SCSS PATCH
|
||
|
|
||
|
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;
|
||
|
|
||
|
// SCSS PATCH {
|
||
|
} 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;
|
||
|
// } SCSS PATCH
|
||
|
} 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,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNjc3MtdG9rZW5pemUuZXM2Il0sIm5hbWVzIjpbInNjc3NUb2tlbml6ZSIsIlNJTkdMRV9RVU9URSIsIkRPVUJMRV9RVU9URSIsIkJBQ0tTTEFTSCIsIlNMQVNIIiwiTkVXTElORSIsIlNQQUNFIiwiRkVFRCIsIlRBQiIsIkNSIiwiT1BFTl9TUVVBUkUiLCJDTE9TRV9TUVVBUkUiLCJPUEVOX1BBUkVOVEhFU0VTIiwiQ0xPU0VfUEFSRU5USEVTRVMiLCJPUEVOX0NVUkxZIiwiQ0xPU0VfQ1VSTFkiLCJTRU1JQ09MT04iLCJBU1RFUklDSyIsIkNPTE9OIiwiQVQiLCJDT01NQSIsIkhBU0giLCJSRV9BVF9FTkQiLCJSRV9XT1JEX0VORCIsIlJFX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