All files tokenize.js

88.14% Statements 156/177
80.49% Branches 66/82
50% Functions 1/2
89.66% Lines 156/174

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 3524x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x   4x 41x 41x                               41x 41x 41x 41x           41x 878x   878x         205x 205x     878x   5x 3x 3x 3x 3x   2x   5x       203x 203x       224x 224x 410x 410x     224x 224x 224x     2x 2x             93x 93x     4x 4x     15x 15x     17x 17x 17x                                                               17x 17x   17x 2x   15x               15x       17x     2x 2x       7x 7x 7x 7x 7x 7x 7x 7x           7x 7x 7x   7x       7x 7x     7x                 7x 7x 7x 7x     24x 24x 24x 2x   22x   24x               24x 24x     10x 10x   10x   10x 2x 2x   10x 10x 8x 1x 1x 1x 7x 3x 3x 3x   4x     10x               10x 4x 4x   10x 10x     272x   272x 8x 8x   8x 8x 8x   8x 2x 2x   6x 6x     8x                 8x 8x 8x 264x 20x 20x 20x 2x   18x     20x   20x                   20x   244x 244x 244x 8x   236x     244x               244x     272x     878x     41x    
const SINGLE_QUOTE = "'".charCodeAt(0)
const DOUBLE_QUOTE = '"'.charCodeAt(0)
const BACKSLASH = '\\'.charCodeAt(0)
const SLASH = '/'.charCodeAt(0)
const NEWLINE = '\n'.charCodeAt(0)
const SPACE = ' '.charCodeAt(0)
const FEED = '\f'.charCodeAt(0)
const TAB = '\t'.charCodeAt(0)
const CR = '\r'.charCodeAt(0)
const OPEN_PARENTHESES = '('.charCodeAt(0)
const CLOSE_PARENTHESES = ')'.charCodeAt(0)
const OPEN_CURLY = '{'.charCodeAt(0)
const CLOSE_CURLY = '}'.charCodeAt(0)
const SEMICOLON = ';'.charCodeAt(0)
const ASTERICK = '*'.charCodeAt(0)
const COLON = ':'.charCodeAt(0)
const AT = '@'.charCodeAt(0)
const COMMA = ','.charCodeAt(0)
 
const RE_AT_END = /[\t\n\f\r "'()/;\\{]/g
const RE_NEW_LINE = /[\n\f\r]/g
const RE_WORD_END = /[\t\n\f\r !"'(),:;@\\{}]|\/(?=\*)/g
const RE_BAD_BRACKET = /.[\n"'(/\\]/
 
module.exports = function tokenize(input) {
  let tokens = []
  let css = input.css.valueOf()
 
  let code,
    next,
    quote,
    lines,
    last,
    content,
    escape,
    nextLine,
    nextOffset,
    escaped,
    escapePos,
    prev,
    n
 
  let length = css.length
  let offset = -1
  let line = 1
  let 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)
        Iif (
          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)
          Iif (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
 
        Iif (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
          Iif (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
}