Skip to content

Commit d0b0150

Browse files
fix: adding quotes when necessary for unquoted sources (#247)
1 parent e3727ab commit d0b0150

File tree

13 files changed

+788
-180
lines changed

13 files changed

+788
-180
lines changed

src/plugins/attribute-plugin.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -415,15 +415,20 @@ export default (options) =>
415415
{
416416
attributesMeta: {},
417417
onattribute(name, value) {
418-
this.attributesMeta[name] = {
419-
// eslint-disable-next-line no-underscore-dangle
420-
startIndex: parser._tokenizer._index - value.length,
421-
};
418+
// eslint-disable-next-line no-underscore-dangle
419+
const endIndex = parser._tokenizer._index;
420+
const startIndex = endIndex - value.length;
421+
const unquoted = html[endIndex] !== '"' && html[endIndex] !== "'";
422+
423+
this.attributesMeta[name] = { startIndex, unquoted };
422424
},
423425
onopentag(tag, attributes) {
424426
Object.keys(attributes).forEach((attribute) => {
425427
const value = attributes[attribute];
426-
const valueStartIndex = this.attributesMeta[attribute].startIndex;
428+
const {
429+
startIndex: valueStartIndex,
430+
unquoted,
431+
} = this.attributesMeta[attribute];
427432

428433
// TODO use code frame for errors
429434

@@ -458,7 +463,7 @@ export default (options) =>
458463

459464
const startIndex = valueStartIndex + source.startIndex;
460465

461-
sources.push({ startIndex, value: source.value });
466+
sources.push({ startIndex, value: source.value, unquoted });
462467
});
463468

464469
return;
@@ -484,7 +489,7 @@ export default (options) =>
484489

485490
const startIndex = valueStartIndex + source.startIndex;
486491

487-
sources.push({ startIndex, value: source.value });
492+
sources.push({ startIndex, value: source.value, unquoted });
488493
});
489494

490495
this.attributesMeta = {};
@@ -510,7 +515,8 @@ export default (options) =>
510515
let index = 0;
511516

512517
for (const source of sources) {
513-
const uri = parse(source.value);
518+
const { value, startIndex, unquoted } = source;
519+
const uri = parse(value);
514520

515521
if (typeof uri.hash !== 'undefined') {
516522
uri.hash = null;
@@ -526,16 +532,17 @@ export default (options) =>
526532
type: 'attribute',
527533
replacementName,
528534
source: decodeURIComponent(source.value),
535+
unquoted,
529536
},
530537
});
531538

532539
// eslint-disable-next-line no-param-reassign
533540
html =
534-
html.substr(0, source.startIndex + offset) +
541+
html.substr(0, startIndex + offset) +
535542
replacementName +
536-
html.substr(source.startIndex + source.value.length + offset);
543+
html.substr(startIndex + value.length + offset);
537544

538-
offset += replacementName.length - source.value.length;
545+
offset += replacementName.length - value.length;
539546
index += 1;
540547
}
541548

src/runtime/getUrl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
module.exports = (url) => {
1+
module.exports = (url, maybeNeedQuotes) => {
22
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
33
url = url && url.__esModule ? url.default : url;
44

55
if (typeof url !== 'string') {
66
return url;
77
}
88

9-
// if (/[\t\n\f\r "'=<>`]/.test(url)) {
10-
// return `"${url}"`;
11-
// }
9+
if (maybeNeedQuotes && /[\t\n\f\r "'=<>`]/.test(url)) {
10+
return `"${url}"`;
11+
}
1212

1313
return url;
1414
};

src/utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ export function getExportCode(html, replacers, options) {
7171
}
7272

7373
for (const replacer of replacers) {
74-
const { replacementName } = replacer;
74+
const { replacementName, unquoted } = replacer;
7575

7676
exportCode = exportCode.replace(
7777
new RegExp(replacementName, 'g'),
78-
() => `" + ___HTML_LOADER_GET_URL_IMPORT___(${replacementName}) + "`
78+
() =>
79+
`" + ___HTML_LOADER_GET_URL_IMPORT___(${replacementName}${
80+
unquoted ? ', true' : ''
81+
}) + "`
7982
);
8083
}
8184

test/__snapshots__/attributes-option.test.js.snap

Lines changed: 312 additions & 74 deletions
Large diffs are not rendered by default.

test/__snapshots__/esModule-option.test.js.snap

Lines changed: 111 additions & 27 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 37 additions & 9 deletions
Large diffs are not rendered by default.

test/__snapshots__/minimize-option.test.js.snap

Lines changed: 166 additions & 34 deletions
Large diffs are not rendered by default.

test/__snapshots__/root-option.test.js.snap

Lines changed: 74 additions & 18 deletions
Large diffs are not rendered by default.

test/fixtures/alias-image.png

-76.3 KB
Binary file not shown.

test/fixtures/simple.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,17 @@ <h2>An Ordered HTML List</h2>
210210
<script>
211211
function test() {}
212212
</script>
213+
214+
<img src=image.png />
215+
<img src="image image.png" />
216+
<img src='image image.png' />
217+
<img src=~aliasImageWithSpace />
218+
<img srcset=image.png />
219+
<img srcset="image%20image.png" />
220+
<img srcset='image%20image.png' />
221+
<img srcset=~aliasImageWithSpace />
222+
<img srcset="image.png 480w, image%20image.png 640w, ~aliasImageWithSpace 800w" sizes="(max-width: 600px) 480px, 800px" src="image.png" alt="Elva dressed as a fairy">
223+
<img src = image.png />
224+
<img src = ~aliasImageWithSpace />
225+
<img src = "~aliasImageWithSpace" />
226+
<img src = '~aliasImageWithSpace' />

0 commit comments

Comments
 (0)