Skip to content

Commit

Permalink
Fix(posthtml#197): do not choke on svg error
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed May 25, 2022
1 parent 9faf495 commit e15972a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/modules/minifySvg.es6
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ export default function minifySvg(tree, options, svgoOptions = {}) {
tree.match({tag: 'svg'}, node => {
let svgStr = tree.render(node, { closingSingleTag: 'slash', quoteAllAttributes: true });
const result = svgo.optimize(svgStr, svgoOptions);

if (result.error) {
console.error('htmlnano fails to minify the svg:');
console.error(result.error);
if (result.modernError) {
console.error(result.modernError);
}

// We return the node as-is
return node;
}

node.tag = false;
node.attrs = {};
// result.data is a string, we need to cast it to an array
Expand Down
17 changes: 17 additions & 0 deletions test/modules/minifySvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,21 @@ describe('minifySvg', () => {
options
);
});

// https://github.com/posthtml/htmlnano/issues/197
it('shouldn\'t choke on svg errors', () => {
const input = `
<!doctype html>
<svg viewBox="0 0 100 100">
<text x="20" y="20" style="fill: black;">&cross;</text>
</svg>
`;
return init(
input,
input,
{
minifySvg: {}
}
);
});
});

0 comments on commit e15972a

Please sign in to comment.