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

Issue #22 - Handle substrings and repetitions correctly in replacePaths #25

Merged
merged 1 commit into from
Feb 28, 2019
Merged
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
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();
});
});