Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 246
kasbah Avatar asked Oct 11 '16 12:10

kasbah


People also ask

What is the use of DOMParser?

The DOMParser interface provides the ability to parse XML or HTML source code from a string into a DOM Document . You can perform the opposite operation—converting a DOM tree into XML or HTML source—using the XMLSerializer interface.

Is DOMParser safe?

DOMParser created documents are created with scripting disabled; the script is parsed, but not run, so it should be safe against XSS.

What is DOM parser in XML?

The XML DOM (Document Object Model) defines the properties and methods for accessing and editing XML. However, before an XML document can be accessed, it must be loaded into an XML DOM object. All modern browsers have a built-in XML parser that can convert text into an XML DOM object.


1 Answers

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.

like image 152
Bálint Avatar answered Oct 05 '22 12:10

Bálint