| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | exports.__esModule = true;
|
|---|
| 4 | exports.FIELDS = void 0;
|
|---|
| 5 | exports["default"] = tokenize;
|
|---|
| 6 | var t = _interopRequireWildcard(require("./tokenTypes"));
|
|---|
| 7 | var _unescapable, _wordDelimiters;
|
|---|
| 8 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|---|
| 9 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|---|
| 10 | var unescapable = (_unescapable = {}, _unescapable[t.tab] = true, _unescapable[t.newline] = true, _unescapable[t.cr] = true, _unescapable[t.feed] = true, _unescapable);
|
|---|
| 11 | var wordDelimiters = (_wordDelimiters = {}, _wordDelimiters[t.space] = true, _wordDelimiters[t.tab] = true, _wordDelimiters[t.newline] = true, _wordDelimiters[t.cr] = true, _wordDelimiters[t.feed] = true, _wordDelimiters[t.ampersand] = true, _wordDelimiters[t.asterisk] = true, _wordDelimiters[t.bang] = true, _wordDelimiters[t.comma] = true, _wordDelimiters[t.colon] = true, _wordDelimiters[t.semicolon] = true, _wordDelimiters[t.openParenthesis] = true, _wordDelimiters[t.closeParenthesis] = true, _wordDelimiters[t.openSquare] = true, _wordDelimiters[t.closeSquare] = true, _wordDelimiters[t.singleQuote] = true, _wordDelimiters[t.doubleQuote] = true, _wordDelimiters[t.plus] = true, _wordDelimiters[t.pipe] = true, _wordDelimiters[t.tilde] = true, _wordDelimiters[t.greaterThan] = true, _wordDelimiters[t.equals] = true, _wordDelimiters[t.dollar] = true, _wordDelimiters[t.caret] = true, _wordDelimiters[t.slash] = true, _wordDelimiters);
|
|---|
| 12 | var hex = {};
|
|---|
| 13 | var hexChars = "0123456789abcdefABCDEF";
|
|---|
| 14 | for (var i = 0; i < hexChars.length; i++) {
|
|---|
| 15 | hex[hexChars.charCodeAt(i)] = true;
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Returns the last index of the bar css word
|
|---|
| 20 | * @param {string} css The string in which the word begins
|
|---|
| 21 | * @param {number} start The index into the string where word's first letter occurs
|
|---|
| 22 | */
|
|---|
| 23 | function consumeWord(css, start) {
|
|---|
| 24 | var next = start;
|
|---|
| 25 | var code;
|
|---|
| 26 | do {
|
|---|
| 27 | code = css.charCodeAt(next);
|
|---|
| 28 | if (wordDelimiters[code]) {
|
|---|
| 29 | return next - 1;
|
|---|
| 30 | } else if (code === t.backslash) {
|
|---|
| 31 | next = consumeEscape(css, next) + 1;
|
|---|
| 32 | } else {
|
|---|
| 33 | // All other characters are part of the word
|
|---|
| 34 | next++;
|
|---|
| 35 | }
|
|---|
| 36 | } while (next < css.length);
|
|---|
| 37 | return next - 1;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * Returns the last index of the escape sequence
|
|---|
| 42 | * @param {string} css The string in which the sequence begins
|
|---|
| 43 | * @param {number} start The index into the string where escape character (`\`) occurs.
|
|---|
| 44 | */
|
|---|
| 45 | function consumeEscape(css, start) {
|
|---|
| 46 | var next = start;
|
|---|
| 47 | var code = css.charCodeAt(next + 1);
|
|---|
| 48 | if (unescapable[code]) {
|
|---|
| 49 | // just consume the escape char
|
|---|
| 50 | } else if (hex[code]) {
|
|---|
| 51 | var hexDigits = 0;
|
|---|
| 52 | // consume up to 6 hex chars
|
|---|
| 53 | do {
|
|---|
| 54 | next++;
|
|---|
| 55 | hexDigits++;
|
|---|
| 56 | code = css.charCodeAt(next + 1);
|
|---|
| 57 | } while (hex[code] && hexDigits < 6);
|
|---|
| 58 | // if fewer than 6 hex chars, a trailing space ends the escape
|
|---|
| 59 | if (hexDigits < 6 && code === t.space) {
|
|---|
| 60 | next++;
|
|---|
| 61 | }
|
|---|
| 62 | } else {
|
|---|
| 63 | // the next char is part of the current word
|
|---|
| 64 | next++;
|
|---|
| 65 | }
|
|---|
| 66 | return next;
|
|---|
| 67 | }
|
|---|
| 68 | var FIELDS = {
|
|---|
| 69 | TYPE: 0,
|
|---|
| 70 | START_LINE: 1,
|
|---|
| 71 | START_COL: 2,
|
|---|
| 72 | END_LINE: 3,
|
|---|
| 73 | END_COL: 4,
|
|---|
| 74 | START_POS: 5,
|
|---|
| 75 | END_POS: 6
|
|---|
| 76 | };
|
|---|
| 77 | exports.FIELDS = FIELDS;
|
|---|
| 78 | function tokenize(input) {
|
|---|
| 79 | var tokens = [];
|
|---|
| 80 | var css = input.css.valueOf();
|
|---|
| 81 | var _css = css,
|
|---|
| 82 | length = _css.length;
|
|---|
| 83 | var offset = -1;
|
|---|
| 84 | var line = 1;
|
|---|
| 85 | var start = 0;
|
|---|
| 86 | var end = 0;
|
|---|
| 87 | var code, content, endColumn, endLine, escaped, escapePos, last, lines, next, nextLine, nextOffset, quote, tokenType;
|
|---|
| 88 | function unclosed(what, fix) {
|
|---|
| 89 | if (input.safe) {
|
|---|
| 90 | // fyi: this is never set to true.
|
|---|
| 91 | css += fix;
|
|---|
| 92 | next = css.length - 1;
|
|---|
| 93 | } else {
|
|---|
| 94 | throw input.error('Unclosed ' + what, line, start - offset, start);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | while (start < length) {
|
|---|
| 98 | code = css.charCodeAt(start);
|
|---|
| 99 | if (code === t.newline) {
|
|---|
| 100 | offset = start;
|
|---|
| 101 | line += 1;
|
|---|
| 102 | }
|
|---|
| 103 | switch (code) {
|
|---|
| 104 | case t.space:
|
|---|
| 105 | case t.tab:
|
|---|
| 106 | case t.newline:
|
|---|
| 107 | case t.cr:
|
|---|
| 108 | case t.feed:
|
|---|
| 109 | next = start;
|
|---|
| 110 | do {
|
|---|
| 111 | next += 1;
|
|---|
| 112 | code = css.charCodeAt(next);
|
|---|
| 113 | if (code === t.newline) {
|
|---|
| 114 | offset = next;
|
|---|
| 115 | line += 1;
|
|---|
| 116 | }
|
|---|
| 117 | } while (code === t.space || code === t.newline || code === t.tab || code === t.cr || code === t.feed);
|
|---|
| 118 | tokenType = t.space;
|
|---|
| 119 | endLine = line;
|
|---|
| 120 | endColumn = next - offset - 1;
|
|---|
| 121 | end = next;
|
|---|
| 122 | break;
|
|---|
| 123 | case t.plus:
|
|---|
| 124 | case t.greaterThan:
|
|---|
| 125 | case t.tilde:
|
|---|
| 126 | case t.pipe:
|
|---|
| 127 | next = start;
|
|---|
| 128 | do {
|
|---|
| 129 | next += 1;
|
|---|
| 130 | code = css.charCodeAt(next);
|
|---|
| 131 | } while (code === t.plus || code === t.greaterThan || code === t.tilde || code === t.pipe);
|
|---|
| 132 | tokenType = t.combinator;
|
|---|
| 133 | endLine = line;
|
|---|
| 134 | endColumn = start - offset;
|
|---|
| 135 | end = next;
|
|---|
| 136 | break;
|
|---|
| 137 |
|
|---|
| 138 | // Consume these characters as single tokens.
|
|---|
| 139 | case t.asterisk:
|
|---|
| 140 | case t.ampersand:
|
|---|
| 141 | case t.bang:
|
|---|
| 142 | case t.comma:
|
|---|
| 143 | case t.equals:
|
|---|
| 144 | case t.dollar:
|
|---|
| 145 | case t.caret:
|
|---|
| 146 | case t.openSquare:
|
|---|
| 147 | case t.closeSquare:
|
|---|
| 148 | case t.colon:
|
|---|
| 149 | case t.semicolon:
|
|---|
| 150 | case t.openParenthesis:
|
|---|
| 151 | case t.closeParenthesis:
|
|---|
| 152 | next = start;
|
|---|
| 153 | tokenType = code;
|
|---|
| 154 | endLine = line;
|
|---|
| 155 | endColumn = start - offset;
|
|---|
| 156 | end = next + 1;
|
|---|
| 157 | break;
|
|---|
| 158 | case t.singleQuote:
|
|---|
| 159 | case t.doubleQuote:
|
|---|
| 160 | quote = code === t.singleQuote ? "'" : '"';
|
|---|
| 161 | next = start;
|
|---|
| 162 | do {
|
|---|
| 163 | escaped = false;
|
|---|
| 164 | next = css.indexOf(quote, next + 1);
|
|---|
| 165 | if (next === -1) {
|
|---|
| 166 | unclosed('quote', quote);
|
|---|
| 167 | }
|
|---|
| 168 | escapePos = next;
|
|---|
| 169 | while (css.charCodeAt(escapePos - 1) === t.backslash) {
|
|---|
| 170 | escapePos -= 1;
|
|---|
| 171 | escaped = !escaped;
|
|---|
| 172 | }
|
|---|
| 173 | } while (escaped);
|
|---|
| 174 | tokenType = t.str;
|
|---|
| 175 | endLine = line;
|
|---|
| 176 | endColumn = start - offset;
|
|---|
| 177 | end = next + 1;
|
|---|
| 178 | break;
|
|---|
| 179 | default:
|
|---|
| 180 | if (code === t.slash && css.charCodeAt(start + 1) === t.asterisk) {
|
|---|
| 181 | next = css.indexOf('*/', start + 2) + 1;
|
|---|
| 182 | if (next === 0) {
|
|---|
| 183 | unclosed('comment', '*/');
|
|---|
| 184 | }
|
|---|
| 185 | content = css.slice(start, next + 1);
|
|---|
| 186 | lines = content.split('\n');
|
|---|
| 187 | last = lines.length - 1;
|
|---|
| 188 | if (last > 0) {
|
|---|
| 189 | nextLine = line + last;
|
|---|
| 190 | nextOffset = next - lines[last].length;
|
|---|
| 191 | } else {
|
|---|
| 192 | nextLine = line;
|
|---|
| 193 | nextOffset = offset;
|
|---|
| 194 | }
|
|---|
| 195 | tokenType = t.comment;
|
|---|
| 196 | line = nextLine;
|
|---|
| 197 | endLine = nextLine;
|
|---|
| 198 | endColumn = next - nextOffset;
|
|---|
| 199 | } else if (code === t.slash) {
|
|---|
| 200 | next = start;
|
|---|
| 201 | tokenType = code;
|
|---|
| 202 | endLine = line;
|
|---|
| 203 | endColumn = start - offset;
|
|---|
| 204 | end = next + 1;
|
|---|
| 205 | } else {
|
|---|
| 206 | next = consumeWord(css, start);
|
|---|
| 207 | tokenType = t.word;
|
|---|
| 208 | endLine = line;
|
|---|
| 209 | endColumn = next - offset;
|
|---|
| 210 | }
|
|---|
| 211 | end = next + 1;
|
|---|
| 212 | break;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // Ensure that the token structure remains consistent
|
|---|
| 216 | tokens.push([tokenType,
|
|---|
| 217 | // [0] Token type
|
|---|
| 218 | line,
|
|---|
| 219 | // [1] Starting line
|
|---|
| 220 | start - offset,
|
|---|
| 221 | // [2] Starting column
|
|---|
| 222 | endLine,
|
|---|
| 223 | // [3] Ending line
|
|---|
| 224 | endColumn,
|
|---|
| 225 | // [4] Ending column
|
|---|
| 226 | start,
|
|---|
| 227 | // [5] Start position / Source index
|
|---|
| 228 | end // [6] End position
|
|---|
| 229 | ]);
|
|---|
| 230 |
|
|---|
| 231 | // Reset offset for the next token
|
|---|
| 232 | if (nextOffset) {
|
|---|
| 233 | offset = nextOffset;
|
|---|
| 234 | nextOffset = null;
|
|---|
| 235 | }
|
|---|
| 236 | start = end;
|
|---|
| 237 | }
|
|---|
| 238 | return tokens;
|
|---|
| 239 | } |
|---|