TobiasNickel / tXml

:zap:very small and fast xml-parser in pure javascript:zap:
MIT License
217 stars 28 forks source link

Parsing doesn't error on certain invalid xml. #41

Open emorris00 opened 2 years ago

emorris00 commented 2 years ago

There are 2 scenarios I have found where the parser does not throw an error even though the xml is invalid.

  1. root level elements without a close tag that aren't marked as a noChildNodes element

    parse("<a><b></b>")
    // returns
    // [
    //   {
    //     tagName: 'a',
    //     attributes: {},
    //     children: [ { tagName: 'b', attributes: {}, children: [] } ]
    //   }
    // ]
  2. root level orphaned close tags (stops parsing after the first one encountered)

    
    parse("</a><b></b>")
    // returns
    // []

parse("") // returns // [ { tagName: 'a', attributes: {}, children: [] } ]



Technically there is a third where it doesn't error if there is more than one root element ex. `<a></a><a></a>` but I am assuming that is intended behavior so it can parse xml fragments. Maybe there should be an option to toggle whether to parse as a fragment or not? Either way this one isn't a big deal because it is very easy to check after parsing if there are multiple root elements.
TobiasNickel commented 2 years ago

yes, I know, this is on purpose. This library skip some checks. just to be faster. if you need a more standard compliant parser, I recommend parse5.