Skip to content

Commit ea21f56

Browse files
feat(html): add normalizeUrlAttributes and widen two attribute rewrites (#21960)
* feat(html): add `normalizeUrlAttributes` and widen two attribute rewrites Minifying trimmed the whitespace around a URL with nothing able to stop it, though it keeps what the DOM parses and changes the string `getAttribute` hands back like every rewrite that already has a switch. `normalizeUrlAttributes` names it, on by default. `collapseBooleanAttributes` gains the `"all"` tier its own TODO asked for: `true` still rewrites only the spelling the spec canonicalizes, `"all"` any value, including the `checked="false"` that already means checked. `sortTokenLists` now sorts whatever `normalizeListAttributes` says, which is what its description already claimed — putting tokens in another order rewrites the separators between them anyway. A `style` attribute needs no switch of its own: it is an embedded body, so a renderer answering with the list it was given keeps it. That worked only synchronously — where the answer is deferred, an answer nothing beats was read as no answer and the built-in's text stood, which is the path webpack's own minimizer takes. It now leaves the attribute as written, delimiter and all, the way a deferred `srcdoc` already did. Co-authored-by: aryanraj45 <[email protected]> * fix(html): offer a deferred `style` list already at its shortest A list the built-in leaves as it found it — `margin:0 auto`, which cannot go unquoted either — is one `_writeAttribute` declines, and the attribute then took the source path without deferring, so no renderer was ever asked about it. The synchronous path asks whatever the built-in made of it, and now this one does too, with the source standing until an answer arrives. Co-authored-by: aryanraj45 <[email protected]> * docs(html): say what `normalizeUrlAttributes` changes for a reader `getAttribute` hands back the content attribute as written, so trimming one is a value a script comparing those bytes reads differently — which is the reason the switch exists. The description read as though the trim were the attribute's own doing, which is true of resolving the URL and of nothing else. Co-authored-by: aryanraj45 <[email protected]> --------- Co-authored-by: aryanraj45 <[email protected]>
1 parent e29b7ab commit ea21f56

26 files changed

Lines changed: 558 additions & 60 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add `normalizeUrlAttributes`, widen boolean collapsing, free `sortTokenLists`.

‎declarations/WebpackOptions.d.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,10 +2556,10 @@ export interface OptimizationMinimizeCss {
25562556
*/
25572557
export interface OptimizationMinimizeHtml {
25582558
/**
2559-
* Write a boolean attribute spelled with its own name (`disabled="disabled"`) as the bare name the spec canonicalizes it to. On by default.
2559+
* Write a boolean attribute as the bare name its presence already means. The DOM reads `checked` from the attribute being there and never from its value, so `checked="checked"` and `checked=""` are the same element — but `getAttribute` hands back what was written. `true`, the default, rewrites only the spelling the spec itself canonicalizes, the attribute's own name; `"all"` rewrites any value, including the `checked="false"` that already means checked.
25602560
* @since 5.110.0
25612561
*/
2562-
collapseBooleanAttributes?: boolean;
2562+
collapseBooleanAttributes?: "all" | boolean;
25632563
/**
25642564
* Collapse each run of whitespace in text to a single space. Left alone inside `pre`, `textarea` and `listing`, where whitespace renders verbatim. `true` (or `"conservative"`) never removes whitespace entirely — dropping it would join two inline elements that render apart. `"smart"` also drops the whitespace that sits against a block element's edge, where no line box reaches it. `"all"` drops the whitespace at every text node's edges, which does change how adjacent inline elements render.
25652565
* @since 5.110.0
@@ -2605,6 +2605,11 @@ export interface OptimizationMinimizeHtml {
26052605
* @since 5.110.0
26062606
*/
26072607
normalizeNumericAttributes?: boolean;
2608+
/**
2609+
* Drop the ASCII whitespace around a URL value (`href`, `src`, `action`, `poster`, …), which resolving the URL skips over, so the request goes to the same place either way. On by default: `getAttribute` hands back the attribute as written, so a script comparing those bytes is the one this is turned off for.
2610+
* @since 5.111.0
2611+
*/
2612+
normalizeUrlAttributes?: boolean;
26082613
/**
26092614
* Drop an attribute whose empty or all-whitespace value leaves it in the state its absence gives: the globals `class`, `id`, `style`, `dir`, `accesskey`, `itemprop`, `itemref`, `itemtype` and `part`, and every attribute reflecting a token list on the elements the spec defines it for — `rel` on `<a>`, `<area>`, `<form>` and `<link>`, `ping` on `<a>` and `<area>`, `headers` on `<td>` and `<th>`, `blocking` on `<link>`, `<script>` and `<style>`, `sizes` on `<link>`, `for` on `<output>` — where an empty list is no tokens. Anywhere else that spelling is an author attribute whose meaning is a script's, so `<x-foo rel="">` and `<label for="">` keep it. Off by default: an attribute selector matches on presence, so `[class]` stops matching. Never dropped: `title` and `lang`, whose empty value means what absence does not; `sandbox`, whose empty list is the most restrictive state an `<iframe>` has; and an event handler, whose empty body still compiles to a function where absence reads null.
26102615
* @since 5.110.0
@@ -2636,7 +2641,7 @@ export interface OptimizationMinimizeHtml {
26362641
*/
26372642
sortAttributes?: boolean;
26382643
/**
2639-
* Print every space-separated token list the DOM reads as a set — `class`, `rel`, `part`, `sandbox`, `blocking`, `itemprop` / `itemref` / `itemtype`, `<output for>` and `<link sizes>` — in token order. Nothing matching those reads order, so this only makes the same markup compress better across pages. Off by default: a script reading `className` or `rel` back sees the new order. The lists the DOM does not read as a set are left alone whatever this says — `ping` is the order its requests go out in and `accesskey` the order its keys are tried.
2644+
* Print every space-separated token list the DOM reads as a set — `class`, `rel`, `part`, `sandbox`, `blocking`, `itemprop` / `itemref` / `itemtype`, `<output for>` and `<link sizes>` — in token order. Nothing matching those reads order, so this only makes the same markup compress better across pages. Off by default: a script reading `className` or `rel` back sees the new order. Asked for on its own: a list this reorders is rewritten whether or not `normalizeListAttributes` is on, since another order is another spelling. The lists the DOM does not read as a set are left alone whatever this says — `ping` is the order its requests go out in and `accesskey` the order its keys are tried.
26402645
* @since 5.110.0
26412646
*/
26422647
sortTokenLists?: boolean;

‎lib/html/htmlMinify.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ const htmlMinify = async (input, sourceMap, minimizerOptions = {}) => {
4343
require(/** @type {string} */ ("webpack"))
4444
);
4545

46-
// TODO widen the value rewrites the switches below name: a boolean attribute
47-
// spelled any other way than its own name, an enumerated value the spec does
48-
// not name.
46+
// TODO widen the value rewrites the switches below name: an enumerated
47+
// value the spec does not name.
4948
const {
5049
askEmbeddedRenderer,
5150
collectEmbeddedDiagnostics,

‎lib/html/syntax.js‎

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8914,12 +8914,13 @@ const mirrorSelectedContent = (node, select) => {
89148914
* What the minifying printer may rewrite. Every entry is on unless it is
89158915
* `false`, so a document one transform breaks can still be minified by the rest.
89168916
* @typedef {object} HtmlTransformOptions
8917-
* @property {boolean=} collapseBooleanAttributes write a boolean attribute spelled with its own name (`disabled="disabled"`) as the bare name
8917+
* @property {(boolean | "all")=} collapseBooleanAttributes write a boolean attribute as the bare name its presence already means; `true` only the spellings the spec canonicalizes, `"all"` any value
89188918
* @property {(boolean | "all" | "some" | string | RegExp | ((comment: string) => boolean))=} comments which comments survive: `"some"` (the default) the ones that carry something, `true` / `"all"` every one, `false` none, or the ones a pattern matches / a predicate accepts, over the comment's own text. A comment a parser or a server reads is kept whatever this says
89198919
* @property {boolean=} normalizeAttributeQuotes drop or re-pick an attribute value's quotes
89208920
* @property {boolean=} normalizeEnumeratedAttributes fold an enumerated value to the keyword it names
89218921
* @property {boolean=} normalizeListAttributes normalize a space-, comma- or descriptor-separated list value (`class`, `rel`, `srcset`, `sizes`, the viewport `content`)
89228922
* @property {boolean=} normalizeNumericAttributes write an integer attribute the one way its rules read it
8923+
* @property {boolean=} normalizeUrlAttributes drop the whitespace around a URL value (`href`, `src`, `action`, …), which resolving the URL skips over
89238924
* @property {boolean=} removeOptionalTags leave out an optional tag other than the `<html>` / `<head>` / `<body>` shell, which is `removeImpliedTags`
89248925
*/
89258926

@@ -9417,6 +9418,7 @@ const _ALL_TRANSFORMS = Object.freeze({
94179418
normalizeEnumeratedAttributes: true,
94189419
normalizeListAttributes: true,
94199420
normalizeNumericAttributes: true,
9421+
normalizeUrlAttributes: true,
94209422
removeOptionalTags: true
94219423
});
94229424

@@ -10409,9 +10411,13 @@ const _rewriteAttributeValue = (element, name, raw, viewport, enumeratedOn) => {
1040910411
return _transforms.normalizeListAttributes ? _normalizeSrcset(raw) : raw;
1041010412
}
1041110413
if (name === "style") return _minifyStyleAttribute(raw);
10412-
const tokenListOn = _transforms.normalizeListAttributes
10413-
? TOKEN_LIST_ATTRIBUTES.get(name)
10414-
: undefined;
10414+
// A list `sortTokenLists` reorders is read whatever `normalizeListAttributes`
10415+
// says: another order rewrites the separators between the tokens anyway.
10416+
const tokenListOn =
10417+
_transforms.normalizeListAttributes ||
10418+
(_sortTokenLists && DOM_TOKEN_LIST_ATTRIBUTES.has(name))
10419+
? TOKEN_LIST_ATTRIBUTES.get(name)
10420+
: undefined;
1041510421
if (
1041610422
tokenListOn !== undefined &&
1041710423
(tokenListOn === null || tokenListOn.has(element))
@@ -10426,7 +10432,9 @@ const _rewriteAttributeValue = (element, name, raw, viewport, enumeratedOn) => {
1042610432
if (viewport && name === "content") return _normalizeViewport(raw);
1042710433
if (COMMA_LIST_ATTRIBUTES.has(name)) return _normalizeCommaList(raw);
1042810434
}
10429-
const urlOn = URL_ATTRIBUTES.get(name);
10435+
const urlOn = _transforms.normalizeUrlAttributes
10436+
? URL_ATTRIBUTES.get(name)
10437+
: undefined;
1043010438
if (urlOn !== undefined && (urlOn === null || urlOn.has(element))) {
1043110439
return _asciiTrim(raw);
1043210440
}
@@ -11038,9 +11046,10 @@ const _minifyOpenTag = (path, open) => {
1103811046
(rawValue === "" ||
1103911047
(html &&
1104011048
_transforms.collapseBooleanAttributes &&
11041-
rawValue.length === attributeName.length &&
11042-
_isBooleanAttribute(elementName, attributeName) &&
11043-
_asciiLowerCase(rawValue) === attributeName))
11049+
(_transforms.collapseBooleanAttributes === "all" ||
11050+
(rawValue.length === attributeName.length &&
11051+
_asciiLowerCase(rawValue) === attributeName)) &&
11052+
_isBooleanAttribute(elementName, attributeName)))
1104411053
) {
1104511054
out += ` ${attributeName}`;
1104611055
unquotedTail = false;
@@ -11075,33 +11084,45 @@ const _minifyOpenTag = (path, open) => {
1107511084
? writeStyle(declarations !== null ? declarations : value)
1107611085
: _writeAttribute(attributeName, value, rawValue, spellable);
1107711086

11087+
// Offered whatever the built-in made of it: a list already at its
11088+
// shortest is one `_writeAttribute` declines, not one to ask nobody about.
11089+
if (isStyle && _deferEmbeddedSource !== undefined) {
11090+
// Declining leaves the attribute as written, delimiter and all — an
11091+
// answer nothing beats is the renderer keeping what it was given.
11092+
const delimiter = open[valueStart - tagStart - 1];
11093+
const asWritten = ` ${attributeName}=${delimiter}${rawValue}${delimiter}`;
11094+
// What stands until an answer arrives, and the tag ending it commits
11095+
// to: the built-in's text where it beat the source, else the source.
11096+
const standing = written !== null ? written.text : asWritten;
11097+
const standingTail = written !== null && written.unquotedTail;
11098+
11099+
// The list is deferred, not the shape around it: the tag ending is
11100+
// read back before an answer arrives, so one changing it is declined.
11101+
out += _deferEmbedded(
11102+
// Decoded when the source spelled it with references, since that
11103+
// is the list itself rather than one attribute's spelling.
11104+
spellable !== null ? spellable : rawValue,
11105+
CSS_TYPE,
11106+
(rendered) => {
11107+
if (typeof rendered !== "string") return standing;
11108+
11109+
const answered = writeStyle(rendered);
11110+
// Declining ends the value quoted: this is the branch the
11111+
// source spelled with a delimiter.
11112+
const tail = answered !== null && answered.unquotedTail;
11113+
11114+
if (tailIsRead && tail !== standingTail) return standing;
11115+
11116+
return answered !== null ? answered.text : asWritten;
11117+
},
11118+
BLOCK_CONTENTS
11119+
);
11120+
unquotedTail = standingTail;
11121+
consumedEnd = valueEnd;
11122+
continue;
11123+
}
1107811124
if (written !== null) {
11079-
// The list is deferred, not the shape around it: whether this value
11080-
// leaves the tag ending unquoted is read back when the tag closes and
11081-
// cannot wait for an answer, so one that would land in the other shape
11082-
// is declined and the built-in's text stands.
11083-
out =
11084-
isStyle && _deferEmbeddedSource !== undefined
11085-
? out +
11086-
_deferEmbedded(
11087-
// Decoded when the source spelled it with references, since
11088-
// that is the list itself rather than one attribute's spelling.
11089-
spellable !== null ? spellable : rawValue,
11090-
CSS_TYPE,
11091-
(rendered) => {
11092-
if (typeof rendered !== "string") return written.text;
11093-
11094-
const answered = writeStyle(rendered);
11095-
11096-
return answered !== null &&
11097-
(!tailIsRead ||
11098-
answered.unquotedTail === written.unquotedTail)
11099-
? answered.text
11100-
: written.text;
11101-
},
11102-
BLOCK_CONTENTS
11103-
)
11104-
: out + written.text;
11125+
out += written.text;
1110511126
unquotedTail = written.unquotedTail;
1110611127
consumedEnd = valueEnd;
1110711128
continue;

‎schemas/WebpackOptions.check.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎schemas/WebpackOptions.json‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3903,8 +3903,15 @@
39033903
"additionalProperties": false,
39043904
"properties": {
39053905
"collapseBooleanAttributes": {
3906-
"description": "Write a boolean attribute spelled with its own name (`disabled=\"disabled\"`) as the bare name the spec canonicalizes it to. On by default.",
3907-
"type": "boolean",
3906+
"description": "Write a boolean attribute as the bare name its presence already means. The DOM reads `checked` from the attribute being there and never from its value, so `checked=\"checked\"` and `checked=\"\"` are the same element — but `getAttribute` hands back what was written. `true`, the default, rewrites only the spelling the spec itself canonicalizes, the attribute's own name; `\"all\"` rewrites any value, including the `checked=\"false\"` that already means checked.",
3907+
"anyOf": [
3908+
{
3909+
"enum": ["all"]
3910+
},
3911+
{
3912+
"type": "boolean"
3913+
}
3914+
],
39083915
"added": "5.110.0"
39093916
},
39103917
"collapseWhitespace": {
@@ -3972,6 +3979,11 @@
39723979
"type": "boolean",
39733980
"added": "5.110.0"
39743981
},
3982+
"normalizeUrlAttributes": {
3983+
"description": "Drop the ASCII whitespace around a URL value (`href`, `src`, `action`, `poster`, …), which resolving the URL skips over, so the request goes to the same place either way. On by default: `getAttribute` hands back the attribute as written, so a script comparing those bytes is the one this is turned off for.",
3984+
"type": "boolean",
3985+
"added": "5.111.0"
3986+
},
39753987
"removeEmptyAttributes": {
39763988
"description": "Drop an attribute whose empty or all-whitespace value leaves it in the state its absence gives: the globals `class`, `id`, `style`, `dir`, `accesskey`, `itemprop`, `itemref`, `itemtype` and `part`, and every attribute reflecting a token list on the elements the spec defines it for — `rel` on `<a>`, `<area>`, `<form>` and `<link>`, `ping` on `<a>` and `<area>`, `headers` on `<td>` and `<th>`, `blocking` on `<link>`, `<script>` and `<style>`, `sizes` on `<link>`, `for` on `<output>` — where an empty list is no tokens. Anywhere else that spelling is an author attribute whose meaning is a script's, so `<x-foo rel=\"\">` and `<label for=\"\">` keep it. Off by default: an attribute selector matches on presence, so `[class]` stops matching. Never dropped: `title` and `lang`, whose empty value means what absence does not; `sandbox`, whose empty list is the most restrictive state an `<iframe>` has; and an event handler, whose empty body still compiles to a function where absence reads null.",
39773989
"type": "boolean",
@@ -4017,7 +4029,7 @@
40174029
"added": "5.110.0"
40184030
},
40194031
"sortTokenLists": {
4020-
"description": "Print every space-separated token list the DOM reads as a set — `class`, `rel`, `part`, `sandbox`, `blocking`, `itemprop` / `itemref` / `itemtype`, `<output for>` and `<link sizes>` — in token order. Nothing matching those reads order, so this only makes the same markup compress better across pages. Off by default: a script reading `className` or `rel` back sees the new order. The lists the DOM does not read as a set are left alone whatever this says — `ping` is the order its requests go out in and `accesskey` the order its keys are tried.",
4032+
"description": "Print every space-separated token list the DOM reads as a set — `class`, `rel`, `part`, `sandbox`, `blocking`, `itemprop` / `itemref` / `itemtype`, `<output for>` and `<link sizes>` — in token order. Nothing matching those reads order, so this only makes the same markup compress better across pages. Off by default: a script reading `className` or `rel` back sees the new order. Asked for on its own: a list this reorders is rewritten whether or not `normalizeListAttributes` is on, since another order is another spelling. The lists the DOM does not read as a set are left alone whatever this says — `ping` is the order its requests go out in and `accesskey` the order its keys are tried.",
40214033
"type": "boolean",
40224034
"added": "5.110.0"
40234035
}

0 commit comments

Comments
 (0)