From c9766b3607eac412f178578e02580ef09e5b69c3 Mon Sep 17 00:00:00 2001 From: xzyfer Date: Sun, 29 Jan 2017 18:20:33 +1100 Subject: [PATCH] Fix tokenization of unquoted imports Characters we're being swallowed after `/` which broken the cases like `@import foo/bar;`. The second `ident` `bar` would be tokenized as only `r`. --- lib/tokenize.js | 1 - test/fixture/double-quoted-import-path.scss | 1 + test/fixture/double-quoted-import.scss | 1 + test/fixture/single-quoted-import-path.scss | 1 + test/fixture/single-quoted-import.scss | 1 + test/fixture/unquoted-import-path.scss | 1 + test/fixture/unquoted-import.scss | 1 + test/import.js | 106 ++++++++++++++++++++ 8 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 test/fixture/double-quoted-import-path.scss create mode 100644 test/fixture/double-quoted-import.scss create mode 100644 test/fixture/single-quoted-import-path.scss create mode 100644 test/fixture/single-quoted-import.scss create mode 100644 test/fixture/unquoted-import-path.scss create mode 100644 test/fixture/unquoted-import.scss create mode 100644 test/import.js diff --git a/lib/tokenize.js b/lib/tokenize.js index 434ef35..56feb2a 100644 --- a/lib/tokenize.js +++ b/lib/tokenize.js @@ -206,7 +206,6 @@ export default function tokenize(input, l, p) { if ( code === slash && css.charCodeAt(pos + 1) !== slash ) { tokens.push(['/', '/', line, pos - offset]); - pos += 2; break; } diff --git a/test/fixture/double-quoted-import-path.scss b/test/fixture/double-quoted-import-path.scss new file mode 100644 index 0000000..2d32486 --- /dev/null +++ b/test/fixture/double-quoted-import-path.scss @@ -0,0 +1 @@ +@import "foo/bar"; diff --git a/test/fixture/double-quoted-import.scss b/test/fixture/double-quoted-import.scss new file mode 100644 index 0000000..db574d5 --- /dev/null +++ b/test/fixture/double-quoted-import.scss @@ -0,0 +1 @@ +@import "foo"; diff --git a/test/fixture/single-quoted-import-path.scss b/test/fixture/single-quoted-import-path.scss new file mode 100644 index 0000000..7dd1367 --- /dev/null +++ b/test/fixture/single-quoted-import-path.scss @@ -0,0 +1 @@ +@import 'foo/bar'; diff --git a/test/fixture/single-quoted-import.scss b/test/fixture/single-quoted-import.scss new file mode 100644 index 0000000..c272486 --- /dev/null +++ b/test/fixture/single-quoted-import.scss @@ -0,0 +1 @@ +@import 'foo'; diff --git a/test/fixture/unquoted-import-path.scss b/test/fixture/unquoted-import-path.scss new file mode 100644 index 0000000..99a214b --- /dev/null +++ b/test/fixture/unquoted-import-path.scss @@ -0,0 +1 @@ +@import foo/bar; diff --git a/test/fixture/unquoted-import.scss b/test/fixture/unquoted-import.scss new file mode 100644 index 0000000..91b0ee2 --- /dev/null +++ b/test/fixture/unquoted-import.scss @@ -0,0 +1 @@ +@import foo; diff --git a/test/import.js b/test/import.js new file mode 100644 index 0000000..636660a --- /dev/null +++ b/test/import.js @@ -0,0 +1,106 @@ +var scss = require('..'); +var fs = require('fs'); +var path = require('path'); +var assert = require('chai').assert; + +var fixture = function(name) { + return fs.readFileSync( + path.join(__dirname, 'fixture', name) + ); +} + +describe('@import', function() { + it('should tokenize a @import with double quotes', function() { + assert.deepEqual( + [ + ['@', '@', 1, 1], + ['ident', 'import', 1, 2, 1, 7], + ['space', ' '], + ['"', '"', 1, 9], + ['string', 'foo', 1, 10, 1, 12], + ['"', '"', 1, 13], + [';', ';', 1, 14], + ['newline', '\n', 2, 0], + ], + scss.tokenize(fixture('double-quoted-import.scss')) + ); + }); + + it('should tokenize a @import with single quotes', function() { + assert.deepEqual( + [ + ['@', '@', 1, 1], + ['ident', 'import', 1, 2, 1, 7], + ['space', ' '], + ['\'', '\'', 1, 9], + ['string', 'foo', 1, 10, 1, 12], + ['\'', '\'', 1, 13], + [';', ';', 1, 14], + ['newline', '\n', 2, 0], + ], + scss.tokenize(fixture('single-quoted-import.scss')) + ); + }); + + it('should tokenize a @import without quotes', function() { + assert.deepEqual( + [ + ['@', '@', 1, 1], + ['ident', 'import', 1, 2, 1, 7], + ['space', ' '], + ['ident', 'foo', 1, 9, 1, 11], + [';', ';', 1, 12], + ['newline', '\n', 2, 0], + ], + scss.tokenize(fixture('unquoted-import.scss')) + ); + }); + + it('should tokenize a @import with double quotes and slash', function() { + assert.deepEqual( + [ + ['@', '@', 1, 1], + ['ident', 'import', 1, 2, 1, 7], + ['space', ' '], + ['"', '"', 1, 9], + ['string', 'foo/bar', 1, 10, 1, 16], + ['"', '"', 1, 17], + [';', ';', 1, 18], + ['newline', '\n', 2, 0], + ], + scss.tokenize(fixture('double-quoted-import-path.scss')) + ); + }); + + it('should tokenize a @import with single quotes and slash', function() { + assert.deepEqual( + [ + ['@', '@', 1, 1], + ['ident', 'import', 1, 2, 1, 7], + ['space', ' '], + ['\'', '\'', 1, 9], + ['string', 'foo/bar', 1, 10, 1, 16], + ['\'', '\'', 1, 17], + [';', ';', 1, 18], + ['newline', '\n', 2, 0], + ], + scss.tokenize(fixture('single-quoted-import-path.scss')) + ); + }); + + it('should tokenize a @import without quotes and slash', function() { + assert.deepEqual( + [ + ['@', '@', 1, 1], + ['ident', 'import', 1, 2, 1, 7], + ['space', ' '], + ['ident', 'foo', 1, 9, 1, 11], + ['/', '/', 1, 12], + ['ident', 'bar', 1, 13, 1, 15], + [';', ';', 1, 16], + ['newline', '\n', 2, 0], + ], + scss.tokenize(fixture('unquoted-import-path.scss')) + ); + }); +});