Skip to content
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
22 changes: 13 additions & 9 deletions src/plugins/source-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,16 +627,13 @@ export default (options) =>
const { startIndex, unquoted } = source;
let { value } = source;
const URLObject = parse(value);
const { hash } = URLObject;

if (URLObject.hash) {
const { hash } = URLObject;

if (hash) {
URLObject.hash = null;
source.value = URLObject.format();

if (hash) {
value = value.slice(0, value.length - hash.length);
}
value = value.slice(0, value.length - hash.length);
}

const importKey = urlToRequest(decodeURIComponent(source.value), root);
Expand All @@ -656,7 +653,11 @@ export default (options) =>
});
}

const replacerKey = JSON.stringify({ importKey, unquoted });
const replacerKey = JSON.stringify({
importKey,
unquoted,
hash,
});
let replacerName = replacersMap.get(replacerKey);

if (!replacerName) {
Expand All @@ -667,20 +668,23 @@ export default (options) =>
type: 'replacer',
value: {
type: 'source',
hash,
importName,
replacerName,
unquoted,
},
});
}

const valueLength = hash ? value.length + hash.length : value.length;

// eslint-disable-next-line no-param-reassign
html =
html.substr(0, startIndex + offset) +
replacerName +
html.substr(startIndex + value.length + offset);
html.substr(startIndex + valueLength + offset);

offset += replacerName.length - value.length;
offset += replacerName.length - valueLength;
}

return html;
Expand Down
14 changes: 12 additions & 2 deletions src/runtime/getUrl.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
module.exports = (url, maybeNeedQuotes) => {
module.exports = (url, options) => {
if (!options) {
// eslint-disable-next-line no-param-reassign
options = {};
}

// eslint-disable-next-line no-underscore-dangle, no-param-reassign
url = url && url.__esModule ? url.default : url;

if (typeof url !== 'string') {
return url;
}

if (maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
if (options.hash) {
// eslint-disable-next-line no-param-reassign
url += options.hash;
}

if (options.maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
return `"${url}"`;
}

Expand Down
12 changes: 8 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ export function getModuleCode(html, replaceableMessages) {
let replacersCode = '';

for (const item of replaceableMessages) {
const { importName, replacerName, unquoted } = item;
const { importName, replacerName, unquoted, hash } = item;

replacersCode += `var ${replacerName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${
unquoted ? ', true' : ''
});\n`;
const getUrlOptions = []
.concat(hash ? [`hash: ${JSON.stringify(hash)}`] : [])
.concat(unquoted ? 'maybeNeedQuotes: true' : []);
const preparedOptions =
getUrlOptions.length > 0 ? `, { ${getUrlOptions.join(', ')} }` : '';

replacersCode += `var ${replacerName} = ${GET_SOURCE_FROM_IMPORT_NAME}(${importName}${preparedOptions});\n`;

code = code.replace(
new RegExp(replacerName, 'g'),
Expand Down
157 changes: 119 additions & 38 deletions test/__snapshots__/attributes-option.test.js.snap

Large diffs are not rendered by default.

51 changes: 39 additions & 12 deletions test/__snapshots__/esModule-option.test.js.snap

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions test/__snapshots__/loader.test.js.snap

Large diffs are not rendered by default.

102 changes: 75 additions & 27 deletions test/__snapshots__/minimize-option.test.js.snap

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/fixtures/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,5 @@ <h2>An Ordered HTML List</h2>
/>

<img data-srcset="image.png 480w, image.png 800w" sizes="(max-width: 600px) 480px, 800px" data-src="image.png" alt="Elva dressed as a fairy">

<img src=~aliasImageWithSpace#hash />
62 changes: 61 additions & 1 deletion test/runtime/__snapshots__/getUrl.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,64 @@ exports[`getUrl should work 26`] = `"\\"image<image.png\\""`;

exports[`getUrl should work 27`] = `"\\"image\`image.png\\""`;

exports[`getUrl should work 28`] = `"image.png"`;
exports[`getUrl should work 28`] = `"image.png#hash"`;

exports[`getUrl should work 29`] = `"\\"image image.png#hash\\""`;

exports[`getUrl should work 30`] = `
"\\"image
image.png#hash\\""
`;

exports[`getUrl should work 31`] = `"\\"image image.png#hash\\""`;

exports[`getUrl should work 32`] = `
"\\"image
image.png#hash\\""
`;

exports[`getUrl should work 33`] = `"\\"image image.png#hash\\""`;

exports[`getUrl should work 34`] = `"\\"image\\"image.png#hash\\""`;

exports[`getUrl should work 35`] = `"\\"image'image.png#hash\\""`;

exports[`getUrl should work 36`] = `"\\"image=image.png#hash\\""`;

exports[`getUrl should work 37`] = `"\\"image>image.png#hash\\""`;

exports[`getUrl should work 38`] = `"\\"image<image.png#hash\\""`;

exports[`getUrl should work 39`] = `"\\"image\`image.png#hash\\""`;

exports[`getUrl should work 40`] = `"image.png#hash"`;

exports[`getUrl should work 41`] = `"image image.png#hash"`;

exports[`getUrl should work 42`] = `
"image
image.png#hash"
`;

exports[`getUrl should work 43`] = `"image image.png#hash"`;

exports[`getUrl should work 44`] = `
"image
image.png#hash"
`;

exports[`getUrl should work 45`] = `"image image.png#hash"`;

exports[`getUrl should work 46`] = `"image\\"image.png#hash"`;

exports[`getUrl should work 47`] = `"image'image.png#hash"`;

exports[`getUrl should work 48`] = `"image=image.png#hash"`;

exports[`getUrl should work 49`] = `"image>image.png#hash"`;

exports[`getUrl should work 50`] = `"image<image.png#hash"`;

exports[`getUrl should work 51`] = `"image\`image.png#hash"`;

exports[`getUrl should work 52`] = `"image.png"`;
94 changes: 82 additions & 12 deletions test/runtime/getUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,88 @@ describe('getUrl', () => {
expect(getUrl('image>image.png')).toMatchSnapshot();
expect(getUrl('image<image.png')).toMatchSnapshot();
expect(getUrl('image`image.png')).toMatchSnapshot();
expect(getUrl('image.png', true)).toMatchSnapshot();
expect(getUrl('image\timage.png', true)).toMatchSnapshot();
expect(getUrl('image\nimage.png', true)).toMatchSnapshot();
expect(getUrl('image\fimage.png', true)).toMatchSnapshot();
expect(getUrl('image\rimage.png', true)).toMatchSnapshot();
expect(getUrl('image image.png', true)).toMatchSnapshot();
expect(getUrl('image"image.png', true)).toMatchSnapshot();
expect(getUrl("image'image.png", true)).toMatchSnapshot();
expect(getUrl('image=image.png', true)).toMatchSnapshot();
expect(getUrl('image>image.png', true)).toMatchSnapshot();
expect(getUrl('image<image.png', true)).toMatchSnapshot();
expect(getUrl('image`image.png', true)).toMatchSnapshot();
expect(getUrl('image.png', { maybeNeedQuotes: true })).toMatchSnapshot();
expect(
getUrl('image\timage.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image\nimage.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image\fimage.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image\rimage.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image image.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image"image.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl("image'image.png", { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image=image.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image>image.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image<image.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image`image.png', { maybeNeedQuotes: true })
).toMatchSnapshot();
expect(
getUrl('image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image\timage.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image\nimage.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image\fimage.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image\rimage.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image"image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl("image'image.png", { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image=image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image>image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image<image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(
getUrl('image`image.png', { maybeNeedQuotes: true, hash: '#hash' })
).toMatchSnapshot();
expect(getUrl('image.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image\timage.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image\nimage.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image\fimage.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image\rimage.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image image.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image"image.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl("image'image.png", { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image=image.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image>image.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image<image.png', { hash: '#hash' })).toMatchSnapshot();
expect(getUrl('image`image.png', { hash: '#hash' })).toMatchSnapshot();
expect(
getUrl({ default: 'image.png', __esModule: true })
).toMatchSnapshot();
Expand Down