scala-js/scala-js-dom

Use `XMLDocument` type when parsing XML string in `DOMParser`

andriygm opened this issue · 3 comments

Currently, DOMParser's parseFromString method returns a Document type when using MIME type "text/xml". We should instead return an XMLDocument. It doesn't look like the XMLDocument type exists, so that should be created as well.

Hmm, I just took a proper look at this and I'm not sure if we can make an improvement here.

We should definitely add XMLDocument, I'm about to open a PR that does that. Update: #804

However, it is difficult to refine the return type of parseFromString based on its arguments. This is a very common problem: for example document.createElement("div") is returning an HTMLDivElement but we are not reflecting that in the type. For the user, the solution is simply to do an asInstanceOf cast.

In the future if we dropped support for Scala 2 (or split sources across Scala 2/3) we could possibly do something interesting with literal types. That is tracked in this issue:

Is parseXMLFromString too unwieldy?

parseXMLFromString is not a real method in the DOM APIs. We only facade the raw APIs (warts and all), and leave it up to other libraries to solve the usability issues.

For example see:
https://github.com/raquo/scala-dom-types#what-about-scala-js-dom