oozcitak / xmlbuilder2

An XML builder for node.js
366 stars 36 forks source link

Pretty printing for mixed content moves text to a new line and introduces spaces. #163

Open frederik opened 1 year ago

frederik commented 1 year ago

Describe the bug

With end({ prettyPrint: true }) mixed inline content is written to a new line, introducing spaces in XML viewers. (e.g. a text will render as "It's bold ." instead of "It's bold.")

To Reproduce Given the following XML

<sec sec-type="chapter"><title>Introduction</title><p>It's <b>bold</b>.</p></sec>

calling end({ prettyPrint: true }) results in the following output:

Introduction

It's bold .

Expected behavior

I would have expected the p element to keep all inline content

<sec sec-type="chapter">
    <title>Introduction</title>
    <p>It's <b>bold</b>.</p>
</sec>

There is a test for mixed content that tests for the current behavior, I am unsure whether that is correct (Oxygen XML and other editors format the text as expected).

Version:

Additional context Highly appreciate this straightforward library! I hope the issue makes sense.

universalhandle commented 1 year ago

Hello, @frederik! Sorry I missed this bug report! Can you reproduce this in v3.1.0?

javadev commented 1 year ago

It may be stored in this structure

{
  "sec": {
    "-sec-type": "chapter",
    "title": "Introduction",
    "p": {
      "#text": "It's ",
      "b": "bold",
      "#text1": "."
    }
  },
  "#omit-xml-declaration": "yes"
}
frederik commented 1 year ago

hey @universalhandle,

I can reproduce it on 3.1.1 (confirmed this on a node:latest container just to be sure it was only 16)

const fs = require('fs');
const { create } = require('xmlbuilder2');

const doc = create({ version: '1.0' })
    .ele('p').txt("It's ").ele('b').txt('bold').up().txt('.').up()
    .doc();

fs.writeFileSync('test.xml', doc.end({ prettyPrint: true }), 'utf8');

package-lock

"node_modules/xmlbuilder2": {
      "version": "3.1.1",
      "resolved": "https://registry.npmjs.org/xmlbuilder2/-/xmlbuilder2-3.1.1.tgz",
      "integrity": "sha512-WCSfbfZnQDdLQLiMdGUQpMxxckeQ4oZNMNhLVkcekTu7xhD4tuUDyAPoY8CwXvBYE6LwBHd6QW2WZXlOWr1vCw==",
      "dependencies": {
        "@oozcitak/dom": "1.15.10",
        "@oozcitak/infra": "1.0.8",
        "@oozcitak/util": "8.3.8",
        "js-yaml": "3.14.1"
      },
      "engines": {
        "node": ">=12.0"
      }
    }

produces:

<?xml version="1.0"?>
<p>
  It's 
  <b>bold</b>
  .
</p>
mdovhopo commented 5 months ago

i did open a PR #180 that fixes the other issue #94 , I believe it should also fix this issue as well... would appreciate reviews/comments.

P.S. i am using my forked version in my project,because correct pretty-printing is important to my usecase