htmlnano 2.0.1 → 2.0.2

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/CHANGELOG.md CHANGED
@@ -2,7 +2,15 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [2.0.2] - 2022-04-06
6
+
7
+ ### Fixed
8
+ - JSON-LD crash [#182]
9
+
10
+
5
11
  ## [2.0.1] - 2022-04-04
12
+ **This version contains a critical bug [#182].**
13
+ **Don't use it.**
6
14
 
7
15
  ### Changed
8
16
  - Speed improvements [#127]
@@ -239,6 +247,7 @@ Otherwise, you have to adapt the config according to the new [PurgeCSS@3](https:
239
247
  - Remove attributes that contains only white spaces.
240
248
 
241
249
 
250
+ [2.0.2]: https://github.com/posthtml/htmlnano/compare/2.0.1...2.0.2
242
251
  [2.0.1]: https://github.com/posthtml/htmlnano/compare/2.0.0...2.0.1
243
252
  [2.0.0]: https://github.com/posthtml/htmlnano/compare/1.1.1...2.0.0
244
253
  [1.1.1]: https://github.com/posthtml/htmlnano/compare/1.1.0...1.1.1
@@ -266,6 +275,7 @@ Otherwise, you have to adapt the config according to the new [PurgeCSS@3](https:
266
275
  [0.1.2]: https://github.com/posthtml/htmlnano/compare/0.1.1...0.1.2
267
276
  [0.1.1]: https://github.com/posthtml/htmlnano/compare/0.1.0...0.1.1
268
277
 
278
+ [#182]: https://github.com/posthtml/htmlnano/issues/182
269
279
  [#180]: https://github.com/posthtml/htmlnano/issues/180
270
280
  [#170]: https://github.com/posthtml/htmlnano/issues/170
271
281
  [#168]: https://github.com/posthtml/htmlnano/issues/168
package/lib/htmlnano.js CHANGED
@@ -97,19 +97,19 @@ function htmlnano(optionsRun, presetRun) {
97
97
 
98
98
  const module = require('./modules/' + moduleName);
99
99
 
100
- if (module.onAttrs) {
100
+ if (typeof module.onAttrs === 'function') {
101
101
  attrsHandlers.push(module.onAttrs(options, moduleOptions));
102
102
  }
103
103
 
104
- if (module.onContent) {
104
+ if (typeof module.onContent === 'function') {
105
105
  contentsHandlers.push(module.onContent(options, moduleOptions));
106
106
  }
107
107
 
108
- if (module.onNode) {
108
+ if (typeof module.onNode === 'function') {
109
109
  nodeHandlers.push(module.onNode(options, moduleOptions));
110
110
  }
111
111
 
112
- if (module.default) {
112
+ if (typeof module.default === 'function') {
113
113
  promise = promise.then(tree => module.default(tree, options, moduleOptions));
114
114
  }
115
115
  }
@@ -120,28 +120,35 @@ function htmlnano(optionsRun, presetRun) {
120
120
 
121
121
  return promise.then(tree => {
122
122
  tree.walk(node => {
123
- if (node.attrs) {
124
- // Convert all attrs' key to lower case
125
- let newAttrsObj = {};
126
- Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
127
- newAttrsObj[attrName.toLowerCase()] = attrValue;
128
- });
129
-
130
- for (const handler of attrsHandlers) {
131
- newAttrsObj = handler(newAttrsObj, node);
123
+ if (node) {
124
+ if (node.attrs && typeof node.attrs === 'object') {
125
+ // Convert all attrs' key to lower case
126
+ let newAttrsObj = {};
127
+ Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
128
+ newAttrsObj[attrName.toLowerCase()] = attrValue;
129
+ });
130
+
131
+ for (const handler of attrsHandlers) {
132
+ newAttrsObj = handler(newAttrsObj, node);
133
+ }
134
+
135
+ node.attrs = newAttrsObj;
132
136
  }
133
137
 
134
- node.attrs = newAttrsObj;
135
- }
138
+ if (node.content) {
139
+ node.content = typeof node.content === 'string' ? [node.content] : node.content;
136
140
 
137
- if (node.content) {
138
- for (const handler of contentsHandlers) {
139
- node.content = handler(node.content, node);
141
+ if (Array.isArray(node.content) && node.content.length > 0) {
142
+ for (const handler of contentsHandlers) {
143
+ const result = handler(node.content, node);
144
+ node.content = typeof result === 'string' ? [result] : result;
145
+ }
146
+ }
140
147
  }
141
- }
142
148
 
143
- for (const handler of nodeHandlers) {
144
- node = handler(node, node);
149
+ for (const handler of nodeHandlers) {
150
+ node = handler(node);
151
+ }
145
152
  }
146
153
 
147
154
  return node;
@@ -8,15 +8,14 @@ const rNodeAttrsTypeJson = /(\/|\+)json/;
8
8
 
9
9
  function onContent() {
10
10
  return (content, node) => {
11
- let newContent = content;
12
-
13
11
  if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) {
14
12
  try {
15
- newContent = JSON.stringify(JSON.parse((content || []).join('')));
13
+ // cast minified JSON to an array
14
+ return [JSON.stringify(JSON.parse((content || []).join('')))];
16
15
  } catch (error) {// Invalid JSON
17
16
  }
18
17
  }
19
18
 
20
- return newContent;
19
+ return content;
21
20
  };
22
21
  }
@@ -21,8 +21,9 @@ function minifySvg(tree, options, svgoOptions = {}) {
21
21
  });
22
22
  const result = svgo.optimize(svgStr, svgoOptions);
23
23
  node.tag = false;
24
- node.attrs = {};
25
- node.content = result.data;
24
+ node.attrs = {}; // result.data is a string, we need to cast it to an array
25
+
26
+ node.content = [result.data];
26
27
  return node;
27
28
  });
28
29
  return tree;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "htmlnano",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Modular HTML minifier, built on top of the PostHTML",
5
5
  "main": "index.js",
6
6
  "author": "Kirill Maltsev <maltsevkirill@gmail.com>",