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

Non-breaking spaces in <foreignObject> get trimmed #1678

Open
timdp opened this issue Jun 3, 2022 · 1 comment · May be fixed by #2064
Open

Non-breaking spaces in <foreignObject> get trimmed #1678

timdp opened this issue Jun 3, 2022 · 1 comment · May be fixed by #2064
Labels

Comments

@timdp
Copy link

timdp commented Jun 3, 2022

Describe the bug
When parsing HTML in <foreignObject>, non-breaking spaces (&nbsp; aka &#xA0; aka &#160;) are getting removed.

To Reproduce

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 300">
  <foreignObject width="400" height="300">
    <div xmlns="http://www.w3.org/1999/xhtml" style="width: 100%; height: 100%; background: blue; color: white; display: flex; justify-content: center; align-items: center;">
      <div>foo </div>
      <div>bar&#xA0;</div>
      <div>baz</div>
    </div>
  </foreignObject>
</svg>

Even with --pretty, this becomes foobarbaz.

Expected behavior
foobar baz

Desktop (please complete the following information):

  • SVGO 2.8.0
  • Node.js 14.9.3

Additional context
I've narrowed it down to:

value: text.trim(),

I didn't check in detail, but presumably, String#trim() will remove any kind of Unicode whitespace, and there's quite a lot of variety there. If a space is non-breaking, I don't think SVGO should touch it.

Suggested fixes:

  • Replace String#trim() with something that only trims [ \t\r\n].
  • Add HTML elements to textElems so the else doesn't get hit.
  • Don't trim anything inside <foreignObject> to avoid implementing an entire HTML parser.
@timdp timdp added the bug label Jun 3, 2022
@fasterthanlime
Copy link

fasterthanlime commented Jun 19, 2024

I've been hit by this in the year of our bard 2024 and the suggested fixes make sense to me, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants