Skip to content
This repository was archived by the owner on Sep 5, 2022. It is now read-only.

Commit b4bea73

Browse files
author
Mehdy Dara
committed
Merge pull request #30 from gedkott/master
Fixes URLs contains query strings or hashes with extension option
2 parents 659860b + 3da650e commit b4bea73

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ function gulpCssBase64(opts) {
5858
return;
5959
}
6060

61-
if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(result[1])) === -1) {
61+
var pureUrl = result[1].split('?')[0].split('#')[0];
62+
if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(pureUrl)) === -1) {
6263
log('Ignores ' + chalk.yellow(result[1]) + ', extension not allowed ' + chalk.yellow(path.extname(result[1])), opts.verbose);
6364
callback();
6465
return;

test/test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,31 @@ describe('gulp-css-base64', function () {
115115
});
116116
});
117117

118+
it('should convert url() content with questionmark at end even if extensionsAllowed is passed', function (done) {
119+
// create the fake file
120+
var fakeFile = new gutil.File({
121+
contents: new Buffer('.button_alert{background:url(\'test/fixtures/image/very-very-small.png?awesomeQuestionmark\') no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}')
122+
});
123+
124+
// Create a css-base64 plugin stream
125+
var stream = base64({
126+
extensionsAllowed: ['.gif', '.jpg', '.png']
127+
});
128+
129+
// write the fake file to it
130+
stream.write(fakeFile);
131+
132+
// wait for the file to come back out
133+
stream.once('data', function (file) {
134+
// make sure it came out the same way it went in
135+
assert(file.isBuffer());
136+
137+
// check the contents
138+
assert.equal(file.contents.toString('utf8'), '.button_alert{background:url(\'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANAQAAAABakNnRAAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAAAEgAAABIAEbJaz4AAAArSURBVAjXY/j/g2H/C4b5Jxj6OxgaOEBoxgmGDg8GIACyuRoYjkowfKgAACBpDLQ2kvRRAAAAAElFTkSuQmCC\') no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}');
139+
done();
140+
});
141+
});
142+
118143
it('should convert url() content with hashtag at end', function (done) {
119144
// create the fake file
120145
var fakeFile = new gutil.File({
@@ -138,6 +163,31 @@ describe('gulp-css-base64', function () {
138163
});
139164
});
140165

166+
it('should convert url() content with hashtag at end even if extensionsAllowed is passed', function (done) {
167+
// create the fake file
168+
var fakeFile = new gutil.File({
169+
contents: new Buffer('.button_alert{background:url(\'test/fixtures/image/very-very-small.png#awesomeHashtag\') no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}')
170+
});
171+
172+
// Create a css-base64 plugin stream
173+
var stream = base64({
174+
extensionsAllowed: ['.gif', '.jpg', '.png']
175+
});
176+
177+
// write the fake file to it
178+
stream.write(fakeFile);
179+
180+
// wait for the file to come back out
181+
stream.once('data', function (file) {
182+
// make sure it came out the same way it went in
183+
assert(file.isBuffer());
184+
185+
// check the contents
186+
assert.equal(file.contents.toString('utf8'), '.button_alert{background:url(\'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANAQAAAABakNnRAAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAAAEgAAABIAEbJaz4AAAArSURBVAjXY/j/g2H/C4b5Jxj6OxgaOEBoxgmGDg8GIACyuRoYjkowfKgAACBpDLQ2kvRRAAAAAElFTkSuQmCC\') no-repeat 4px 5px;padding-left:12px;font-size:12px;color:#888;text-decoration:underline}');
187+
done();
188+
});
189+
});
190+
141191
it('should ignore if image weight is greater than maxWeightResource default value', function (done) {
142192
// create the fake file
143193
var fakeFile = new gutil.File({

0 commit comments

Comments
 (0)