htmlnano 2.1.1 → 2.1.3
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 +53 -12
- package/dist/_modules/collapseAttributeWhitespace.d.mts +57 -0
- package/dist/_modules/collapseAttributeWhitespace.d.ts +57 -0
- package/dist/_modules/collapseAttributeWhitespace.js +296 -0
- package/dist/_modules/collapseAttributeWhitespace.mjs +293 -0
- package/dist/_modules/collapseBooleanAttributes.d.mts +60 -0
- package/dist/_modules/collapseBooleanAttributes.d.ts +60 -0
- package/dist/_modules/collapseBooleanAttributes.js +159 -0
- package/{lib/modules → dist/_modules}/collapseBooleanAttributes.mjs +39 -57
- package/dist/_modules/collapseWhitespace.d.mts +57 -0
- package/dist/_modules/collapseWhitespace.d.ts +57 -0
- package/dist/_modules/collapseWhitespace.js +172 -0
- package/dist/_modules/collapseWhitespace.mjs +170 -0
- package/dist/_modules/custom.d.mts +57 -0
- package/dist/_modules/custom.d.ts +57 -0
- package/dist/_modules/custom.js +22 -0
- package/dist/_modules/custom.mjs +20 -0
- package/dist/_modules/deduplicateAttributeValues.d.mts +56 -0
- package/dist/_modules/deduplicateAttributeValues.d.ts +56 -0
- package/dist/_modules/deduplicateAttributeValues.js +38 -0
- package/dist/_modules/deduplicateAttributeValues.mjs +36 -0
- package/dist/_modules/example.d.mts +59 -0
- package/dist/_modules/example.d.ts +59 -0
- package/dist/_modules/example.js +67 -0
- package/dist/_modules/example.mjs +65 -0
- package/dist/_modules/mergeScripts.d.mts +56 -0
- package/dist/_modules/mergeScripts.d.ts +56 -0
- package/dist/_modules/mergeScripts.js +53 -0
- package/dist/_modules/mergeScripts.mjs +51 -0
- package/dist/_modules/mergeStyles.d.mts +56 -0
- package/dist/_modules/mergeStyles.d.ts +56 -0
- package/dist/_modules/mergeStyles.js +42 -0
- package/dist/_modules/mergeStyles.mjs +40 -0
- package/dist/_modules/minifyConditionalComments.d.mts +56 -0
- package/dist/_modules/minifyConditionalComments.d.ts +56 -0
- package/dist/_modules/minifyConditionalComments.js +54 -0
- package/{lib/modules → dist/_modules}/minifyConditionalComments.mjs +21 -22
- package/dist/_modules/minifyCss.d.mts +56 -0
- package/dist/_modules/minifyCss.d.ts +56 -0
- package/dist/_modules/minifyCss.js +84 -0
- package/dist/_modules/minifyCss.mjs +82 -0
- package/dist/_modules/minifyJs.d.mts +56 -0
- package/dist/_modules/minifyJs.d.ts +56 -0
- package/dist/_modules/minifyJs.js +108 -0
- package/dist/_modules/minifyJs.mjs +106 -0
- package/dist/_modules/minifyJson.d.mts +56 -0
- package/dist/_modules/minifyJson.d.ts +56 -0
- package/dist/_modules/minifyJson.js +35 -0
- package/dist/_modules/minifyJson.mjs +33 -0
- package/dist/_modules/minifySvg.d.mts +56 -0
- package/dist/_modules/minifySvg.d.ts +56 -0
- package/dist/_modules/minifySvg.js +40 -0
- package/dist/_modules/minifySvg.mjs +38 -0
- package/dist/_modules/minifyUrls.d.mts +56 -0
- package/dist/_modules/minifyUrls.d.ts +56 -0
- package/dist/_modules/minifyUrls.js +180 -0
- package/dist/_modules/minifyUrls.mjs +178 -0
- package/dist/_modules/normalizeAttributeValues.d.mts +56 -0
- package/dist/_modules/normalizeAttributeValues.d.ts +56 -0
- package/dist/_modules/normalizeAttributeValues.js +234 -0
- package/dist/_modules/normalizeAttributeValues.mjs +232 -0
- package/dist/_modules/removeAttributeQuotes.d.mts +56 -0
- package/dist/_modules/removeAttributeQuotes.d.ts +56 -0
- package/dist/_modules/removeAttributeQuotes.js +15 -0
- package/dist/_modules/removeAttributeQuotes.mjs +13 -0
- package/dist/_modules/removeComments.d.mts +58 -0
- package/dist/_modules/removeComments.d.ts +58 -0
- package/dist/_modules/removeComments.js +83 -0
- package/{lib/modules → dist/_modules}/removeComments.mjs +24 -35
- package/dist/_modules/removeEmptyAttributes.d.mts +56 -0
- package/dist/_modules/removeEmptyAttributes.d.ts +56 -0
- package/dist/_modules/removeEmptyAttributes.js +197 -0
- package/dist/_modules/removeEmptyAttributes.mjs +195 -0
- package/dist/_modules/removeOptionalTags.d.mts +56 -0
- package/dist/_modules/removeOptionalTags.d.ts +56 -0
- package/dist/_modules/removeOptionalTags.js +190 -0
- package/{lib/modules → dist/_modules}/removeOptionalTags.mjs +54 -91
- package/dist/_modules/removeRedundantAttributes.d.mts +57 -0
- package/dist/_modules/removeRedundantAttributes.d.ts +57 -0
- package/dist/_modules/removeRedundantAttributes.js +128 -0
- package/{lib/modules → dist/_modules}/removeRedundantAttributes.mjs +43 -59
- package/dist/_modules/removeUnusedCss.d.mts +60 -0
- package/dist/_modules/removeUnusedCss.d.ts +60 -0
- package/dist/_modules/removeUnusedCss.js +134 -0
- package/dist/_modules/removeUnusedCss.mjs +132 -0
- package/dist/_modules/sortAttributes.d.mts +57 -0
- package/dist/_modules/sortAttributes.d.ts +57 -0
- package/dist/_modules/sortAttributes.js +102 -0
- package/{lib/modules → dist/_modules}/sortAttributes.mjs +39 -60
- package/dist/_modules/sortAttributesWithLists.d.mts +56 -0
- package/dist/_modules/sortAttributesWithLists.d.ts +56 -0
- package/dist/_modules/sortAttributesWithLists.js +118 -0
- package/{lib/modules → dist/_modules}/sortAttributesWithLists.mjs +41 -60
- package/dist/helpers.js +72 -0
- package/dist/helpers.mjs +63 -0
- package/dist/index.js +223 -0
- package/dist/index.mjs +209 -0
- package/dist/presets/ampSafe.js +19 -0
- package/{lib → dist}/presets/ampSafe.mjs +6 -4
- package/dist/presets/max.js +28 -0
- package/{lib → dist}/presets/max.mjs +6 -4
- package/dist/presets/safe.js +60 -0
- package/{lib → dist}/presets/safe.mjs +13 -20
- package/package.json +53 -59
- package/.eslintignore +0 -3
- package/CHANGELOG.md +0 -398
- package/docs/README.md +0 -33
- package/docs/babel.config.js +0 -3
- package/docs/docs/010-introduction.md +0 -22
- package/docs/docs/020-usage.md +0 -117
- package/docs/docs/030-config.md +0 -21
- package/docs/docs/040-presets.md +0 -75
- package/docs/docs/050-modules.md +0 -855
- package/docs/docs/060-contribute.md +0 -16
- package/docs/docusaurus.config.js +0 -65
- package/docs/netlify.toml +0 -4
- package/docs/package-lock.json +0 -21796
- package/docs/package.json +0 -40
- package/docs/sidebars.js +0 -26
- package/docs/versioned_docs/version-1.1.1/010-introduction.md +0 -22
- package/docs/versioned_docs/version-1.1.1/020-usage.md +0 -77
- package/docs/versioned_docs/version-1.1.1/030-config.md +0 -21
- package/docs/versioned_docs/version-1.1.1/040-presets.md +0 -75
- package/docs/versioned_docs/version-1.1.1/050-modules.md +0 -785
- package/docs/versioned_docs/version-1.1.1/060-contribute.md +0 -16
- package/docs/versioned_docs/version-2.0.0/010-introduction.md +0 -22
- package/docs/versioned_docs/version-2.0.0/020-usage.md +0 -77
- package/docs/versioned_docs/version-2.0.0/030-config.md +0 -21
- package/docs/versioned_docs/version-2.0.0/040-presets.md +0 -75
- package/docs/versioned_docs/version-2.0.0/050-modules.md +0 -838
- package/docs/versioned_docs/version-2.0.0/060-contribute.md +0 -16
- package/docs/versioned_sidebars/version-1.1.1-sidebars.json +0 -8
- package/docs/versioned_sidebars/version-2.0.0-sidebars.json +0 -8
- package/docs/versions.json +0 -4
- package/index.cjs +0 -11
- package/index.d.cts +0 -3
- package/index.d.mts +0 -3
- package/index.d.ts +0 -93
- package/index.mjs +0 -2
- package/lib/helpers.cjs +0 -79
- package/lib/helpers.mjs +0 -53
- package/lib/htmlnano.cjs +0 -200
- package/lib/htmlnano.mjs +0 -196
- package/lib/modules/collapseAttributeWhitespace.cjs +0 -86
- package/lib/modules/collapseAttributeWhitespace.mjs +0 -104
- package/lib/modules/collapseBooleanAttributes.cjs +0 -62
- package/lib/modules/collapseWhitespace.cjs +0 -100
- package/lib/modules/collapseWhitespace.mjs +0 -132
- package/lib/modules/custom.cjs +0 -19
- package/lib/modules/custom.mjs +0 -16
- package/lib/modules/deduplicateAttributeValues.cjs +0 -38
- package/lib/modules/deduplicateAttributeValues.mjs +0 -40
- package/lib/modules/example.cjs +0 -85
- package/lib/modules/example.mjs +0 -75
- package/lib/modules/mergeScripts.cjs +0 -54
- package/lib/modules/mergeScripts.mjs +0 -56
- package/lib/modules/mergeStyles.cjs +0 -38
- package/lib/modules/mergeStyles.mjs +0 -36
- package/lib/modules/minifyConditionalComments.cjs +0 -47
- package/lib/modules/minifyCss.cjs +0 -73
- package/lib/modules/minifyCss.mjs +0 -88
- package/lib/modules/minifyJs.cjs +0 -103
- package/lib/modules/minifyJs.mjs +0 -121
- package/lib/modules/minifyJson.cjs +0 -24
- package/lib/modules/minifyJson.mjs +0 -21
- package/lib/modules/minifySvg.cjs +0 -37
- package/lib/modules/minifySvg.mjs +0 -30
- package/lib/modules/minifyUrls.cjs +0 -141
- package/lib/modules/minifyUrls.mjs +0 -229
- package/lib/modules/normalizeAttributeValues.cjs +0 -120
- package/lib/modules/normalizeAttributeValues.mjs +0 -140
- package/lib/modules/removeAttributeQuotes.cjs +0 -17
- package/lib/modules/removeAttributeQuotes.mjs +0 -12
- package/lib/modules/removeComments.cjs +0 -86
- package/lib/modules/removeEmptyAttributes.cjs +0 -72
- package/lib/modules/removeEmptyAttributes.mjs +0 -121
- package/lib/modules/removeOptionalTags.cjs +0 -183
- package/lib/modules/removeRedundantAttributes.cjs +0 -112
- package/lib/modules/removeUnusedCss.cjs +0 -113
- package/lib/modules/removeUnusedCss.mjs +0 -122
- package/lib/modules/sortAttributes.cjs +0 -99
- package/lib/modules/sortAttributesWithLists.cjs +0 -115
- package/lib/presets/ampSafe.cjs +0 -18
- package/lib/presets/max.cjs +0 -27
- package/lib/presets/safe.cjs +0 -65
- package/test.js +0 -48
package/README.md
CHANGED
|
@@ -2,21 +2,62 @@
|
|
|
2
2
|
[](http://badge.fury.io/js/htmlnano)
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
Modular HTML minifier, built on top of the [PostHTML](https://github.com/posthtml/posthtml). Inspired by [cssnano](
|
|
5
|
+
Modular HTML minifier, built on top of the [PostHTML](https://github.com/posthtml/posthtml). Inspired by [cssnano](https://github.com/cssnano/cssnano).
|
|
6
6
|
|
|
7
|
-
##
|
|
8
|
-
[html-minifier-terser@6.0.2]: https://www.npmjs.com/package/html-minifier-terser
|
|
9
|
-
[htmlnano@2.0.0]: https://www.npmjs.com/package/htmlnano
|
|
7
|
+
## Benchmarks
|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
| [github.com](https://github.com/) | 232 | 203 | 173 |
|
|
15
|
-
| [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) | 81 | 76 | 75 |
|
|
16
|
-
| [npmjs.com](https://www.npmjs.com/features) | 43 | 40 | 38 |
|
|
17
|
-
| [tc39.es](https://tc39.es/ecma262/) | 6001 | 5465 | 5459 |
|
|
18
|
-
| **Avg. minify rate** | 0% | **9%** | **13%** |
|
|
9
|
+
[html-minifier-terser]: https://www.npmjs.com/package/html-minifier-terser/v/7.2.0
|
|
10
|
+
[html-minifier-next]: https://www.npmjs.com/package/html-minifier-next/v/1.1.5
|
|
11
|
+
[htmlnano]: https://www.npmjs.com/package/htmlnano/v/2.1.2
|
|
19
12
|
|
|
13
|
+
| Website | Source (KB) | [html-minifier-terser] | [html-minifier-next] | [htmlnano] |
|
|
14
|
+
| ----------------------------------------------------------- | ----------: | ---------------------: | -------------------: | ---------: |
|
|
15
|
+
| [stackoverflow.blog](https://stackoverflow.blog/) | 342 | 1.7% | 1.7% | 4.1% |
|
|
16
|
+
| [github.com](https://github.com/) | 541 | 3.7% | 3.7% | 18.1% |
|
|
17
|
+
| [en.wikipedia.org](https://en.wikipedia.org/wiki/Main_Page) | 218 | 4.6% | 4.6% | 5.0% |
|
|
18
|
+
| [npmjs.com](https://www.npmjs.com/package/eslint) | 459 | 0.5% | 0.5% | 0.9% |
|
|
19
|
+
| [tc39.es](https://tc39.es/ecma262/) | 7198 | 8.6% | 8.6% | 8.7% |
|
|
20
|
+
| [reddit.com](https://reddit.com/) | 539 | 6.1% | 6.1% | 6.9% |
|
|
21
|
+
| [apple.com](https://www.apple.com/) | 190 | 7.6% | 7.6% | 12.3% |
|
|
22
|
+
| [w3.org](https://www.w3.org/) | 48 | 19.0% | 19.0% | 23.2% |
|
|
23
|
+
| [weather.com](https://weather.com) | 1823 | 0.2% | 0.2% | 13.1% |
|
|
24
|
+
| **Avg. minify rate** | 0% | **5.8%** | **5.8%** | **10.3%** |
|
|
25
|
+
|
|
26
|
+
Latest benchmarks: https://github.com/maltsev/html-minifiers-benchmark (updated daily).
|
|
20
27
|
|
|
21
28
|
## Documentation
|
|
22
29
|
https://htmlnano.netlify.app
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm install htmlnano
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
const htmlnano = require('htmlnano');
|
|
40
|
+
const options = {
|
|
41
|
+
removeEmptyAttributes: false, // Disable the module "removeEmptyAttributes"
|
|
42
|
+
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
|
|
43
|
+
};
|
|
44
|
+
// posthtml, posthtml-render, and posthtml-parse options
|
|
45
|
+
const postHtmlOptions = {
|
|
46
|
+
sync: true, // https://github.com/posthtml/posthtml#usage
|
|
47
|
+
lowerCaseTags: true, // https://github.com/posthtml/posthtml-parser#options
|
|
48
|
+
quoteAllAttributes: false, // https://github.com/posthtml/posthtml-render#options
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
htmlnano
|
|
52
|
+
// "preset" arg might be skipped (see "Presets" section below for more info)
|
|
53
|
+
// "postHtmlOptions" arg might be skipped
|
|
54
|
+
.process(html, options, preset, postHtmlOptions)
|
|
55
|
+
.then(function (result) {
|
|
56
|
+
// result.html is minified
|
|
57
|
+
})
|
|
58
|
+
.catch(function (err) {
|
|
59
|
+
console.error(err);
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
More usage examples (PostHTML, Gulp, Webpack): https://htmlnano.netlify.app/next/usage
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import PostHTML from 'posthtml';
|
|
2
|
+
import { MinifyOptions } from 'terser';
|
|
3
|
+
import { Options } from 'cssnano';
|
|
4
|
+
import { Config } from 'svgo';
|
|
5
|
+
|
|
6
|
+
type PostHTMLTreeLike = [PostHTML.Node] & PostHTML.NodeAPI & {
|
|
7
|
+
options?: {
|
|
8
|
+
quoteAllAttributes?: boolean | undefined;
|
|
9
|
+
} | undefined;
|
|
10
|
+
render(): string;
|
|
11
|
+
render(node: PostHTML.Node | PostHTMLTreeLike, renderOptions?: any): string;
|
|
12
|
+
};
|
|
13
|
+
type MaybeArray<T> = T | Array<T>;
|
|
14
|
+
type PostHTMLNodeLike = PostHTML.Node | string;
|
|
15
|
+
interface HtmlnanoOptions {
|
|
16
|
+
skipConfigLoading?: boolean;
|
|
17
|
+
skipInternalWarnings?: boolean;
|
|
18
|
+
collapseAttributeWhitespace?: boolean;
|
|
19
|
+
collapseBooleanAttributes?: {
|
|
20
|
+
amphtml?: boolean;
|
|
21
|
+
};
|
|
22
|
+
collapseWhitespace?: 'conservative' | 'all' | 'aggressive';
|
|
23
|
+
custom?: MaybeArray<(tree: PostHTMLTreeLike, options?: any) => (PostHTML.Node | PostHTMLTreeLike)>;
|
|
24
|
+
deduplicateAttributeValues?: boolean;
|
|
25
|
+
minifyUrls?: URL | string | false;
|
|
26
|
+
mergeStyles?: boolean;
|
|
27
|
+
mergeScripts?: boolean;
|
|
28
|
+
minifyCss?: Options | boolean;
|
|
29
|
+
minifyConditionalComments?: boolean;
|
|
30
|
+
minifyJs?: MinifyOptions | boolean;
|
|
31
|
+
minifyJson?: boolean;
|
|
32
|
+
minifySvg?: Config | boolean;
|
|
33
|
+
normalizeAttributeValues?: boolean;
|
|
34
|
+
removeAttributeQuotes?: boolean;
|
|
35
|
+
removeComments?: boolean | 'safe' | 'all' | RegExp | ((comment: string) => boolean);
|
|
36
|
+
removeEmptyAttributes?: boolean;
|
|
37
|
+
removeRedundantAttributes?: boolean;
|
|
38
|
+
removeOptionalTags?: boolean;
|
|
39
|
+
removeUnusedCss?: boolean;
|
|
40
|
+
sortAttributes?: boolean | 'alphabetical' | 'frequency';
|
|
41
|
+
sortAttributesWithLists?: boolean | 'alphabetical' | 'frequency';
|
|
42
|
+
}
|
|
43
|
+
type HtmlnanoModuleAttrsHandler = (attrs: Record<string, string | boolean | void>, node: PostHTML.Node) => Record<string, string | boolean | void>;
|
|
44
|
+
type HtmlnanoModuleContentHandler = (content: Array<PostHTMLNodeLike>, node: PostHTML.Node) => MaybeArray<PostHTMLNodeLike>;
|
|
45
|
+
type HtmlnanoModuleNodeHandler = (node: PostHTMLNodeLike) => PostHTML.Node | string;
|
|
46
|
+
type OptionalOptions<T> = T extends boolean | string | Function | number | null | undefined ? T : T extends object ? Partial<T> : T;
|
|
47
|
+
type HtmlnanoModule<Options = any> = {
|
|
48
|
+
onAttrs?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleAttrsHandler;
|
|
49
|
+
onContent?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleContentHandler;
|
|
50
|
+
onNode?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleNodeHandler;
|
|
51
|
+
default?: (tree: PostHTMLTreeLike, options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => PostHTMLTreeLike | Promise<PostHTMLTreeLike>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
declare const attributesWithLists: Set<string>;
|
|
55
|
+
declare const mod: HtmlnanoModule;
|
|
56
|
+
|
|
57
|
+
export { attributesWithLists, mod as default };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import PostHTML from 'posthtml';
|
|
2
|
+
import { MinifyOptions } from 'terser';
|
|
3
|
+
import { Options } from 'cssnano';
|
|
4
|
+
import { Config } from 'svgo';
|
|
5
|
+
|
|
6
|
+
type PostHTMLTreeLike = [PostHTML.Node] & PostHTML.NodeAPI & {
|
|
7
|
+
options?: {
|
|
8
|
+
quoteAllAttributes?: boolean | undefined;
|
|
9
|
+
} | undefined;
|
|
10
|
+
render(): string;
|
|
11
|
+
render(node: PostHTML.Node | PostHTMLTreeLike, renderOptions?: any): string;
|
|
12
|
+
};
|
|
13
|
+
type MaybeArray<T> = T | Array<T>;
|
|
14
|
+
type PostHTMLNodeLike = PostHTML.Node | string;
|
|
15
|
+
interface HtmlnanoOptions {
|
|
16
|
+
skipConfigLoading?: boolean;
|
|
17
|
+
skipInternalWarnings?: boolean;
|
|
18
|
+
collapseAttributeWhitespace?: boolean;
|
|
19
|
+
collapseBooleanAttributes?: {
|
|
20
|
+
amphtml?: boolean;
|
|
21
|
+
};
|
|
22
|
+
collapseWhitespace?: 'conservative' | 'all' | 'aggressive';
|
|
23
|
+
custom?: MaybeArray<(tree: PostHTMLTreeLike, options?: any) => (PostHTML.Node | PostHTMLTreeLike)>;
|
|
24
|
+
deduplicateAttributeValues?: boolean;
|
|
25
|
+
minifyUrls?: URL | string | false;
|
|
26
|
+
mergeStyles?: boolean;
|
|
27
|
+
mergeScripts?: boolean;
|
|
28
|
+
minifyCss?: Options | boolean;
|
|
29
|
+
minifyConditionalComments?: boolean;
|
|
30
|
+
minifyJs?: MinifyOptions | boolean;
|
|
31
|
+
minifyJson?: boolean;
|
|
32
|
+
minifySvg?: Config | boolean;
|
|
33
|
+
normalizeAttributeValues?: boolean;
|
|
34
|
+
removeAttributeQuotes?: boolean;
|
|
35
|
+
removeComments?: boolean | 'safe' | 'all' | RegExp | ((comment: string) => boolean);
|
|
36
|
+
removeEmptyAttributes?: boolean;
|
|
37
|
+
removeRedundantAttributes?: boolean;
|
|
38
|
+
removeOptionalTags?: boolean;
|
|
39
|
+
removeUnusedCss?: boolean;
|
|
40
|
+
sortAttributes?: boolean | 'alphabetical' | 'frequency';
|
|
41
|
+
sortAttributesWithLists?: boolean | 'alphabetical' | 'frequency';
|
|
42
|
+
}
|
|
43
|
+
type HtmlnanoModuleAttrsHandler = (attrs: Record<string, string | boolean | void>, node: PostHTML.Node) => Record<string, string | boolean | void>;
|
|
44
|
+
type HtmlnanoModuleContentHandler = (content: Array<PostHTMLNodeLike>, node: PostHTML.Node) => MaybeArray<PostHTMLNodeLike>;
|
|
45
|
+
type HtmlnanoModuleNodeHandler = (node: PostHTMLNodeLike) => PostHTML.Node | string;
|
|
46
|
+
type OptionalOptions<T> = T extends boolean | string | Function | number | null | undefined ? T : T extends object ? Partial<T> : T;
|
|
47
|
+
type HtmlnanoModule<Options = any> = {
|
|
48
|
+
onAttrs?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleAttrsHandler;
|
|
49
|
+
onContent?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleContentHandler;
|
|
50
|
+
onNode?: (options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => HtmlnanoModuleNodeHandler;
|
|
51
|
+
default?: (tree: PostHTMLTreeLike, options: Partial<HtmlnanoOptions>, moduleOptions: OptionalOptions<Options>) => PostHTMLTreeLike | Promise<PostHTMLTreeLike>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
declare const attributesWithLists: Set<string>;
|
|
55
|
+
declare const mod: HtmlnanoModule;
|
|
56
|
+
|
|
57
|
+
export { attributesWithLists, mod as default };
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
+
|
|
3
|
+
var helpers_js = require('../helpers.js');
|
|
4
|
+
|
|
5
|
+
const attributesWithLists = new Set([
|
|
6
|
+
'class',
|
|
7
|
+
'dropzone',
|
|
8
|
+
'rel',
|
|
9
|
+
'ping',
|
|
10
|
+
'sandbox',
|
|
11
|
+
/**
|
|
12
|
+
* https://github.com/posthtml/htmlnano/issues/180
|
|
13
|
+
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-sizes
|
|
14
|
+
*
|
|
15
|
+
* "sizes" of <img> should not be modified, while "sizes" of <link> will only have one entry in most cases.
|
|
16
|
+
*/ // 'sizes', // link
|
|
17
|
+
'headers' // td, th
|
|
18
|
+
]);
|
|
19
|
+
/** empty set means the attribute is alwasy trimmable */ const attributesWithSingleValue = new Map([
|
|
20
|
+
[
|
|
21
|
+
'accept',
|
|
22
|
+
new Set([
|
|
23
|
+
'input'
|
|
24
|
+
])
|
|
25
|
+
],
|
|
26
|
+
[
|
|
27
|
+
'action',
|
|
28
|
+
new Set([
|
|
29
|
+
'form'
|
|
30
|
+
])
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
'accesskey',
|
|
34
|
+
new Set()
|
|
35
|
+
],
|
|
36
|
+
[
|
|
37
|
+
'accept-charset',
|
|
38
|
+
new Set([
|
|
39
|
+
'form'
|
|
40
|
+
])
|
|
41
|
+
],
|
|
42
|
+
[
|
|
43
|
+
'cite',
|
|
44
|
+
new Set([
|
|
45
|
+
'blockquote',
|
|
46
|
+
'del',
|
|
47
|
+
'ins',
|
|
48
|
+
'q'
|
|
49
|
+
])
|
|
50
|
+
],
|
|
51
|
+
[
|
|
52
|
+
'cols',
|
|
53
|
+
new Set([
|
|
54
|
+
'textarea'
|
|
55
|
+
])
|
|
56
|
+
],
|
|
57
|
+
[
|
|
58
|
+
'colspan',
|
|
59
|
+
new Set([
|
|
60
|
+
'td',
|
|
61
|
+
'th'
|
|
62
|
+
])
|
|
63
|
+
],
|
|
64
|
+
[
|
|
65
|
+
'data',
|
|
66
|
+
new Set([
|
|
67
|
+
'object'
|
|
68
|
+
])
|
|
69
|
+
],
|
|
70
|
+
[
|
|
71
|
+
'dropzone',
|
|
72
|
+
new Set()
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
'formaction',
|
|
76
|
+
new Set([
|
|
77
|
+
'button',
|
|
78
|
+
'input'
|
|
79
|
+
])
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
'height',
|
|
83
|
+
new Set([
|
|
84
|
+
'canvas',
|
|
85
|
+
'embed',
|
|
86
|
+
'iframe',
|
|
87
|
+
'img',
|
|
88
|
+
'input',
|
|
89
|
+
'object',
|
|
90
|
+
'video'
|
|
91
|
+
])
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
'high',
|
|
95
|
+
new Set([
|
|
96
|
+
'meter'
|
|
97
|
+
])
|
|
98
|
+
],
|
|
99
|
+
[
|
|
100
|
+
'href',
|
|
101
|
+
new Set([
|
|
102
|
+
'a',
|
|
103
|
+
'area',
|
|
104
|
+
'base',
|
|
105
|
+
'link'
|
|
106
|
+
])
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
'itemid',
|
|
110
|
+
new Set()
|
|
111
|
+
],
|
|
112
|
+
[
|
|
113
|
+
'low',
|
|
114
|
+
new Set([
|
|
115
|
+
'meter'
|
|
116
|
+
])
|
|
117
|
+
],
|
|
118
|
+
[
|
|
119
|
+
'manifest',
|
|
120
|
+
new Set([
|
|
121
|
+
'html'
|
|
122
|
+
])
|
|
123
|
+
],
|
|
124
|
+
[
|
|
125
|
+
'max',
|
|
126
|
+
new Set([
|
|
127
|
+
'meter',
|
|
128
|
+
'progress'
|
|
129
|
+
])
|
|
130
|
+
],
|
|
131
|
+
[
|
|
132
|
+
'maxlength',
|
|
133
|
+
new Set([
|
|
134
|
+
'input',
|
|
135
|
+
'textarea'
|
|
136
|
+
])
|
|
137
|
+
],
|
|
138
|
+
[
|
|
139
|
+
'media',
|
|
140
|
+
new Set([
|
|
141
|
+
'source'
|
|
142
|
+
])
|
|
143
|
+
],
|
|
144
|
+
[
|
|
145
|
+
'min',
|
|
146
|
+
new Set([
|
|
147
|
+
'meter'
|
|
148
|
+
])
|
|
149
|
+
],
|
|
150
|
+
[
|
|
151
|
+
'minlength',
|
|
152
|
+
new Set([
|
|
153
|
+
'input',
|
|
154
|
+
'textarea'
|
|
155
|
+
])
|
|
156
|
+
],
|
|
157
|
+
[
|
|
158
|
+
'optimum',
|
|
159
|
+
new Set([
|
|
160
|
+
'meter'
|
|
161
|
+
])
|
|
162
|
+
],
|
|
163
|
+
[
|
|
164
|
+
'ping',
|
|
165
|
+
new Set([
|
|
166
|
+
'a',
|
|
167
|
+
'area'
|
|
168
|
+
])
|
|
169
|
+
],
|
|
170
|
+
[
|
|
171
|
+
'poster',
|
|
172
|
+
new Set([
|
|
173
|
+
'video'
|
|
174
|
+
])
|
|
175
|
+
],
|
|
176
|
+
[
|
|
177
|
+
'profile',
|
|
178
|
+
new Set([
|
|
179
|
+
'head'
|
|
180
|
+
])
|
|
181
|
+
],
|
|
182
|
+
[
|
|
183
|
+
'rows',
|
|
184
|
+
new Set([
|
|
185
|
+
'textarea'
|
|
186
|
+
])
|
|
187
|
+
],
|
|
188
|
+
[
|
|
189
|
+
'rowspan',
|
|
190
|
+
new Set([
|
|
191
|
+
'td',
|
|
192
|
+
'th'
|
|
193
|
+
])
|
|
194
|
+
],
|
|
195
|
+
[
|
|
196
|
+
'size',
|
|
197
|
+
new Set([
|
|
198
|
+
'input',
|
|
199
|
+
'select'
|
|
200
|
+
])
|
|
201
|
+
],
|
|
202
|
+
[
|
|
203
|
+
'span',
|
|
204
|
+
new Set([
|
|
205
|
+
'col',
|
|
206
|
+
'colgroup'
|
|
207
|
+
])
|
|
208
|
+
],
|
|
209
|
+
[
|
|
210
|
+
'src',
|
|
211
|
+
new Set([
|
|
212
|
+
'audio',
|
|
213
|
+
'embed',
|
|
214
|
+
'iframe',
|
|
215
|
+
'img',
|
|
216
|
+
'input',
|
|
217
|
+
'script',
|
|
218
|
+
'source',
|
|
219
|
+
'track',
|
|
220
|
+
'video'
|
|
221
|
+
])
|
|
222
|
+
],
|
|
223
|
+
[
|
|
224
|
+
'start',
|
|
225
|
+
new Set([
|
|
226
|
+
'ol'
|
|
227
|
+
])
|
|
228
|
+
],
|
|
229
|
+
[
|
|
230
|
+
'step',
|
|
231
|
+
new Set([
|
|
232
|
+
'input'
|
|
233
|
+
])
|
|
234
|
+
],
|
|
235
|
+
[
|
|
236
|
+
'style',
|
|
237
|
+
new Set()
|
|
238
|
+
],
|
|
239
|
+
[
|
|
240
|
+
'tabindex',
|
|
241
|
+
new Set()
|
|
242
|
+
],
|
|
243
|
+
[
|
|
244
|
+
'usemap',
|
|
245
|
+
new Set([
|
|
246
|
+
'img',
|
|
247
|
+
'object'
|
|
248
|
+
])
|
|
249
|
+
],
|
|
250
|
+
[
|
|
251
|
+
'value',
|
|
252
|
+
new Set([
|
|
253
|
+
'li',
|
|
254
|
+
'meter',
|
|
255
|
+
'progress'
|
|
256
|
+
])
|
|
257
|
+
],
|
|
258
|
+
[
|
|
259
|
+
'width',
|
|
260
|
+
new Set([
|
|
261
|
+
'canvas',
|
|
262
|
+
'embed',
|
|
263
|
+
'iframe',
|
|
264
|
+
'img',
|
|
265
|
+
'input',
|
|
266
|
+
'object',
|
|
267
|
+
'video'
|
|
268
|
+
])
|
|
269
|
+
]
|
|
270
|
+
]);
|
|
271
|
+
/** Collapse whitespaces inside list-like attributes (e.g. class, rel) */ const mod = {
|
|
272
|
+
onAttrs () {
|
|
273
|
+
return (attrs, node)=>{
|
|
274
|
+
const newAttrs = attrs;
|
|
275
|
+
Object.entries(attrs).forEach(([attrName, attrValue])=>{
|
|
276
|
+
if (typeof attrValue !== 'string') return;
|
|
277
|
+
if (attributesWithLists.has(attrName)) {
|
|
278
|
+
newAttrs[attrName] = attrValue.replace(/\s+/g, ' ').trim();
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (helpers_js.isEventHandler(attrName)) {
|
|
282
|
+
newAttrs[attrName] = attrValue.trim();
|
|
283
|
+
} else if (node.tag && attributesWithSingleValue.has(attrName)) {
|
|
284
|
+
const tagSet = attributesWithSingleValue.get(attrName);
|
|
285
|
+
if (tagSet.size === 0 || tagSet.has(node.tag)) {
|
|
286
|
+
newAttrs[attrName] = attrValue.trim();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
return newAttrs;
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
exports.attributesWithLists = attributesWithLists;
|
|
296
|
+
exports.default = mod;
|