Skip to content

fix: escape carriage return as 
 in builder output - #16

Open
gaoflow wants to merge 1 commit into
NaturalIntelligence:mainfrom
gaoflow:escape-carriage-return
Open

fix: escape carriage return as 
 in builder output#16
gaoflow wants to merge 1 commit into
NaturalIntelligence:mainfrom
gaoflow:escape-carriage-return

Conversation

@gaoflow

@gaoflow gaoflow commented Jul 27, 2026

Copy link
Copy Markdown

The builder emits a raw carriage return in text and attribute values. A raw CR is normalized to LF by any conforming XML parser (XML 1.0 §2.11), so the CR is silently lost on the next parse — including through fast-xml-parser's own round trip:

const b = new XMLBuilder();
const p = new XMLParser({ htmlEntities: true });
p.parse(b.build({ n: "A\rB" })).n;   // "A\nB", not "A\rB"

Fix: add CR to the default entity table so it serializes as 
, which is how CR has to be represented in XML. LF and TAB are left verbatim — they survive a round trip; CR is the only character that gets folded.

A sweep of U+0001–U+00FF in both text and attribute positions confirms CR is the only value that fails the round trip. The added tests cover the escaped output, the round trip, and that LF/TAB stay unescaped.

…tput

A raw CR emitted in text or attribute values is folded to LF by any
conforming XML parser (XML 1.0 2.11), so a build -> parse round trip
silently turned "\r" into "\n". Add CR to the default entity table
so it is preserved; LF and TAB stay verbatim.
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