`preserveOrder: true` with `format: true`: output starts with a line break and has none at the end — intended?

Open
#30 0 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NaturalIntelligence/fast-xml-builder

All issues in NaturalIntelligence/fast-xml-builder

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.