@ohos-ports/html-minifier-next 8.4.2-beta.0 → 8.10.8-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -33
- package/cli.js +70 -54
- package/dist/types/htmlminifier.d.ts +87 -33
- package/dist/types/htmlminifier.d.ts.map +1 -1
- package/dist/types/htmlparser.d.ts +4 -1
- package/dist/types/htmlparser.d.ts.map +1 -1
- package/dist/types/lib/attributes.d.ts +12 -10
- package/dist/types/lib/attributes.d.ts.map +1 -1
- package/dist/types/lib/constants.d.ts +38 -3
- package/dist/types/lib/constants.d.ts.map +1 -1
- package/dist/types/lib/content.d.ts.map +1 -1
- package/dist/types/lib/elements.d.ts.map +1 -1
- package/dist/types/lib/fragments.d.ts +1 -0
- package/dist/types/lib/fragments.d.ts.map +1 -1
- package/dist/types/lib/option-definitions.d.ts.map +1 -1
- package/dist/types/lib/options.d.ts +14 -46
- package/dist/types/lib/options.d.ts.map +1 -1
- package/dist/types/lib/unused-css.d.ts +34 -4
- package/dist/types/lib/unused-css.d.ts.map +1 -1
- package/dist/types/lib/urls.d.ts.map +1 -1
- package/dist/types/lib/utils.d.ts +31 -1
- package/dist/types/lib/utils.d.ts.map +1 -1
- package/dist/types/lib/whitespace.d.ts +57 -5
- package/dist/types/lib/whitespace.d.ts.map +1 -1
- package/dist/types/presets.d.ts +1 -2
- package/dist/types/presets.d.ts.map +1 -1
- package/dist/types/tokenchain.d.ts.map +1 -1
- package/html-minifier-next.schema.json +15 -7
- package/package.json +17 -18
- package/src/htmlminifier.js +1103 -392
- package/src/htmlparser.js +452 -200
- package/src/lib/attributes.js +201 -112
- package/src/lib/constants.js +130 -19
- package/src/lib/content.js +1 -6
- package/src/lib/elements.js +17 -12
- package/src/lib/file-pool.js +18 -2
- package/src/lib/file-write.js +87 -0
- package/src/lib/fragments.js +74 -8
- package/src/lib/option-definitions.js +17 -10
- package/src/lib/options.js +151 -71
- package/src/lib/unused-css.js +145 -27
- package/src/lib/urls.js +0 -9
- package/src/lib/utils.js +210 -45
- package/src/lib/whitespace.js +173 -63
- package/src/presets.js +1 -2
- package/src/tokenchain.js +1 -4
package/src/lib/attributes.js
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
RE_ATTR_WS_CHECK,
|
|
10
10
|
RE_ATTR_WS_COLLAPSE,
|
|
11
11
|
RE_ATTR_WS_TRIM,
|
|
12
|
+
RE_WS_CHAR,
|
|
12
13
|
generalDefaults,
|
|
13
14
|
tagDefaults,
|
|
14
15
|
executableScriptsMimetypes,
|
|
@@ -22,30 +23,41 @@ import {
|
|
|
22
23
|
} from './constants.js';
|
|
23
24
|
import { trimWhitespace, collapseWhitespaceAll } from './whitespace.js';
|
|
24
25
|
import { shouldMinifyInnerHTML } from './options.js';
|
|
25
|
-
import {
|
|
26
|
-
import { identity, isThenable } from './utils.js';
|
|
26
|
+
import { collectUsage } from './unused-css.js';
|
|
27
|
+
import { escapeXML, identity, isThenable, lowercase } from './utils.js';
|
|
27
28
|
|
|
28
29
|
/** @import { ProcessedOptions } from './options.js' */
|
|
29
30
|
|
|
30
|
-
// Type definitions
|
|
31
|
-
|
|
32
31
|
/**
|
|
33
32
|
* @typedef {{ name: string, value?: string | undefined, quote?: string, customAssign?: string, customOpen?: string, customClose?: string }} HTMLAttribute
|
|
34
33
|
* Internal counterpart of the public typedef in htmlminifier.js—keep in sync.
|
|
35
34
|
*/
|
|
36
35
|
|
|
37
36
|
// Lazy-load entities (used for `decodeEntities`, event-handler attribute
|
|
38
|
-
// decoding before `minifyJS`,
|
|
37
|
+
// decoding before `minifyJS`, `srcdoc` decoding/re-encoding, and values SVGO reads)
|
|
39
38
|
|
|
40
39
|
/** @type {Promise<Function> | undefined} */
|
|
41
40
|
let decodeHTMLStrictPromise;
|
|
41
|
+
/** @type {((text: string) => string) | undefined} The resolved `decodeHTMLStrict`, once the lazy import above has settled */
|
|
42
|
+
let decodeHTMLStrictFn;
|
|
42
43
|
async function getDecodeHTMLStrict() {
|
|
43
44
|
if (!decodeHTMLStrictPromise) {
|
|
44
|
-
decodeHTMLStrictPromise = import('entities').then(m => m.decodeHTMLStrict);
|
|
45
|
+
decodeHTMLStrictPromise = import('entities').then(m => (decodeHTMLStrictFn = m.decodeHTMLStrict));
|
|
45
46
|
}
|
|
46
47
|
return decodeHTMLStrictPromise;
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
/** @type {Promise<Function> | undefined} */
|
|
51
|
+
let decodeHTMLAttributePromise;
|
|
52
|
+
/** @type {((text: string) => string) | undefined} The resolved `decodeHTMLAttribute`, once the lazy import above has settled */
|
|
53
|
+
let decodeHTMLAttributeFn;
|
|
54
|
+
async function getDecodeHTMLAttribute() {
|
|
55
|
+
if (!decodeHTMLAttributePromise) {
|
|
56
|
+
decodeHTMLAttributePromise = import('entities').then(m => (decodeHTMLAttributeFn = m.decodeHTMLAttribute));
|
|
57
|
+
}
|
|
58
|
+
return decodeHTMLAttributePromise;
|
|
59
|
+
}
|
|
60
|
+
|
|
49
61
|
/** @type {Promise<Function> | undefined} */
|
|
50
62
|
let escapeAttributePromise;
|
|
51
63
|
async function getEscapeAttribute() {
|
|
@@ -73,12 +85,13 @@ function isIgnoredComment(text, options) {
|
|
|
73
85
|
/**
|
|
74
86
|
* @param {string} attrName
|
|
75
87
|
* @param {{customEventAttributes?: RegExp[]}} options
|
|
88
|
+
* @param {string} [attrNameOut] - The name as written, for the patterns
|
|
76
89
|
*/
|
|
77
|
-
function isEventAttribute(attrName, options) {
|
|
90
|
+
function isEventAttribute(attrName, options, attrNameOut = attrName) {
|
|
78
91
|
const patterns = options.customEventAttributes;
|
|
79
92
|
if (patterns) {
|
|
80
93
|
for (const pattern of patterns) {
|
|
81
|
-
if (pattern.test(
|
|
94
|
+
if (pattern.test(attrNameOut)) {
|
|
82
95
|
return true;
|
|
83
96
|
}
|
|
84
97
|
}
|
|
@@ -135,6 +148,16 @@ function deduplicateAttributes(attrs, caseSensitive) {
|
|
|
135
148
|
return attrs;
|
|
136
149
|
}
|
|
137
150
|
|
|
151
|
+
// Names that can have a general or tag-specific default value, for the fast pre-check
|
|
152
|
+
// in `isAttributeRedundant` (a Set lookup beats two `Object.hasOwn` plus property reads);
|
|
153
|
+
// the legacy rules (`script[language|charset]`, `a[name]`) are named here explicitly
|
|
154
|
+
const redundantDefaultNames = new Set([...Object.keys(generalDefaults), 'language', 'charset', 'name']);
|
|
155
|
+
for (const defaults of Object.values(tagDefaults)) {
|
|
156
|
+
for (const name of Object.keys(defaults)) {
|
|
157
|
+
redundantDefaultNames.add(name);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
138
161
|
/**
|
|
139
162
|
* @param {string} tag
|
|
140
163
|
* @param {string} attrName
|
|
@@ -142,27 +165,29 @@ function deduplicateAttributes(attrs, caseSensitive) {
|
|
|
142
165
|
* @param {HTMLAttribute[]} attrs
|
|
143
166
|
*/
|
|
144
167
|
function isAttributeRedundant(tag, attrName, attrValue, attrs) {
|
|
145
|
-
// Fast
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
168
|
+
// Fast path: Check if this element–attribute combination can possibly be redundant before
|
|
169
|
+
// normalizing the value; own properties only, so `constructor` has no default
|
|
170
|
+
if (!redundantDefaultNames.has(attrName)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
const generalDefault = Object.hasOwn(generalDefaults, attrName)
|
|
174
|
+
? /** @type {Record<string, string>} */ (generalDefaults)[attrName]
|
|
175
|
+
: undefined;
|
|
176
|
+
const tagAttrDefaults = Object.hasOwn(tagDefaults, tag)
|
|
177
|
+
? /** @type {Record<string, string>} */ (/** @type {Record<string, unknown>} */ (tagDefaults)[tag])
|
|
178
|
+
: undefined;
|
|
179
|
+
const tagDefault = tagAttrDefaults && Object.hasOwn(tagAttrDefaults, attrName) ? tagAttrDefaults[attrName] : undefined;
|
|
153
180
|
|
|
154
|
-
// Check for legacy attribute rules (element- and attribute-specific)
|
|
155
181
|
const isLegacyAttr = (tag === 'script' && (attrName === 'language' || attrName === 'charset')) || (tag === 'a' && attrName === 'name');
|
|
156
182
|
|
|
157
|
-
//
|
|
158
|
-
if (
|
|
183
|
+
// Without a default for this very attribute, nor a legacy rule, the value cannot be redundant
|
|
184
|
+
if (generalDefault === undefined && tagDefault === undefined && !isLegacyAttr) {
|
|
159
185
|
return false;
|
|
160
186
|
}
|
|
161
187
|
|
|
162
188
|
// Value needs to be checked, so normalize it
|
|
163
189
|
attrValue = attrValue ? trimWhitespace(attrValue.toLowerCase()) : '';
|
|
164
190
|
|
|
165
|
-
// Legacy attribute checks
|
|
166
191
|
if (tag === 'script' && attrName === 'language' && attrValue === 'javascript') {
|
|
167
192
|
return true;
|
|
168
193
|
}
|
|
@@ -173,13 +198,7 @@ function isAttributeRedundant(tag, attrName, attrValue, attrs) {
|
|
|
173
198
|
return true;
|
|
174
199
|
}
|
|
175
200
|
|
|
176
|
-
|
|
177
|
-
if (hasGeneralDefault && /** @type {Record<string, string>} */ (generalDefaults)[attrName] === attrValue) {
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Check tag-specific defaults
|
|
182
|
-
return tagHasDefaults && /** @type {Record<string, string>} */ (/** @type {Record<string, unknown>} */ (tagDefaults)[tag])[attrName] === attrValue;
|
|
201
|
+
return attrValue === generalDefault || attrValue === tagDefault;
|
|
183
202
|
}
|
|
184
203
|
|
|
185
204
|
function isScriptTypeAttribute(attrValue = '') {
|
|
@@ -230,30 +249,47 @@ function isStyleElement(tag, attrs) {
|
|
|
230
249
|
}
|
|
231
250
|
|
|
232
251
|
/**
|
|
252
|
+
* Whether an attribute collapses to its name; takes name and value as written, lower-casing
|
|
253
|
+
* the value only for the few names that depend on it
|
|
254
|
+
*
|
|
233
255
|
* @param {string} attrName
|
|
234
256
|
* @param {string} attrValue
|
|
235
257
|
*/
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
258
|
+
function collapsesToBooleanName(attrName, attrValue) {
|
|
259
|
+
const name = attrName.toLowerCase();
|
|
260
|
+
if (isSimpleBoolean.has(name)) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
const values = collapsibleValues.get(name);
|
|
264
|
+
if (values) {
|
|
265
|
+
return values.has(attrValue.toLowerCase());
|
|
266
|
+
}
|
|
267
|
+
return name === 'draggable' && !isBooleanValue.has(attrValue.toLowerCase());
|
|
240
268
|
}
|
|
241
269
|
|
|
242
270
|
const uriTypeAttributes = new Map([
|
|
243
271
|
['a', new Set(['href'])],
|
|
244
272
|
['area', new Set(['href'])],
|
|
245
|
-
['
|
|
273
|
+
['audio', new Set(['src'])],
|
|
246
274
|
['base', new Set(['href'])],
|
|
247
|
-
['img', new Set(['src', 'longdesc', 'usemap'])],
|
|
248
|
-
['object', new Set(['classid', 'codebase', 'data', 'usemap'])],
|
|
249
|
-
['q', new Set(['cite'])],
|
|
250
275
|
['blockquote', new Set(['cite'])],
|
|
251
|
-
['
|
|
276
|
+
['button', new Set(['formaction'])],
|
|
252
277
|
['del', new Set(['cite'])],
|
|
278
|
+
['embed', new Set(['src'])],
|
|
253
279
|
['form', new Set(['action'])],
|
|
254
|
-
['
|
|
280
|
+
['frame', new Set(['longdesc', 'src'])],
|
|
255
281
|
['head', new Set(['profile'])],
|
|
256
|
-
['
|
|
282
|
+
['iframe', new Set(['src'])],
|
|
283
|
+
['img', new Set(['longdesc', 'src', 'usemap'])],
|
|
284
|
+
['input', new Set(['formaction', 'src', 'usemap'])],
|
|
285
|
+
['ins', new Set(['cite'])],
|
|
286
|
+
['link', new Set(['href'])],
|
|
287
|
+
['object', new Set(['classid', 'codebase', 'data', 'usemap'])],
|
|
288
|
+
['q', new Set(['cite'])],
|
|
289
|
+
['script', new Set(['src'])],
|
|
290
|
+
['source', new Set(['src'])],
|
|
291
|
+
['track', new Set(['src'])],
|
|
292
|
+
['video', new Set(['poster', 'src'])]
|
|
257
293
|
]);
|
|
258
294
|
|
|
259
295
|
/**
|
|
@@ -268,15 +304,15 @@ function isUriTypeAttribute(attrName, tag) {
|
|
|
268
304
|
const numberTypeAttributes = new Map([
|
|
269
305
|
['a', new Set(['tabindex'])],
|
|
270
306
|
['area', new Set(['tabindex'])],
|
|
271
|
-
['object', new Set(['tabindex'])],
|
|
272
307
|
['button', new Set(['tabindex'])],
|
|
308
|
+
['col', new Set(['span'])],
|
|
309
|
+
['colgroup', new Set(['span'])],
|
|
273
310
|
['input', new Set(['maxlength', 'tabindex'])],
|
|
311
|
+
['object', new Set(['tabindex'])],
|
|
274
312
|
['select', new Set(['size', 'tabindex'])],
|
|
275
|
-
['
|
|
276
|
-
['
|
|
277
|
-
['
|
|
278
|
-
['th', new Set(['rowspan', 'colspan'])],
|
|
279
|
-
['td', new Set(['rowspan', 'colspan'])]
|
|
313
|
+
['td', new Set(['colspan', 'rowspan'])],
|
|
314
|
+
['textarea', new Set(['cols', 'rows', 'tabindex'])],
|
|
315
|
+
['th', new Set(['colspan', 'rowspan'])]
|
|
280
316
|
]);
|
|
281
317
|
|
|
282
318
|
/**
|
|
@@ -323,6 +359,18 @@ function isSrcset(attrName, tag) {
|
|
|
323
359
|
(attrName === 'imagesrcset' && tag === 'link');
|
|
324
360
|
}
|
|
325
361
|
|
|
362
|
+
// Whitespace (as `\s`) and comma for the `srcset` parser, with ASCII checked by code first
|
|
363
|
+
/** @param {number} code */
|
|
364
|
+
function isWsChar(code) {
|
|
365
|
+
return code === 32 || (code >= 9 && code <= 13) ||
|
|
366
|
+
(code > 127 && RE_WS_CHAR.test(String.fromCharCode(code)));
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** @param {number} code */
|
|
370
|
+
function isWsOrComma(code) {
|
|
371
|
+
return code === 44 /* , */ || isWsChar(code);
|
|
372
|
+
}
|
|
373
|
+
|
|
326
374
|
/**
|
|
327
375
|
* @param {string} tag
|
|
328
376
|
* @param {HTMLAttribute[]} attrs
|
|
@@ -360,14 +408,16 @@ function isContentSecurityPolicy(tag, attrs) {
|
|
|
360
408
|
* @param {string} attrName
|
|
361
409
|
* @param {string | undefined} attrValue
|
|
362
410
|
* @param {{removeEmptyAttributes?: boolean | Function}} options
|
|
411
|
+
* @param {string} [tagOut] - The tag name as written, for the hook
|
|
412
|
+
* @param {string} [attrNameOut] - The attribute name as written, for the hook
|
|
363
413
|
*/
|
|
364
|
-
function canDeleteEmptyAttribute(tag, attrName, attrValue, options) {
|
|
414
|
+
function canDeleteEmptyAttribute(tag, attrName, attrValue, options, tagOut = tag, attrNameOut = attrName) {
|
|
365
415
|
const isValueEmpty = !attrValue || attrValue.trim() === '';
|
|
366
416
|
if (!isValueEmpty) {
|
|
367
417
|
return false;
|
|
368
418
|
}
|
|
369
419
|
if (typeof options.removeEmptyAttributes === 'function') {
|
|
370
|
-
return options.removeEmptyAttributes(
|
|
420
|
+
return options.removeEmptyAttributes(attrNameOut, tagOut);
|
|
371
421
|
}
|
|
372
422
|
return (tag === 'input' && attrName === 'value') || RE_EMPTY_ATTRIBUTE.test(attrName);
|
|
373
423
|
}
|
|
@@ -391,6 +441,20 @@ const collapseAttributeWhitespaceExempt = new Set(['pattern', 'placeholder', 'ti
|
|
|
391
441
|
// `value` whitespace matters only on form-submission and machine-readable elements
|
|
392
442
|
const valueWhitespaceExemptElements = new Set(['button', 'data', 'input', 'option', 'param']);
|
|
393
443
|
|
|
444
|
+
/**
|
|
445
|
+
* Drop repeated names from a whitespace-separated class list, keeping the first of each.
|
|
446
|
+
* @param {string} value
|
|
447
|
+
* @returns {string}
|
|
448
|
+
*/
|
|
449
|
+
function dedupeClassNames(value) {
|
|
450
|
+
if (!RE_ATTR_WS_CHECK.test(value)) {
|
|
451
|
+
return value;
|
|
452
|
+
}
|
|
453
|
+
const names = value.split(RE_ATTR_WS_COLLAPSE);
|
|
454
|
+
const unique = new Set(names);
|
|
455
|
+
return unique.size === names.length ? value : [...unique].join(' ');
|
|
456
|
+
}
|
|
457
|
+
|
|
394
458
|
// Returns the cleaned attribute value directly (sync) or as a Promise (async);
|
|
395
459
|
// callers must handle both cases—use `isThenable()` to distinguish
|
|
396
460
|
/**
|
|
@@ -400,18 +464,18 @@ const valueWhitespaceExemptElements = new Set(['button', 'data', 'input', 'optio
|
|
|
400
464
|
* @param {ProcessedOptions} options
|
|
401
465
|
* @param {HTMLAttribute[]} attrs
|
|
402
466
|
* @param {Function} minifyHTMLSelf
|
|
467
|
+
* @param {string[]} [markers] - What custom fragment and `htmlmin:ignore` placeholders hold
|
|
468
|
+
* @param {string} [attrNameOut] - The name as written, for the patterns
|
|
403
469
|
*/
|
|
404
|
-
function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTMLSelf) {
|
|
405
|
-
const isEventAttr = isEventAttribute(attrName, options);
|
|
470
|
+
function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTMLSelf, markers = [], attrNameOut = attrName) {
|
|
471
|
+
const isEventAttr = isEventAttribute(attrName, options, attrNameOut);
|
|
406
472
|
|
|
407
|
-
//
|
|
408
|
-
//
|
|
473
|
+
// Special spaces (no-break space, hair space, etc.) stay, for consistency
|
|
474
|
+
// with `collapseWhitespace`
|
|
409
475
|
if (options.collapseAttributeWhitespace && !collapseAttributeWhitespaceExempt.has(attrName) && !(attrName === 'value' && valueWhitespaceExemptElements.has(tag)) && !isEventAttr) {
|
|
410
476
|
// Fast path: Only process if whitespace exists (avoids regex overhead on clean values)
|
|
411
477
|
if (RE_ATTR_WS_CHECK.test(attrValue)) {
|
|
412
478
|
// Two-pass approach (faster than single-pass with callback)
|
|
413
|
-
// First: Collapse internal whitespace sequences to single space
|
|
414
|
-
// Second: Trim leading/trailing whitespace
|
|
415
479
|
attrValue = attrValue.replace(RE_ATTR_WS_COLLAPSE, ' ').replace(RE_ATTR_WS_TRIM, '');
|
|
416
480
|
}
|
|
417
481
|
}
|
|
@@ -448,13 +512,16 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
448
512
|
|
|
449
513
|
if (attrName === 'class') {
|
|
450
514
|
attrValue = trimWhitespace(attrValue);
|
|
515
|
+
// Names around a fragment placeholder (`ignoreCustomFragments`, `htmlmin:ignore`)
|
|
516
|
+
// may not repeat once a template renders
|
|
517
|
+
const holdsFragment = markers.some(marker => attrValue.indexOf(marker) !== -1);
|
|
451
518
|
if (options.sortClassNames) {
|
|
452
|
-
//
|
|
519
|
+
// `createSortFns` has replaced any truthy non-function value by now
|
|
453
520
|
attrValue = /** @type {(value: string) => string} */ (options.sortClassNames)(attrValue);
|
|
454
521
|
} else {
|
|
455
522
|
attrValue = collapseWhitespaceAll(attrValue);
|
|
456
523
|
}
|
|
457
|
-
return attrValue;
|
|
524
|
+
return holdsFragment ? attrValue : dedupeClassNames(attrValue);
|
|
458
525
|
}
|
|
459
526
|
|
|
460
527
|
if (isUriTypeAttribute(attrName, tag)) {
|
|
@@ -490,8 +557,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
490
557
|
if (isThenable(cssResult)) {
|
|
491
558
|
return cssResult
|
|
492
559
|
.then((/** @type {string} */ minified) => {
|
|
493
|
-
//
|
|
494
|
-
// I.e., `color:` or `margin:;padding:` should be treated as empty
|
|
560
|
+
// CSS of nothing but properties without values (`color:`, `margin:;padding:`) is empty
|
|
495
561
|
if (minified && /^(?:[a-z-]+:[;\s]*)+$/i.test(minified)) return '';
|
|
496
562
|
return minified;
|
|
497
563
|
})
|
|
@@ -518,11 +584,11 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
518
584
|
let pos = 0;
|
|
519
585
|
while (pos < value.length) {
|
|
520
586
|
// Skip whitespace and separator commas
|
|
521
|
-
while (pos < value.length &&
|
|
587
|
+
while (pos < value.length && isWsOrComma(value.charCodeAt(pos))) pos++;
|
|
522
588
|
if (pos >= value.length) break;
|
|
523
589
|
const start = pos;
|
|
524
590
|
// URL: a run of non-whitespace characters (which may contain commas)
|
|
525
|
-
while (pos < value.length &&
|
|
591
|
+
while (pos < value.length && !isWsChar(value.charCodeAt(pos))) pos++;
|
|
526
592
|
if (value.charAt(pos - 1) === ',') {
|
|
527
593
|
// Trailing comma(s) end the candidate—a URL without descriptor
|
|
528
594
|
candidates.push(value.slice(start, pos).replace(/,+$/, ''));
|
|
@@ -530,9 +596,11 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
530
596
|
}
|
|
531
597
|
// Descriptor: everything up to the next comma outside parentheses
|
|
532
598
|
let inParens = false;
|
|
533
|
-
while (pos < value.length
|
|
534
|
-
|
|
535
|
-
|
|
599
|
+
while (pos < value.length) {
|
|
600
|
+
const code = value.charCodeAt(pos);
|
|
601
|
+
if (!inParens && code === 44 /* , */) break;
|
|
602
|
+
if (code === 40 /* ( */) inParens = true;
|
|
603
|
+
else if (code === 41 /* ) */) inParens = false;
|
|
536
604
|
pos++;
|
|
537
605
|
}
|
|
538
606
|
candidates.push(trimWhitespace(value.slice(start, pos)));
|
|
@@ -581,7 +649,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
581
649
|
return collapseWhitespaceAll(attrValue);
|
|
582
650
|
}
|
|
583
651
|
|
|
584
|
-
if (options.customAttrCollapse && options.customAttrCollapse.test(
|
|
652
|
+
if (options.customAttrCollapse && options.customAttrCollapse.test(attrNameOut)) {
|
|
585
653
|
return trimWhitespace(attrValue.replace(/ ?[\n\r]+ ?/g, '').replace(/\s{2,}/g, options.conservativeCollapse ? ' ' : ''));
|
|
586
654
|
}
|
|
587
655
|
|
|
@@ -591,8 +659,8 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
591
659
|
|
|
592
660
|
if (isMediaQuery(tag, attrs, attrName)) {
|
|
593
661
|
attrValue = trimWhitespace(attrValue);
|
|
594
|
-
// Only
|
|
595
|
-
//
|
|
662
|
+
// Only media queries with features (in parentheses) need minifying; media types like `all`,
|
|
663
|
+
// `screen`, or `print` are minimal already
|
|
596
664
|
if (!/[()]/.test(attrValue)) {
|
|
597
665
|
return attrValue;
|
|
598
666
|
}
|
|
@@ -609,7 +677,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTM
|
|
|
609
677
|
}
|
|
610
678
|
|
|
611
679
|
if (tag === 'iframe' && attrName === 'srcdoc') {
|
|
612
|
-
// Fast
|
|
680
|
+
// Fast path: Skip if nothing would change
|
|
613
681
|
if (!shouldMinifyInnerHTML(options)) {
|
|
614
682
|
return attrValue;
|
|
615
683
|
}
|
|
@@ -650,7 +718,7 @@ async function minifySrcdoc(attrValue, options, minifyHTMLSelf) {
|
|
|
650
718
|
...options,
|
|
651
719
|
cssContext: {
|
|
652
720
|
warned: options.cssContext ? options.cssContext.warned : new Set(),
|
|
653
|
-
|
|
721
|
+
...collectUsage(markup, options.removeUnusedCSS.scripts, /** @type {((text: string) => string) | undefined} */ (decode))
|
|
654
722
|
}
|
|
655
723
|
};
|
|
656
724
|
}
|
|
@@ -691,34 +759,54 @@ function chooseAttributeQuote(attrValue, options) {
|
|
|
691
759
|
* @param {HTMLAttribute} attr
|
|
692
760
|
* @param {HTMLAttribute[]} attrs
|
|
693
761
|
* @param {string} tag
|
|
762
|
+
* @param {string} tagOut - The tag name as written, for hooks
|
|
694
763
|
* @param {ProcessedOptions} options
|
|
695
764
|
* @param {Function} minifyHTML
|
|
765
|
+
* @param {string[]} [markers] - What custom fragment and `htmlmin:ignore` placeholders hold
|
|
696
766
|
*/
|
|
697
|
-
function normalizeAttr(attr, attrs, tag, options, minifyHTML) {
|
|
767
|
+
function normalizeAttr(attr, attrs, tag, tagOut, options, minifyHTML, markers) {
|
|
698
768
|
const attrName = options.name(attr.name);
|
|
769
|
+
// The name to write and to hand to hooks: as in the source for HTML names
|
|
770
|
+
// under `caseSensitive`, as named otherwise (foreign names are named as written anyway)
|
|
771
|
+
const attrNameOut = options.namesAsWritten && options.name === lowercase ? attr.name : attrName;
|
|
699
772
|
const attrValue = attr.value;
|
|
700
773
|
|
|
701
|
-
// Entity decoding requires a lazy import—async only
|
|
774
|
+
// Entity decoding requires a lazy import—async only while that import is unresolved
|
|
702
775
|
if (options.decodeEntities && attrValue && attrValue.indexOf('&') !== -1) {
|
|
776
|
+
// What SVGO reads is written for XML, which takes a value decoded fully, as HTML reads it
|
|
777
|
+
if (options.insideSVG && options.minifySVG) {
|
|
778
|
+
if (decodeHTMLAttributeFn) {
|
|
779
|
+
return normalizeAttrContinue(attrName, attrNameOut, decodeHTMLAttributeFn(attrValue), attr, attrs, tag, tagOut, options, minifyHTML, markers);
|
|
780
|
+
}
|
|
781
|
+
return getDecodeHTMLAttribute().then(decode => {
|
|
782
|
+
return normalizeAttrContinue(attrName, attrNameOut, decode(attrValue), attr, attrs, tag, tagOut, options, minifyHTML, markers);
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
if (decodeHTMLStrictFn) {
|
|
786
|
+
return normalizeAttrContinue(attrName, attrNameOut, decodeHTMLStrictFn(attrValue), attr, attrs, tag, tagOut, options, minifyHTML, markers);
|
|
787
|
+
}
|
|
703
788
|
return getDecodeHTMLStrict().then(decode => {
|
|
704
|
-
return normalizeAttrContinue(attrName, decode(attrValue), attr, attrs, tag, options, minifyHTML);
|
|
789
|
+
return normalizeAttrContinue(attrName, attrNameOut, decode(attrValue), attr, attrs, tag, tagOut, options, minifyHTML, markers);
|
|
705
790
|
});
|
|
706
791
|
}
|
|
707
792
|
|
|
708
|
-
return normalizeAttrContinue(attrName, attrValue, attr, attrs, tag, options, minifyHTML);
|
|
793
|
+
return normalizeAttrContinue(attrName, attrNameOut, attrValue, attr, attrs, tag, tagOut, options, minifyHTML, markers);
|
|
709
794
|
}
|
|
710
795
|
|
|
711
796
|
// Internal: Handles attribute normalization after entity decoding (if any)
|
|
712
797
|
/**
|
|
713
798
|
* @param {string} attrName
|
|
799
|
+
* @param {string} attrNameOut
|
|
714
800
|
* @param {string | undefined} attrValue
|
|
715
801
|
* @param {HTMLAttribute} attr
|
|
716
802
|
* @param {HTMLAttribute[]} attrs
|
|
717
803
|
* @param {string} tag
|
|
804
|
+
* @param {string} tagOut
|
|
718
805
|
* @param {ProcessedOptions} options
|
|
719
806
|
* @param {Function} minifyHTML
|
|
807
|
+
* @param {string[]} [markers] - What custom fragment and `htmlmin:ignore` placeholders hold
|
|
720
808
|
*/
|
|
721
|
-
function normalizeAttrContinue(attrName, attrValue, attr, attrs, tag, options, minifyHTML) {
|
|
809
|
+
function normalizeAttrContinue(attrName, attrNameOut, attrValue, attr, attrs, tag, tagOut, options, minifyHTML, markers) {
|
|
722
810
|
if ((options.removeRedundantAttributes &&
|
|
723
811
|
isAttributeRedundant(tag, attrName, attrValue ?? '', attrs)) ||
|
|
724
812
|
(options.removeDefaultTypeAttributes && attrName === 'type' && (
|
|
@@ -729,37 +817,41 @@ function normalizeAttrContinue(attrName, attrValue, attr, attrs, tag, options, m
|
|
|
729
817
|
}
|
|
730
818
|
|
|
731
819
|
if (attrValue) {
|
|
732
|
-
const cleaned = cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTML);
|
|
820
|
+
const cleaned = cleanAttributeValue(tag, attrName, attrValue, options, attrs, minifyHTML, markers, attrNameOut);
|
|
733
821
|
if (isThenable(cleaned)) {
|
|
734
|
-
return cleaned.then((/** @type {string | undefined} */ v) => normalizeAttrFinish(attrName, v, attr, tag, options));
|
|
822
|
+
return cleaned.then((/** @type {string | undefined} */ v) => normalizeAttrFinish(attrName, attrNameOut, v, attr, tag, tagOut, options));
|
|
735
823
|
}
|
|
736
|
-
return normalizeAttrFinish(attrName, cleaned, attr, tag, options);
|
|
824
|
+
return normalizeAttrFinish(attrName, attrNameOut, cleaned, attr, tag, tagOut, options);
|
|
737
825
|
}
|
|
738
826
|
|
|
739
|
-
return normalizeAttrFinish(attrName, attrValue, attr, tag, options);
|
|
827
|
+
return normalizeAttrFinish(attrName, attrNameOut, attrValue, attr, tag, tagOut, options);
|
|
740
828
|
}
|
|
741
829
|
|
|
742
830
|
// Internal: Final checks and result assembly after value cleaning
|
|
743
831
|
/**
|
|
744
832
|
* @param {string} attrName
|
|
833
|
+
* @param {string} attrNameOut
|
|
745
834
|
* @param {string | undefined} attrValue
|
|
746
835
|
* @param {HTMLAttribute} attr
|
|
747
836
|
* @param {string} tag
|
|
837
|
+
* @param {string} tagOut
|
|
748
838
|
* @param {ProcessedOptions} options
|
|
749
839
|
*/
|
|
750
|
-
function normalizeAttrFinish(attrName, attrValue, attr, tag, options) {
|
|
840
|
+
function normalizeAttrFinish(attrName, attrNameOut, attrValue, attr, tag, tagOut, options) {
|
|
751
841
|
if (options.removeEmptyAttributes &&
|
|
752
|
-
canDeleteEmptyAttribute(tag, attrName, attrValue, options)) {
|
|
842
|
+
canDeleteEmptyAttribute(tag, attrName, attrValue, options, tagOut, attrNameOut)) {
|
|
753
843
|
return;
|
|
754
844
|
}
|
|
755
845
|
|
|
756
|
-
if (
|
|
846
|
+
if (attrValue && options.insideSVG && options.minifySVG) {
|
|
847
|
+
attrValue = escapeXML(attrValue, true);
|
|
848
|
+
} else if (options.decodeEntities && attrValue && attrValue.indexOf('&') !== -1) {
|
|
757
849
|
attrValue = attrValue.replace(RE_AMP_ENTITY, '&$1');
|
|
758
850
|
}
|
|
759
851
|
|
|
760
852
|
return {
|
|
761
853
|
attr,
|
|
762
|
-
name:
|
|
854
|
+
name: attrNameOut,
|
|
763
855
|
value: attrValue
|
|
764
856
|
};
|
|
765
857
|
}
|
|
@@ -776,37 +868,49 @@ function buildAttr(normalized, hasUnarySlash, options, isLast, uidAttr) {
|
|
|
776
868
|
let attrValue = normalized.value;
|
|
777
869
|
const attr = normalized.attr;
|
|
778
870
|
let attrQuote = attr.quote;
|
|
779
|
-
|
|
780
|
-
|
|
871
|
+
const readAsXML = Boolean(options.insideSVG) && Boolean(options.minifySVG);
|
|
872
|
+
|
|
873
|
+
// SVGO reads the whole SVG block as XML, where every attribute carries a value
|
|
874
|
+
if (readAsXML && typeof attrValue === 'undefined') {
|
|
875
|
+
attrValue = '';
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// An attribute written as its name alone skips the quoting work below
|
|
879
|
+
if (typeof attrValue === 'undefined' ||
|
|
880
|
+
(options.collapseBooleanAttributes && collapsesToBooleanName(attrName, attrValue ?? '')) ||
|
|
881
|
+
(options.collapseEmptyAttributes && attrValue === '' && (attr.customAssign ?? '=') === '=')) {
|
|
882
|
+
return attr.customOpen + attrName + (isLast ? '' : ' ') + attr.customClose;
|
|
883
|
+
}
|
|
781
884
|
|
|
782
|
-
// Determine if need to add/keep quotes
|
|
783
885
|
const shouldAddQuotes = typeof attrValue !== 'undefined' && (
|
|
784
886
|
// If `removeAttributeQuotes` is enabled, add quotes only if they can’t be removed
|
|
785
887
|
(options.removeAttributeQuotes && ((uidAttr ? attrValue.indexOf(uidAttr) !== -1 : false) || !canRemoveAttributeQuotes(attrValue))) ||
|
|
786
|
-
// If `removeAttributeQuotes` is not enabled, preserve original quote style or add
|
|
888
|
+
// If `removeAttributeQuotes` is not enabled, preserve original quote style, or add
|
|
889
|
+
// quotes if value requires them
|
|
787
890
|
(!options.removeAttributeQuotes && (attrQuote !== '' || !canRemoveAttributeQuotes(attrValue) ||
|
|
788
|
-
//
|
|
789
|
-
|
|
790
|
-
//
|
|
791
|
-
//
|
|
891
|
+
// XML quotes every attribute value
|
|
892
|
+
readAsXML ||
|
|
893
|
+
// With `removeTagWhitespace`, unquoted values that aren’t last get a space added, which can
|
|
894
|
+
// create ambiguous or invalid HTML—add quotes to be safe
|
|
792
895
|
(options.removeTagWhitespace && attrQuote === '' && !isLast)))
|
|
793
896
|
);
|
|
794
897
|
|
|
898
|
+
let emittedAttrValue;
|
|
795
899
|
if (shouldAddQuotes) {
|
|
796
900
|
attrValue = attrValue ?? '';
|
|
797
|
-
// Determine the appropriate quote character
|
|
798
901
|
if (!options.preventAttributesEscaping) {
|
|
799
|
-
// Normal mode: Choose
|
|
800
|
-
//
|
|
902
|
+
// Normal mode: Choose the quote type that minimizes escaping, unless the original quotes are
|
|
903
|
+
// kept and need no escaping
|
|
801
904
|
const needsEscaping = (attrQuote === '"' && attrValue.indexOf('"') !== -1) || (attrQuote === "'" && attrValue.indexOf("'") !== -1);
|
|
802
905
|
|
|
803
906
|
if (options.removeAttributeQuotes || typeof options.quoteCharacter !== 'undefined' || needsEscaping || attrQuote === '') {
|
|
804
907
|
attrQuote = chooseAttributeQuote(attrValue, options);
|
|
805
908
|
}
|
|
806
909
|
|
|
910
|
+
// `indexOf` first, as values rarely hold the quote and as it’s cheaper than `replace`
|
|
807
911
|
if (attrQuote === '"') {
|
|
808
|
-
attrValue = attrValue.replace(/"/g, '"');
|
|
809
|
-
} else {
|
|
912
|
+
if (attrValue.indexOf('"') !== -1) attrValue = attrValue.replace(/"/g, '"');
|
|
913
|
+
} else if (attrValue.indexOf("'") !== -1) {
|
|
810
914
|
attrValue = attrValue.replace(/'/g, ''');
|
|
811
915
|
}
|
|
812
916
|
} else {
|
|
@@ -823,7 +927,7 @@ function buildAttr(normalized, hasUnarySlash, options, isLast, uidAttr) {
|
|
|
823
927
|
} else {
|
|
824
928
|
attrValue = attrValue.replace(/'/g, ''');
|
|
825
929
|
}
|
|
826
|
-
//
|
|
930
|
+
// No `quoteCharacter` set: Prefer the quote type the value doesn’t hold, default to double
|
|
827
931
|
} else if (typeof options.quoteCharacter === 'undefined') {
|
|
828
932
|
if (attrQuote === '"' && hasDoubleQuote && !hasSingleQuote) {
|
|
829
933
|
attrQuote = "'";
|
|
@@ -852,7 +956,7 @@ function buildAttr(normalized, hasUnarySlash, options, isLast, uidAttr) {
|
|
|
852
956
|
} else {
|
|
853
957
|
// `quoteCharacter` is explicitly set
|
|
854
958
|
const preferredQuote = options.quoteCharacter === '\'' ? '\'' : '"';
|
|
855
|
-
//
|
|
959
|
+
// If the preferred quote conflicts with value content, switch to the opposite one
|
|
856
960
|
if ((preferredQuote === '"' && hasDoubleQuote && !hasSingleQuote) || (preferredQuote === "'" && hasSingleQuote && !hasDoubleQuote)) {
|
|
857
961
|
attrQuote = preferredQuote === '"' ? "'" : '"';
|
|
858
962
|
} else if ((preferredQuote === '"' && hasDoubleQuote && hasSingleQuote) || (preferredQuote === "'" && hasSingleQuote && hasDoubleQuote)) {
|
|
@@ -873,30 +977,16 @@ function buildAttr(normalized, hasUnarySlash, options, isLast, uidAttr) {
|
|
|
873
977
|
emittedAttrValue += ' ';
|
|
874
978
|
}
|
|
875
979
|
} else if (isLast && !hasUnarySlash) {
|
|
876
|
-
// Last attribute in a non-self-closing tag:
|
|
877
|
-
// No space needed
|
|
980
|
+
// Last attribute in a non-self-closing tag: No space needed
|
|
878
981
|
emittedAttrValue = attrValue;
|
|
879
982
|
} else {
|
|
880
|
-
// Not last attribute, or
|
|
881
|
-
// Unquoted values must have space after them to delimit from next attribute
|
|
983
|
+
// Not last attribute, or a self-closing tag: Unquoted values need a space to delimit them
|
|
882
984
|
emittedAttrValue = attrValue + ' ';
|
|
883
985
|
}
|
|
884
986
|
|
|
885
|
-
|
|
886
|
-
isBooleanAttribute(attrName.toLowerCase(), (attrValue || '').toLowerCase()))) {
|
|
887
|
-
attrFragment = attrName;
|
|
888
|
-
if (!isLast) {
|
|
889
|
-
attrFragment += ' ';
|
|
890
|
-
}
|
|
891
|
-
} else {
|
|
892
|
-
attrFragment = attrName + attr.customAssign + emittedAttrValue;
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
return attr.customOpen + attrFragment + attr.customClose;
|
|
987
|
+
return attr.customOpen + attrName + attr.customAssign + emittedAttrValue + attr.customClose;
|
|
896
988
|
}
|
|
897
989
|
|
|
898
|
-
// Exports
|
|
899
|
-
|
|
900
990
|
export {
|
|
901
991
|
// Validators
|
|
902
992
|
isIgnoredComment,
|
|
@@ -909,7 +999,6 @@ export {
|
|
|
909
999
|
isExecutableScript,
|
|
910
1000
|
isStyleLinkTypeAttribute,
|
|
911
1001
|
isStyleElement,
|
|
912
|
-
isBooleanAttribute,
|
|
913
1002
|
isUriTypeAttribute,
|
|
914
1003
|
isNumberTypeAttribute,
|
|
915
1004
|
isLinkType,
|