Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(#182): correct types and type checks #183

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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