htmlnano 2.0.3 → 2.1.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 (81) hide show
  1. package/.eslintignore +3 -2
  2. package/CHANGELOG.md +43 -3
  3. package/docs/docs/040-presets.md +4 -4
  4. package/docs/docs/050-modules.md +1 -1
  5. package/docs/docs/060-contribute.md +1 -1
  6. package/docs/docusaurus.config.js +5 -0
  7. package/docs/package-lock.json +7592 -11615
  8. package/docs/package.json +4 -3
  9. package/docs/versioned_docs/version-1.1.1/040-presets.md +4 -4
  10. package/docs/versioned_docs/version-1.1.1/050-modules.md +1 -2
  11. package/docs/versioned_docs/version-1.1.1/060-contribute.md +1 -1
  12. package/docs/versioned_docs/version-2.0.0/040-presets.md +4 -4
  13. package/docs/versioned_docs/version-2.0.0/050-modules.md +1 -1
  14. package/docs/versioned_docs/version-2.0.0/060-contribute.md +1 -1
  15. package/index.cjs +11 -0
  16. package/index.d.cts +3 -0
  17. package/index.d.mts +3 -0
  18. package/index.d.ts +3 -3
  19. package/index.mjs +2 -0
  20. package/lib/helpers.cjs +78 -0
  21. package/lib/helpers.mjs +52 -0
  22. package/lib/htmlnano.cjs +200 -0
  23. package/lib/htmlnano.mjs +196 -0
  24. package/lib/modules/{collapseAttributeWhitespace.js → collapseAttributeWhitespace.cjs} +2 -3
  25. package/lib/modules/collapseAttributeWhitespace.mjs +104 -0
  26. package/lib/modules/collapseBooleanAttributes.mjs +175 -0
  27. package/lib/modules/{collapseWhitespace.js → collapseWhitespace.cjs} +3 -2
  28. package/lib/modules/collapseWhitespace.mjs +132 -0
  29. package/lib/modules/custom.mjs +16 -0
  30. package/lib/modules/{deduplicateAttributeValues.js → deduplicateAttributeValues.cjs} +1 -1
  31. package/lib/modules/deduplicateAttributeValues.mjs +40 -0
  32. package/lib/modules/example.cjs +85 -0
  33. package/lib/modules/example.mjs +75 -0
  34. package/lib/modules/{mergeScripts.js → mergeScripts.cjs} +3 -1
  35. package/lib/modules/mergeScripts.mjs +56 -0
  36. package/lib/modules/{mergeStyles.js → mergeStyles.cjs} +4 -2
  37. package/lib/modules/mergeStyles.mjs +36 -0
  38. package/lib/modules/{minifyConditionalComments.js → minifyConditionalComments.cjs} +2 -2
  39. package/lib/modules/minifyConditionalComments.mjs +49 -0
  40. package/lib/modules/{minifyCss.js → minifyCss.cjs} +12 -8
  41. package/lib/modules/minifyCss.mjs +88 -0
  42. package/lib/modules/{minifyJs.js → minifyJs.cjs} +25 -10
  43. package/lib/modules/minifyJs.mjs +121 -0
  44. package/lib/modules/{minifyJson.js → minifyJson.cjs} +4 -0
  45. package/lib/modules/minifyJson.mjs +21 -0
  46. package/lib/modules/minifySvg.cjs +37 -0
  47. package/lib/modules/minifySvg.mjs +30 -0
  48. package/lib/modules/{minifyUrls.js → minifyUrls.cjs} +7 -8
  49. package/lib/modules/minifyUrls.mjs +229 -0
  50. package/lib/modules/{normalizeAttributeValues.js → normalizeAttributeValues.cjs} +0 -5
  51. package/lib/modules/normalizeAttributeValues.mjs +140 -0
  52. package/lib/modules/removeAttributeQuotes.mjs +12 -0
  53. package/lib/modules/{removeComments.js → removeComments.cjs} +1 -1
  54. package/lib/modules/removeComments.mjs +92 -0
  55. package/lib/modules/{removeEmptyAttributes.js → removeEmptyAttributes.cjs} +1 -1
  56. package/lib/modules/removeEmptyAttributes.mjs +121 -0
  57. package/lib/modules/{removeOptionalTags.js → removeOptionalTags.cjs} +1 -1
  58. package/lib/modules/removeOptionalTags.mjs +225 -0
  59. package/lib/modules/{removeRedundantAttributes.js → removeRedundantAttributes.cjs} +1 -2
  60. package/lib/modules/removeRedundantAttributes.mjs +141 -0
  61. package/lib/modules/{removeUnusedCss.js → removeUnusedCss.cjs} +12 -13
  62. package/lib/modules/removeUnusedCss.mjs +122 -0
  63. package/lib/modules/sortAttributes.mjs +121 -0
  64. package/lib/modules/{sortAttributesWithLists.js → sortAttributesWithLists.cjs} +1 -1
  65. package/lib/modules/sortAttributesWithLists.mjs +135 -0
  66. package/lib/presets/{ampSafe.js → ampSafe.cjs} +4 -9
  67. package/lib/presets/ampSafe.mjs +11 -0
  68. package/lib/presets/{max.js → max.cjs} +4 -9
  69. package/lib/presets/max.mjs +20 -0
  70. package/lib/presets/{safe.js → safe.cjs} +2 -3
  71. package/lib/presets/safe.mjs +65 -0
  72. package/package.json +40 -12
  73. package/test.js +48 -0
  74. package/index.js +0 -1
  75. package/lib/helpers.js +0 -52
  76. package/lib/htmlnano.js +0 -147
  77. package/lib/modules/minifySvg.js +0 -38
  78. /package/lib/modules/{collapseBooleanAttributes.js → collapseBooleanAttributes.cjs} +0 -0
  79. /package/lib/modules/{custom.js → custom.cjs} +0 -0
  80. /package/lib/modules/{removeAttributeQuotes.js → removeAttributeQuotes.cjs} +0 -0
  81. /package/lib/modules/{sortAttributes.js → sortAttributes.cjs} +0 -0
@@ -0,0 +1,121 @@
1
+ import { isEventHandler, optionalImport } from '../helpers.mjs';
2
+ import { redundantScriptTypes } from './removeRedundantAttributes.mjs';
3
+
4
+ /** Minify JS with Terser */
5
+ export default async function minifyJs (tree, options, terserOptions) {
6
+ const terser = await optionalImport('terser');
7
+
8
+ if (!terser) return tree;
9
+
10
+ let promises = [];
11
+ tree.walk(node => {
12
+ const nodeAttrs = node.attrs || {};
13
+
14
+ /**
15
+ * Skip SRI
16
+ *
17
+ * If the input <script /> has an SRI attribute, it means that the original <script /> could be trusted,
18
+ * and should not be altered anymore.
19
+ *
20
+ * htmlnano is exactly an MITM that SRI is designed to protect from. If htmlnano or its dependencies get
21
+ * compromised and introduces malicious code, then it is up to the original SRI to protect the end user.
22
+ *
23
+ * So htmlnano will simply skip <script /> that has SRI.
24
+ * If developers do trust htmlnano, they should generate SRI after htmlnano modify the <script />.
25
+ */
26
+ if ('integrity' in nodeAttrs) {
27
+ return node;
28
+ }
29
+
30
+ if (node.tag && node.tag === 'script') {
31
+ const mimeType = nodeAttrs.type || 'text/javascript';
32
+ if (redundantScriptTypes.has(mimeType) || mimeType === 'module') {
33
+ promises.push(processScriptNode(node, terserOptions, terser));
34
+ }
35
+ }
36
+
37
+ if (node.attrs) {
38
+ promises = promises.concat(processNodeWithOnAttrs(node, terserOptions, terser));
39
+ }
40
+
41
+ return node;
42
+ });
43
+
44
+ return Promise.all(promises).then(() => tree);
45
+ }
46
+
47
+
48
+ function stripCdata (js) {
49
+ const leftStrippedJs = js.replace(/\/\/\s*<!\[CDATA\[/, '').replace(/\/\*\s*<!\[CDATA\[\s*\*\//, '');
50
+ if (leftStrippedJs === js) {
51
+ return js;
52
+ }
53
+
54
+ const strippedJs = leftStrippedJs.replace(/\/\/\s*\]\]>/, '').replace(/\/\*\s*\]\]>\s*\*\//, '');
55
+ return leftStrippedJs === strippedJs ? js : strippedJs;
56
+ }
57
+
58
+
59
+ function processScriptNode (scriptNode, terserOptions, terser) {
60
+ let js = (scriptNode.content || []).join('').trim();
61
+ if (!js) {
62
+ return scriptNode;
63
+ }
64
+
65
+ // Improve performance by avoiding calling stripCdata again and again
66
+ let isCdataWrapped = false;
67
+ if (js.includes('CDATA')) {
68
+ const strippedJs = stripCdata(js);
69
+ isCdataWrapped = js !== strippedJs;
70
+ js = strippedJs;
71
+ }
72
+
73
+ return terser
74
+ .minify(js, terserOptions)
75
+ .then(result => {
76
+ if (result.error) {
77
+ throw new Error(result.error);
78
+ }
79
+ if (result.code === undefined) {
80
+ return;
81
+ }
82
+
83
+ let content = result.code;
84
+ if (isCdataWrapped) {
85
+ content = '/*<![CDATA[*/' + content + '/*]]>*/';
86
+ }
87
+
88
+ scriptNode.content = [content];
89
+ });
90
+ }
91
+
92
+
93
+ function processNodeWithOnAttrs (node, terserOptions, terser) {
94
+ const jsWrapperStart = 'a=function(){';
95
+ const jsWrapperEnd = '};a();';
96
+
97
+ const promises = [];
98
+ for (const attrName of Object.keys(node.attrs || {})) {
99
+ if (!isEventHandler(attrName)) {
100
+ continue;
101
+ }
102
+
103
+ // For example onclick="return false" is valid,
104
+ // but "return false;" is invalid (error: 'return' outside of function)
105
+ // Therefore the attribute's code should be wrapped inside function:
106
+ // "function _(){return false;}"
107
+ let wrappedJs = jsWrapperStart + node.attrs[attrName] + jsWrapperEnd;
108
+ let promise = terser
109
+ .minify(wrappedJs, terserOptions)
110
+ .then(({ code }) => {
111
+ let minifiedJs = code.substring(
112
+ jsWrapperStart.length,
113
+ code.length - jsWrapperEnd.length
114
+ );
115
+ node.attrs[attrName] = minifiedJs;
116
+ });
117
+ promises.push(promise);
118
+ }
119
+
120
+ return promises;
121
+ }
@@ -7,6 +7,10 @@ exports.onContent = onContent;
7
7
  const rNodeAttrsTypeJson = /(\/|\+)json/;
8
8
  function onContent() {
9
9
  return (content, node) => {
10
+ // Skip SRI, reasons are documented in "minifyJs" module
11
+ if (node.attrs && 'integrity' in node.attrs) {
12
+ return content;
13
+ }
10
14
  if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) {
11
15
  try {
12
16
  // cast minified JSON to an array
@@ -0,0 +1,21 @@
1
+ const rNodeAttrsTypeJson = /(\/|\+)json/;
2
+
3
+ export function onContent() {
4
+ return (content, node) => {
5
+ // Skip SRI, reasons are documented in "minifyJs" module
6
+ if (node.attrs && 'integrity' in node.attrs) {
7
+ return content;
8
+ }
9
+
10
+ if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) {
11
+ try {
12
+ // cast minified JSON to an array
13
+ return [JSON.stringify(JSON.parse((content || []).join('')))];
14
+ } catch (error) {
15
+ // Invalid JSON
16
+ }
17
+ }
18
+
19
+ return content;
20
+ };
21
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = minifySvg;
7
+ var _helpers = require("../helpers.cjs");
8
+ /** Minify SVG with SVGO */
9
+ async function minifySvg(tree, options, svgoOptions = {}) {
10
+ const svgo = await (0, _helpers.optionalImport)('svgo');
11
+ if (!svgo) return tree;
12
+ tree.match({
13
+ tag: 'svg'
14
+ }, node => {
15
+ let svgStr = tree.render(node, {
16
+ closingSingleTag: 'slash',
17
+ quoteAllAttributes: true
18
+ });
19
+ try {
20
+ const result = svgo.optimize(svgStr, svgoOptions);
21
+ node.tag = false;
22
+ node.attrs = {};
23
+ // result.data is a string, we need to cast it to an array
24
+ node.content = [result.data];
25
+ return node;
26
+ } catch (error) {
27
+ console.error('htmlnano fails to minify the svg:');
28
+ console.error(error);
29
+ if (error.name === 'SvgoParserError') {
30
+ console.error(error.toString());
31
+ }
32
+ // We return the node as-is
33
+ return node;
34
+ }
35
+ });
36
+ return tree;
37
+ }
@@ -0,0 +1,30 @@
1
+ import { optionalImport } from '../helpers.mjs';
2
+
3
+ /** Minify SVG with SVGO */
4
+ export default async function minifySvg(tree, options, svgoOptions = {}) {
5
+ const svgo = await optionalImport('svgo');
6
+
7
+ if (!svgo) return tree;
8
+
9
+ tree.match({tag: 'svg'}, node => {
10
+ let svgStr = tree.render(node, { closingSingleTag: 'slash', quoteAllAttributes: true });
11
+ try {
12
+ const result = svgo.optimize(svgStr, svgoOptions);
13
+ node.tag = false;
14
+ node.attrs = {};
15
+ // result.data is a string, we need to cast it to an array
16
+ node.content = [result.data];
17
+ return node;
18
+ } catch (error) {
19
+ console.error('htmlnano fails to minify the svg:');
20
+ console.error(error);
21
+ if (error.name === 'SvgoParserError') {
22
+ console.error(error.toString());
23
+ }
24
+ // We return the node as-is
25
+ return node;
26
+ }
27
+ });
28
+
29
+ return tree;
30
+ }
@@ -4,11 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = minifyUrls;
7
- var _helpers = require("../helpers");
8
- const RelateUrl = (0, _helpers.optionalRequire)('relateurl');
9
- const srcset = (0, _helpers.optionalRequire)('srcset');
10
- const terser = (0, _helpers.optionalRequire)('terser');
11
-
7
+ var _helpers = require("../helpers.cjs");
12
8
  // Adopts from https://github.com/kangax/html-minifier/blob/51ce10f4daedb1de483ffbcccecc41be1c873da2/src/htmlminifier.js#L209-L221
13
9
  const tagsHaveUriValuesForAttributes = new Set(['a', 'area', 'link', 'base', 'object', 'blockquote', 'q', 'del', 'ins', 'form', 'input', 'head', 'audio', 'embed', 'iframe', 'img', 'script', 'track', 'video']);
14
10
  const tagsHasHrefAttributes = new Set(['a', 'area', 'link', 'base']);
@@ -53,7 +49,10 @@ let relateUrlInstance;
53
49
  let STORED_URL_BASE;
54
50
 
55
51
  /** Convert absolute url into relative url */
56
- function minifyUrls(tree, options, moduleOptions) {
52
+ async function minifyUrls(tree, options, moduleOptions) {
53
+ const RelateUrl = await (0, _helpers.optionalImport)('relateurl');
54
+ const srcset = await (0, _helpers.optionalImport)('srcset');
55
+ const terser = await (0, _helpers.optionalImport)('terser');
57
56
  let promises = [];
58
57
  const urlBase = processModuleOptions(moduleOptions);
59
58
 
@@ -84,7 +83,7 @@ function minifyUrls(tree, options, moduleOptions) {
84
83
  const attrNameLower = attrName.toLowerCase();
85
84
  if (isUriTypeAttribute(node.tag, attrNameLower)) {
86
85
  if (isJavaScriptUrl(attrValue)) {
87
- promises.push(minifyJavaScriptUrl(node, attrName));
86
+ promises.push(minifyJavaScriptUrl(node, attrName, terser));
88
87
  } else {
89
88
  if (relateUrlInstance) {
90
89
  // FIXME!
@@ -125,7 +124,7 @@ function isJavaScriptUrl(url) {
125
124
  }
126
125
  const jsWrapperStart = 'function a(){';
127
126
  const jsWrapperEnd = '}a();';
128
- function minifyJavaScriptUrl(node, attrName) {
127
+ function minifyJavaScriptUrl(node, attrName, terser) {
129
128
  if (!terser) return Promise.resolve();
130
129
  let result = node.attrs[attrName];
131
130
  if (result) {
@@ -0,0 +1,229 @@
1
+ import { optionalImport } from '../helpers.mjs';
2
+
3
+ // Adopts from https://github.com/kangax/html-minifier/blob/51ce10f4daedb1de483ffbcccecc41be1c873da2/src/htmlminifier.js#L209-L221
4
+ const tagsHaveUriValuesForAttributes = new Set([
5
+ 'a',
6
+ 'area',
7
+ 'link',
8
+ 'base',
9
+ 'object',
10
+ 'blockquote',
11
+ 'q',
12
+ 'del',
13
+ 'ins',
14
+ 'form',
15
+ 'input',
16
+ 'head',
17
+ 'audio',
18
+ 'embed',
19
+ 'iframe',
20
+ 'img',
21
+ 'script',
22
+ 'track',
23
+ 'video',
24
+ ]);
25
+
26
+ const tagsHasHrefAttributes = new Set([
27
+ 'a',
28
+ 'area',
29
+ 'link',
30
+ 'base'
31
+ ]);
32
+
33
+ const attributesOfImgTagHasUriValues = new Set([
34
+ 'src',
35
+ 'longdesc',
36
+ 'usemap'
37
+ ]);
38
+
39
+ const attributesOfObjectTagHasUriValues = new Set([
40
+ 'classid',
41
+ 'codebase',
42
+ 'data',
43
+ 'usemap'
44
+ ]);
45
+
46
+ const tagsHasCiteAttributes = new Set([
47
+ 'blockquote',
48
+ 'q',
49
+ 'ins',
50
+ 'del'
51
+ ]);
52
+
53
+ const tagsHasSrcAttributes = new Set([
54
+ 'audio',
55
+ 'embed',
56
+ 'iframe',
57
+ 'img',
58
+ 'input',
59
+ 'script',
60
+ 'track',
61
+ 'video',
62
+ /**
63
+ * https://html.spec.whatwg.org/#attr-source-src
64
+ *
65
+ * Although most of browsers recommend not to use "src" in <source>,
66
+ * but technically it does comply with HTML Standard.
67
+ */
68
+ 'source'
69
+ ]);
70
+
71
+ const isUriTypeAttribute = (tag, attr) => {
72
+ return (
73
+ tagsHasHrefAttributes.has(tag) && attr === 'href' ||
74
+ tag === 'img' && attributesOfImgTagHasUriValues.has(attr) ||
75
+ tag === 'object' && attributesOfObjectTagHasUriValues.has(attr) ||
76
+ tagsHasCiteAttributes.has(tag) && attr === 'cite' ||
77
+ tag === 'form' && attr === 'action' ||
78
+ tag === 'input' && attr === 'usemap' ||
79
+ tag === 'head' && attr === 'profile' ||
80
+ tag === 'script' && attr === 'for' ||
81
+ tagsHasSrcAttributes.has(tag) && attr === 'src'
82
+ );
83
+ };
84
+
85
+ const isSrcsetAttribute = (tag, attr) => {
86
+ return (
87
+ tag === 'source' && attr === 'srcset' ||
88
+ tag === 'img' && attr === 'srcset' ||
89
+ tag === 'link' && attr === 'imagesrcset'
90
+ );
91
+ };
92
+
93
+ const processModuleOptions = options => {
94
+ // FIXME!
95
+ // relateurl@1.0.0-alpha only supports URL while stable version (0.2.7) only supports string
96
+ // should convert input into URL instance after relateurl@1 is stable
97
+ if (typeof options === 'string') return options;
98
+ if (options instanceof URL) return options.toString();
99
+
100
+ return false;
101
+ };
102
+
103
+ const isLinkRelCanonical = ({ tag, attrs }) => {
104
+ // Return false early for non-"link" tag
105
+ if (tag !== 'link') return false;
106
+
107
+ for (const [attrName, attrValue] of Object.entries(attrs)) {
108
+ if (attrName.toLowerCase() === 'rel' && attrValue === 'canonical') return true;
109
+ }
110
+
111
+ return false;
112
+ };
113
+
114
+ const JAVASCRIPT_URL_PROTOCOL = 'javascript:';
115
+
116
+ let relateUrlInstance;
117
+ let STORED_URL_BASE;
118
+
119
+ /** Convert absolute url into relative url */
120
+ export default async function minifyUrls(tree, options, moduleOptions) {
121
+ const RelateUrl = await optionalImport('relateurl');
122
+ const srcset = await optionalImport('srcset');
123
+ const terser = await optionalImport('terser');
124
+
125
+ let promises = [];
126
+
127
+ const urlBase = processModuleOptions(moduleOptions);
128
+
129
+ // Invalid configuration, return tree directly
130
+ if (!urlBase) return tree;
131
+
132
+ /** Bring up a reusable RelateUrl instances (only once)
133
+ *
134
+ * STORED_URL_BASE is used to invalidate RelateUrl instances,
135
+ * avoiding require.cache acrossing multiple htmlnano instance with different configuration,
136
+ * e.g. unit tests cases.
137
+ */
138
+ if (!relateUrlInstance || STORED_URL_BASE !== urlBase) {
139
+ if (RelateUrl) {
140
+ relateUrlInstance = new RelateUrl(urlBase);
141
+ }
142
+ STORED_URL_BASE = urlBase;
143
+ }
144
+
145
+ tree.walk(node => {
146
+ if (!node.attrs) return node;
147
+
148
+ if (!node.tag) return node;
149
+
150
+ if (!tagsHaveUriValuesForAttributes.has(node.tag)) return node;
151
+
152
+ // Prevent link[rel=canonical] being processed
153
+ // Can't be excluded by isUriTypeAttribute()
154
+ if (isLinkRelCanonical(node)) return node;
155
+
156
+ for (const [attrName, attrValue] of Object.entries(node.attrs)) {
157
+ const attrNameLower = attrName.toLowerCase();
158
+
159
+ if (isUriTypeAttribute(node.tag, attrNameLower)) {
160
+ if (isJavaScriptUrl(attrValue)) {
161
+ promises.push(minifyJavaScriptUrl(node, attrName, terser));
162
+ } else {
163
+ if (relateUrlInstance) {
164
+ // FIXME!
165
+ // relateurl@1.0.0-alpha only supports URL while stable version (0.2.7) only supports string
166
+ // the WHATWG URL API is very strict while attrValue might not be a valid URL
167
+ // new URL should be used, and relateUrl#relate should be wrapped in try...catch after relateurl@1 is stable
168
+ node.attrs[attrName] = relateUrlInstance.relate(attrValue);
169
+ }
170
+ }
171
+
172
+ continue;
173
+ }
174
+
175
+ if (isSrcsetAttribute(node.tag, attrNameLower)) {
176
+ if (srcset) {
177
+ try {
178
+ const parsedSrcset = srcset.parse(attrValue, { strict: true });
179
+
180
+ node.attrs[attrName] = srcset.stringify(parsedSrcset.map(srcset => {
181
+ if (relateUrlInstance) {
182
+ srcset.url = relateUrlInstance.relate(srcset.url);
183
+ }
184
+
185
+ return srcset;
186
+ }));
187
+ } catch (e) {
188
+ // srcset will throw an Error for invalid srcset.
189
+ }
190
+ }
191
+
192
+ continue;
193
+ }
194
+ }
195
+
196
+ return node;
197
+ });
198
+
199
+ if (promises.length > 0) return Promise.all(promises).then(() => tree);
200
+ return Promise.resolve(tree);
201
+ }
202
+
203
+ function isJavaScriptUrl(url) {
204
+ return typeof url === 'string' && url.toLowerCase().startsWith(JAVASCRIPT_URL_PROTOCOL);
205
+ }
206
+
207
+ const jsWrapperStart = 'function a(){';
208
+ const jsWrapperEnd = '}a();';
209
+
210
+ function minifyJavaScriptUrl(node, attrName, terser) {
211
+ if (!terser) return Promise.resolve();
212
+
213
+ let result = node.attrs[attrName];
214
+ if (result) {
215
+ result = jsWrapperStart + result.slice(JAVASCRIPT_URL_PROTOCOL.length) + jsWrapperEnd;
216
+
217
+ return terser
218
+ .minify(result, {}) // Default Option is good enough
219
+ .then(({ code }) => {
220
+ const minifiedJs = code.substring(
221
+ jsWrapperStart.length,
222
+ code.length - jsWrapperEnd.length
223
+ );
224
+ node.attrs[attrName] = JAVASCRIPT_URL_PROTOCOL + minifiedJs;
225
+ });
226
+ }
227
+
228
+ return Promise.resolve();
229
+ }
@@ -63,11 +63,6 @@ const invalidValueDefault = {
63
63
  default: 'metadata',
64
64
  valid: ['subtitles', 'captions', 'descriptions', 'chapters', 'metadata']
65
65
  },
66
- autocomplete: {
67
- tag: null,
68
- default: 'on',
69
- valid: ['on', 'off']
70
- },
71
66
  type: {
72
67
  tag: ['button'],
73
68
  default: 'submit',
@@ -0,0 +1,140 @@
1
+ const caseInsensitiveAttributes = {
2
+ autocomplete: ['form'],
3
+ charset: ['meta', 'script'],
4
+ contenteditable: null,
5
+ crossorigin: ['audio', 'img', 'link', 'script', 'video'],
6
+ dir: null,
7
+ draggable: null,
8
+ dropzone: null,
9
+ formmethod: ['button', 'input'],
10
+ inputmode: ['input', 'textarea'],
11
+ kind: ['track'],
12
+ method: ['form'],
13
+ preload: ['audio', 'video'],
14
+ referrerpolicy: null,
15
+ sandbox: ['iframe'],
16
+ spellcheck: null,
17
+ scope: ['th'],
18
+ shape: ['area'],
19
+ sizes: ['link'],
20
+ step: ['input'],
21
+ translate: null,
22
+ type: [
23
+ 'a',
24
+ 'link',
25
+ 'button',
26
+ 'embed',
27
+ 'object',
28
+ 'script',
29
+ 'source',
30
+ 'style',
31
+ 'input',
32
+ 'menu',
33
+ 'menuitem'
34
+ ],
35
+ wrap: ['textarea']
36
+ };
37
+
38
+ // https://html.spec.whatwg.org/#invalid-value-default
39
+ /** @typedef { [key: string]: { tag: null | string[], default: string, valid: string[] } } */
40
+ const invalidValueDefault = {
41
+ crossorigin: {
42
+ tag: null,
43
+ default: 'anonymous',
44
+ valid: ['', 'anonymous', 'use-credentials']
45
+ },
46
+ // https://html.spec.whatwg.org/#referrer-policy-attributes
47
+ // The attribute's invalid value default and missing value default are both the empty string state.
48
+ referrerpolicy: {
49
+ tag: null,
50
+ default: '',
51
+ valid: ['', 'url', 'origin', 'no-referrer', 'no-referrer-when-downgrade', 'same-origin', 'origin-when-cross-origin', 'strict-origin-when-cross-origin', 'unsafe-url']
52
+ },
53
+ // https://html.spec.whatwg.org/#lazy-loading-attributes
54
+ loading: {
55
+ tag: ['img', 'iframe'],
56
+ default: 'eager',
57
+ valid: ['lazy', 'eager']
58
+ },
59
+ // https://html.spec.whatwg.org/#the-img-element
60
+ // https://html.spec.whatwg.org/#image-decoding-hint
61
+ decoding: {
62
+ tag: ['img'],
63
+ default: 'auto',
64
+ valid: ['auto', 'sync', 'async']
65
+ },
66
+ // https://html.spec.whatwg.org/#the-track-element
67
+ kind: {
68
+ tag: ['track'],
69
+ default: 'metadata',
70
+ valid: ['subtitles', 'captions', 'descriptions', 'chapters', 'metadata']
71
+ },
72
+ type: {
73
+ tag: ['button'],
74
+ default: 'submit',
75
+ valid: ['submit', 'reset', 'button']
76
+ },
77
+ wrap: {
78
+ tag: ['textarea'],
79
+ default: 'soft',
80
+ valid: ['soft', 'hard']
81
+ },
82
+ // https://html.spec.whatwg.org/#the-hidden-attribute
83
+ hidden: {
84
+ tag: null,
85
+ default: 'hidden',
86
+ valid: ['hidden', 'until-found']
87
+ },
88
+ // https://html.spec.whatwg.org/#autocapitalization
89
+ autocapitalize: {
90
+ tag: null,
91
+ default: 'sentences',
92
+ valid: ['none', 'off', 'on', 'sentences', 'words', 'characters']
93
+ },
94
+ // https://html.spec.whatwg.org/#the-marquee-element
95
+ behavior: {
96
+ tag: ['marquee'],
97
+ default: 'scroll',
98
+ valid: ['scroll', 'slide', 'alternate']
99
+ },
100
+ direction: {
101
+ tag: ['marquee'],
102
+ default: 'left',
103
+ valid: ['left', 'right', 'up', 'down']
104
+ }
105
+ };
106
+
107
+ export function onAttrs() {
108
+ return (attrs, node) => {
109
+ const newAttrs = attrs;
110
+
111
+ Object.entries(attrs).forEach(([attrName, attrValue]) => {
112
+ let newAttrValue = attrValue;
113
+
114
+ if (
115
+ Object.hasOwnProperty.call(caseInsensitiveAttributes, attrName)
116
+ && (
117
+ caseInsensitiveAttributes[attrName] === null
118
+ || caseInsensitiveAttributes[attrName].includes(node.tag)
119
+ )
120
+ ) {
121
+ newAttrValue = typeof attrValue.toLowerCase === 'function' ? attrValue.toLowerCase() : attrValue;
122
+ }
123
+
124
+ if (
125
+ Object.hasOwnProperty.call(invalidValueDefault, attrName)
126
+ ) {
127
+ const meta = invalidValueDefault[attrName];
128
+ if (meta.tag === null || (node && node.tag && meta.tag.includes(node.tag))) {
129
+ if (!meta.valid.includes(newAttrValue)) {
130
+ newAttrValue = meta.default;
131
+ }
132
+ }
133
+ }
134
+
135
+ newAttrs[attrName] = newAttrValue;
136
+ });
137
+
138
+ return newAttrs;
139
+ };
140
+ }
@@ -0,0 +1,12 @@
1
+ // Specification: https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
2
+ // See also: https://github.com/posthtml/posthtml-render/pull/30
3
+ // See also: https://github.com/posthtml/htmlnano/issues/6#issuecomment-707105334
4
+
5
+ /** Disable quoteAllAttributes while not overriding the configuration */
6
+ export default function removeAttributeQuotes(tree) {
7
+ if (tree.options && typeof tree.options.quoteAllAttributes === 'undefined') {
8
+ tree.options.quoteAllAttributes = false;
9
+ }
10
+
11
+ return tree;
12
+ }
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.onContent = onContent;
7
7
  exports.onNode = onNode;
8
- var _helpers = require("../helpers");
8
+ var _helpers = require("../helpers.cjs");
9
9
  const MATCH_EXCERPT_REGEXP = /<!-- ?more ?-->/i;
10
10
 
11
11
  /** Removes HTML comments */