Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text elements render differently in XMLBuilder and XMLBuilderCB #94

Open
msironi opened this issue Jun 14, 2021 · 2 comments
Open

text elements render differently in XMLBuilder and XMLBuilderCB #94

msironi opened this issue Jun 14, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@msironi
Copy link

msironi commented Jun 14, 2021

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]
@msironi msironi added the bug Something isn't working label Jun 14, 2021
@Dadibom
Copy link

Dadibom commented Oct 4, 2021

I was going to open an issue regarding not being able to pretty print when using createCB and writing in chunks. Am I right to assume whatever fixes this will fix both issues ?

@mdovhopo
Copy link

mdovhopo commented May 2, 2024

@msironi i opened a PR that fixes this issue if you still care :) i saw the same issue as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants