`preserveOrder: true` with `format: true`: output starts with a line break and has none at the end — intended?
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 54/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- javascript
- Domain
- backend
Research direction
Start with spec/j2x_ordered_spec.js and the toXml() formatting path mentioned in the issue. Compare the ordered and keyed outputs for the same input, including the case with ?xml, and check how the existing specs define the leading and trailing line breaks. Done means the intended behavior is decided and the output and specs agree.
Written by the indexing model from the issue text.
Description
Description
With format: true, the ordered mode and the keyed mode put line breaks in different places for the same document:
- The ordered mode starts the output with a line break and doesn't end it with one.
- The keyed mode starts directly with the root element and ends with a line break.
When the input starts with ?xml, the ordered mode writes no line break before the declaration. So the ordered mode isn't consistent with itself either. The top-level indentation in toXml() is the line break itself, and only ?xml is exempt from it (tempInd).
Code
import XMLBuilder from "fast-xml-builder"; // 1.3.1
const ordered = new XMLBuilder({ format: true, preserveOrder: true, ignoreAttributes: false });
const keyed = new XMLBuilder({ format: true });
ordered.build([{ root: [{ a: [{ "#text": "1" }] }] }]);
keyed.build({ root: { a: "1" } });
ordered.build([
{ "?xml": [], ":@": { "@_version": "1.0" } },
{ root: [{ a: [{ "#text": "1" }] }] },
]);
Output
"\n<root>\n <a>1</a>\n</root>"
"<root>\n <a>1</a>\n</root>\n"
"<?xml version=\"1.0\"?>\n<root>\n <a>1</a>\n</root>"
Question
Several specs expect the leading line break, for example "should build formatted XML" in spec/j2x_ordered_spec.js. So I'm not sure whether this is intended. Would you accept a change that makes the ordered mode behave like the keyed mode, either by default in a major version or behind an option? I'd be happy to open a PR.
Would you like to work on this issue?
- Yes
- No
- Dominant language
- JavaScript
- Stars
- 22
- Forks
- 9
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NaturalIntelligence/fast-xml-builder
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
-
`preserveOrder: true` with `format: true` formats mixed content wrongly and adds whitespace to content that ends with `>`Possibly taken @neumaennl claimed this 8 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
`XMLBuilder` object should be exported as a named export, not just the typePossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
NaturalIntelligence/fast-xml-builder#25 · 1 comment ·
-
sanitizeName not applied to names inside stopNodes (quote injection / tag break-out bypass)Possibly taken @hicay claimed this 5 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
NaturalIntelligence/fast-xml-builder#34 · 1 comment ·
-
enhancement
Difficulty 3/5 1-2 days Newbie friendliness 48/100
NaturalIntelligence/fast-xml-builder#4 · 5 comments · 2 reactions ·
All issues in NaturalIntelligence/fast-xml-builder
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
Extended search drops any query term that contains a line breakPossibly taken @goranai claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
area:mcp
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day