svgo icon indicating copy to clipboard operation
svgo copied to clipboard

Incorrect parsing of html tags within `foreignObject` tags

Open lowlighter opened this issue 4 years ago • 0 comments

Describe the bug Empty html tags are transformed into self-closing tags which breaks expected image. Required whitespaces from text nodes are removed.

To Reproduce

import SVGO from "svgo"

const svg = `
<svg xmlns="http://www.w3.org/2000/svg">
  <foreignObject x="0" y="0" width="100%" height="100%">
    <div xmlns="http://www.w3.org/1999/xhtml">
      Lorem <span>ipsum</span> dolor
    </div>
    <div class="empty-div"></div>
  </foreignObject>
</svg>`

const {data} = await SVGO.optimize(svg, {multipass:true})
console.log(data)

Expected behavior

+<svg xmlns="http://www.w3.org/2000/svg"><foreignObject x="0" y="0" width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml">Lorem <span>ipsum</span> dolor</div><div class="empty-div"></div></foreignObject></svg>
-<svg xmlns="http://www.w3.org/2000/svg"><foreignObject x="0" y="0" width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml">Lorem<span>ipsum</span>dolor</div><div class="empty-div"/></foreignObject></svg>
  • Empty <div> tag should not be transformed into self closing tags (breaks html interpretation by creating "unclosed" tags)
  • Spaces within html text nodes are not preserved (note that xml:space="preserve" or using &nbsp; change nothing)

Desktop (please complete the following information):

  • SVGO Version: 2.3.0
  • NodeJs Version: 14.9.0
  • OS: Windows

Additional context Relates to #1421, but add more insight about troubleshooting and reproduction Updated https://github.com/lowlighter/metrics/issues/172#issue-822378888 to display original and optimized image

lowlighter avatar Apr 24 '21 19:04 lowlighter

bump

Milo123459 avatar Aug 08 '21 17:08 Milo123459

bump....

Milo123459 avatar Oct 05 '21 21:10 Milo123459