UNPKG

2.05 kBJavaScriptView Raw
1/* eslint-disable no-param-reassign */
2
3const tokenizer = require('postcss/lib/tokenize');
4const Input = require('postcss/lib/input');
5
6module.exports = {
7 isInlineComment(token) {
8 if (token[0] === 'word' && token[1].slice(0, 2) === '//') {
9 const first = token;
10 const bits = [];
11 let last;
12 let remainingInput;
13
14 while (token) {
15 if (/\r?\n/.test(token[1])) {
16 // If there are quotes, fix tokenizer creating one token from start quote to end quote
17 if (/['"].*\r?\n/.test(token[1])) {
18 // Add string before newline to inline comment token
19 bits.push(token[1].substring(0, token[1].indexOf('\n')));
20
21 // Get remaining input and retokenize
22 remainingInput = token[1].substring(token[1].indexOf('\n'));
23 remainingInput += this.input.css.valueOf().substring(this.tokenizer.position());
24 } else {
25 // If the tokenizer went to the next line go back
26 this.tokenizer.back(token);
27 }
28 break;
29 }
30
31 bits.push(token[1]);
32 last = token;
33 token = this.tokenizer.nextToken({ ignoreUnclosed: true });
34 }
35
36 const newToken = ['comment', bits.join(''), first[2], last[2]];
37 this.inlineComment(newToken);
38
39 // Replace tokenizer to retokenize the rest of the string
40 // we need replace it after we added new token with inline comment because token position is calculated for old input (#145)
41 if (remainingInput) {
42 this.input = new Input(remainingInput);
43 this.tokenizer = tokenizer(this.input);
44 }
45
46 return true;
47 } else if (token[1] === '/') {
48 // issue #135
49 const next = this.tokenizer.nextToken({ ignoreUnclosed: true });
50
51 if (next[0] === 'comment' && /^\/\*/.test(next[1])) {
52 next[0] = 'word';
53 next[1] = next[1].slice(1);
54 token[1] = '//';
55 this.tokenizer.back(next);
56 return module.exports.isInlineComment.bind(this)(token);
57 }
58 }
59
60 return false;
61 }
62};