I want to check if the string is well-formed XML in JavaScript.

Asked 1 years ago, Updated 1 years ago, 34 views

What I wanted to do was http://mizchi.github.io/md2react/ where I tried to detect and warn you when you entered corrupted HTML using markdown's direct HTML input notation.

At first, I used the following link to write the following code.
Check for XML errors using JavaScript-Stack Overflow

 parser=new DOMParser()
checkValidXML=(xmlString)->
  parsererrorNS=parser.parseFromString('INVALID', 'text/xml').getElementsByTagName('parserror')[0].namespaceURI
  dom=parser.parseFromString(xmlString, 'text/xml')

  if dom.getElementsByTagNameNS(parserrrorNS, 'parserrror').length>0
    US>throw new Error ('Error parsing XML')
  return dom;

However, in this case, <a>foo</a> and <span>aaa> are misdetected.What I really want to detect is half-finished strings like <a>foo</.

Div and pre go through.(In the current md2 react implementation, even a little bit closer to the safety side is allowed to misdetect)

This implementation also depends on DOMParser, so if possible, I would like to use a pure JS implementation to detect broken HTML (XML) correctly. Is there any better way or existing implementation?

javascript

2022-09-30 20:23

1 Answers

The isInvalidXML function appears to be running twice in "<a>" and "<a>".

If you send the string "<a>foo</a>" directly from the console to the isInvalidXML function,
The result seems to be returned without any problems, so
The problem may have existed before isInvalidXML was called.

I haven't followed it very closely, but it seems that it was already "<a>" as of the node.value of the compile function.


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.