Skip to content

Handle spaces between variable name and colon #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/tokenizer/tokenize-at-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ export default function tokenizeAtRule (state) {
else {
atEndPattern.lastIndex = state.pos + 1;
atEndPattern.test(state.css);
const spaceColonPos = state.css.indexOf(' :', state.pos);

if (atEndPattern.lastIndex === 0) {
state.nextPos = state.css.length - 1;
}
// for cases where there is extra whitespace between a variable name and ":" (#92)
else if (spaceColonPos > state.pos && state.css.slice(state.pos, state.pos + 5) !== "@page") {
state.nextPos = spaceColonPos + 1;
}
else {
state.nextPos = atEndPattern.lastIndex - 2;
}
Expand Down
25 changes: 25 additions & 0 deletions test/parser/variables.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ describe('Parser', () => {
expect(root.first.value).to.eql('42');
});

it('parses variables with no whitespace between ":" and value', () => {
const root = parse('@var :42;');

expect(root.first.prop).to.eql('@var');
expect(root.first.value).to.eql('42');
});

it('parses mutliple variables with whitespaces between name and ":"', () => {
const root = parse('@foo : 42; @bar : 35;');

expect(root.first.prop).to.eql('@foo');
expect(root.first.value).to.eql('42');
expect(root.nodes[1].prop).to.eql('@bar');
expect(root.nodes[1].value).to.eql('35');
});

it('parses multiple variables with no whitespace between ":" and value', () => {
const root = parse('@foo :42; @bar :35');

expect(root.first.prop).to.eql('@foo');
expect(root.first.value).to.eql('42');
expect(root.nodes[1].prop).to.eql('@bar');
expect(root.nodes[1].value).to.eql('35');
});

it('parses string variables', () => {
const root = parse('@var: "test";');

Expand Down
19 changes: 19 additions & 0 deletions test/tokenize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,24 @@ describe('#tokenize()', () => {
[')', ')', 1, 7]
]);
});

it('tokenizes @page with extra whitespace', () => {
testTokens('@page :left { margin: 0; }', [
[ 'at-word', '@page', 1, 1, 1, 5 ],
[ 'space', ' ' ],
[ ':', ':', 1, 9 ],
[ 'word', 'left', 1, 10, 1, 13 ],
[ 'space', ' ' ],
[ '{', '{', 1, 15 ],
[ 'space', ' ' ],
[ 'word', 'margin', 1, 17, 1, 22 ],
[ ':', ':', 1, 23 ],
[ 'space', ' ' ],
[ 'word', '0', 1, 25, 1, 25 ],
[ ';', ';', 1, 26 ],
[ 'space', ' ' ],
[ '}', '}', 1, 28 ]
]);
});
});
});