Skip to content

Commit 2ded01c

Browse files
authored
Fix parentheses (#22), close #20
1 parent 686983f commit 2ded01c

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

lib/css-parser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var embeddedRegexp = /^data:(.*?),(.*?)/;
22
var commentRegexp = /\/\*([\s\S]*?)\*\//g;
3-
var urlsRegexp = /((?:@import\s+)?url\s*\(\s*['"]?)(.*?)(['"]?\s*\))|(@import\s+['"]?)([^;'"]+)/ig;
3+
var urlsRegexp = /(?:@import\s+)?url\s*\(\s*(("(.*?)")|('(.*?)')|(.*?))\s*\)|(?:@import\s+)(("(.*?)")|('(.*?)')|(.*?))[\s;]/ig;
44

55
function isEmbedded (src) {
66
return embeddedRegexp.test(src.trim());
@@ -13,8 +13,8 @@ function getUrls (text) {
1313
text = text.replace(commentRegexp, '');
1414

1515
while (urlMatch = urlsRegexp.exec(text)) {
16-
// Match 2 group if '[@import] url(path)', match 5 group if '@import path'
17-
url = urlMatch[2] || urlMatch[5];
16+
// Match 3, 5, 6 group if '[@import] url(path)', match 9, 11, 12 group if '@import path'
17+
url = urlMatch[3] || urlMatch[5] || urlMatch[6] || urlMatch[9] || urlMatch[11] || urlMatch[12];
1818

1919
if (url && !isEmbedded(url) && urls.indexOf(url) === -1) {
2020
urls.push(url);

test/css-parser-test.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,80 @@ describe('Parse css urls', function(){
8282
urls.should.containEql(' a.css');
8383
});
8484

85+
it('should handle urls without extension', function() {
86+
var text = '.image { background: url("image-without-ext"); } ';
87+
var urls = parseCssUrls(text);
88+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
89+
urls.should.containEql('image-without-ext');
90+
});
91+
92+
describe('quotes', function() {
93+
it('should find url without quotes', function() {
94+
var text = '.image { background: url(bg.jpg); } ';
95+
var urls = parseCssUrls(text);
96+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
97+
urls.should.containEql('bg.jpg');
98+
});
99+
100+
it('should find url containing single quotes inside double quotes', function() {
101+
var text = '.image { background: url("bg\'1.jpg"); }';
102+
var urls = parseCssUrls(text);
103+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
104+
urls.should.containEql('bg\'1.jpg');
105+
});
106+
107+
it('should find url containing double quotes inside single quotes', function() {
108+
var text = '.image { background: url(\'bg" 23\'); }';
109+
var urls = parseCssUrls(text);
110+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
111+
urls.should.containEql('bg" 23');
112+
});
113+
114+
it('should find import without quotes', function() {
115+
var text = '@import new.css;';
116+
var urls = parseCssUrls(text);
117+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
118+
urls.should.containEql('new.css');
119+
});
120+
121+
it('should find import containing single quotes inside double quotes', function() {
122+
var text = '@import "new\'11\'.css";';
123+
var urls = parseCssUrls(text);
124+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
125+
urls.should.containEql('new\'11\'.css');
126+
});
127+
128+
it('should find import containing double quotes inside single quotes', function() {
129+
var text = '@import \'new" _2_".css\';';
130+
var urls = parseCssUrls(text);
131+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
132+
urls.should.containEql('new" _2_".css');
133+
});
134+
});
135+
136+
describe('parentheses', function() {
137+
it('should handle parentheses inside url in filename', function() {
138+
var text = '.image { background: url("test (2).png"); } ';
139+
var urls = parseCssUrls(text);
140+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
141+
urls.should.containEql('test (2).png');
142+
});
143+
144+
it('should handle parentheses inside url in extension', function() {
145+
var text = '.image { background: url(\'test.png (2)\'); } ';
146+
var urls = parseCssUrls(text);
147+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
148+
urls.should.containEql('test.png (2)');
149+
});
150+
151+
it('should handle parentheses inside @import', function() {
152+
var text = '@import "import)).css" ';
153+
var urls = parseCssUrls(text);
154+
urls.should.be.instanceof(Array).and.have.lengthOf(1);
155+
urls.should.containEql('import)).css');
156+
});
157+
});
158+
85159
describe('comments', function() {
86160
it('should ignore comments and return empty array if there are only comments in text', function(){
87161
var text = '\

0 commit comments

Comments
 (0)