diff --git a/index.js b/index.js index cc919f0..547050b 100644 --- a/index.js +++ b/index.js @@ -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); }; diff --git a/test/font.woff b/test/font.woff new file mode 100644 index 0000000..42e672a --- /dev/null +++ b/test/font.woff @@ -0,0 +1 @@ +Test FONT FILENAME diff --git a/test/font.woff2 b/test/font.woff2 new file mode 100644 index 0000000..8b0f1d2 --- /dev/null +++ b/test/font.woff2 @@ -0,0 +1 @@ +Test FONT FILENAME NUMBER 2 diff --git a/test/index.js b/test/index.js index 3bff7f7..db3e113 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); + }); });