Skip to content

Commit 238c435

Browse files
committed
Added sourceIndex to word node
1 parent b434edd commit 238c435

File tree

2 files changed

+58
-57
lines changed

2 files changed

+58
-57
lines changed

lib/parse.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = function (input) {
4141
} else if (code === slash || code === comma || code === colon) {
4242
before = token;
4343
} else {
44-
tokens.push({ type: 'space', value: token, sourceIndex: pos });
44+
tokens.push({ type: 'space', sourceIndex: pos, value: token });
4545
}
4646

4747
pos = next;
@@ -114,10 +114,10 @@ module.exports = function (input) {
114114
whitespacePos -= 1;
115115
code = value.charCodeAt(whitespacePos);
116116
} while (code <= 32);
117-
token.nodes = [{ type: 'word', value: value.slice(pos, whitespacePos + 1) }];
117+
token.nodes = [{ type: 'word', sourceIndex: pos, value: value.slice(pos, whitespacePos + 1) }];
118118
if (token.unclosed && whitespacePos + 1 !== next) {
119119
token.after = '';
120-
token.nodes.push({ type: 'space', value: value.slice(whitespacePos + 1, next), sourceIndex: whitespacePos + 1 });
120+
token.nodes.push({ type: 'space', sourceIndex: whitespacePos + 1, value: value.slice(whitespacePos + 1, next) });
121121
} else {
122122
token.after = value.slice(whitespacePos + 1, next);
123123
}
@@ -162,13 +162,14 @@ module.exports = function (input) {
162162
code === openParentheses || code === closeParentheses && balanced
163163
));
164164
token = value.slice(pos, next);
165-
pos = next;
166165

167166
if (openParentheses === code) {
168167
name = token;
169168
} else {
170-
tokens.push({ type: 'word', value: token });
169+
tokens.push({ type: 'word', sourceIndex: pos, value: token });
171170
}
171+
172+
pos = next;
172173
}
173174
}
174175

test/parse.js

+52-52
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,35 @@ var tests = [{
99
message: 'should process escaped parentheses (open)',
1010
fixture: '\\(',
1111
expected: [
12-
{ type: 'word', value: '\\(' }
12+
{ type: 'word', sourceIndex: 0, value: '\\(' }
1313
]
1414
}, {
1515
message: 'should process escaped parentheses (close)',
1616
fixture: '\\)',
1717
expected: [
18-
{ type: 'word', value: '\\)' }
18+
{ type: 'word', sourceIndex: 0, value: '\\)' }
1919
]
2020
}, {
2121
message: 'should process escaped parentheses (both)',
2222
fixture: '\\(\\)',
2323
expected: [
24-
{ type: 'word', value: '\\(\\)' }
24+
{ type: 'word', sourceIndex: 0, value: '\\(\\)' }
2525
]
2626
}, {
2727
message: 'should process escaped parentheses (both)',
2828
fixture: '\\( \\)',
2929
expected: [
30-
{ type: 'word', value: '\\(' },
30+
{ type: 'word', sourceIndex: 0, value: '\\(' },
3131
{ type: 'space', sourceIndex: 2, value: ' ' },
32-
{ type: 'word', value: '\\)' }
32+
{ type: 'word', sourceIndex: 3, value: '\\)' }
3333
]
3434
}, {
3535
message: 'should process unopened parentheses as word',
3636
fixture: '() )wo)rd)',
3737
expected: [
3838
{ type: 'function', value: '', before: '', after: '', nodes: [] },
3939
{ type: 'space', sourceIndex: 2, value: ' ' },
40-
{ type: 'word', value: ')wo)rd)' }
40+
{ type: 'word', sourceIndex: 3, value: ')wo)rd)' }
4141
]
4242
}, {
4343
message: 'should add before prop',
@@ -50,7 +50,7 @@ var tests = [{
5050
fixture: '( | )',
5151
expected: [
5252
{ type: 'function', value: '', before: ' ', after: ' ', nodes: [
53-
{ type: 'word', value: '|' }
53+
{ type: 'word', sourceIndex: 2, value: '|' }
5454
] }
5555
]
5656
}, {
@@ -78,7 +78,7 @@ var tests = [{
7878
{ type: 'function', value: '', before: ' ', after: '', nodes: [] }
7979
] }
8080
] },
81-
{ type: 'word', value: 'word' }
81+
{ type: 'word', sourceIndex: 13, value: 'word' }
8282
]
8383
}, {
8484
message: 'should process divider (/)',
@@ -177,9 +177,9 @@ var tests = [{
177177
message: 'should process quoted strings and words',
178178
fixture: 'word1"string"word2',
179179
expected: [
180-
{ type: 'word', value: 'word1' },
180+
{ type: 'word', sourceIndex: 0, value: 'word1' },
181181
{ type: 'string', sourceIndex: 5, value: 'string', quote: '"' },
182-
{ type: 'word', value: 'word2' }
182+
{ type: 'word', sourceIndex: 13, value: 'word2' }
183183
]
184184
}, {
185185
message: 'should process quoted strings and spaces',
@@ -194,55 +194,55 @@ var tests = [{
194194
fixture: ' \\"word\\\'\\ \\\t ',
195195
expected: [
196196
{ type: 'space', sourceIndex: 0, value: ' ' },
197-
{ type: 'word', value: '\\"word\\\'\\ \\\t'},
197+
{ type: 'word', sourceIndex: 1, value: '\\"word\\\'\\ \\\t'},
198198
{ type: 'space', sourceIndex: 13, value: ' ' }
199199
]
200200
}, {
201201
message: 'should correctly proceess font value',
202202
fixture: 'bold italic 12px \t /3 \'Open Sans\', Arial, "Helvetica Neue", sans-serif',
203203
expected: [
204-
{ type: 'word', value: 'bold' },
204+
{ type: 'word', sourceIndex: 0, value: 'bold' },
205205
{ type: 'space', sourceIndex: 4, value: ' ' },
206-
{ type: 'word', value: 'italic' },
206+
{ type: 'word', sourceIndex: 5, value: 'italic' },
207207
{ type: 'space', sourceIndex: 11, value: ' ' },
208-
{ type: 'word', value: '12px' },
208+
{ type: 'word', sourceIndex: 12, value: '12px' },
209209
{ type: 'div', value: '/' , before: ' \t ', after: '' },
210-
{ type: 'word', value: '3' },
210+
{ type: 'word', sourceIndex: 20, value: '3' },
211211
{ type: 'space', sourceIndex: 21, value: ' ' },
212212
{ type: 'string', sourceIndex: 22, value: 'Open Sans', quote: '\'' },
213213
{ type: 'div', value: ',', before: '', after: ' ' },
214-
{ type: 'word', value: 'Arial' },
214+
{ type: 'word', sourceIndex: 35, value: 'Arial' },
215215
{ type: 'div', value: ',', before: '', after: ' ' },
216216
{ type: 'string', sourceIndex: 42, value: 'Helvetica Neue', quote: '"' },
217217
{ type: 'div', value: ',', before: '', after: ' ' },
218-
{ type: 'word', value: 'sans-serif' },
218+
{ type: 'word', sourceIndex: 60, value: 'sans-serif' },
219219
]
220220
}, {
221221
message: 'should correctly proceess color value',
222222
fixture: 'rgba( 29, 439 , 29 )',
223223
expected: [
224224
{ type: 'function', value: 'rgba', before: ' ', after: ' ', nodes: [
225-
{ type: 'word', value: '29' },
226-
{ type: 'div', value: ',', before: '', after: ' ' },
227-
{ type: 'word', value: '439' },
228-
{ type: 'div', value: ',', before: ' ', after: ' ' },
229-
{ type: 'word', value: '29' },
225+
{ type: 'word', sourceIndex: 6, value: '29' },
226+
{ type: 'div', value: ',', before: '', after: ' ' },
227+
{ type: 'word', sourceIndex: 10, value: '439' },
228+
{ type: 'div', value: ',', before: ' ', after: ' ' },
229+
{ type: 'word', sourceIndex: 16, value: '29' },
230230
] }
231231
]
232232
}, {
233233
message: 'should correctly process url function',
234234
fixture: 'url( /gfx/img/bg.jpg )',
235235
expected: [
236236
{ type: 'function', value: 'url', before: ' ', after: ' ', nodes: [
237-
{ type: 'word', value: '/gfx/img/bg.jpg' }
237+
{ type: 'word', sourceIndex: 5, value: '/gfx/img/bg.jpg' }
238238
] }
239239
]
240240
}, {
241241
message: 'should add unclosed: true prop for url function',
242242
fixture: 'url( /gfx/img/bg.jpg ',
243243
expected: [
244244
{ type: 'function', value: 'url', before: ' ', after: '', unclosed: true, nodes: [
245-
{ type: 'word', value: '/gfx/img/bg.jpg' },
245+
{ type: 'word', sourceIndex: 5, value: '/gfx/img/bg.jpg' },
246246
{ type: 'space', sourceIndex: 20, value: ' ' }
247247
] }
248248
]
@@ -253,7 +253,7 @@ var tests = [{
253253
{ type: 'function', value: 'url', before: ' ', after: ' ', nodes: [
254254
{ type: 'string', sourceIndex: 5, quote: '"', value: '/gfx/img/bg.jpg' },
255255
{ type: 'space', sourceIndex: 22, value: ' ' },
256-
{ type: 'word', value: 'hello' }
256+
{ type: 'word', sourceIndex: 23, value: 'hello' }
257257
] }
258258
]
259259
}, {
@@ -263,45 +263,45 @@ var tests = [{
263263
{type: 'function', value: 'calc', before: '', after: '', nodes: [
264264
{ type: 'function', value: '', before: '', after: '', nodes: [
265265
{ type: 'function', value: '', before: '', after: '', nodes: [
266-
{ type: 'word', value: '768px' },
266+
{ type: 'word', sourceIndex: 7, value: '768px' },
267267
{ type: 'space', sourceIndex: 12, value: ' ' },
268-
{ type: 'word', value: '-' },
268+
{ type: 'word', sourceIndex: 13, value: '-' },
269269
{ type: 'space', sourceIndex: 14, value: ' ' },
270-
{ type: 'word', value: '100vw' }
270+
{ type: 'word', sourceIndex: 15, value: '100vw' }
271271
] },
272272
{ type: 'div', value: '/', before: ' ', after: ' ' },
273-
{ type: 'word', value: '2' }
273+
{ type: 'word', sourceIndex: 24, value: '2' }
274274
] },
275275
{ type: 'space', sourceIndex: 26, value: ' ' },
276-
{ type: 'word', value: '-' },
276+
{ type: 'word', sourceIndex: 27, value: '-' },
277277
{ type: 'space', sourceIndex: 28, value: ' ' },
278-
{ type: 'word', value: '15px' }
278+
{ type: 'word', sourceIndex: 29, value: '15px' }
279279
] }
280280
]
281281
}, {
282282
message: 'should process colons with params',
283283
fixture: '(min-width: 700px) and (orientation: \\$landscape)',
284284
expected: [
285285
{ type: 'function', value: '', before: '', after: '', nodes: [
286-
{ type: 'word', value: 'min-width' },
286+
{ type: 'word', sourceIndex: 1, value: 'min-width' },
287287
{ type: 'div', value: ':', before: '', after: ' ' },
288-
{ type: 'word', value: '700px' }
288+
{ type: 'word', sourceIndex: 12, value: '700px' }
289289
] },
290290
{ type: 'space', sourceIndex: 18, value: ' ' },
291-
{ type: 'word', value: 'and' },
291+
{ type: 'word', sourceIndex: 19, value: 'and' },
292292
{ type: 'space', sourceIndex: 22, value: ' ' },
293293
{ type: 'function', value: '', before: '', after: '', nodes: [
294-
{ type: 'word', value: 'orientation' },
294+
{ type: 'word', sourceIndex: 24, value: 'orientation' },
295295
{ type: 'div', value: ':', before: '', after: ' ' },
296-
{ type: 'word', value: '\\$landscape' }
296+
{ type: 'word', sourceIndex: 37, value: '\\$landscape' }
297297
] }
298298
]
299299
}, {
300300
message: 'should escape parentheses with backslash',
301301
fixture: 'url( http://website.com/assets\\)_test )',
302302
expected: [
303303
{ type: 'function', value: 'url', before: ' ', after: ' ', nodes: [
304-
{ type: 'word', value: 'http://website.com/assets\\)_test' }
304+
{ type: 'word', sourceIndex: 5, value: 'http://website.com/assets\\)_test' }
305305
] }
306306
]
307307
}, {
@@ -310,34 +310,34 @@ var tests = [{
310310
expected: [
311311
{ type: 'function', value: 'fn1', before: '', after: '', nodes: [
312312
{ type: 'function', value: 'fn2', before: '', after: '', nodes: [
313-
{ type: 'word', value: '255' }
313+
{ type: 'word', sourceIndex: 8, value: '255' }
314314
] },
315315
{ type: 'div', value: ',', before: '', after: ' ' },
316316
{ type: 'function', value: 'fn3', before: '', after: '', nodes: [
317-
{ type: 'word', value: '.2' }
317+
{ type: 'word', sourceIndex: 18, value: '.2' }
318318
] },
319319
] },
320320
{ type: 'div', value: ',', before: '', after: ' ' },
321321
{ type: 'function', value: 'fn4', before: '', after: '', nodes: [
322322
{ type: 'function', value: 'fn5', before: '', after: '', nodes: [
323-
{ type: 'word', value: '255' },
323+
{ type: 'word', sourceIndex: 32, value: '255' },
324324
{ type: 'div', value: ',', before: '', after: '' },
325-
{ type: 'word', value: '.2' }
325+
{ type: 'word', sourceIndex: 36, value: '.2' }
326326
] },
327327
{ type: 'div', value: ',', before: '', after: ' '},
328-
{ type: 'word', value: 'fn6' }
328+
{ type: 'word', sourceIndex: 41, value: 'fn6' }
329329
] }
330330
]
331331
}, {
332332
message: 'shouldn\'t throw an error on unclosed function',
333333
fixture: '(0 32 word ',
334334
expected: [
335335
{ type: 'function', value: '', before: '', after: '', unclosed: true, nodes: [
336-
{ type: 'word', value: '0' },
336+
{ type: 'word', sourceIndex: 1, value: '0' },
337337
{ type: 'space', sourceIndex: 2, value: ' ' },
338-
{ type: 'word', value: '32' },
338+
{ type: 'word', sourceIndex: 3, value: '32' },
339339
{ type: 'space', sourceIndex: 5, value: ' ' },
340-
{ type: 'word', value: 'word' },
340+
{ type: 'word', sourceIndex: 6, value: 'word' },
341341
{ type: 'space', sourceIndex: 10, value: ' ' }
342342
] }
343343
]
@@ -356,31 +356,31 @@ var tests = [{
356356
message: 'shouldn\'t throw an error on unopened function',
357357
fixture: '0 32 word ) ',
358358
expected: [
359-
{ type: 'word', value: '0' },
359+
{ type: 'word', sourceIndex: 0, value: '0' },
360360
{ type: 'space', sourceIndex: 1, value: ' ' },
361-
{ type: 'word', value: '32' },
361+
{ type: 'word', sourceIndex: 2, value: '32' },
362362
{ type: 'space', sourceIndex: 4, value: ' ' },
363-
{ type: 'word', value: 'word' },
363+
{ type: 'word', sourceIndex: 5, value: 'word' },
364364
{ type: 'space', sourceIndex: 9, value: ' ' },
365-
{ type: 'word', value: ')' },
365+
{ type: 'word', sourceIndex: 10, value: ')' },
366366
{ type: 'space', sourceIndex: 11, value: ' ' }
367367
]
368368
}, {
369369
message: 'should process escaped spaces as word in fonts',
370370
fixture: 'Bond\\ 007',
371371
expected: [
372-
{ type: 'word', value: 'Bond\\ 007' }
372+
{ type: 'word', sourceIndex: 0, value: 'Bond\\ 007' }
373373
]
374374
}, {
375375
message: 'should parse double url and comma',
376376
fixture: 'url(foo/bar.jpg), url(http://website.com/img.jpg)',
377377
expected: [
378378
{ type: 'function', value: 'url', before: '', after: '', nodes: [
379-
{ type: 'word', value: 'foo/bar.jpg' }
379+
{ type: 'word', sourceIndex: 4, value: 'foo/bar.jpg' }
380380
] },
381381
{ type: 'div', value: ',', before: '', after: ' ' },
382382
{ type: 'function', value: 'url', before: '', after: '', nodes: [
383-
{ type: 'word', value: 'http://website.com/img.jpg' }
383+
{ type: 'word', sourceIndex: 22, value: 'http://website.com/img.jpg' }
384384
] },
385385
]
386386
}];

0 commit comments

Comments
 (0)