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 +1 -1
- package/dist/htmlminifier.cjs +336 -0
- package/dist/htmlminifier.esm.bundle.js +336 -0
- package/dist/types/htmlminifier.d.ts +382 -0
- package/dist/types/htmlminifier.d.ts.map +1 -0
- package/dist/types/htmlparser.d.ts +8 -0
- package/dist/types/htmlparser.d.ts.map +1 -0
- package/dist/types/tokenchain.d.ts +9 -0
- package/dist/types/tokenchain.d.ts.map +1 -0
- package/dist/types/utils.d.ts +2 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/package.json +12 -6
- package/src/htmlminifier.js +337 -1
package/src/htmlminifier.js
CHANGED
|
@@ -1452,6 +1452,11 @@ function joinResultSegments(results, options, restoreCustom, restoreIgnore) {
|
|
|
1452
1452
|
return options.collapseWhitespace ? collapseWhitespace(str, options, true, true) : str;
|
|
1453
1453
|
}
|
|
1454
1454
|
|
|
1455
|
+
/**
|
|
1456
|
+
* @param {string} value
|
|
1457
|
+
* @param {MinifierOptions} [options]
|
|
1458
|
+
* @returns {Promise<string>}
|
|
1459
|
+
*/
|
|
1455
1460
|
export const minify = async function (value, options) {
|
|
1456
1461
|
const start = Date.now();
|
|
1457
1462
|
options = processOptions(options || {});
|
|
@@ -1460,4 +1465,335 @@ export const minify = async function (value, options) {
|
|
|
1460
1465
|
return result;
|
|
1461
1466
|
};
|
|
1462
1467
|
|
|
1463
|
-
export default { minify };
|
|
1468
|
+
export default { minify };
|
|
1469
|
+
|
|
1470
|
+
/**
|
|
1471
|
+
* @typedef {Object} HTMLAttribute
|
|
1472
|
+
* Representation of an attribute from the HTML parser.
|
|
1473
|
+
*
|
|
1474
|
+
* @prop {string} name
|
|
1475
|
+
* @prop {string} [value]
|
|
1476
|
+
* @prop {string} [quote]
|
|
1477
|
+
* @prop {string} [customAssign]
|
|
1478
|
+
* @prop {string} [customOpen]
|
|
1479
|
+
* @prop {string} [customClose]
|
|
1480
|
+
*/
|
|
1481
|
+
|
|
1482
|
+
/**
|
|
1483
|
+
* @typedef {Object} MinifierOptions
|
|
1484
|
+
* Options that control how HTML is minified. All of these are optional
|
|
1485
|
+
* and usually default to a disabled/safe value unless noted.
|
|
1486
|
+
*
|
|
1487
|
+
* @prop {(tag: string, attrs: HTMLAttribute[], canCollapseWhitespace: (tag: string) => boolean) => boolean} [canCollapseWhitespace]
|
|
1488
|
+
* Predicate that determines whether whitespace inside a given element
|
|
1489
|
+
* can be collapsed.
|
|
1490
|
+
*
|
|
1491
|
+
* Default: Built-in `canCollapseWhitespace` function
|
|
1492
|
+
*
|
|
1493
|
+
* @prop {(tag: string | null, attrs: HTMLAttribute[] | undefined, canTrimWhitespace: (tag: string) => boolean) => boolean} [canTrimWhitespace]
|
|
1494
|
+
* Predicate that determines whether leading/trailing whitespace around
|
|
1495
|
+
* the element may be trimmed.
|
|
1496
|
+
*
|
|
1497
|
+
* Default: Built-in `canTrimWhitespace` function
|
|
1498
|
+
*
|
|
1499
|
+
* @prop {boolean} [caseSensitive]
|
|
1500
|
+
* When true, tag and attribute names are treated as case-sensitive.
|
|
1501
|
+
* Useful for custom HTML tags.
|
|
1502
|
+
* If false (default) names are lower-cased via the `name` function.
|
|
1503
|
+
*
|
|
1504
|
+
* Default: `false`
|
|
1505
|
+
*
|
|
1506
|
+
* @prop {boolean} [collapseBooleanAttributes]
|
|
1507
|
+
* Collapse boolean attributes to their name only (for example
|
|
1508
|
+
* `disabled="disabled"` -> `disabled`).
|
|
1509
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#collapse_boolean_attributes
|
|
1510
|
+
*
|
|
1511
|
+
* Default: `false`
|
|
1512
|
+
*
|
|
1513
|
+
* @prop {boolean} [collapseInlineTagWhitespace]
|
|
1514
|
+
* When false (default) whitespace around `inline` tags is preserved in
|
|
1515
|
+
* more cases. When true, whitespace around inline tags may be collapsed.
|
|
1516
|
+
* Must also enable `collapseWhitespace` to have effect.
|
|
1517
|
+
*
|
|
1518
|
+
* Default: `false`
|
|
1519
|
+
*
|
|
1520
|
+
* @prop {boolean} [collapseWhitespace]
|
|
1521
|
+
* Collapse multiple whitespace characters into one where allowed. Also
|
|
1522
|
+
* controls trimming behaviour in several code paths.
|
|
1523
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#collapse_whitespace
|
|
1524
|
+
*
|
|
1525
|
+
* Default: `false`
|
|
1526
|
+
*
|
|
1527
|
+
* @prop {boolean} [conservativeCollapse]
|
|
1528
|
+
* If true, be conservative when collapsing whitespace (preserve more
|
|
1529
|
+
* whitespace in edge cases). Affects collapse algorithms.
|
|
1530
|
+
* Must also enable `collapseWhitespace` to have effect.
|
|
1531
|
+
*
|
|
1532
|
+
* Default: `false`
|
|
1533
|
+
*
|
|
1534
|
+
* @prop {boolean} [continueOnParseError]
|
|
1535
|
+
* When true, the parser will attempt to continue on recoverable parse
|
|
1536
|
+
* errors. Otherwise, parsing errors may throw.
|
|
1537
|
+
*
|
|
1538
|
+
* Default: `false`
|
|
1539
|
+
*
|
|
1540
|
+
* @prop {RegExp[]} [customAttrAssign]
|
|
1541
|
+
* Array of regexes used to recognise custom attribute assignment
|
|
1542
|
+
* operators (e.g. `'<div flex?="{{mode != cover}}"></div>'`).
|
|
1543
|
+
* These are concatenated with the built-in assignment patterns.
|
|
1544
|
+
*
|
|
1545
|
+
* Default: `[]`
|
|
1546
|
+
*
|
|
1547
|
+
* @prop {RegExp} [customAttrCollapse]
|
|
1548
|
+
* Regex matching attribute names whose values should be collapsed.
|
|
1549
|
+
* Basically used to remove newlines and excess spaces inside attribute values,
|
|
1550
|
+
* e.g. `/ng-class/`.
|
|
1551
|
+
*
|
|
1552
|
+
* @prop {[RegExp, RegExp][]} [customAttrSurround]
|
|
1553
|
+
* Array of `[openRegExp, closeRegExp]` pairs used by the parser to
|
|
1554
|
+
* detect custom attribute surround patterns (for non-standard syntaxes,
|
|
1555
|
+
* e.g. `<input {{#if value}}checked="checked"{{/if}}>`).
|
|
1556
|
+
*
|
|
1557
|
+
* @prop {RegExp[]} [customEventAttributes]
|
|
1558
|
+
* Array of regexes used to detect event handler attributes for `minifyJS`
|
|
1559
|
+
* (e.g. `ng-click`). The default matches standard `on…` event attributes.
|
|
1560
|
+
*
|
|
1561
|
+
* Default: `[/^on[a-z]{3,}$/]`
|
|
1562
|
+
*
|
|
1563
|
+
* @prop {number} [customFragmentQuantifierLimit]
|
|
1564
|
+
* Limits the quantifier used when building a safe regex for custom
|
|
1565
|
+
* fragments to avoid ReDoS. See source use for details.
|
|
1566
|
+
*
|
|
1567
|
+
* Default: `200`
|
|
1568
|
+
*
|
|
1569
|
+
* @prop {boolean} [decodeEntities]
|
|
1570
|
+
* When true, decodes HTML entities in text and attributes before
|
|
1571
|
+
* processing, and re-encodes ambiguous ampersands when outputting.
|
|
1572
|
+
*
|
|
1573
|
+
* Default: `false`
|
|
1574
|
+
*
|
|
1575
|
+
* @prop {boolean} [html5]
|
|
1576
|
+
* Parse and emit using HTML5 rules. Set to `false` to use non-HTML5
|
|
1577
|
+
* parsing behavior.
|
|
1578
|
+
*
|
|
1579
|
+
* Default: `true`
|
|
1580
|
+
*
|
|
1581
|
+
* @prop {RegExp[]} [ignoreCustomComments]
|
|
1582
|
+
* Comments matching any pattern in this array of regexes will be
|
|
1583
|
+
* preserved when `removeComments` is enabled. The default preserves
|
|
1584
|
+
* “bang” comments and comments starting with `#`.
|
|
1585
|
+
*
|
|
1586
|
+
* Default: `[/^!/, /^\s*#/]`
|
|
1587
|
+
*
|
|
1588
|
+
* @prop {RegExp[]} [ignoreCustomFragments]
|
|
1589
|
+
* Array of regexes used to identify fragments that should be
|
|
1590
|
+
* preserved (for example server templates). These fragments are temporarily
|
|
1591
|
+
* replaced during minification to avoid corrupting template code.
|
|
1592
|
+
* The default preserves ASP/PHP-style tags.
|
|
1593
|
+
*
|
|
1594
|
+
* Default: `[/<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/]`
|
|
1595
|
+
*
|
|
1596
|
+
* @prop {boolean} [includeAutoGeneratedTags]
|
|
1597
|
+
* If false, tags marked as auto-generated by the parser will be omitted
|
|
1598
|
+
* from output. Useful to skip injected tags.
|
|
1599
|
+
*
|
|
1600
|
+
* Default: `true`
|
|
1601
|
+
*
|
|
1602
|
+
* @prop {ArrayLike<string>} [inlineCustomElements]
|
|
1603
|
+
* Collection of custom element tag names that should be treated as inline
|
|
1604
|
+
* elements for white-space handling, alongside the built-in inline elements.
|
|
1605
|
+
*
|
|
1606
|
+
* Default: `[]`
|
|
1607
|
+
*
|
|
1608
|
+
* @prop {boolean} [keepClosingSlash]
|
|
1609
|
+
* Preserve the trailing slash in self-closing tags when present.
|
|
1610
|
+
*
|
|
1611
|
+
* Default: `false`
|
|
1612
|
+
*
|
|
1613
|
+
* @prop {(message: unknown) => void} [log]
|
|
1614
|
+
* Logging function used by the minifier for warnings/errors/info.
|
|
1615
|
+
* You can directly provide `console.log`, but `message` may also be an `Error`
|
|
1616
|
+
* object or other non-string value.
|
|
1617
|
+
*
|
|
1618
|
+
* Default: `() => {}` (no-op function)
|
|
1619
|
+
*
|
|
1620
|
+
* @prop {number} [maxInputLength]
|
|
1621
|
+
* The maximum allowed input length. Used as a guard against ReDoS via
|
|
1622
|
+
* pathological inputs. If the input exceeds this length an error is
|
|
1623
|
+
* thrown.
|
|
1624
|
+
*
|
|
1625
|
+
* Default: No limit
|
|
1626
|
+
*
|
|
1627
|
+
* @prop {number} [maxLineLength]
|
|
1628
|
+
* Maximum line length for the output. When set the minifier will wrap
|
|
1629
|
+
* output to the given number of characters where possible.
|
|
1630
|
+
*
|
|
1631
|
+
* Default: No limit
|
|
1632
|
+
*
|
|
1633
|
+
* @prop {boolean | Partial<import("lightningcss").TransformOptions<import("lightningcss").CustomAtRules>> | ((text: string, type?: string) => Promise<string> | string)} [minifyCSS]
|
|
1634
|
+
* When true, enables CSS minification for inline `<style>` tags or
|
|
1635
|
+
* `style` attributes. If an object is provided, it is passed to
|
|
1636
|
+
* [Lightning CSS](https://www.npmjs.com/package/lightningcss)
|
|
1637
|
+
* as transform options. If a function is provided, it will be used to perform
|
|
1638
|
+
* custom CSS minification. If disabled, CSS is not minified.
|
|
1639
|
+
*
|
|
1640
|
+
* Default: `false`
|
|
1641
|
+
*
|
|
1642
|
+
* @prop {boolean | import("terser").MinifyOptions | ((text: string, inline?: boolean) => Promise<string> | string)} [minifyJS]
|
|
1643
|
+
* When true, enables JS minification for `<script>` contents and
|
|
1644
|
+
* event handler attributes. If an object is provided, it is passed to
|
|
1645
|
+
* [terser](https://www.npmjs.com/package/terser) as minify options.
|
|
1646
|
+
* If a function is provided, it will be used to perform
|
|
1647
|
+
* custom JS minification. If disabled, JS is not minified.
|
|
1648
|
+
*
|
|
1649
|
+
* Default: `false`
|
|
1650
|
+
*
|
|
1651
|
+
* @prop {boolean | string | import("relateurl").Options | ((text: string) => Promise<string> | string)} [minifyURLs]
|
|
1652
|
+
* When true, enables URL rewriting/minification. If an object is provided,
|
|
1653
|
+
* it is passed to [relateurl](https://www.npmjs.com/package/relateurl)
|
|
1654
|
+
* as options. If a string is provided, it is treated as an `{ site: string }`
|
|
1655
|
+
* options object. If a function is provided, it will be used to perform
|
|
1656
|
+
* custom URL minification. If disabled, URLs are not minified.
|
|
1657
|
+
*
|
|
1658
|
+
* Default: `false`
|
|
1659
|
+
*
|
|
1660
|
+
* @prop {(name: string) => string} [name]
|
|
1661
|
+
* Function used to normalise tag/attribute names. By default, this lowercases
|
|
1662
|
+
* names, unless `caseSensitive` is enabled.
|
|
1663
|
+
*
|
|
1664
|
+
* Default: `(name) => name.toLowerCase()`,
|
|
1665
|
+
* or `(name) => name` (no-op function) if `caseSensitive` is enabled.
|
|
1666
|
+
*
|
|
1667
|
+
* @prop {boolean} [noNewlinesBeforeTagClose]
|
|
1668
|
+
* When wrapping lines, prevent inserting a newline directly before a
|
|
1669
|
+
* closing tag (useful to keep tags like `</a>` on the same line).
|
|
1670
|
+
*
|
|
1671
|
+
* Default: `false`
|
|
1672
|
+
*
|
|
1673
|
+
* @prop {boolean} [preserveLineBreaks]
|
|
1674
|
+
* Preserve a single line break at the start/end of text nodes when
|
|
1675
|
+
* collapsing/trimming whitespace.
|
|
1676
|
+
* Must also enable `collapseWhitespace` to have effect.
|
|
1677
|
+
*
|
|
1678
|
+
* Default: `false`
|
|
1679
|
+
*
|
|
1680
|
+
* @prop {boolean} [preventAttributesEscaping]
|
|
1681
|
+
* When true, attribute values will not be HTML-escaped (dangerous for
|
|
1682
|
+
* untrusted input). By default, attributes are escaped.
|
|
1683
|
+
*
|
|
1684
|
+
* Default: `false`
|
|
1685
|
+
*
|
|
1686
|
+
* @prop {boolean} [processConditionalComments]
|
|
1687
|
+
* When true, conditional comments (for example `<!--[if IE]> … <![endif]-->`)
|
|
1688
|
+
* will have their inner content processed by the minifier.
|
|
1689
|
+
* Useful to minify HTML that appears inside conditional comments.
|
|
1690
|
+
*
|
|
1691
|
+
* Default: `false`
|
|
1692
|
+
*
|
|
1693
|
+
* @prop {string[]} [processScripts]
|
|
1694
|
+
* Array of `type` attribute values for `<script>` elements whose contents
|
|
1695
|
+
* should be processed as HTML
|
|
1696
|
+
* (e.g. `text/ng-template`, `text/x-handlebars-template`, etc.).
|
|
1697
|
+
* When present, the contents of matching script tags are recursively minified,
|
|
1698
|
+
* like normal HTML content.
|
|
1699
|
+
*
|
|
1700
|
+
* Default: `[]`
|
|
1701
|
+
*
|
|
1702
|
+
* @prop {"\"" | "'"} [quoteCharacter]
|
|
1703
|
+
* Preferred quote character for attribute values. If unspecified the
|
|
1704
|
+
* minifier picks the safest quote based on the attribute value.
|
|
1705
|
+
*
|
|
1706
|
+
* Default: Auto-detected
|
|
1707
|
+
*
|
|
1708
|
+
* @prop {boolean} [removeAttributeQuotes]
|
|
1709
|
+
* Remove quotes around attribute values where it is safe to do so.
|
|
1710
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_attribute_quotes
|
|
1711
|
+
*
|
|
1712
|
+
* Default: `false`
|
|
1713
|
+
*
|
|
1714
|
+
* @prop {boolean} [removeComments]
|
|
1715
|
+
* Remove HTML comments. Comments that match `ignoreCustomComments` will
|
|
1716
|
+
* still be preserved.
|
|
1717
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_comments
|
|
1718
|
+
*
|
|
1719
|
+
* Default: `false`
|
|
1720
|
+
*
|
|
1721
|
+
* @prop {boolean | ((attrName: string, tag: string) => boolean)} [removeEmptyAttributes]
|
|
1722
|
+
* If true, removes attributes whose values are empty (some attributes
|
|
1723
|
+
* are excluded by name). Can also be a function to customise which empty
|
|
1724
|
+
* attributes are removed.
|
|
1725
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_or_blank_attributes
|
|
1726
|
+
*
|
|
1727
|
+
* Default: `false`
|
|
1728
|
+
*
|
|
1729
|
+
* @prop {boolean} [removeEmptyElements]
|
|
1730
|
+
* Remove elements that are empty and safe to remove (for example
|
|
1731
|
+
* `<script />` without `src`).
|
|
1732
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_empty_elements
|
|
1733
|
+
*
|
|
1734
|
+
* Default: `false`
|
|
1735
|
+
*
|
|
1736
|
+
* @prop {boolean} [removeOptionalTags]
|
|
1737
|
+
* Drop optional start/end tags where the HTML specification permits it
|
|
1738
|
+
* (for example `</li>`, optional `<html>` etc.).
|
|
1739
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_optional_tags
|
|
1740
|
+
*
|
|
1741
|
+
* Default: `false`
|
|
1742
|
+
*
|
|
1743
|
+
* @prop {boolean} [removeRedundantAttributes]
|
|
1744
|
+
* Remove attributes that are redundant because they match the element's
|
|
1745
|
+
* default values (for example `<button type="submit">`).
|
|
1746
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#remove_redundant_attributes
|
|
1747
|
+
*
|
|
1748
|
+
* Default: `false`
|
|
1749
|
+
*
|
|
1750
|
+
* @prop {boolean} [removeScriptTypeAttributes]
|
|
1751
|
+
* Remove `type` attributes from `<script>` when they are unnecessary
|
|
1752
|
+
* (e.g. `type="text/javascript"`).
|
|
1753
|
+
*
|
|
1754
|
+
* Default: `false`
|
|
1755
|
+
*
|
|
1756
|
+
* @prop {boolean} [removeStyleLinkTypeAttributes]
|
|
1757
|
+
* Remove `type` attributes from `<style>` and `<link>` elements when
|
|
1758
|
+
* they are unnecessary (e.g. `type="text/css"`).
|
|
1759
|
+
*
|
|
1760
|
+
* Default: `false`
|
|
1761
|
+
*
|
|
1762
|
+
* @prop {boolean} [removeTagWhitespace]
|
|
1763
|
+
* **Note that this will currently result in invalid HTML!**
|
|
1764
|
+
*
|
|
1765
|
+
* When true, extra whitespace between tag name and attributes (or before
|
|
1766
|
+
* the closing bracket) will be removed where possible. Affects output spacing
|
|
1767
|
+
* such as the space used in the short doctype representation.
|
|
1768
|
+
*
|
|
1769
|
+
* Default: `false`
|
|
1770
|
+
*
|
|
1771
|
+
* @prop {boolean | ((tag: string, attrs: HTMLAttribute[]) => void)} [sortAttributes]
|
|
1772
|
+
* When true, enables sorting of attributes. If a function is provided it
|
|
1773
|
+
* will be used as a custom attribute sorter, which should mutate `attrs`
|
|
1774
|
+
* in-place to the desired order. If disabled, the minifier will attempt to
|
|
1775
|
+
* preserve the order from the input.
|
|
1776
|
+
*
|
|
1777
|
+
* Default: `false`
|
|
1778
|
+
*
|
|
1779
|
+
* @prop {boolean | ((value: string) => string)} [sortClassName]
|
|
1780
|
+
* When true, enables sorting of class names inside `class` attributes.
|
|
1781
|
+
* If a function is provided it will be used to transform/sort the class
|
|
1782
|
+
* name string. If disabled, the minifier will attempt to preserve the
|
|
1783
|
+
* class-name order from the input.
|
|
1784
|
+
*
|
|
1785
|
+
* Default: `false`
|
|
1786
|
+
*
|
|
1787
|
+
* @prop {boolean} [trimCustomFragments]
|
|
1788
|
+
* When true, whitespace around ignored custom fragments may be trimmed
|
|
1789
|
+
* more aggressively. This affects how preserved fragments interact with
|
|
1790
|
+
* surrounding whitespace collapse.
|
|
1791
|
+
*
|
|
1792
|
+
* Default: `false`
|
|
1793
|
+
*
|
|
1794
|
+
* @prop {boolean} [useShortDoctype]
|
|
1795
|
+
* Replace the HTML doctype with the short `<!doctype html>` form.
|
|
1796
|
+
* See also: https://perfectionkills.com/experimenting-with-html-minifier/#use_short_doctype
|
|
1797
|
+
*
|
|
1798
|
+
* Default: `false`
|
|
1799
|
+
*/
|