Skip to content

Commit

Permalink
Fix(posthtml#182): correct types and type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Apr 6, 2022
1 parent 50d7f06 commit f992afd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
49 changes: 28 additions & 21 deletions lib/htmlnano.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/minifyJson.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
3 changes: 2 additions & 1 deletion lib/modules/minifySvg.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down

0 comments on commit f992afd

Please sign in to comment.