`preserveOrder: true` with `format: true` formats mixed content wrongly and adds whitespace to content that ends with `>`
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:
- 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.
- 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. - Unindented text. If text is followed by a tag, the content ends with
>, so the text is written directly after the opening tag. - 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>whenprocessEntitiesisfalse, 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di NaturalIntelligence/fast-xml-builder
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
-
`XMLBuilder` object should be exported as a named export, not just the typeForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
NaturalIntelligence/fast-xml-builder#25 · 1 commento ·
-
sanitizeName not applied to names inside stopNodes (quote injection / tag break-out bypass)Forse già presa @hicay l’ha presa 5 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
NaturalIntelligence/fast-xml-builder#34 · 1 commento ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 54/100
-
enhancement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 48/100
NaturalIntelligence/fast-xml-builder#4 · 5 commenti · 2 reazioni ·
Tutte le issue di NaturalIntelligence/fast-xml-builder
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 84/100
-
Extended search drops any query term that contains a line breakForse già presa @goranai l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
0.3.14: pdfmake.virtualfs renamed to sharedVirtualfs without changelog entry (breaking change)Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
area:mcp
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno