@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.
Files changed (46) hide show
  1. package/README.md +147 -33
  2. package/cli.js +70 -54
  3. package/dist/types/htmlminifier.d.ts +87 -33
  4. package/dist/types/htmlminifier.d.ts.map +1 -1
  5. package/dist/types/htmlparser.d.ts +4 -1
  6. package/dist/types/htmlparser.d.ts.map +1 -1
  7. package/dist/types/lib/attributes.d.ts +12 -10
  8. package/dist/types/lib/attributes.d.ts.map +1 -1
  9. package/dist/types/lib/constants.d.ts +38 -3
  10. package/dist/types/lib/constants.d.ts.map +1 -1
  11. package/dist/types/lib/content.d.ts.map +1 -1
  12. package/dist/types/lib/elements.d.ts.map +1 -1
  13. package/dist/types/lib/fragments.d.ts +1 -0
  14. package/dist/types/lib/fragments.d.ts.map +1 -1
  15. package/dist/types/lib/option-definitions.d.ts.map +1 -1
  16. package/dist/types/lib/options.d.ts +14 -46
  17. package/dist/types/lib/options.d.ts.map +1 -1
  18. package/dist/types/lib/unused-css.d.ts +34 -4
  19. package/dist/types/lib/unused-css.d.ts.map +1 -1
  20. package/dist/types/lib/urls.d.ts.map +1 -1
  21. package/dist/types/lib/utils.d.ts +31 -1
  22. package/dist/types/lib/utils.d.ts.map +1 -1
  23. package/dist/types/lib/whitespace.d.ts +57 -5
  24. package/dist/types/lib/whitespace.d.ts.map +1 -1
  25. package/dist/types/presets.d.ts +1 -2
  26. package/dist/types/presets.d.ts.map +1 -1
  27. package/dist/types/tokenchain.d.ts.map +1 -1
  28. package/html-minifier-next.schema.json +15 -7
  29. package/package.json +17 -18
  30. package/src/htmlminifier.js +1103 -392
  31. package/src/htmlparser.js +452 -200
  32. package/src/lib/attributes.js +201 -112
  33. package/src/lib/constants.js +130 -19
  34. package/src/lib/content.js +1 -6
  35. package/src/lib/elements.js +17 -12
  36. package/src/lib/file-pool.js +18 -2
  37. package/src/lib/file-write.js +87 -0
  38. package/src/lib/fragments.js +74 -8
  39. package/src/lib/option-definitions.js +17 -10
  40. package/src/lib/options.js +151 -71
  41. package/src/lib/unused-css.js +145 -27
  42. package/src/lib/urls.js +0 -9
  43. package/src/lib/utils.js +210 -45
  44. package/src/lib/whitespace.js +173 -63
  45. package/src/presets.js +1 -2
  46. package/src/tokenchain.js +1 -4
@@ -25,8 +25,9 @@ const RE_LITERAL_FLAGS = /^[gds]*$/;
25
25
  * Literal delimiters around a body, found by scanning; `excluded` holds the
26
26
  * characters a negated-class body cannot cross, null when the body spans every
27
27
  * character
28
- * @typedef {{search: RegExp, anchored: RegExp}} PatternFragment
29
- * Everything else, found by running the pattern itself
28
+ * @typedef {{search: RegExp, anchored: RegExp, close: string | null}} PatternFragment
29
+ * Everything else, found by running the pattern itself; `close` holds the literal
30
+ * every match ends with, where one follows an unbounded any-character body
30
31
  */
31
32
 
32
33
  /**
@@ -91,6 +92,50 @@ function toDelimitedFragment(pattern) {
91
92
  return { open, close, min, max, excluded: inner === null ? null : new RegExp('[' + inner + ']', 'g') };
92
93
  }
93
94
 
95
+ /**
96
+ * Whether a regex source has a `|` outside groups and classes, which lets a match skip the rest
97
+ * @param {string} source
98
+ * @returns {boolean}
99
+ */
100
+ function hasTopLevelAlternation(source) {
101
+ let depth = 0;
102
+ let inClass = false;
103
+
104
+ for (let i = 0; i < source.length; i++) {
105
+ const char = source[i];
106
+ if (char === '\\') i++;
107
+ else if (inClass) inClass = char !== ']';
108
+ else if (char === '[') inClass = true;
109
+ else if (char === '(') depth++;
110
+ else if (char === ')') depth--;
111
+ else if (char === '|' && depth === 0) return true;
112
+ }
113
+
114
+ return false;
115
+ }
116
+
117
+ /**
118
+ * The literal all matches of a pattern end with, where an unbounded any-character body leads
119
+ * up to it: Without that literal ahead, each opener runs the body to the end of the input
120
+ * only to fail, which costs O(n²) on input that opens fragments it never closes
121
+ * @param {RegExp} pattern
122
+ * @returns {string | null}
123
+ */
124
+ function toClosingLiteral(pattern) {
125
+ const match = RE_DELIMITED.exec(pattern.source);
126
+ if (!match) return null;
127
+
128
+ const [, rawOpen, dot, negated, simple, , , rawClose] = match;
129
+ if (!simple || negated !== undefined || (dot && !pattern.flags.includes('s'))) return null;
130
+ if (hasTopLevelAlternation(rawOpen ?? '')) return null;
131
+
132
+ const close = toLiteral(rawClose ?? '');
133
+ // `indexOf` can stand in for the pattern only where case folding leaves the literal alone
134
+ if (!close || (pattern.flags.includes('i') && close.toLowerCase() !== close.toUpperCase())) return null;
135
+
136
+ return close;
137
+ }
138
+
94
139
  /**
95
140
  * Prepare a fragment pattern for matching, by scanning where the shape allows it
96
141
  * @param {RegExp} pattern
@@ -102,7 +147,11 @@ function toFragment(pattern) {
102
147
 
103
148
  // `g` and `y` are ours to set, the rest belong to the pattern
104
149
  const flags = pattern.flags.replace(/[gy]/g, '');
105
- return { search: new RegExp(pattern.source, flags + 'g'), anchored: new RegExp(pattern.source, flags + 'y') };
150
+ return {
151
+ search: new RegExp(pattern.source, flags + 'g'),
152
+ anchored: new RegExp(pattern.source, flags + 'y'),
153
+ close: toClosingLiteral(pattern)
154
+ };
106
155
  }
107
156
 
108
157
  /**
@@ -149,6 +198,15 @@ function replaceCustomFragments(value, fragments, replacer) {
149
198
  * @returns {{start: number, end: number} | null}
150
199
  */
151
200
  const scan = (fragment, index, from, anchored) => {
201
+ // The closing delimiter first: An opener starting with `<` is slow to find in markup
202
+ if (/** @type {number} */ (closesAt[index]) < from) {
203
+ closesAt[index] = value.indexOf(fragment.close, from);
204
+ if (closesAt[index] === -1) {
205
+ exhausted[index] = true;
206
+ return null;
207
+ }
208
+ }
209
+
152
210
  let openFrom = from;
153
211
 
154
212
  for (;;) {
@@ -183,11 +241,21 @@ function replaceCustomFragments(value, fragments, replacer) {
183
241
  /**
184
242
  * Earliest match of one regex fragment at or after `from`
185
243
  * @param {PatternFragment} fragment
244
+ * @param {number} index - Which fragment, for the forward-only bookkeeping
186
245
  * @param {number} from
187
246
  * @param {boolean} anchored
188
247
  * @returns {{start: number, end: number} | null}
189
248
  */
190
- const run = (fragment, from, anchored) => {
249
+ const run = (fragment, index, from, anchored) => {
250
+ if (fragment.close !== null) {
251
+ // `Infinity` marks a closing literal that is nowhere ahead, so the search is not repeated
252
+ if (/** @type {number} */ (closesAt[index]) < from) {
253
+ const close = value.indexOf(fragment.close, from);
254
+ closesAt[index] = close === -1 ? Infinity : close;
255
+ }
256
+ if (closesAt[index] === Infinity) return null;
257
+ }
258
+
191
259
  const pattern = anchored ? fragment.anchored : fragment.search;
192
260
  pattern.lastIndex = from;
193
261
 
@@ -217,13 +285,13 @@ function replaceCustomFragments(value, fragments, replacer) {
217
285
  /** @type {{start: number, end: number} | null} */
218
286
  let match;
219
287
  if (anchored) {
220
- match = 'open' in fragment ? scan(fragment, i, from, true) : run(fragment, from, true);
288
+ match = 'open' in fragment ? scan(fragment, i, from, true) : run(fragment, i, from, true);
221
289
  } else {
222
290
  // Matches only ever move forward, so the last one found still stands
223
291
  const previous = found[i];
224
292
  match = previous && previous.start >= from
225
293
  ? previous
226
- : ('open' in fragment ? scan(fragment, i, from, false) : run(fragment, from, false));
294
+ : ('open' in fragment ? scan(fragment, i, from, false) : run(fragment, i, from, false));
227
295
  found[i] = match;
228
296
  // Nothing ahead now means nothing ahead later, either
229
297
  if (!match) exhausted[i] = true;
@@ -265,8 +333,6 @@ function replaceCustomFragments(value, fragments, replacer) {
265
333
  return copied === 0 ? value : out + value.slice(copied);
266
334
  }
267
335
 
268
- // Exports
269
-
270
336
  export {
271
337
  toDelimitedFragment,
272
338
  toFragment,
@@ -24,7 +24,7 @@ const optionDefinitions = {
24
24
  type: 'int'
25
25
  },
26
26
  caseSensitive: {
27
- description: 'Treat attributes in case-sensitive manner (useful for custom HTML elements)',
27
+ description: 'Keep element and attribute names as written (e.g., for framework components like `<MyButton>`)',
28
28
  type: 'boolean'
29
29
  },
30
30
  collapseAttributeWhitespace: {
@@ -35,10 +35,18 @@ const optionDefinitions = {
35
35
  description: 'Omit attribute values from boolean attributes',
36
36
  type: 'boolean'
37
37
  },
38
+ collapseEmptyAttributes: {
39
+ description: 'Omit empty attribute values (e.g., `alt=""` → `alt`)',
40
+ type: 'boolean'
41
+ },
38
42
  collapseInlineTagWhitespace: {
39
43
  description: 'Collapse whitespace more aggressively between inline elements—use with `--collapse-whitespace`',
40
44
  type: 'boolean'
41
45
  },
46
+ collapseNoBreakSpaces: {
47
+ description: 'Remove whitespace next to no-break spaces, narrow no-break spaces, and figure spaces in text (e.g., `a &nbsp; b` → `a&nbsp;b`), keeping runs of them—use with `--collapse-whitespace`',
48
+ type: 'boolean'
49
+ },
42
50
  collapseWhitespace: {
43
51
  description: 'Collapse whitespace that contributes to text nodes in a document tree',
44
52
  type: 'boolean'
@@ -81,7 +89,7 @@ const optionDefinitions = {
81
89
  type: 'regexpArray'
82
90
  },
83
91
  ignoreCustomFragments: {
84
- description: 'Array of regexes that allow to ignore certain fragments, when matched (e.g., `<?php \u2026 ?>`, `{{ \u2026 }}`)',
92
+ description: 'Array of regexes that allow to ignore certain fragments, when matched (e.g., `<?php \u2026 ?>`, `{{ \u2026 }}`, etc.)',
85
93
  type: 'regexpArray'
86
94
  },
87
95
  includeAutoGeneratedTags: {
@@ -101,7 +109,7 @@ const optionDefinitions = {
101
109
  type: 'int'
102
110
  },
103
111
  maxLineLength: {
104
- description: 'Specify a maximum line length; compressed output will be split by newlines at valid HTML split-points',
112
+ description: 'Specify a maximum line length; output will be split by newlines in tags only (between attributes or before a tag’s `>`), where they can’t change the page, so lines may run longer',
105
113
  type: 'int'
106
114
  },
107
115
  mergeScripts: {
@@ -125,7 +133,7 @@ const optionDefinitions = {
125
133
  type: 'json'
126
134
  },
127
135
  noNewlinesBeforeTagClose: {
128
- description: 'Never add a newline before a tag that closes an element—use with `--max-line-length`',
136
+ description: 'Never split a line in a tag that closes an element—use with `--max-line-length`',
129
137
  type: 'boolean'
130
138
  },
131
139
  partialMarkup: {
@@ -169,7 +177,7 @@ const optionDefinitions = {
169
177
  type: 'boolean'
170
178
  },
171
179
  removeEmptyElementsExcept: {
172
- description: 'Array of elements to preserve (e.g., `td`, `<span aria-hidden="true">`)—use with `--remove-empty-elements`',
180
+ description: 'Array of elements to preserve (e.g., `td`, `<i class="test">`, etc.)—use with `--remove-empty-elements`',
173
181
  type: 'jsonArray'
174
182
  },
175
183
  removeOptionalTags: {
@@ -185,7 +193,7 @@ const optionDefinitions = {
185
193
  type: 'boolean'
186
194
  },
187
195
  removeUnusedCSS: {
188
- description: 'Remove rules from `style` elements whose class or ID selectors the document never references—use with `--minify-css`; note that class names only applied by external scripts cannot be detected—use `{"safelist": […]}` for those',
196
+ description: 'Remove CSS from `style` elements that name an element, class, or ID the document doesn’t reference—use with `--minify-css`, and `{"safelist": […]}` for names only external scripts use',
189
197
  type: 'jsonObject'
190
198
  },
191
199
  sortAttributes: {
@@ -197,7 +205,7 @@ const optionDefinitions = {
197
205
  type: 'boolean'
198
206
  },
199
207
  trimCustomFragments: {
200
- description: 'Trim whitespace around custom fragments (`--ignore-custom-fragments`)—use with `--collapse-whitespace`',
208
+ description: 'Trim whitespace around custom fragments (`--ignore-custom-fragments`) where it does not affect the output—use with `--collapse-whitespace`',
201
209
  type: 'boolean'
202
210
  },
203
211
  strictCustomFragments: {
@@ -218,13 +226,12 @@ const optionDefaults = {
218
226
  ],
219
227
  ignoreCustomFragments: [
220
228
  /<%[\s\S]*?%>/,
221
- /<\?[\s\S]*?\?>/
229
+ // Template code and XML declarations, not processing instructions HTML reads itself
230
+ /<\?(?:php[\t\n\r ]|=|\$|xml(?:-stylesheet)?(?![\w-])|\s)[\s\S]*?\?>/i
222
231
  ],
223
232
  includeAutoGeneratedTags: false
224
233
  };
225
234
 
226
- // Exports
227
-
228
235
  export {
229
236
  optionDefinitions,
230
237
  optionDefaults