Skip to content

Commit cab6a2d

Browse files
committed
fixes #75; import with url() should have importPath
1 parent a47925d commit cab6a2d

File tree

4 files changed

+81
-45
lines changed

4 files changed

+81
-45
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
s.js
2+
13
/.vscode
24
# Logs
35
logs
@@ -31,4 +33,4 @@ node_modules
3133
.node_repl_history
3234
.idea
3335
DS_Store
34-
dist
36+
dist

lib/less-parser.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default class LessParser extends Parser {
133133
}
134134

135135
import (token) {
136-
/* eslint complexity: 0 */
136+
/* eslint complexity: 0 */
137137
let last = false,
138138
open = false,
139139
end = { line: 0, column: 0 };
@@ -170,15 +170,28 @@ export default class LessParser extends Parser {
170170
if (directives.length) {
171171
node.raws.between = tokn[1];
172172
}
173+
else if (node.urlFunc) {
174+
node.raws.beforeUrl = tokn[1];
175+
}
173176
else if (node.importPath) {
174-
node.raws.after = tokn[1];
177+
if (node.urlFunc) {
178+
node.raws.afterUrl = tokn[1];
179+
}
180+
else {
181+
node.raws.after = tokn[1];
182+
}
175183
}
176184
else {
177185
node.raws.afterName = tokn[1];
178186
}
179187
}
188+
else if (tokn[0] === 'word' && tokn[1] === 'url') {
189+
node.urlFunc = true;
190+
}
180191
else {
181-
node.importPath = tokn[1];
192+
if (tokn[0] !== '(' && tokn[0] !== ')') {
193+
node.importPath = tokn[1];
194+
}
182195
}
183196

184197
if (this.pos === this.tokens.length) {

lib/less-stringifier.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ export default class LessStringifier extends Stringifier {
88
import (node) {
99
this.builder(`@${ node.name }`);
1010
this.builder((node.raws.afterName || '') +
11-
(node.directives || '') +
12-
(node.raws.between || '') +
13-
(node.importPath || '') +
14-
(node.raws.after || ''));
11+
(node.directives || '') +
12+
(node.raws.between || '') +
13+
(node.urlFunc ? 'url(' : '') +
14+
(node.raws.beforeUrl || '') +
15+
(node.importPath || '') +
16+
(node.raws.afterUrl || '') +
17+
(node.urlFunc ? ')' : '') +
18+
(node.raws.after || ''));
1519

1620
if (node.raws.semicolon) {
1721
this.builder(';');

test/parser/import.spec.js

+54-37
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,68 @@ import lessSyntax from '../../lib/less-syntax';
77
import postcss from 'postcss';
88

99
describe('Parser', () => {
10+
describe('Import', () => {
11+
it('should parse @imports as Import', (done) => {
12+
const lessText = '@import "foo.less";';
1013

11-
it('should parse @imports as Import', (done) => {
12-
const lessText = '@import "foo.less";';
14+
postcss()
15+
.process(lessText, { syntax: lessSyntax })
16+
.then((result) => {
17+
expect(result).to.be.not.null;
18+
expect(result.css).to.equal(lessText);
19+
expect(result.root.first).to.be.an.instanceof(Import);
20+
expect(result.root.first.importPath).to.equal('"foo.less"');
1321

14-
postcss()
15-
.process(lessText, { syntax: lessSyntax })
16-
.then((result) => {
17-
expect(result).to.be.not.null;
18-
expect(result.css).to.equal(lessText);
19-
expect(result.root.first).to.be.an.instanceof(Import);
20-
expect(result.root.first.importPath).to.equal('"foo.less"');
22+
done();
23+
}).catch(done);
24+
});
2125

22-
done();
23-
}).catch(done);
24-
});
26+
it('should parse @imports with a url function as Import', (done) => {
27+
const lessText = '@import url("foo.less");';
2528

26-
it('should parse @imports as Import, no space', (done) => {
27-
const lessText = '@import"foo.less";';
29+
postcss()
30+
.process(lessText, { syntax: lessSyntax })
31+
.then((result) => {
32+
expect(result).to.be.not.null;
33+
expect(result.css).to.equal(lessText);
34+
expect(result.root.first).to.be.an.instanceof(Import);
35+
expect(result.root.first.importPath).to.equal('"foo.less"');
36+
expect(result.root.first.urlFunc).to.equal(true);
2837

29-
postcss()
30-
.process(lessText, { syntax: lessSyntax })
31-
.then((result) => {
32-
expect(result).to.be.not.null;
33-
expect(result.css).to.equal(lessText);
34-
expect(result.root.first).to.be.an.instanceof(Import);
35-
expect(result.root.first.importPath).to.equal('"foo.less"');
36-
expect(result.root.first.raws.afterName).to.be.undefined;
38+
done();
39+
}).catch(done);
40+
});
3741

38-
done();
39-
}).catch(done);
40-
});
42+
it('should parse @imports as Import, no space', (done) => {
43+
const lessText = '@import"foo.less";';
44+
45+
postcss()
46+
.process(lessText, { syntax: lessSyntax })
47+
.then((result) => {
48+
expect(result).to.be.not.null;
49+
expect(result.css).to.equal(lessText);
50+
expect(result.root.first).to.be.an.instanceof(Import);
51+
expect(result.root.first.importPath).to.equal('"foo.less"');
52+
expect(result.root.first.raws.afterName).to.be.undefined;
53+
54+
done();
55+
}).catch(done);
56+
});
4157

42-
it('should parse @imports with directives', (done) => {
43-
const lessText = '@import (inline) "foo.less";';
58+
it('should parse @imports with directives', (done) => {
59+
const lessText = '@import (inline) "foo.less";';
4460

45-
postcss()
46-
.process(lessText, { syntax: lessSyntax })
47-
.then((result) => {
48-
expect(result).to.be.not.null;
49-
expect(result.css).to.equal(lessText);
50-
expect(result.root.first).to.be.an.instanceof(Import);
51-
expect(result.root.first.directives).to.equal('(inline)');
52-
expect(result.root.first.importPath).to.equal('"foo.less"');
61+
postcss()
62+
.process(lessText, { syntax: lessSyntax })
63+
.then((result) => {
64+
expect(result).to.be.not.null;
65+
expect(result.css).to.equal(lessText);
66+
expect(result.root.first).to.be.an.instanceof(Import);
67+
expect(result.root.first.directives).to.equal('(inline)');
68+
expect(result.root.first.importPath).to.equal('"foo.less"');
5369

54-
done();
55-
}).catch(done);
70+
done();
71+
}).catch(done);
72+
});
5673
});
5774
});

0 commit comments

Comments
 (0)