Skip to content

Commit 943aff7

Browse files
davidyorrshellscape
authored andcommitted
Handle spaces between variable name and colon (#98)
* Handle spaces between variable name and colon Fixes #92 * Separate additional tests relating to #92
1 parent ca9e4c0 commit 943aff7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

lib/tokenizer/tokenize-at-rule.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ export default function tokenizeAtRule (state) {
3535
else {
3636
atEndPattern.lastIndex = state.pos + 1;
3737
atEndPattern.test(state.css);
38+
const spaceColonPos = state.css.indexOf(' :', state.pos);
3839

3940
if (atEndPattern.lastIndex === 0) {
4041
state.nextPos = state.css.length - 1;
4142
}
43+
// for cases where there is extra whitespace between a variable name and ":" (#92)
44+
else if (spaceColonPos > state.pos && state.css.slice(state.pos, state.pos + 5) !== "@page") {
45+
state.nextPos = spaceColonPos + 1;
46+
}
4247
else {
4348
state.nextPos = atEndPattern.lastIndex - 2;
4449
}

test/parser/variables.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ describe('Parser', () => {
2020
expect(root.first.value).to.eql('42');
2121
});
2222

23+
it('parses variables with no whitespace between ":" and value', () => {
24+
const root = parse('@var :42;');
25+
26+
expect(root.first.prop).to.eql('@var');
27+
expect(root.first.value).to.eql('42');
28+
});
29+
30+
it('parses mutliple variables with whitespaces between name and ":"', () => {
31+
const root = parse('@foo : 42; @bar : 35;');
32+
33+
expect(root.first.prop).to.eql('@foo');
34+
expect(root.first.value).to.eql('42');
35+
expect(root.nodes[1].prop).to.eql('@bar');
36+
expect(root.nodes[1].value).to.eql('35');
37+
});
38+
39+
it('parses multiple variables with no whitespace between ":" and value', () => {
40+
const root = parse('@foo :42; @bar :35');
41+
42+
expect(root.first.prop).to.eql('@foo');
43+
expect(root.first.value).to.eql('42');
44+
expect(root.nodes[1].prop).to.eql('@bar');
45+
expect(root.nodes[1].value).to.eql('35');
46+
});
47+
2348
it('parses string variables', () => {
2449
const root = parse('@var: "test";');
2550

test/tokenize.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,24 @@ describe('#tokenize()', () => {
169169
[')', ')', 1, 7]
170170
]);
171171
});
172+
173+
it('tokenizes @page with extra whitespace', () => {
174+
testTokens('@page :left { margin: 0; }', [
175+
[ 'at-word', '@page', 1, 1, 1, 5 ],
176+
[ 'space', ' ' ],
177+
[ ':', ':', 1, 9 ],
178+
[ 'word', 'left', 1, 10, 1, 13 ],
179+
[ 'space', ' ' ],
180+
[ '{', '{', 1, 15 ],
181+
[ 'space', ' ' ],
182+
[ 'word', 'margin', 1, 17, 1, 22 ],
183+
[ ':', ':', 1, 23 ],
184+
[ 'space', ' ' ],
185+
[ 'word', '0', 1, 25, 1, 25 ],
186+
[ ';', ';', 1, 26 ],
187+
[ 'space', ' ' ],
188+
[ '}', '}', 1, 28 ]
189+
]);
190+
});
172191
});
173192
});

0 commit comments

Comments
 (0)