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.49k stars 302 forks source link

Nested CDATA with tags causes unexpected error #537

Closed markcellus closed 1 year ago

markcellus commented 1 year ago

Description

Hello, thanks for building this package! I'm getting an unexpected error when attempting to parse XML with nested CDATA. ### Input

Code

var fastXmlParser = require("fast-xml-parser")
 const parser = new fastXmlParser.XMLParser();
console.log(parser.parse('<description><![CDATA[<pre><code><category><![CDATA[Architecture]]></category></code></pre><a href="https://test.com">link</a>]]></description>'))

Output

 TypeError: Cannot read properties of undefined (reading 'addChild')

expected data

{description: "<pre><code><category><![CDATA[Architecture]]></category></code></pre><a href=\"https://test.com\">link</a>"}

Would you like to work on this issue?

Bookmark this repository for further updates.

amitguptagwl commented 1 year ago

It look like you have nested CDATA which is not allowed by the library.

markcellus commented 1 year ago

Right, but shouldn't it just escape the nested CDATA? Or, at the very least, show an error that is more helpful?

amitguptagwl commented 1 year ago

have you checked with validator?

markcellus commented 1 year ago

Ran it through the validator (output is below). I know that the package considers the XML invalid due to the nested CDATA. But what is the recommended approach of escaping the nested CDATA? If you can let me know what that is, I'll just do that and close this issue. Thanks!


var {XMLValidator} = require("fast-xml-parser")
 const result = XMLValidator.validate('<description><![CDATA[<pre><code><category><![CDATA[Architecture]]></category></code></pre><a href="https://test.com">link</a>]]></description>');
console.log(result)

Which gives me

{
  err: { 
    code: 'InvalidTag';
    msg: "Expected closing tag 'description' (opened in line 1, col 1) instead of closing tag 'category'.",
    line: 1,
    col: 68 
  };
};
amitguptagwl commented 1 year ago

currently no.