Skip to content

fix(html): keep CDATA content with the surrounding text - #2565

Open
Yahiro025 wants to merge 1 commit into
beautifier:mainfrom
Yahiro025:fix-html-cdata-inline
Open

Yahiro025 wants to merge 1 commit into
beautifier:mainfrom
Yahiro025:fix-html-cdata-inline

Conversation

@Yahiro025

Copy link
Copy Markdown

Description

  • Source branch in your fork has meaningful name (not main)

Fixes Issue: #2365

CDATA is text, but the tokenizer reports it as a comment token, so the beautifier printed it like a tag: on its own line, indented, with newlines around it. That changes rendering for inline content in foreign content (SVG, MathML), where CDATA is the way to include characters that would otherwise be markup:

html('<simple><![CDATA[You will see this in the document]]></simple>')
// before: '<simple>\n    <![CDATA[...]]>\n</simple>'
// after:  '<simple><![CDATA[...]]></simple>'

CDATA tokens are now routed through the text handler, so they are placed like the surrounding text. Block-level CDATA that already sits on its own line keeps its line. CDATA inside <script> and <style> is read as raw content and is unchanged.

Tests are added to test/data/html/tests.js (HTML is JavaScript-only, so there is no Python port): inline CDATA, the case from the issue, and a block-level CDATA case. Without the change the new cases fail; with it they pass. The full JS source suite passes (39,301 JS / 4,847 CSS / 12,396 HTML tests).

Before Merge Checklist

  • JavaScript implementation
  • Python implementation (NA if HTML beautifier)
  • Added Tests to data file(s)
  • Added command-line option(s) (NA if
  • README.md documents new feature/option(s)

CDATA is text, but the tokenizer reports it as a comment token, so the
beautifier printed it like a tag: on its own line, indented, and with
newlines around it. Route CDATA tokens through the text handler so they
are placed like the surrounding text. Block-level CDATA that already
has its own line keeps it.

The HTML beautifier is JavaScript-only, so no Python port is needed.

This branch has not been deployed

No deployments
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 this pull request may close these issues.

1 participant