Skip to content

Commit 85fee43

Browse files
committed
Add ending offset to tokens list, prepare to remove storing content.
1 parent 115e74d commit 85fee43

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/parser.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export default class Parser {
4747
this.css = typeof input.css === 'string' ? input.css : input.css.selector;
4848

4949
if (this.lossy) {
50-
this.tokens = tokenize({safe: input.safe, css: this.css.trim()});
50+
this.css = this.css.trim();
51+
this.tokens = tokenize({safe: input.safe, css: this.css});
5152
} else {
5253
this.tokens = tokenize(Object.assign({}, input, {css: this.css}));
5354
}
@@ -63,7 +64,7 @@ export default class Parser {
6364
this.position < this.tokens.length &&
6465
this.currToken[0] !== tokens.closeSquare
6566
) {
66-
str += this.currToken[1];
67+
str += this.content();
6768
this.position ++;
6869
}
6970
if (this.position === this.tokens.length && !~str.indexOf(']')) {
@@ -112,7 +113,7 @@ export default class Parser {
112113

113114
combinator () {
114115
const current = this.currToken;
115-
if (current[1] === '|') {
116+
if (this.content() === '|') {
116117
return this.namespace();
117118
}
118119
const node = new Combinator({
@@ -128,8 +129,9 @@ export default class Parser {
128129
while ( this.position < this.tokens.length && this.currToken &&
129130
(this.currToken[0] === tokens.space ||
130131
this.currToken[0] === tokens.combinator)) {
132+
const content = this.content();
131133
if (this.nextToken && this.nextToken[0] === tokens.combinator) {
132-
node.spaces.before = this.parseSpace(this.currToken[1]);
134+
node.spaces.before = this.parseSpace(content);
133135
node.source = getSource(
134136
this.nextToken[2],
135137
this.nextToken[3],
@@ -138,11 +140,11 @@ export default class Parser {
138140
);
139141
node.sourceIndex = this.nextToken[6];
140142
} else if (this.prevToken && this.prevToken[0] === tokens.combinator) {
141-
node.spaces.after = this.parseSpace(this.currToken[1]);
143+
node.spaces.after = this.parseSpace(content);
142144
} else if (this.currToken[0] === tokens.combinator) {
143-
node.value = this.currToken[1];
145+
node.value = content;
144146
} else if (this.currToken[0] === tokens.space) {
145-
node.value = this.parseSpace(this.currToken[1], ' ');
147+
node.value = this.parseSpace(content, ' ');
146148
}
147149
this.position ++;
148150
}
@@ -164,7 +166,7 @@ export default class Parser {
164166
comment () {
165167
const current = this.currToken;
166168
this.newNode(new Comment({
167-
value: current[1],
169+
value: this.content(),
168170
source: getSource(
169171
current[2],
170172
current[3],
@@ -199,7 +201,7 @@ export default class Parser {
199201
}
200202

201203
namespace () {
202-
const before = this.prevToken && this.prevToken[1] || true;
204+
const before = this.prevToken && this.content(this.prevToken) || true;
203205
if (this.nextToken[0] === tokens.word) {
204206
this.position ++;
205207
return this.word(before);
@@ -212,7 +214,7 @@ export default class Parser {
212214
nesting () {
213215
const current = this.currToken;
214216
this.newNode(new Nesting({
215-
value: current[1],
217+
value: this.content(),
216218
source: getSource(
217219
current[2],
218220
current[3],
@@ -273,7 +275,7 @@ export default class Parser {
273275
let pseudoStr = '';
274276
let startingToken = this.currToken;
275277
while (this.currToken && this.currToken[0] === tokens.colon) {
276-
pseudoStr += this.currToken[1];
278+
pseudoStr += this.content();
277279
this.position ++;
278280
}
279281
if (!this.currToken) {
@@ -312,21 +314,21 @@ export default class Parser {
312314
}
313315

314316
space () {
315-
const token = this.currToken;
317+
const content = this.content();
316318
// Handle space before and after the selector
317319
if (
318320
this.position === 0 ||
319321
this.prevToken[0] === tokens.comma ||
320322
this.prevToken[0] === tokens.openParenthesis
321323
) {
322-
this.spaces = this.parseSpace(token[1]);
324+
this.spaces = this.parseSpace(content);
323325
this.position ++;
324326
} else if (
325327
this.position === (this.tokens.length - 1) ||
326328
this.nextToken[0] === tokens.comma ||
327329
this.nextToken[0] === tokens.closeParenthesis
328330
) {
329-
this.current.last.spaces.after = this.parseSpace(token[1]);
331+
this.current.last.spaces.after = this.parseSpace(content);
330332
this.position ++;
331333
} else {
332334
this.combinator();
@@ -336,7 +338,7 @@ export default class Parser {
336338
string () {
337339
const current = this.currToken;
338340
this.newNode(new Str({
339-
value: current[1],
341+
value: this.content(),
340342
source: getSource(
341343
current[2],
342344
current[3],
@@ -350,13 +352,13 @@ export default class Parser {
350352

351353
universal (namespace) {
352354
const nextToken = this.nextToken;
353-
if (nextToken && nextToken[1] === '|') {
355+
if (nextToken && this.content(nextToken) === '|') {
354356
this.position ++;
355357
return this.namespace();
356358
}
357359
const current = this.currToken;
358360
this.newNode(new Universal({
359-
value: current[1],
361+
value: this.content(),
360362
source: getSource(
361363
current[2],
362364
current[3],
@@ -370,15 +372,15 @@ export default class Parser {
370372

371373
splitWord (namespace, firstCallback) {
372374
let nextToken = this.nextToken;
373-
let word = this.currToken[1];
375+
let word = this.content();
374376
while (nextToken && nextToken[0] === tokens.word) {
375377
this.position ++;
376-
let current = this.currToken[1];
378+
let current = this.content();
377379
word += current;
378380
if (current.lastIndexOf('\\') === current.length - 1) {
379381
let next = this.nextToken;
380382
if (next && next[0] === tokens.space) {
381-
word += this.parseSpace(next[1], ' ');
383+
word += this.parseSpace(this.content(next), ' ');
382384
this.position ++;
383385
}
384386
}
@@ -435,7 +437,7 @@ export default class Parser {
435437

436438
word (namespace) {
437439
const nextToken = this.nextToken;
438-
if (nextToken && nextToken[1] === '|') {
440+
if (nextToken && this.content(nextToken) === '|') {
439441
this.position ++;
440442
return this.namespace();
441443
}
@@ -526,15 +528,16 @@ export default class Parser {
526528
}
527529

528530
parseParenthesisToken (token) {
531+
const content = this.content(token);
529532
if (!this.lossy) {
530-
return token[1];
533+
return content;
531534
}
532535

533536
if (token[0] === tokens.space) {
534-
return this.parseSpace(token[1], ' ');
537+
return this.parseSpace(content, ' ');
535538
}
536539

537-
return this.parseValue(token[1]);
540+
return this.parseValue(content);
538541
}
539542

540543
newNode (node, namespace) {
@@ -548,6 +551,10 @@ export default class Parser {
548551
return this.current.append(node);
549552
}
550553

554+
content (token = this.currToken) {
555+
return this.css.slice(token[6], token[7]);
556+
}
557+
551558
get currToken () {
552559
return this.tokens[this.position];
553560
}

src/tokenize.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ export default function tokenize (input) {
205205
start - offset, // [3] Starting column
206206
endLine, // [4] Ending line
207207
endColumn, // [5] Ending column
208-
start, // [6] Source index
208+
start, // [6] Start position / Source index
209+
end, // [7] End position
209210
]);
210211

211212
// Reset offset for the next token

0 commit comments

Comments
 (0)