Skip to content

Commit

Permalink
Issue #22 - Handle substrings and repetitions correctly in replacePat…
Browse files Browse the repository at this point in the history
…hs (#25)

Sort strings so longer strings are handled first, so the longer
paths aren't mistakenly given a hash of some shorter substring path.

Add global flag to replacePaths regex to ensure all instances of a path
are handled.
  • Loading branch information
MalucoMarinero authored and XhmikosR committed Feb 28, 2019
1 parent 747d00b commit 9e4fac7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ const staticify = (root, options) => {
};

const replacePaths = fileContents => {
return Object.keys(versions).reduce((f, url) => {
return f.replace(url, getVersionedPath(url));
return Object.keys(versions).sort((a, b) => {
return b.length - a.length;
}).reduce((f, url) => {
return f.replace(new RegExp(url, 'g'), getVersionedPath(url));
}, fileContents);
};

Expand Down
1 change: 1 addition & 0 deletions test/font.woff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test FONT FILENAME
1 change: 1 addition & 0 deletions test/font.woff2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test FONT FILENAME NUMBER 2
34 changes: 34 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,38 @@ describe('.replacePaths', () => {
results.includes('index.js').should.be.false();
results.should.match(/index\.[0-9a-f]{32}\.js/i);
});

it('should replace all paths, not just the first instance of a path', () => {
const results = staticify(ROOT).replacePaths('/test/font.woff;/test/font.woff');

const lines = results.split(';');
lines[0].should.match(/test\/font\.[0-9a-f]{7}\.woff/i);
lines[1].should.match(/test\/font\.[0-9a-f]{7}\.woff/i);
lines[0].should.equal(lines[1]);
results.includes('test/font.woff').should.be.false();
});

it('should not mix up paths that are substrings of one another', () => {
const results = staticify(ROOT).replacePaths('/test/font.woff;/test/font.woff2;/test/font.woff');

const lines = results.split(';');
lines[0].should.equal(lines[2]);
lines[1].should.not.equal(lines[2]);
lines[0].should.match(/test\/font\.[0-9a-f]{7}\.woff/i);
lines[1].should.match(/test\/font\.[0-9a-f]{7}\.woff2/i);
results.includes('test/font.woff').should.be.false();
results.includes('test/font.woff2').should.be.false();
});

it('should not mix up paths that are substrings of one another (long)', () => {
const results = staticify(ROOT, {shortHash: false}).replacePaths('/test/font.woff;/test/font.woff2;/test/font.woff');

const lines = results.split(';');
lines[0].should.equal(lines[2]);
lines[1].should.not.equal(lines[2]);
lines[0].should.match(/test\/font\.[0-9a-f]{32}\.woff/i);
lines[1].should.match(/test\/font\.[0-9a-f]{32}\.woff2/i);
results.includes('test/font.woff').should.be.false();
results.includes('test/font.woff2').should.be.false();
});
});

0 comments on commit 9e4fac7

Please sign in to comment.