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

wip: prepend is not applied to all urls when used with a srcset #86

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ class AssetRewrite extends Filter {
*/

var re = new RegExp(
'["\'(=]\\s*([^"\'()=]*' +
'["\'(=,]\\s*([^"\'()=,]*' +
escapeRegExp(assetPath) +
'[^"\n\'()\\>=]*)(\\?[^"\')> ]*)?\\s*\\\\*\\s*["\')>s]',
'[^"\n\'()\\>=,]*)(\\?[^"\')> ]*)?\\s*\\\\*\\s*["\')>s,]',
'g'
);
var match = null;
Expand Down
75 changes: 73 additions & 2 deletions tests/filter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('broccoli-asset-rev', function () {
src: url('fonts/Fiz/Light/Fiz-Light.eot');
src: url('fonts/Fiz/Light/Fiz-Light.eot?#iefix') format('embedded-opentype'), url('fonts/Fiz/Light/Fiz-Light.woff') format('woff'), url('fonts/Fiz/Light/Fiz-Light.ttf') format('truetype'), url('fonts/Fiz/Light/Fiz-Light.svg#Fiz') format('svg');
}

@font-face {
font-family: Fiz;
font-weight: 200;
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('broccoli-asset-rev', function () {
src: url('fonts/Fiz/Light/fingerprinted-Fiz-Light.eot');
src: url('fonts/Fiz/Light/fingerprinted-Fiz-Light.eot?#iefix') format('embedded-opentype'), url('fonts/Fiz/Light/fingerprinted-Fiz-Light.woff') format('woff'), url('fonts/Fiz/Light/fingerprinted-Fiz-Light.ttf') format('truetype'), url('fonts/Fiz/Light/fingerprinted-Fiz-Light.svg#Fiz') format('svg');
}

@font-face {
font-family: Fiz;
font-weight: 200;
Expand Down Expand Up @@ -521,4 +521,75 @@ describe('broccoli-asset-rev', function () {

expect(run2ProcessedCount).to.equal(0);
});

it('replaces assets in srcset attributes', async function () {
const input = await createTempDir();
const subject = new AssetRewrite(input.path(), {
assetMap: {
'/assets/img/small.png': '/assets/img/other-small.png',
'/assets/img/medium.png': '/assets/img/other-medium.png',
'/assets/img/big.png': '/assets/img/other-big.png',
},
});
const output = createBuilder(subject);
input.write({
'srcset.html': `<source srcset="/assets/img/small.png 800w, /assets/img/medium.png 1600w" sizes="(max-width: 800px) 800px, 1600px" type="image/png">`,
});

await output.build();
expect(output.changes()).to.deep.equal({
'srcset.html': 'create',
});
expect(output.read()).to.deep.equal({
'srcset.html': `<source srcset="/assets/img/other-small.png 800w, /assets/img/other-medium.png 1600w" sizes="(max-width: 800px) 800px, 1600px" type="image/png">`,
});
});

it('replaces assets in srcset attributes with prepended path', async function () {
const input = await createTempDir();
const subject = new AssetRewrite(input.path(), {
assetMap: {
'/assets/img/small.png': '/assets/img/other-small.png',
'/assets/img/medium.png': '/assets/img/other-medium.png',
'/assets/img/big.png': '/assets/img/other-big.png',
},
prepend: 'my-path/',
});
const output = createBuilder(subject);
input.write({
'srcset.html': `<source srcset="/assets/img/small.png 800w, /assets/img/medium.png 1600w" sizes="(max-width: 800px) 800px, 1600px" type="image/png">`,
});

await output.build();
expect(output.changes()).to.deep.equal({
'srcset.html': 'create',
});
expect(output.read()).to.deep.equal({
'srcset.html': `<source srcset="my-path/assets/img/other-small.png 800w, my-path/assets/img/other-medium.png 1600w" sizes="(max-width: 800px) 800px, 1600px" type="image/png">`,
});
});

it('replaces assets in srcset attributes with prepended domain', async function () {
const input = await createTempDir();
const subject = new AssetRewrite(input.path(), {
assetMap: {
'/assets/img/small.png': '/assets/img/other-small.png',
'/assets/img/medium.png': '/assets/img/other-medium.png',
'/assets/img/big.png': '/assets/img/other-big.png',
},
prepend: 'https://subdomain.cloudfront.net/',
});
const output = createBuilder(subject);
input.write({
'srcset.html': `<source srcset="/assets/img/small.png 800w, /assets/img/medium.png 1600w" sizes="(max-width: 800px) 800px, 1600px" type="image/png">`,
});

await output.build();
expect(output.changes()).to.deep.equal({
'srcset.html': 'create',
});
expect(output.read()).to.deep.equal({
'srcset.html': `<source srcset="https://subdomain.cloudfront.net/assets/img/other-small.png 800w, https://subdomain.cloudfront.net/assets/img/other-medium.png 1600w" sizes="(max-width: 800px) 800px, 1600px" type="image/png">`,
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@st-h how about adding a test for the line break issue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Good call. Just haven't had any time to look into this further.

});