NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.43k stars 297 forks source link

`unpairedTags` option does not work correctly with text nodes #609

Open mohd-akram opened 10 months ago

mohd-akram commented 10 months ago

Description

The unpairedTags option does not seem to behave correctly with text nodes.

Input

Code

const assert = require('assert/strict');

const { XMLParser, XMLBuilder } = require('fast-xml-parser');

const html = '<div>1<br>2<br></div>';
console.log(html);

const parsingOptions = {
  preserveOrder: true,
  unpairedTags: ['br'],
};
const parser = new XMLParser(parsingOptions);
const result = parser.parse(html);
console.log(JSON.stringify(result, null, 2));

const builderOptions = {
  preserveOrder: true,
  unpairedTags: ['br'],
}
const builder = new XMLBuilder(builderOptions);
const output = builder.build(result);
console.log(output);

assert.equal(output, html);

Output

[
  {
    "div": [
      {
        "#text": 1
      },
      {
        "br": [
          {
            "#text": 2
          }
        ]
      },
      {
        "br": []
      }
    ]
  }
]
<div>1<br><br></div>

expected data

[
  {
    "div": [
      {
        "#text": 1
      },
      {
        "br": []
      },
      {
        "#text": 2
      },
      {
        "br": []
      }
    ]
  }
]
<div>1<br>2<br></div>

Would you like to work on this issue?

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

github-actions[bot] commented 10 months ago

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.