html-minifier-next 4.0.2 → 4.1.1

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 CHANGED
@@ -197,7 +197,7 @@ const result = await minify(html, {
197
197
 
198
198
  ## Minification comparison
199
199
 
200
- How does HTML Minifier Next compare to other solutions, like [minimize](https://github.com/Swaagie/minimize), [htmlcompressor.com](http://htmlcompressor.com/), [htmlnano](https://github.com/posthtml/htmlnano), and [minify-html](https://github.com/wilsonzlin/minify-html)? (All with the most aggressive settings, but without [hyper-optimization](https://meiert.com/blog/the-ways-of-writing-html/#toc-hyper-optimized).)
200
+ How does HTML Minifier Next compare to other solutions, like [minimize](https://github.com/Swaagie/minimize), [htmlcompressor.com](http://htmlcompressor.com/), [htmlnano](https://github.com/posthtml/htmlnano), and [minify-html](https://github.com/wilsonzlin/minify-html)? (All with the most aggressive settings, though without [hyper-optimization](https://meiert.com/blog/the-ways-of-writing-html/#toc-hyper-optimized).)
201
201
 
202
202
  | Site | Original Size (KB) | HTML Minifier Next | minimize | html­compressor.com | htmlnano | minify-html |
203
203
  | --- | --- | --- | --- | --- | --- | --- |
@@ -1982,6 +1982,11 @@ function joinResultSegments(results, options, restoreCustom, restoreIgnore) {
1982
1982
  return options.collapseWhitespace ? collapseWhitespace(str, options, true, true) : str;
1983
1983
  }
1984
1984
 
1985
+ /**
1986
+ * @param {string} value
1987
+ * @param {MinifierOptions} [options]
1988
+ * @returns {Promise<string>}
1989
+ */
1985
1990
  const minify = async function (value, options) {
1986
1991
  const start = Date.now();
1987
1992
  options = processOptions(options || {});
@@ -1992,5 +1997,336 @@ const minify = async function (value, options) {
1992
1997
 
1993
1998
  var htmlminifier = { minify };
1994
1999
 
2000
+ /**
2001
+ * @typedef {Object} HTMLAttribute
2002
+ * Representation of an attribute from the HTML parser.
2003
+ *
2004
+ * @prop {string} name
2005
+ * @prop {string} [value]
2006
+ * @prop {string} [quote]
2007
+ * @prop {string} [customAssign]
2008
+ * @prop {string} [customOpen]
2009
+ * @prop {string} [customClose]
2010
+ */
2011
+
2012
+ /**
2013
+ * @typedef {Object} MinifierOptions
2014
+ * Options that control how HTML is minified. All of these are optional
2015
+ * and usually default to a disabled/safe value unless noted.
2016
+ *
2017
+ * @prop {(tag: string, attrs: HTMLAttribute[], canCollapseWhitespace: (tag: string) => boolean) => boolean} [canCollapseWhitespace]
2018
+ * Predicate that determines whether whitespace inside a given element
2019
+ * can be collapsed.
2020
+ *
2021
+ * Default: Built-in `canCollapseWhitespace` function
2022
+ *
2023
+ * @prop {(tag: string | null, attrs: HTMLAttribute[] | undefined, canTrimWhitespace: (tag: string) => boolean) => boolean} [canTrimWhitespace]
2024
+ * Predicate that determines whether leading/trailing whitespace around
2025
+ * the element may be trimmed.
2026
+ *
2027
+ * Default: Built-in `canTrimWhitespace` function
2028
+ *
2029
+ * @prop {boolean} [caseSensitive]
2030
+ * When true, tag and attribute names are treated as case-sensitive.
2031
+ * Useful for custom HTML tags.
2032
+ * If false (default) names are lower-cased via the `name` function.
2033
+ *
2034
+ * Default: `false`
2035
+ *
2036
+ * @prop {boolean} [collapseBooleanAttributes]
2037
+ * Collapse boolean attributes to their name only (for example
2038
+ * `disabled="disabled"` -> `disabled`).
2039
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes
2040
+ *
2041
+ * Default: `false`
2042
+ *
2043
+ * @prop {boolean} [collapseInlineTagWhitespace]
2044
+ * When false (default) whitespace around `inline` tags is preserved in
2045
+ * more cases. When true, whitespace around inline tags may be collapsed.
2046
+ * Must also enable `collapseWhitespace` to have effect.
2047
+ *
2048
+ * Default: `false`
2049
+ *
2050
+ * @prop {boolean} [collapseWhitespace]
2051
+ * Collapse multiple whitespace characters into one where allowed. Also
2052
+ * controls trimming behaviour in several code paths.
2053
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace
2054
+ *
2055
+ * Default: `false`
2056
+ *
2057
+ * @prop {boolean} [conservativeCollapse]
2058
+ * If true, be conservative when collapsing whitespace (preserve more
2059
+ * whitespace in edge cases). Affects collapse algorithms.
2060
+ * Must also enable `collapseWhitespace` to have effect.
2061
+ *
2062
+ * Default: `false`
2063
+ *
2064
+ * @prop {boolean} [continueOnParseError]
2065
+ * When true, the parser will attempt to continue on recoverable parse
2066
+ * errors. Otherwise, parsing errors may throw.
2067
+ *
2068
+ * Default: `false`
2069
+ *
2070
+ * @prop {RegExp[]} [customAttrAssign]
2071
+ * Array of regexes used to recognise custom attribute assignment
2072
+ * operators (e.g. `'<div flex?="{{mode != cover}}"></div>'`).
2073
+ * These are concatenated with the built-in assignment patterns.
2074
+ *
2075
+ * Default: `[]`
2076
+ *
2077
+ * @prop {RegExp} [customAttrCollapse]
2078
+ * Regex matching attribute names whose values should be collapsed.
2079
+ * Basically used to remove newlines and excess spaces inside attribute values,
2080
+ * e.g. `/ng-class/`.
2081
+ *
2082
+ * @prop {[RegExp, RegExp][]} [customAttrSurround]
2083
+ * Array of `[openRegExp, closeRegExp]` pairs used by the parser to
2084
+ * detect custom attribute surround patterns (for non-standard syntaxes,
2085
+ * e.g. `<input {{#if value}}checked="checked"{{/if}}>`).
2086
+ *
2087
+ * @prop {RegExp[]} [customEventAttributes]
2088
+ * Array of regexes used to detect event handler attributes for `minifyJS`
2089
+ * (e.g. `ng-click`). The default matches standard `on…` event attributes.
2090
+ *
2091
+ * Default: `[/^on[a-z]{3,}$/]`
2092
+ *
2093
+ * @prop {number} [customFragmentQuantifierLimit]
2094
+ * Limits the quantifier used when building a safe regex for custom
2095
+ * fragments to avoid ReDoS. See source use for details.
2096
+ *
2097
+ * Default: `200`
2098
+ *
2099
+ * @prop {boolean} [decodeEntities]
2100
+ * When true, decodes HTML entities in text and attributes before
2101
+ * processing, and re-encodes ambiguous ampersands when outputting.
2102
+ *
2103
+ * Default: `false`
2104
+ *
2105
+ * @prop {boolean} [html5]
2106
+ * Parse and emit using HTML5 rules. Set to `false` to use non-HTML5
2107
+ * parsing behavior.
2108
+ *
2109
+ * Default: `true`
2110
+ *
2111
+ * @prop {RegExp[]} [ignoreCustomComments]
2112
+ * Comments matching any pattern in this array of regexes will be
2113
+ * preserved when `removeComments` is enabled. The default preserves
2114
+ * “bang” comments and comments starting with `#`.
2115
+ *
2116
+ * Default: `[/^!/, /^\s*#/]`
2117
+ *
2118
+ * @prop {RegExp[]} [ignoreCustomFragments]
2119
+ * Array of regexes used to identify fragments that should be
2120
+ * preserved (for example server templates). These fragments are temporarily
2121
+ * replaced during minification to avoid corrupting template code.
2122
+ * The default preserves ASP/PHP-style tags.
2123
+ *
2124
+ * Default: `[/<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/]`
2125
+ *
2126
+ * @prop {boolean} [includeAutoGeneratedTags]
2127
+ * If false, tags marked as auto-generated by the parser will be omitted
2128
+ * from output. Useful to skip injected tags.
2129
+ *
2130
+ * Default: `true`
2131
+ *
2132
+ * @prop {ArrayLike<string>} [inlineCustomElements]
2133
+ * Collection of custom element tag names that should be treated as inline
2134
+ * elements for white-space handling, alongside the built-in inline elements.
2135
+ *
2136
+ * Default: `[]`
2137
+ *
2138
+ * @prop {boolean} [keepClosingSlash]
2139
+ * Preserve the trailing slash in self-closing tags when present.
2140
+ *
2141
+ * Default: `false`
2142
+ *
2143
+ * @prop {(message: unknown) => void} [log]
2144
+ * Logging function used by the minifier for warnings/errors/info.
2145
+ * You can directly provide `console.log`, but `message` may also be an `Error`
2146
+ * object or other non-string value.
2147
+ *
2148
+ * Default: `() => {}` (no-op function)
2149
+ *
2150
+ * @prop {number} [maxInputLength]
2151
+ * The maximum allowed input length. Used as a guard against ReDoS via
2152
+ * pathological inputs. If the input exceeds this length an error is
2153
+ * thrown.
2154
+ *
2155
+ * Default: No limit
2156
+ *
2157
+ * @prop {number} [maxLineLength]
2158
+ * Maximum line length for the output. When set the minifier will wrap
2159
+ * output to the given number of characters where possible.
2160
+ *
2161
+ * Default: No limit
2162
+ *
2163
+ * @prop {boolean | Partial<import("lightningcss").TransformOptions<import("lightningcss").CustomAtRules>> | ((text: string, type?: string) => Promise<string> | string)} [minifyCSS]
2164
+ * When true, enables CSS minification for inline `<style>` tags or
2165
+ * `style` attributes. If an object is provided, it is passed to
2166
+ * [Lightning CSS](https://www.npmjs.com/package/lightningcss)
2167
+ * as transform options. If a function is provided, it will be used to perform
2168
+ * custom CSS minification. If disabled, CSS is not minified.
2169
+ *
2170
+ * Default: `false`
2171
+ *
2172
+ * @prop {boolean | import("terser").MinifyOptions | ((text: string, inline?: boolean) => Promise<string> | string)} [minifyJS]
2173
+ * When true, enables JS minification for `<script>` contents and
2174
+ * event handler attributes. If an object is provided, it is passed to
2175
+ * [terser](https://www.npmjs.com/package/terser) as minify options.
2176
+ * If a function is provided, it will be used to perform
2177
+ * custom JS minification. If disabled, JS is not minified.
2178
+ *
2179
+ * Default: `false`
2180
+ *
2181
+ * @prop {boolean | string | import("relateurl").Options | ((text: string) => Promise<string> | string)} [minifyURLs]
2182
+ * When true, enables URL rewriting/minification. If an object is provided,
2183
+ * it is passed to [relateurl](https://www.npmjs.com/package/relateurl)
2184
+ * as options. If a string is provided, it is treated as an `{ site: string }`
2185
+ * options object. If a function is provided, it will be used to perform
2186
+ * custom URL minification. If disabled, URLs are not minified.
2187
+ *
2188
+ * Default: `false`
2189
+ *
2190
+ * @prop {(name: string) => string} [name]
2191
+ * Function used to normalise tag/attribute names. By default, this lowercases
2192
+ * names, unless `caseSensitive` is enabled.
2193
+ *
2194
+ * Default: `(name) => name.toLowerCase()`,
2195
+ * or `(name) => name` (no-op function) if `caseSensitive` is enabled.
2196
+ *
2197
+ * @prop {boolean} [noNewlinesBeforeTagClose]
2198
+ * When wrapping lines, prevent inserting a newline directly before a
2199
+ * closing tag (useful to keep tags like `</a>` on the same line).
2200
+ *
2201
+ * Default: `false`
2202
+ *
2203
+ * @prop {boolean} [preserveLineBreaks]
2204
+ * Preserve a single line break at the start/end of text nodes when
2205
+ * collapsing/trimming whitespace.
2206
+ * Must also enable `collapseWhitespace` to have effect.
2207
+ *
2208
+ * Default: `false`
2209
+ *
2210
+ * @prop {boolean} [preventAttributesEscaping]
2211
+ * When true, attribute values will not be HTML-escaped (dangerous for
2212
+ * untrusted input). By default, attributes are escaped.
2213
+ *
2214
+ * Default: `false`
2215
+ *
2216
+ * @prop {boolean} [processConditionalComments]
2217
+ * When true, conditional comments (for example `<!--[if IE]> … <![endif]-->`)
2218
+ * will have their inner content processed by the minifier.
2219
+ * Useful to minify HTML that appears inside conditional comments.
2220
+ *
2221
+ * Default: `false`
2222
+ *
2223
+ * @prop {string[]} [processScripts]
2224
+ * Array of `type` attribute values for `<script>` elements whose contents
2225
+ * should be processed as HTML
2226
+ * (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.).
2227
+ * When present, the contents of matching script tags are recursively minified,
2228
+ * like normal HTML content.
2229
+ *
2230
+ * Default: `[]`
2231
+ *
2232
+ * @prop {"\"" | "'"} [quoteCharacter]
2233
+ * Preferred quote character for attribute values. If unspecified the
2234
+ * minifier picks the safest quote based on the attribute value.
2235
+ *
2236
+ * Default: Auto-detected
2237
+ *
2238
+ * @prop {boolean} [removeAttributeQuotes]
2239
+ * Remove quotes around attribute values where it is safe to do so.
2240
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_attribute_quotes
2241
+ *
2242
+ * Default: `false`
2243
+ *
2244
+ * @prop {boolean} [removeComments]
2245
+ * Remove HTML comments. Comments that match `ignoreCustomComments` will
2246
+ * still be preserved.
2247
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_comments
2248
+ *
2249
+ * Default: `false`
2250
+ *
2251
+ * @prop {boolean | ((attrName: string, tag: string) => boolean)} [removeEmptyAttributes]
2252
+ * If true, removes attributes whose values are empty (some attributes
2253
+ * are excluded by name). Can also be a function to customise which empty
2254
+ * attributes are removed.
2255
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_or_blank_attributes
2256
+ *
2257
+ * Default: `false`
2258
+ *
2259
+ * @prop {boolean} [removeEmptyElements]
2260
+ * Remove elements that are empty and safe to remove (for example
2261
+ * `<script />` without `src`).
2262
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_elements
2263
+ *
2264
+ * Default: `false`
2265
+ *
2266
+ * @prop {boolean} [removeOptionalTags]
2267
+ * Drop optional start/end tags where the HTML specification permits it
2268
+ * (for example `</li>`, optional `<html>` etc.).
2269
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_optional_tags
2270
+ *
2271
+ * Default: `false`
2272
+ *
2273
+ * @prop {boolean} [removeRedundantAttributes]
2274
+ * Remove attributes that are redundant because they match the element's
2275
+ * default values (for example `<button type="submit">`).
2276
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_redundant_attributes
2277
+ *
2278
+ * Default: `false`
2279
+ *
2280
+ * @prop {boolean} [removeScriptTypeAttributes]
2281
+ * Remove `type` attributes from `<script>` when they are unnecessary
2282
+ * (e.g. `type="text/javascript"`).
2283
+ *
2284
+ * Default: `false`
2285
+ *
2286
+ * @prop {boolean} [removeStyleLinkTypeAttributes]
2287
+ * Remove `type` attributes from `<style>` and `<link>` elements when
2288
+ * they are unnecessary (e.g. `type="text/css"`).
2289
+ *
2290
+ * Default: `false`
2291
+ *
2292
+ * @prop {boolean} [removeTagWhitespace]
2293
+ * **Note that this will currently result in invalid HTML!**
2294
+ *
2295
+ * When true, extra whitespace between tag name and attributes (or before
2296
+ * the closing bracket) will be removed where possible. Affects output spacing
2297
+ * such as the space used in the short doctype representation.
2298
+ *
2299
+ * Default: `false`
2300
+ *
2301
+ * @prop {boolean | ((tag: string, attrs: HTMLAttribute[]) => void)} [sortAttributes]
2302
+ * When true, enables sorting of attributes. If a function is provided it
2303
+ * will be used as a custom attribute sorter, which should mutate `attrs`
2304
+ * in-place to the desired order. If disabled, the minifier will attempt to
2305
+ * preserve the order from the input.
2306
+ *
2307
+ * Default: `false`
2308
+ *
2309
+ * @prop {boolean | ((value: string) => string)} [sortClassName]
2310
+ * When true, enables sorting of class names inside `class` attributes.
2311
+ * If a function is provided it will be used to transform/sort the class
2312
+ * name string. If disabled, the minifier will attempt to preserve the
2313
+ * class-name order from the input.
2314
+ *
2315
+ * Default: `false`
2316
+ *
2317
+ * @prop {boolean} [trimCustomFragments]
2318
+ * When true, whitespace around ignored custom fragments may be trimmed
2319
+ * more aggressively. This affects how preserved fragments interact with
2320
+ * surrounding whitespace collapse.
2321
+ *
2322
+ * Default: `false`
2323
+ *
2324
+ * @prop {boolean} [useShortDoctype]
2325
+ * Replace the HTML doctype with the short `<!doctype html>` form.
2326
+ * See also: https://perfectionkills.com/experimenting-with-html-minifier/#use_short_doctype
2327
+ *
2328
+ * Default: `false`
2329
+ */
2330
+
1995
2331
  exports.default = htmlminifier;
1996
2332
  exports.minify = minify;