kasbah
kasbah

Reputation: 943

What is the point of creating new DOMParser instances?

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?

Upvotes: 26

Views: 3038

Answers (3)

Azerum
Azerum

Reputation: 371

There seem to be no significant difference between using one vs multiple instances. The spec says that the current API shape is due to historical reasons:

The design of DOMParser, as a class that needs to be constructed and then have its parseFromString() method called, is an unfortunate historical artifact. If we were designing this functionality today it would be a standalone function. For parsing HTML, the modern alternative is Document.parseHTMLUnsafe().

Upvotes: 1

Bálint
Bálint

Reputation: 4049

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.

Upvotes: 2

RpR
RpR

Reputation: 372

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..

Upvotes: -1

Related Questions