posthtml / htmlnano

Modular HTML minifier, built on top of the PostHTML

Home Page:https://htmlnano.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

named HTML entities in inline-SVG cause errors

SirkoS opened this issue Β· comments

This is a copy of an issue I filed with parcel. There I was told to bring it up here as well as it seems to be rooted in htmlnano. I did not (yet) further investigate and produce a MWE with just htmlnano, but I guess the example HTML should already be rather straight forward.

πŸ› bug report

When running parcel on an HTML document with an embedded SVG that uses named HTML entities like below, the build fails with an error.

<!doctype html>
<html>
<body>
  <svg viewBox="0 0 100 100">
    <text x="20" y="20" style="fill: black;">&cross;</text>
  </svg>
</body>
</html>

Replacing the HTML entity with its numeric variation (&#10007;) seems to work just fine.

πŸ€” Expected Behavior

No error. Simply return an (almost) unchanged HTML file.

😯 Current Behavior

Γ— Build failed.

@parcel/optimizer-htmlnano: Cannot read properties of undefined (reading 'attrs')

  TypeError: Cannot read properties of undefined (reading 'attrs')
  at path\node_modules\.pnpm\htmlnano@2.0.2_svgo@2.8.0\node_modules\htmlnano\lib\modules\sortAttributesWithLists.js:77:15
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:105:26)
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:111:5)
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:105:17)
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:111:5)
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:105:17)
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:111:5)
  at traverse (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:105:17)
  at Array.walk (path\node_modules\.pnpm\posthtml@0.16.6\node_modules\posthtml\lib\api.js:39:10)
  at sortAttributesWithListsInAlphabeticalOrder (path\node_modules\.pnpm\htmlnano@2.0.2_svgo@2.8.0\node_modules\htmlnano\lib\modules\sortAttributesWithLists.js:76:8)

πŸ’ Possible Solution

Named entities are not defined in standalone SVG according to this SO answer. However, they work in inline SVG (open the above file directly in your browser).

So it seems this inline SVG is treated as standalone SVG at some point during the pipeline.

🌍 Your Environment

Software Version(s)
Parcel 2.5.0
Node 18.2.0

Reproduction:

{
  "dependencies": {
    "htmlnano": "2.0.2",
    "svgo": "2.8.0"
  }
}
const htmlnano = require("htmlnano");
const posthtml = require("posthtml");

const INPUT = `<!doctype html>
  <svg viewBox="0 0 100 100">
    <text x="20" y="20" style="fill: black;">&cross;</text>
  </svg>`;

const htmlNanoConfig = {
  minifyJs: false,
  minifyCss: false,
  minifySvg: {},
};

posthtml([htmlnano(htmlNanoConfig)])
  .process(INPUT)
  .then(
    function (result) {
      console.log(result.html);
    },
    function (err) {
      console.error(err);
    }
  );
commented

HTML entities contain invalid characters which cause the svgo to fail to parse them.