diff --git a/lib/htmlnano.es6 b/lib/htmlnano.es6
index 41344a1..7016a0f 100644
--- a/lib/htmlnano.es6
+++ b/lib/htmlnano.es6
@@ -79,16 +79,16 @@ function htmlnano(optionsRun, presetRun) {
const module = require('./modules/' + moduleName);
- if (module.onAttrs) {
+ if (typeof module.onAttrs === 'function') {
attrsHandlers.push(module.onAttrs(options, moduleOptions));
}
- if (module.onContent) {
+ if (typeof module.onContent === 'function') {
contentsHandlers.push(module.onContent(options, moduleOptions));
}
- if (module.onNode) {
+ if (typeof module.onNode === 'function') {
nodeHandlers.push(module.onNode(options, moduleOptions));
}
- if (module.default) {
+ if (typeof module.default === 'function') {
promise = promise.then(tree => module.default(tree, options, moduleOptions));
}
}
@@ -99,28 +99,35 @@ function htmlnano(optionsRun, presetRun) {
return promise.then(tree => {
tree.walk(node => {
- if (node.attrs) {
- // Convert all attrs' key to lower case
- let newAttrsObj = {};
- Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
- newAttrsObj[attrName.toLowerCase()] = attrValue;
- });
-
- for (const handler of attrsHandlers) {
- newAttrsObj = handler(newAttrsObj, node);
+ if (node) {
+ if (node.attrs && typeof node.attrs === 'object') {
+ // Convert all attrs' key to lower case
+ let newAttrsObj = {};
+ Object.entries(node.attrs).forEach(([attrName, attrValue]) => {
+ newAttrsObj[attrName.toLowerCase()] = attrValue;
+ });
+
+ for (const handler of attrsHandlers) {
+ newAttrsObj = handler(newAttrsObj, node);
+ }
+
+ node.attrs = newAttrsObj;
}
- node.attrs = newAttrsObj;
- }
+ if (node.content) {
+ node.content = typeof node.content === 'string' ? [node.content] : node.content;
- if (node.content) {
- for (const handler of contentsHandlers) {
- node.content = handler(node.content, node);
+ if (Array.isArray(node.content) && node.content.length > 0) {
+ for (const handler of contentsHandlers) {
+ const result = handler(node.content, node);
+ node.content = typeof result === 'string' ? [result] : result;
+ }
+ }
}
- }
- for (const handler of nodeHandlers) {
- node = handler(node, node);
+ for (const handler of nodeHandlers) {
+ node = handler(node);
+ }
}
return node;
diff --git a/lib/modules/minifyJson.es6 b/lib/modules/minifyJson.es6
index 8466ed6..2fd8f2a 100644
--- a/lib/modules/minifyJson.es6
+++ b/lib/modules/minifyJson.es6
@@ -2,15 +2,15 @@ const rNodeAttrsTypeJson = /(\/|\+)json/;
export function onContent() {
return (content, node) => {
- let newContent = content;
if (node.attrs && node.attrs.type && rNodeAttrsTypeJson.test(node.attrs.type)) {
try {
- newContent = JSON.stringify(JSON.parse((content || []).join('')));
+ // cast minified JSON to an array
+ return [JSON.stringify(JSON.parse((content || []).join('')))];
} catch (error) {
// Invalid JSON
}
}
- return newContent;
+ return content;
};
}
diff --git a/lib/modules/minifySvg.es6 b/lib/modules/minifySvg.es6
index 6a63a9c..ea22a2f 100644
--- a/lib/modules/minifySvg.es6
+++ b/lib/modules/minifySvg.es6
@@ -11,7 +11,8 @@ export default function minifySvg(tree, options, svgoOptions = {}) {
const result = svgo.optimize(svgStr, svgoOptions);
node.tag = false;
node.attrs = {};
- node.content = result.data;
+ // result.data is a string, we need to cast it to an array
+ node.content = [result.data];
return node;
});