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

named HTML entities in inline-SVG cause errors #197

Closed
SirkoS opened this issue May 23, 2022 · 2 comments · Fixed by #198
Closed

named HTML entities in inline-SVG cause errors #197

SirkoS opened this issue May 23, 2022 · 2 comments · Fixed by #198

Comments

@SirkoS
Copy link

SirkoS commented May 23, 2022

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
@mischnic
Copy link

mischnic commented May 23, 2022

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);
    }
  );

@SukkaW
Copy link
Contributor

SukkaW commented May 25, 2022

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

SukkaW added a commit to SukkaW/htmlnano that referenced this issue May 25, 2022
maltsev added a commit that referenced this issue May 25, 2022
Fix(#197): do not choke on svg error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants