NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.

Home Page:https://naturalintelligence.github.io/fast-xml-parser/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parser erroneously adds `"@_/": true` attr for self-closing stop nodes

smoores-dev opened this issue · comments

  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool? (there doesn't seem to be a way to add stop nodes to the online tool)

Description

When the allowBooleanAttributes flag is set on XMLParser, the parser will interpret the closing / in a self-closing stop node as a boolean attribute. This introduces further problems when attempting to build the parsed data back into a string, as it produces invalid XML. E.g., <script src="script.js" /> becomes <script src="script.js" //> (note the double slash).

It is possible to implement a workaround by adding an updateTag method that removes @_/ attributes.

Input

<script src="script.js" />

Code

const parser = new XMLParser({
    allowBooleanAttributes: true,
    ignoreAttributes: false,
    stopNodes: ["*.pre", "*.script"],
})

parser.parse(inputString)

Output

expected data

{ script: { '@_src': 'script.js', '@_/': true } }

Would you like to work on this issue?

  • Yes (could use some guidance, though)
  • No

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

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.

Thanks to highlight it. We would have to put a condition in the code to avoid an attribute having the name "/" or "/>" or ">" or any other possibility.

This check can be placed in a buildAttributesMap function and attribute function for v5.