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

Commit b414ffa

Browse files
author
Gedalia Kott
committed
Fixes URLs with query strings and hashes in them
The previous fix did not test a corner case where the user passes extensions allowed in the options. When there are no otpions passed for extensions allowed the extension check is skipped and thus the file is processed appropriately. I added virtual copies of the tests so feel free to point me to a better way of doing that. I just don't know what a better way to do it is without overwriting the original tests. The fix I made was stripping query strings and hashes from the regex results before the extension check and then passing in that stripped version into the check instead of the actual result which has the hash and/or query string.
1 parent be06eaf commit b414ffa

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ function gulpCssBase64(opts) {
6060
return;
6161
}
6262

63-
if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(result[1])) == -1) {
63+
var pureURL = stripQueryStringFromURL(stripHashFromURL(result[1]));
64+
if (opts.extensionsAllowed.length !== 0 && opts.extensionsAllowed.indexOf(path.extname(pureURL)) == -1) {
6465
log('Ignores ' + chalk.yellow(result[1]) + ', extension not allowed ' + chalk.yellow(path.extname(result[1])), opts.verbose);
6566
callback();
6667
return;
@@ -194,5 +195,13 @@ function log(message, isVerbose) {
194195
}
195196
}
196197

198+
function stripQueryStringFromURL(urlString) {
199+
return urlString.split("?")[0];
200+
}
201+
202+
function stripHashFromURL(urlString) {
203+
return urlString.split("#")[0];
204+
}
205+
197206
// Exporting the plugin main function
198207
module.exports = gulpCssBase64;

test/test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ describe('gulp-css-base64', function () {
117117
});
118118
});
119119

120+
it('should convert url() content with questionmark at end when extensionsAllowed are passed', function (done) {
121+
// create the fake file
122+
var fakeFile = new gutil.File({
123+
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}')
124+
});
125+
126+
// Create a css-base64 plugin stream
127+
var stream = base64({
128+
extensionsAllowed: [".png"]
129+
});
130+
131+
// write the fake file to it
132+
stream.write(fakeFile);
133+
134+
// wait for the file to come back out
135+
stream.once('data', function (file) {
136+
// make sure it came out the same way it went in
137+
assert(file.isBuffer());
138+
139+
// check the contents
140+
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}');
141+
done();
142+
});
143+
});
144+
120145
it('should convert url() content with hashtag at end', function (done) {
121146
// create the fake file
122147
var fakeFile = new gutil.File({
@@ -140,6 +165,29 @@ describe('gulp-css-base64', function () {
140165
});
141166
});
142167

168+
it('should convert url() content with hashtag at end when extensionsAllowed are passed', function (done) {
169+
// create the fake file
170+
var fakeFile = new gutil.File({
171+
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}')
172+
});
173+
174+
// Create a css-base64 plugin stream
175+
var stream = base64();
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+
143191
it('should ignore if image weight is greater than maxWeightResource default value', function (done) {
144192
// create the fake file
145193
var fakeFile = new gutil.File({

0 commit comments

Comments
 (0)