tokenize-comment.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 newline = '\n'.charCodeAt(0),
  12. space = ' '.charCodeAt(0),
  13. feed = '\f'.charCodeAt(0),
  14. tab = '\t'.charCodeAt(0),
  15. cr = '\r'.charCodeAt(0),
  16. hash = '#'.charCodeAt(0),
  17. backslash = '\\'.charCodeAt(0),
  18. slash = '/'.charCodeAt(0),
  19. openCurly = '{'.charCodeAt(0),
  20. closeCurly = '}'.charCodeAt(0),
  21. asterisk = '*'.charCodeAt(0),
  22. wordEnd = /[ \n\t\r\(\)\{\},:;@!'"\\]|\*(?=\/)|#(?={)/g;
  23. function tokenize(input, l, p) {
  24. var tokens = [];
  25. var css = input.css.valueOf();
  26. var code = void 0,
  27. next = void 0,
  28. lines = void 0,
  29. last = void 0,
  30. content = void 0,
  31. escape = void 0,
  32. nextLine = void 0,
  33. nextOffset = void 0,
  34. escaped = void 0,
  35. escapePos = void 0,
  36. inInterpolant = void 0,
  37. inComment = void 0,
  38. inString = void 0;
  39. var length = css.length;
  40. var offset = -1;
  41. var line = l || 1;
  42. var pos = p || 0;
  43. loop: while (pos < length) {
  44. code = css.charCodeAt(pos);
  45. if (code === newline) {
  46. offset = pos;
  47. line += 1;
  48. }
  49. switch (code) {
  50. case space:
  51. case tab:
  52. case cr:
  53. case feed:
  54. next = pos;
  55. do {
  56. next += 1;
  57. code = css.charCodeAt(next);
  58. if (code === newline) {
  59. offset = next;
  60. line += 1;
  61. }
  62. } while (code === space || code === tab || code === cr || code === feed);
  63. tokens.push(['space', css.slice(pos, next)]);
  64. pos = next - 1;
  65. break;
  66. case newline:
  67. tokens.push(['newline', '\n', line, pos - offset]);
  68. break;
  69. case closeCurly:
  70. tokens.push(['endInterpolant', '}', line, pos - offset]);
  71. break;
  72. case backslash:
  73. next = pos;
  74. escape = true;
  75. while (css.charCodeAt(next + 1) === backslash) {
  76. next += 1;
  77. escape = !escape;
  78. }
  79. code = css.charCodeAt(next + 1);
  80. if (escape && code !== slash && code !== space && code !== newline && code !== tab && code !== cr && code !== feed) {
  81. next += 1;
  82. }
  83. tokens.push(['word', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
  84. pos = next;
  85. break;
  86. default:
  87. if (code === asterisk && css.charCodeAt(pos + 1) === slash) {
  88. next = pos;
  89. pos = next - 1;
  90. break loop;
  91. }
  92. if (code === hash && css.charCodeAt(pos + 1) === openCurly) {
  93. tokens.push(['startInterpolant', '#{', line, pos + 1 - offset]);
  94. next = pos + 1;
  95. var _tokenizeInterpolant = (0, _tokenizeInterpolant3.default)(input, line, next + 1),
  96. t = _tokenizeInterpolant.tokens,
  97. _p = _tokenizeInterpolant.pos;
  98. tokens = tokens.concat(t);
  99. next = _p;
  100. pos = next;
  101. break;
  102. }
  103. wordEnd.lastIndex = pos + 1;
  104. wordEnd.test(css);
  105. if (wordEnd.lastIndex === 0) {
  106. next = css.length - 1;
  107. } else {
  108. next = wordEnd.lastIndex - 2;
  109. }
  110. tokens.push(['word', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
  111. pos = next;
  112. break;
  113. }
  114. pos++;
  115. }
  116. return { tokens: tokens, line: line, pos: pos, offset: offset };
  117. }