tokenize-string.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.default = tokenize;
  4. var _input = require('./input');
  5. var _input2 = _interopRequireDefault(_input);
  6. var _tokenizeString = require('./tokenize-string');
  7. var _tokenizeString2 = _interopRequireDefault(_tokenizeString);
  8. var _tokenizeInterpolant2 = require('./tokenize-interpolant');
  9. var _tokenizeInterpolant3 = _interopRequireDefault(_tokenizeInterpolant2);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. var singleQuote = "'".charCodeAt(0),
  12. doubleQuote = '"'.charCodeAt(0),
  13. newline = '\n'.charCodeAt(0),
  14. space = ' '.charCodeAt(0),
  15. feed = '\f'.charCodeAt(0),
  16. tab = '\t'.charCodeAt(0),
  17. cr = '\r'.charCodeAt(0),
  18. hash = '#'.charCodeAt(0),
  19. backslash = '\\'.charCodeAt(0),
  20. slash = '/'.charCodeAt(0),
  21. openCurly = '{'.charCodeAt(0),
  22. closeCurly = '}'.charCodeAt(0),
  23. interpolantEnd = /([.\s]*?)[^\\](?=(}))/gm,
  24. sQuoteEnd = /([.\s]*?)[^\\](?=((#{)|'))/gm,
  25. dQuoteEnd = /([.\s]*?)[^\\](?=((#{)|"))/gm;
  26. function tokenize(input, l, p, quote) {
  27. var tokens = [];
  28. var css = input.css.valueOf();
  29. var code = void 0,
  30. next = void 0,
  31. lines = void 0,
  32. last = void 0,
  33. content = void 0,
  34. escape = void 0,
  35. nextLine = void 0,
  36. nextOffset = void 0,
  37. escaped = void 0,
  38. escapePos = void 0,
  39. inInterpolant = void 0,
  40. inComment = void 0,
  41. inString = void 0;
  42. var length = css.length;
  43. var offset = -1;
  44. var line = l || 1;
  45. var pos = p || 0;
  46. var quoteEnd = quote === "'" ? sQuoteEnd : dQuoteEnd;
  47. var quoteChar = quote.charCodeAt(0);
  48. loop: while (pos < length) {
  49. code = css.charCodeAt(pos);
  50. if (code === newline) {
  51. offset = pos;
  52. line += 1;
  53. }
  54. switch (code) {
  55. case closeCurly:
  56. tokens.push(['endInterpolant', '}', line, pos - offset]);
  57. break;
  58. case quoteChar:
  59. tokens.push([quote, quote, line, pos - offset]);
  60. break loop;
  61. case backslash:
  62. next = pos;
  63. escape = true;
  64. while (css.charCodeAt(next + 1) === backslash) {
  65. next += 1;
  66. escape = !escape;
  67. }
  68. code = css.charCodeAt(next + 1);
  69. if (escape && code !== slash && code !== space && code !== newline && code !== tab && code !== cr && code !== feed) {
  70. next += 1;
  71. }
  72. tokens.push(['string', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
  73. pos = next;
  74. break;
  75. default:
  76. if (code === hash && css.charCodeAt(pos + 1) === openCurly) {
  77. tokens.push(['startInterpolant', '#{', line, pos + 1 - offset]);
  78. next = pos + 1;
  79. var _tokenizeInterpolant = (0, _tokenizeInterpolant3.default)(input, line, next + 1),
  80. t = _tokenizeInterpolant.tokens,
  81. _p = _tokenizeInterpolant.pos;
  82. tokens = tokens.concat(t);
  83. next = _p;
  84. pos = next;
  85. } else {
  86. quoteEnd.lastIndex = pos;
  87. quoteEnd.test(css);
  88. if (quoteEnd.lastIndex === 0) {
  89. next = css.length - 1;
  90. } else {
  91. next = quoteEnd.lastIndex - 1;
  92. }
  93. tokens.push(['string', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
  94. pos = next;
  95. }
  96. break;
  97. }
  98. pos++;
  99. }
  100. return { tokens: tokens, pos: pos };
  101. }