tokenize-string.js
3.79 KB
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
'use strict';
exports.__esModule = true;
exports.default = tokenize;
var _input = require('./input');
var _input2 = _interopRequireDefault(_input);
var _tokenizeString = require('./tokenize-string');
var _tokenizeString2 = _interopRequireDefault(_tokenizeString);
var _tokenizeInterpolant2 = require('./tokenize-interpolant');
var _tokenizeInterpolant3 = _interopRequireDefault(_tokenizeInterpolant2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var singleQuote = "'".charCodeAt(0),
doubleQuote = '"'.charCodeAt(0),
newline = '\n'.charCodeAt(0),
space = ' '.charCodeAt(0),
feed = '\f'.charCodeAt(0),
tab = '\t'.charCodeAt(0),
cr = '\r'.charCodeAt(0),
hash = '#'.charCodeAt(0),
backslash = '\\'.charCodeAt(0),
slash = '/'.charCodeAt(0),
openCurly = '{'.charCodeAt(0),
closeCurly = '}'.charCodeAt(0),
interpolantEnd = /([.\s]*?)[^\\](?=(}))/gm,
sQuoteEnd = /([.\s]*?)[^\\](?=((#{)|'))/gm,
dQuoteEnd = /([.\s]*?)[^\\](?=((#{)|"))/gm;
function tokenize(input, l, p, quote) {
var tokens = [];
var css = input.css.valueOf();
var code = void 0,
next = void 0,
lines = void 0,
last = void 0,
content = void 0,
escape = void 0,
nextLine = void 0,
nextOffset = void 0,
escaped = void 0,
escapePos = void 0,
inInterpolant = void 0,
inComment = void 0,
inString = void 0;
var length = css.length;
var offset = -1;
var line = l || 1;
var pos = p || 0;
var quoteEnd = quote === "'" ? sQuoteEnd : dQuoteEnd;
var quoteChar = quote.charCodeAt(0);
loop: while (pos < length) {
code = css.charCodeAt(pos);
if (code === newline) {
offset = pos;
line += 1;
}
switch (code) {
case closeCurly:
tokens.push(['endInterpolant', '}', line, pos - offset]);
break;
case quoteChar:
tokens.push([quote, quote, line, pos - offset]);
break loop;
case backslash:
next = pos;
escape = true;
while (css.charCodeAt(next + 1) === backslash) {
next += 1;
escape = !escape;
}
code = css.charCodeAt(next + 1);
if (escape && code !== slash && code !== space && code !== newline && code !== tab && code !== cr && code !== feed) {
next += 1;
}
tokens.push(['string', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
pos = next;
break;
default:
if (code === hash && css.charCodeAt(pos + 1) === openCurly) {
tokens.push(['startInterpolant', '#{', line, pos + 1 - offset]);
next = pos + 1;
var _tokenizeInterpolant = (0, _tokenizeInterpolant3.default)(input, line, next + 1),
t = _tokenizeInterpolant.tokens,
_p = _tokenizeInterpolant.pos;
tokens = tokens.concat(t);
next = _p;
pos = next;
} else {
quoteEnd.lastIndex = pos;
quoteEnd.test(css);
if (quoteEnd.lastIndex === 0) {
next = css.length - 1;
} else {
next = quoteEnd.lastIndex - 1;
}
tokens.push(['string', css.slice(pos, next + 1), line, pos - offset, line, next - offset]);
pos = next;
}
break;
}
pos++;
}
return { tokens: tokens, pos: pos };
}