jindw / xmldom

A PURE JS W3C Standard based(XML DOM Level2 CORE) DOMParser and XMLSerializer.
Other
819 stars 265 forks source link

Return <parsererror/> on errors #173

Open tomek-he-him opened 8 years ago

tomek-he-him commented 8 years ago

Hey,

the living spec demands that whenever we encounter an error while parsing, we should return a new document with a child element <parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">.

I’m not saying it’s reasonable behavior, but the spec says so and browser implementations follow:

> const parser = new DOMParser()
> const output = parser.parseFromString('<invalid', 'application/xml')
> const serializer = new XMLSerializer()
> serializer.serializeToString(output)
`<invalid><parsererror xmlns="http://www.w3.org/1999/xhtml" style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 9: Couldn't find end of Start Tag invalid
</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror></invalid>`

However, xmldom does things differently:

> const { DOMParser, XMLSerializer } = require('xmldom')
> const parser = new DOMParser()
> const output = parser.parseFromString('<invalid', 'application/xml')
[xmldom error]  unexpected end of input
@#[line:1,col:1]

Would it be possible to align error handling to the spec?