Skip to content

fix: preserve all white space within foreignObject elements#2064

Open
johnkenny54 wants to merge 1 commit into
svg:mainfrom
johnkenny54:issue-1728
Open

fix: preserve all white space within foreignObject elements#2064
johnkenny54 wants to merge 1 commit into
svg:mainfrom
johnkenny54:issue-1728

Conversation

@johnkenny54

@johnkenny54 johnkenny54 commented Aug 20, 2024

Copy link
Copy Markdown
Contributor

This PR modifies SVGO to always preserve white space within <foreignObject> elements. In general there is no way to know the semantics of the <foreignObject> content, so all white space within the element is preserved.

The following changes were made:

  • parser.js - keeps track of whether the parser is within a <foreignObject> element, and if so does not trim text or comment nodes.
  • stringifier.js - keeps track of whether the stringifier is within a <foreignObject> element, and if so does not emit prettification indentation or newlines.
  • _collections.js - removed 'pre' from textElems set - this was added by PR fix: dont trim pre elements #1796, but is not necessary since all <foreignObject> content now preserves white space.`.
  • Added a test case to confirm that CDATA and comments are preserved within a <foreignObject>.
  • Modified expected results in several test files. In all cases, the expected result is now to preserve the contents of the <foreignObject> element exactly.

Resolves #1678, resolves #1473.

@rnwst

rnwst commented Nov 1, 2024

Copy link
Copy Markdown

Thanks for this PR. If all it does is to preserve white space within foreignObject elements, then it does not address #1728. #1728 is about invalid self-closing tags, not preserving whitespace.

@KTibow KTibow added the merge conflicts Changes currently conflict with main label Mar 8, 2026
@KTibow KTibow changed the title Preserve all white space within foreignObject elements. fix: preserve all white space within foreignObject elements Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge conflicts Changes currently conflict with main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-breaking spaces in <foreignObject> get trimmed Incorrect parsing of html tags within foreignObject tags

3 participants