| 1 |
const SINGLE_QUOTE = "'".charCodeAt(0) |
| 2 |
const DOUBLE_QUOTE = '"'.charCodeAt(0) |
| 3 |
const BACKSLASH = '\\'.charCodeAt(0) |
| 4 |
const SLASH = '/'.charCodeAt(0) |
| 5 |
const NEWLINE = '\n'.charCodeAt(0) |
| 6 |
const SPACE = ' '.charCodeAt(0) |
| 7 |
const FEED = '\f'.charCodeAt(0) |
| 8 |
const TAB = '\t'.charCodeAt(0) |
| 9 |
const CR = '\r'.charCodeAt(0) |
| 10 |
const OPEN_PARENTHESES = '('.charCodeAt(0) |
| 11 |
const CLOSE_PARENTHESES = ')'.charCodeAt(0) |
| 12 |
const OPEN_CURLY = '{'.charCodeAt(0) |
| 13 |
const CLOSE_CURLY = '}'.charCodeAt(0) |
| 14 |
const SEMICOLON = ';'.charCodeAt(0) |
| 15 |
const ASTERICK = '*'.charCodeAt(0) |
| 16 |
const COLON = ':'.charCodeAt(0) |
| 17 |
const AT = '@'.charCodeAt(0) |
| 18 |
const COMMA = ','.charCodeAt(0) |
| 19 |
|
| 20 |
const RE_AT_END = /[\t\n\f\r "'()/;\\{]/g |
| 21 |
const RE_NEW_LINE = /[\n\f\r]/g |
| 22 |
const RE_WORD_END = /[\t\n\f\r !"'(),:;@\\{}]|\/(?=\*)/g |
| 23 |
const RE_BAD_BRACKET = /.[\n"'(/\\]/ |
| 24 |
|
| 25 |
module.exports = function tokenize(input) { |
| 26 |
let tokens = [] |
| 27 |
let css = input.css.valueOf() |
| 28 |
|
| 29 |
let code, content, escape, escaped, escapePos, n, next, prev, quote |
| 30 |
|
| 31 |
let length = css.length |
| 32 |
let offset = 0 |
| 33 |
let pos = 0 |
| 34 |
|
| 35 |
function unclosed(what) { |
| 36 |
throw input.error('Unclosed ' + what, pos) |
| 37 |
} |
| 38 |
|
| 39 |
while (pos < length) { |
| 40 |
code = css.charCodeAt(pos) |
| 41 |
|
| 42 |
switch (code) { |
| 43 |
case CR: |
| 44 |
if (css.charCodeAt(pos + 1) === NEWLINE) { |
| 45 |
tokens.push(['newline', '\r\n', offset, offset + 2]) |
| 46 |
offset += 2 |
| 47 |
pos += 1 |
| 48 |
} else { |
| 49 |
tokens.push(['newline', '\r', offset, offset + 1]) |
| 50 |
offset++ |
| 51 |
} |
| 52 |
break |
| 53 |
|
| 54 |
case NEWLINE: |
| 55 |
case FEED: |
| 56 |
tokens.push(['newline', css.slice(pos, pos + 1), offset, offset + 1]) |
| 57 |
offset++ |
| 58 |
break |
| 59 |
|
| 60 |
case SPACE: |
| 61 |
case TAB: |
| 62 |
next = pos |
| 63 |
do { |
| 64 |
next += 1 |
| 65 |
code = css.charCodeAt(next) |
| 66 |
} while (code === SPACE || code === TAB) |
| 67 |
|
| 68 |
tokens.push([ |
| 69 |
'space', |
| 70 |
css.slice(pos, next), |
| 71 |
offset, |
| 72 |
offset + (next - pos) |
| 73 |
]) |
| 74 |
offset += next - pos |
| 75 |
pos = next - 1 |
| 76 |
break |
| 77 |
|
| 78 |
case OPEN_CURLY: |
| 79 |
tokens.push(['{', '{', offset, offset + 1]) |
| 80 |
offset++ |
| 81 |
break |
| 82 |
|
| 83 |
case CLOSE_CURLY: |
| 84 |
tokens.push(['}', '}', offset, offset + 1]) |
| 85 |
offset++ |
| 86 |
break |
| 87 |
|
| 88 |
case COLON: |
| 89 |
tokens.push([':', ':', offset, offset + 1]) |
| 90 |
offset++ |
| 91 |
break |
| 92 |
|
| 93 |
case SEMICOLON: |
| 94 |
tokens.push([';', ';', offset, offset + 1]) |
| 95 |
offset++ |
| 96 |
break |
| 97 |
|
| 98 |
case COMMA: |
| 99 |
tokens.push([',', ',', offset, offset + 1]) |
| 100 |
offset++ |
| 101 |
break |
| 102 |
|
| 103 |
case OPEN_PARENTHESES: |
| 104 |
prev = tokens.length ? tokens[tokens.length - 1][1] : '' |
| 105 |
n = css.charCodeAt(pos + 1) |
| 106 |
if ( |
| 107 |
prev === 'url' && |
| 108 |
n !== SINGLE_QUOTE && |
| 109 |
n !== DOUBLE_QUOTE && |
| 110 |
n !== SPACE && |
| 111 |
n !== NEWLINE && |
| 112 |
n !== TAB && |
| 113 |
n !== FEED && |
| 114 |
n !== CR |
| 115 |
) { |
| 116 |
next = pos |
| 117 |
do { |
| 118 |
escaped = false |
| 119 |
next = css.indexOf(')', next + 1) |
| 120 |
if (next === -1) unclosed('bracket') |
| 121 |
escapePos = next |
| 122 |
while (css.charCodeAt(escapePos - 1) === BACKSLASH) { |
| 123 |
escapePos -= 1 |
| 124 |
escaped = !escaped |
| 125 |
} |
| 126 |
} while (escaped) |
| 127 |
|
| 128 |
tokens.push(['brackets', css.slice(pos, next + 1), offset, next + 1]) |
| 129 |
offset = next + 1 |
| 130 |
pos = next |
| 131 |
} else { |
| 132 |
next = css.indexOf(')', pos + 1) |
| 133 |
content = css.slice(pos, next + 1) |
| 134 |
|
| 135 |
if (next === -1 || RE_BAD_BRACKET.test(content)) { |
| 136 |
tokens.push(['(', '(', offset, offset + 1]) |
| 137 |
offset++ |
| 138 |
} else { |
| 139 |
tokens.push(['brackets', content, offset, next + 1]) |
| 140 |
offset = next + 1 |
| 141 |
pos = next |
| 142 |
} |
| 143 |
} |
| 144 |
break |
| 145 |
|
| 146 |
case CLOSE_PARENTHESES: |
| 147 |
tokens.push([')', ')', offset, offset + 1]) |
| 148 |
offset++ |
| 149 |
break |
| 150 |
|
| 151 |
case SINGLE_QUOTE: |
| 152 |
case DOUBLE_QUOTE: |
| 153 |
quote = code === SINGLE_QUOTE ? "'" : '"' |
| 154 |
next = pos |
| 155 |
do { |
| 156 |
escaped = false |
| 157 |
next = css.indexOf(quote, next + 1) |
| 158 |
if (next === -1) unclosed('quote') |
| 159 |
escapePos = next |
| 160 |
while (css.charCodeAt(escapePos - 1) === BACKSLASH) { |
| 161 |
escapePos -= 1 |
| 162 |
escaped = !escaped |
| 163 |
} |
| 164 |
} while (escaped) |
| 165 |
|
| 166 |
content = css.slice(pos, next + 1) |
| 167 |
|
| 168 |
tokens.push(['string', content, offset, offset + content.length]) |
| 169 |
|
| 170 |
offset += content.length |
| 171 |
pos = next |
| 172 |
break |
| 173 |
|
| 174 |
case AT: |
| 175 |
RE_AT_END.lastIndex = pos + 1 |
| 176 |
RE_AT_END.test(css) |
| 177 |
if (RE_AT_END.lastIndex === 0) { |
| 178 |
next = css.length - 1 |
| 179 |
} else { |
| 180 |
next = RE_AT_END.lastIndex - 2 |
| 181 |
} |
| 182 |
|
| 183 |
tokens.push([ |
| 184 |
'at-word', |
| 185 |
css.slice(pos, next + 1), |
| 186 |
offset, |
| 187 |
offset + (next - pos + 1) |
| 188 |
]) |
| 189 |
|
| 190 |
offset += next - pos + 1 |
| 191 |
pos = next |
| 192 |
break |
| 193 |
|
| 194 |
case BACKSLASH: |
| 195 |
next = pos |
| 196 |
escape = true |
| 197 |
|
| 198 |
while (css.charCodeAt(next + 1) === BACKSLASH) { |
| 199 |
next += 1 |
| 200 |
escape = !escape |
| 201 |
} |
| 202 |
code = css.charCodeAt(next + 1) |
| 203 |
if (escape) { |
| 204 |
if (code === CR && css.charCodeAt(next + 2) === NEWLINE) { |
| 205 |
next += 2 |
| 206 |
} else if (code === CR || code === NEWLINE || code === FEED) { |
| 207 |
next += 1 |
| 208 |
} else { |
| 209 |
next += 1 |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
tokens.push([ |
| 214 |
'word', |
| 215 |
css.slice(pos, next + 1), |
| 216 |
offset, |
| 217 |
offset + (next - pos + 1) |
| 218 |
]) |
| 219 |
|
| 220 |
offset += next - pos + 1 |
| 221 |
pos = next |
| 222 |
break |
| 223 |
|
| 224 |
default: |
| 225 |
n = css.charCodeAt(pos + 1) |
| 226 |
|
| 227 |
if (code === SLASH && n === ASTERICK) { |
| 228 |
next = css.indexOf('*/', pos + 2) + 1 |
| 229 |
if (next === 0) unclosed('comment') |
| 230 |
|
| 231 |
content = css.slice(pos, next + 1) |
| 232 |
|
| 233 |
tokens.push(['comment', content, offset, offset + content.length]) |
| 234 |
|
| 235 |
offset += content.length |
| 236 |
pos = next |
| 237 |
} else if (code === SLASH && n === SLASH) { |
| 238 |
RE_NEW_LINE.lastIndex = pos + 1 |
| 239 |
RE_NEW_LINE.test(css) |
| 240 |
if (RE_NEW_LINE.lastIndex === 0) { |
| 241 |
next = css.length - 1 |
| 242 |
} else { |
| 243 |
next = RE_NEW_LINE.lastIndex - 2 |
| 244 |
} |
| 245 |
|
| 246 |
content = css.slice(pos, next + 1) |
| 247 |
|
| 248 |
tokens.push([ |
| 249 |
'comment', |
| 250 |
content, |
| 251 |
offset, |
| 252 |
offset + content.length, |
| 253 |
'inline' |
| 254 |
]) |
| 255 |
|
| 256 |
offset += content.length |
| 257 |
pos = next |
| 258 |
} else { |
| 259 |
RE_WORD_END.lastIndex = pos + 1 |
| 260 |
RE_WORD_END.test(css) |
| 261 |
if (RE_WORD_END.lastIndex === 0) { |
| 262 |
next = css.length - 1 |
| 263 |
} else { |
| 264 |
next = RE_WORD_END.lastIndex - 2 |
| 265 |
} |
| 266 |
|
| 267 |
content = css.slice(pos, next + 1) |
| 268 |
|
| 269 |
tokens.push(['word', content, offset, offset + content.length]) |
| 270 |
|
| 271 |
offset += content.length |
| 272 |
pos = next |
| 273 |
} |
| 274 |
|
| 275 |
break |
| 276 |
} |
| 277 |
|
| 278 |
pos++ |
| 279 |
} |
| 280 |
|
| 281 |
return tokens |
| 282 |
} |