23

If you look at the example of DOMParser from MDN:

var parser = new DOMParser();
var doc = parser.parseFromString(stringContainingXMLSource, "application/xml");
// returns a Document, but not a SVGDocument nor a HTMLDocument

parser = new DOMParser();
doc = parser.parseFromString(stringContainingXMLSource, "image/svg+xml");
// returns a SVGDocument, which also is a Document.

parser = new DOMParser();
doc = parser.parseFromString(stringContainingHTMLSource, "text/html");
// returns a HTMLDocument, which also is a Document.

They keep creating new DOMParser instances. But why? Wouldn't one parser instance suffice? What about code that does a lot of parsing, is there a performance advantage in creating new instances?

EDIT: People are getting hung up on the example. To phrase my question better: why isn't DOMParser more like JSON and its parse method? Why isn't parseFromString a static method?

kasbah
  • 903
  • 5
  • 23
  • 9
    I would interpret that as 3 'separate' examples, not as an encouragement to create a new parser for each parse operation. – Pieter Witvoet Oct 11 '16 at 12:49
  • Maybe, but generally I see a lot of code around that does `(new DOMParser).parseFromString`. I guess that's for convenience, but why does it even have this interface, is any state kept in your DOMParser instance? I looked at the [spec](https://w3c.github.io/DOM-Parsing/#dom-domparser) a bit but couldn't find a reason for it. – kasbah Oct 11 '16 at 13:05
  • Currently both DOMParser and XMLSerializer Objects have only one method, but the use of a constructor enables to add more in the future if needed. – Kaiido Oct 21 '16 at 02:58
  • Have you tried to ask the author of the page? – guest271314 Oct 24 '16 at 09:41
  • @Kalido, I don't see why they can't add more methods later if it wasn't a class. It would make sense to me if it was more like `JSON.parse`. I thought there might be a reason for it but there doesn't seem to be. @guest271314 I am more interested in why DOMParser has this interface. – kasbah Nov 06 '16 at 11:09

2 Answers2

2

The example you posted is just 3 different examples concatenated into 1, and they all declare a new DOMParser, so each is runnable on its own.

Maybe, but generally I see a lot of code around that does (new DOMParser).parseFromString.

If they use the (new DOMParser()).parseFromString that is because they only use it once, and they don't need it anywhere else, thus making a separate variable for it is redundant.

This code:

var
  proto = DOMParser.prototype
, nativeParse = proto.parseFromString
;

// Firefox/Opera/IE throw errors on unsupported types
try {
    // WebKit returns null on unsupported types
    if ((new DOMParser()).parseFromString("", "text/html")) {
        // text/html parsing is natively supported
        return;
    }
} catch (ex) {}

proto.parseFromString = function(markup, type) {
    if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
        var
          doc = document.implementation.createHTMLDocument("")
        ;
            if (markup.toLowerCase().indexOf('<!doctype') > -1) {
                doc.documentElement.innerHTML = markup;
            }
            else {
                doc.body.innerHTML = markup;
            }
        return doc;
    } else {
        return nativeParse.apply(this, arguments);
    }
};

This is pretty much a fail-safe if the browser doesn't support the DOMParser object.

Bálint
  • 4,009
  • 2
  • 16
  • 27
  • 4
    My question was really: why did the designers choose this interface for the DOMParser? It doesn't make any sense. It has one method which is a stateless function. – kasbah Nov 06 '16 at 11:05
  • 1
    @kasbah 1.) JavaScript wants to be object oriented 2.) Because it depends on the current extension (like WebKit). – Bálint Nov 06 '16 at 12:28
-1

If the MIME type is text/xml, the resulting object will be an XMLDocument, if the MIME type is image/svg+xml, it will be an SVGDocument and if the MIME type is text/html, it will be an HTMLDocument.

So its not about one parser instance but its about what we required...

The example you posted is just 3 different examples concatenated into 1, and they all declare a new DOMParser.

You can do it by one parser instance also but you just have to change MIME type in parseFromString method according to your exact requirement. And if you required all of these then you have to call parseFromString mehod with different MIME type 3 times by same parser instance.Hope this will help you..

RpR
  • 364
  • 1
  • 9