Skip to content

fix: format content by its child entries, not by the string - #32

Open
neumaennl wants to merge 1 commit into
NaturalIntelligence:mainfrom
neumaennl:fix/format-by-child-entries
Open

neumaennl wants to merge 1 commit into
NaturalIntelligence:mainfrom
neumaennl:fix/format-by-child-entries

Conversation

@neumaennl

Copy link
Copy Markdown

Fixes #31

Problem

With preserveOrder: true and format: true, arrToStr() decided how to format an element's content by looking at the generated string (endsWith(">"), includes("/>"), includes("</")). That wrote a whitespace-only line, left the closing tag on the line of text after a comment, did not indent text before a tag, and added whitespace to content that ends with > but has no child tag: CDATA, text with processEntities: false, and the raw content of stop nodes. The issue has examples of each.

Change

  • src/orderedJs2Xml.js: a new helper, hasMarkupChild(), checks whether a child is an element, a comment or a processing instruction. Only then is the content written on lines of its own, with a line break and indentation added before a leading text or CDATA child. Stop nodes are never formatted. Everything else is written inline. This replaces the two branches that inspected the string.
  • spec/j2x_ordered_spec.js: one spec per case from the issue. All six fail without the change.

All existing specs pass unchanged. eslint reports two errors in spec/j2x_ordered_spec.js (format and isAttribute unused) that were there before this change.

The leading line break of the output is a separate question (#30) and not changed here.

Type

  • Bug Fix
  • Refactoring / Technology upgrade
  • New Feature

🤖 Generated with Claude Code

With preserveOrder and format, arrToStr() decided how to format an
element's content by looking at the generated string. That wrote a
whitespace-only line, left the closing tag on the line of text after a
comment, did not indent text before a tag, and added whitespace to
content that ends with ">" but has no child tag, such as CDATA, text
with processEntities: false and stop nodes.

The content is now written on lines of its own only if a child is an
element, a comment or a processing instruction, and never for a stop
node.

Fixes NaturalIntelligence#31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

preserveOrder: true with format: true formats mixed content wrongly and adds whitespace to content that ends with >

1 participant