Skip to content

Commit 5dab020

Browse files
authored
Fix handling of non-base64 encoded data URI, closes #9 (#10)
1 parent c68a1dd commit 5dab020

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

lib/css-parser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
var embeddedRegexp = /data:(.*?);base64,/;
1+
var embeddedRegexp = /^data:(.*?),(.*?)/;
22
var commentRegexp = /\/\*([\s\S]*?)\*\//g;
33
var urlsRegexp = /((?:@import\s+)?url\s*\(\s*['"]?)(.*?)(['"]?\s*\))|(@import\s+['"]?)([^;'"]+)/ig;
44

55
function isEmbedded (src) {
6-
return embeddedRegexp.test(src);
6+
return embeddedRegexp.test(src.trim());
77
}
88

99
function getUrls (text) {

test/css-parser-test.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('Parse css urls', function(){
121121
});
122122
});
123123

124-
describe('base64 dataURI', function() {
124+
describe('Data URI', function() {
125125
it('should ignore base64 encoded images', function(){
126126
var text = '\
127127
div.image { \
@@ -150,5 +150,23 @@ describe('Parse css urls', function(){
150150
urls.should.be.instanceof(Array);
151151
urls.should.have.length(0);
152152
});
153+
154+
it('should ignore utf-8 encoded data uri', function() {
155+
var text = 'img.grayscale { \
156+
filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'></svg>"); \
157+
}';
158+
var urls = parseCssUrls(text);
159+
urls.should.be.instanceof(Array);
160+
urls.should.have.length(0);
161+
});
162+
163+
it('should ignore minimal data uri ("data:,")', function() {
164+
var text = 'img.grayscale { \
165+
filter: url("data:,"); \
166+
}';
167+
var urls = parseCssUrls(text);
168+
urls.should.be.instanceof(Array);
169+
urls.should.have.length(0);
170+
})
153171
});
154172
});

0 commit comments

Comments
 (0)