Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid replacing filenames contained in longer filenames #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module.exports = plugin;

var escapeRegExp = require('lodash.escaperegexp');
var path = require('path');
var PluginError = require('plugin-error');
var through = require('through2');
Expand Down Expand Up @@ -78,10 +79,26 @@ function plugin(options) {
cache.forEach(function replaceInFile(file) {
var contents = file.contents.toString();

renames.forEach(function replaceOnce(rename) {
renames.forEach(function replaceOnce(rename, index) {
var unreved = options.modifyUnreved ? options.modifyUnreved(rename.unreved) : rename.unreved;
var reved = options.modifyReved ? options.modifyReved(rename.reved) : rename.reved;
contents = contents.split(unreved).join(reved);
var regexp = new RegExp(escapeRegExp(unreved), 'g');
var containingUnreved = [];
for (var i = 0; i < index; i+=1){
var longerUnreved = options.modifyUnreved ? options.modifyUnreved(renames[i].unreved) : renames[i].unreved;
if(longerUnreved.indexOf(unreved) !== -1){
containingUnreved.push(longerUnreved);
}
}
contents = contents.replace(regexp, function(match, offset, string){
for(var i = 0; i < containingUnreved.length; i+=1){
var element = containingUnreved[i];
if (string.substr(offset, element.length) === element || string.substr(offset + match.length - element.length, element.length) === element) {
return match;
}
}
return reved;
})
if (options.prefix) {
contents = contents.split('/' + options.prefix).join(options.prefix + '/');
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"license": "MIT",
"dependencies": {
"lodash.escaperegexp": "^4.1.2",
"plugin-error": "^0.1.2",
"through2": "^2.0.0"
},
Expand Down
34 changes: 34 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,40 @@ it('should not replace filenames in extensions not in replaceInExtensions', func
filesToRevFilter.end();
});

it('should not replace filenames contained in longer filenames', function(cb) {
var css = '@font-face { src:url(\'font.woff2\'), url(\'font.woff\'), url(\'myfont.woff\'); }';

var manifest = es.readArray([
new Vinyl({
path: 'rev-manifest.json',
contents: new Buffer(JSON.stringify({
'font.woff': 'font.woff?v=f56bc5932b',
'font.woff2': 'font.woff2?v=c6d1b13464',
'myfont.woff': 'myfont.woff?v=d98ag4566d'
}))
})
]);

var stream = revReplace({ manifest: manifest });

stream.on('data', function(file) {
var content = file.contents.toString();

assert.equal(content, "@font-face { src:url('font.woff2?v=c6d1b13464'), url('font.woff?v=f56bc5932b'), url('myfont.woff?v=d98ag4566d'); }");
});

stream.on('end', function() {
cb();
});

stream.write(new Vinyl({
path: 'index.css',
contents: new Buffer(css)
}));

stream.end();
});

it('should not canonicalize URIs when option is off', function (cb) {
var filesToRevFilter = filter(['**/*.css'], {restore: true});

Expand Down