Skip to content

text elements render differently in XMLBuilder and XMLBuilderCB #94

@msironi

Description

@msironi

Describe the bug
XMLBuilder and XMLBuilderCB handle text elements differently. By default XMLBuilder emits this:

<value>text</value>

whereas XMLBuilderCB emits:

<value>
  text
</value>

XMLBuilder includes an indentTextOnlyNodes option that can be used to control the behavior but XMLBuilderCB does not.

To Reproduce
jest test:

import "jest";
import * as xb2 from "xmlbuilder2";

test("xml text bug", () => {
    const b1 = xb2.create();
    b1.dec({}).ele("root").ele("value").txt("frobozz").up();
    let xml = b1.end({ prettyPrint: true });
    expect(xml).toBe(
        `<?xml version="1.0"?>\n` +
        `<root>\n` +
        `  <value>frobozz</value>\n` +
        `</root>`
    );
    xml = b1.end({ prettyPrint: true, indentTextOnlyNodes: true });
    expect(xml).toBe(
        `<?xml version="1.0"?>\n` +
        `<root>\n` +
        `  <value>\n` +
        `    frobozz\n` +
        `  </value>\n` +
        `</root>`
    );

    xml = "";
    const b2 = xb2.createCB({
        data: (chunk: string) => xml += chunk,
        prettyPrint: true,
    });
    b2.dec({}).ele("root").ele("value").txt("frobozz").up().up();
    // Expecting that this should match the first expectation above, but instead it matches the second.
    // Worse createCB options does not have an indentTextOnlyNodes that can be used to alter the behavior.
    expect(xml).toBe(
        `<?xml version="1.0"?>\n` +
        `<root>\n` +
        `  <value>\n` +
        `    frobozz\n` +
        `  </value>\n` +
        `</root>`
    );
});

Expected behavior
Expecting that both XMLBuilder and XMLBuilderDB emit identical elements by default, and that both would let you alter the behavior using indentTextOnlyNodes .

Version:

  • node.js: [14.15.4]
  • xmlbuilder2 [2.4.1]

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions