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

Aperta Adatta ai principianti
#31 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@neumaennl ci sta già lavorando.

Dal 10/11/2026.

  • #32 di @neumaennl — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
backend

Direzione di ricerca

Inizia dalla logica di formattazione di arrToStr() descritta nell’issue e verifica come elabora ogni voce figlia, inclusi elementi, commenti, istruzioni di elaborazione e nodi di arresto. Aggiungi casi di regressione per i sei esempi e confronta l’XML generato con l’output previsto; l’issue è completata quando il contenuto misto è formattato correttamente senza aggiungere spazi al contenuto privo di tag figli.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

With preserveOrder: true and format: true, arrToStr() decides how to format an element's content by looking at the generated string: whether it ends with >, or contains /> or </. That check gets several cases wrong. Some of them only look odd, others add whitespace to the content:

  1. Whitespace-only line. If the first child is a tag and text follows later, the output gets a line that contains only the indentation, because the content already starts with its own line break and indentation.
  2. Closing tag on the text's line. If a comment or processing instruction is followed by text, the content contains neither /> nor </, so the closing tag follows the text directly.
  3. Unindented text. If text is followed by a tag, the content ends with >, so the text is written directly after the opening tag.
  4. Whitespace added to content without child tags. If the content ends with > but has no child tag, a line break and indentation are added before the closing tag. This happens with CDATA-only content, with text ending in > when processEntities is false, and with the raw content of stop nodes.
Code
import XMLBuilder from "fast-xml-builder"; // 1.3.1

const options = { preserveOrder: true, format: true, commentPropName: "#comment", cdataPropName: "#cdata" };
const builder = new XMLBuilder(options);
const text = (value) => [{ "#text": value }];

builder.build([{ p: [{ b: text("two") }, { "#text": "three" }] }]);   // 1
builder.build([{ q: [{ "#comment": text("c") }, { "#text": "three" }] }]); // 2
builder.build([{ r: [{ "#text": "one" }, { b: text("two") }] }]);     // 3
builder.build([{ d: [{ "#cdata": text("a") }] }]);                    // 4
new XMLBuilder({ ...options, processEntities: false }).build([{ x: text("a>") }]); // 4
new XMLBuilder({ ...options, stopNodes: ["root.script"] })
  .build([{ root: [{ script: [{ b: text("x") }] }] }]);               // 4
Output
"\n<p>\n  \n  <b>two</b>\n  three\n</p>"
"\n<q>\n  <!--c-->\n  three</q>"
"\n<r>one\n  <b>two</b>\n</r>"
"\n<d><![CDATA[a]]>\n</d>"
"\n<x>a>\n</x>"
"\n<root>\n  <script><b>x</b>\n  </script>\n</root>"
Expected output
"\n<p>\n  <b>two</b>\n  three\n</p>"
"\n<q>\n  <!--c-->\n  three\n</q>"
"\n<r>\n  one\n  <b>two</b>\n</r>"
"\n<d><![CDATA[a]]></d>"
"\n<x>a></x>"
"\n<root>\n  <script><b>x</b></script>\n</root>"

The leading line break is a separate question (#30).

A fix would decide by the child entries instead of the string: the content is 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.

Would you like to work on this issue?

  • Yes
  • No
Lingua principale
JavaScript
Stelle
22
Fork
9
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NaturalIntelligence/fast-xml-builder

Tutte le issue di NaturalIntelligence/fast-xml-builder

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.