Skip to content

Commit 9ad8a76

Browse files
committed
fix(html): preserve template identifier case in attributes
1 parent 1043489 commit 9ad8a76

8 files changed

Lines changed: 34 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Preserve case-sensitive template identifiers when minifying HTML tag attributes.

‎lib/html/syntax.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10998,11 +10998,12 @@ const _minifyOpenTag = (path, open) => {
1099810998
out += separator;
1099910999
openedSpaceAt = out.length;
1100011000
}
11001-
// The parse interned exactly `_asciiLowerCase(<name slice>)` — read it back
11002-
// rather than re-slicing and re-folding. Foreign content keeps source case.
11003-
const attributeName = html
11004-
? path.attributeName(attribute)
11005-
: open.slice(nameStart - tagStart, nameEnd - tagStart);
11001+
// Template identifiers are parsed as attribute names too, but their case
11002+
// belongs to the template language. Keep their source spelling like SVG's.
11003+
const attributeName =
11004+
html && !verbatim
11005+
? path.attributeName(attribute)
11006+
: open.slice(nameStart - tagStart, nameEnd - tagStart);
1100611007
const valueStart = path.attributeValueStart(attribute);
1100711008
if (valueStart === -1) {
1100811009
// Valueless is the empty value, so it goes with the empty ones.

‎test/configCases/html/minimize-attributes/__snapshots__/ConfigCacheTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ b\\">newline in a value</p>
3333
<p a=1>duplicate attribute</p>
3434
<p class=\\"a b\\">repeated class token, folded</p>
3535
36-
<input type=text {% if required %} required{% endif %}>
36+
<input type=text {% if isRequired %} required{% endif %}>
3737
<p class=\\"btn{% if active %} btn-active{% endif %}\\">the same list, as text</p>
3838
<p data-slash=/end/>value ending in a slash</p>
3939
<a href=x/>unquoted value ending in a slash</a>

‎test/configCases/html/minimize-attributes/__snapshots__/ConfigTest.snap‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ b\\">newline in a value</p>
3333
<p a=1>duplicate attribute</p>
3434
<p class=\\"a b\\">repeated class token, folded</p>
3535
36-
<input type=text {% if required %} required{% endif %}>
36+
<input type=text {% if isRequired %} required{% endif %}>
3737
<p class=\\"btn{% if active %} btn-active{% endif %}\\">the same list, as text</p>
3838
<p data-slash=/end/>value ending in a slash</p>
3939
<a href=x/>unquoted value ending in a slash</a>

‎test/configCases/html/minimize-attributes/page.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<p class="a b a">repeated class token, folded</p>
5050
<!-- `{%` / `%}` tokenize as attribute names and repeat, so the bytes between
5151
the names kept are copied from the source rather than rebuilt (#22013) -->
52-
<input type="text"{% if required %} required{% endif %}>
52+
<input type="text"{% if isRequired %} required{% endif %}>
5353
<p class="btn{% if active %} btn-active{% endif %}">the same list, as text</p>
5454
<p data-slash="/end/">value ending in a slash</p>
5555
<a href=x/>unquoted value ending in a slash</a>

‎test/configCases/html/minimize-attributes/test.config.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = {
1515
path.join(options.output.path, "page.html"),
1616
"utf8"
1717
);
18+
const condition = /\{% if (\w+) %\} required\{% endif %\}/.exec(page);
19+
expect(condition && condition[1]).toBe("isRequired");
1820
expect(page).toMatchSnapshot();
1921

2022
// An `&` keeps its escape only where the tokenizer would read on into a

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5699,13 +5699,28 @@ describe("SourceProcessor — a duplicate attribute name", () => {
56995699
for (const tag of [
57005700
'<input type="text"{% if x %} a{% endif %}>',
57015701
"<input type='text'{% if x %} a{% endif %}>",
5702-
'<INPUT TYPE="TEXT"{% if x %} a{% endif %}>',
57035702
'<input type="&#x74;ext"{% if x %} a{% endif %}>'
57045703
]) {
57055704
expect(minify(tag)).toBe("<input type=text {% if x %} a{% endif %}>");
57065705
}
57075706
});
57085707

5708+
it("preserves case-sensitive template names", () => {
5709+
for (const condition of ["isRequired", "Foo", "options.isRequired"]) {
5710+
const printed = minify(
5711+
`<input type="text"{% if ${condition} %} required{% endif %}>`
5712+
);
5713+
const match = /\{% if ([^ ]+) %\}/.exec(printed);
5714+
expect(match && match[1]).toBe(condition);
5715+
}
5716+
});
5717+
5718+
it("preserves source attribute names on a template tag", () => {
5719+
expect(
5720+
minify('<INPUT TYPE="TEXT"{% if isRequired %} required{% endif %}>')
5721+
).toMatchSnapshot();
5722+
});
5723+
57095724
it("separates an unquoted value from what the source wrote next", () => {
57105725
// Dropping the quotes leaves nothing between the value and the template, and
57115726
// an unquoted value reads on to the next ASCII whitespace or `>`.

‎test/unitCases/__snapshots__/HtmlSyntax.unittest.js.snap‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3+
exports[`SourceProcessor — a duplicate attribute name preserves source attribute names on a template tag 1`] = `"<input TYPE=TEXT {% if isRequired %} required{% endif %}>"`;
4+
35
exports[`SourceProcessor — beautifying should beautify a comment 1`] = `"<div><!--c--></div>"`;
46
57
exports[`SourceProcessor — beautifying should beautify a form the table kept 1`] = `"<table><p><form></form></p></table>"`;

0 commit comments

Comments
 (0)