diff --git a/files/en-us/web/api/domparser/domparser/index.html b/files/en-us/web/api/domparser/domparser/index.html index 3616fda59fa85e5..3d0517aa024eaa8 100644 --- a/files/en-us/web/api/domparser/domparser/index.html +++ b/files/en-us/web/api/domparser/domparser/index.html @@ -12,16 +12,37 @@
var parser = new DOMParser();+
const parser = new DOMParser();
None.
-A new DOMParser
object. This object can be used to parse the text of a document using the parseFromString()
method.
Specification | +Status | +Comment | +
---|---|---|
{{SpecName('HTML WHATWG', '#dom-domparser-constructor', 'DOMParser()')}} + | +{{Spec2('HTML WHATWG')}} | ++ |
{{Compat("api.DOMParser.DOMParser")}}
diff --git a/files/en-us/web/api/domparser/index.html b/files/en-us/web/api/domparser/index.html index 7abdb625f762e6d..8931757bd41a41a 100644 --- a/files/en-us/web/api/domparser/index.html +++ b/files/en-us/web/api/domparser/index.html @@ -2,19 +2,14 @@ title: DOMParser slug: Web/API/DOMParser tags: -- API -- DOM -- DOM Parsing -- Document -- HTML -- HTMLDocument -- MakeBrowserAgnostic -- NeedsMarkupWork -- Parsing -- Reference -- SVG -- XML -- XMLDocument + - API + - DOM + - DOM Parsing + - Document + - HTML + - HTMLDocument + - Parsing + - Reference ---{{APIRef("DOM")}}
@@ -22,12 +17,6 @@ the ability to parse {{Glossary("XML")}} or {{Glossary("HTML")}} source code from a string into a DOM {{domxref("Document")}}. -Note: {{domxref("XMLHttpRequest")}} can parse XML and HTML directly
- from a URL-addressable resource, returning a Document
in its
- {{domxref("XMLHttpRequest.response", "response")}} property.
You can perform the opposite operation—converting a DOM tree into XML or HTML source—using the {{domxref("XMLSerializer")}} interface.
@@ -36,169 +25,54 @@ {{domxref("Element.outerHTML", "outerHTML")}} properties. These properties can also be read to fetch HTML fragments corresponding to the corresponding DOM subtree. -const domparser = new DOMParser()- -
Note that {{domxref("XMLHttpRequest")}} can parse XML and HTML directly
+ from a URL-addressable resource, returning a Document
in its
+ {{domxref("XMLHttpRequest.response", "response")}} property.
const doc = domparser.parseFromString(string, mimeType)- -
Either {{domxref("Document")}} or {{domxref("XMLDocument")}} depending on the
- mimeType
argument.
DOMParser
object.This method has 2 parameters (both required):
+string
mimeType
A {{domxref("DOMString")}}. This string determines a class of the method's return - value. The possible values are the following:
- -mimeType |
- doc.constructor |
-
---|---|
text/html |
- {{domxref("Document")}} |
-
text/xml |
- {{domxref("XMLDocument")}} |
-
application/xml |
- {{domxref("XMLDocument")}} |
-
application/xhtml+xml |
- {{domxref("XMLDocument")}} |
-
image/svg+xml |
- {{domxref("XMLDocument")}} |
-
Once you have created a parser object, you can parse XML from a string using the
- parseFromString()
method:
let parser = new DOMParser() -let doc = parser.parseFromString(stringContainingXMLSource, "application/xml") -+
This example shows how to parse XML, SVG, and HTML. Note that a MIME type of
+text/html
will invoke the HTML parser, and any of the other MIME types
+that are accepted by this method will invoke the XML parser.
const parser = new DOMParser(); -Note that if the parsing process fails, the
- -DOMParser
does not throw an - exception, but instead returns an error document:<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml"> - (error description) - <sourcetext>(a snippet of the source XML)</sourcetext> -</parsererror> -+const xmlString = "<warning>Beware of the tiger</warning>"; +const doc1 = parser.parseFromString(xmlString, "application/xml"); +// XMLDocument -The parsing errors are also reported to the Error Console, with the document URI (see below) as the - source of the error.
+const svgString = "<circle cx=\"50\" cy=\"50\" r=\"50\"/>"; +const doc2 = parser.parseFromString(svgString, "image/svg+xml"); +// XMLDocument -Parsing SVG or HTML
+const htmlString = "<strong>Beware of the leopard</strong>"; +const doc3 = parser.parseFromString(htmlString, "text/html"); +// HTMLDocument -The
+console.log(doc1.documentElement.textContent) +// "Beware of the tiger" -DOMParser
can also be used to parse an SVG document - {{geckoRelease("10.0")}} or an HTML document {{geckoRelease("12.0")}}. There are three - different results possible, selected by the MIME type given.-
- -- If the MIME type is
-text/xml
, the result will be an -XMLDocument
- If the MIME type is
-image/svg+xml
, the result will be an -SVGDocument
- If the MIME type is
-text/html
, the result will be an -HTMLDocument
let parser = new DOMParser() -let doc = parser.parseFromString(stringContainingXMLSource, "application/xml") -// returns a Document, but not an SVGDocument nor an HTMLDocument - -parser = new DOMParser(); -doc = parser.parseFromString(stringContainingSVGSource, "image/svg+xml") -// returns a SVGDocument, which also is a Document. - -parser = new DOMParser(); -doc = parser.parseFromString(stringContainingHTMLSource, "text/html") -// returns an HTMLDocument, which also is a Document. -+console.log(doc2.firstChild.tagName); +// "circle" -DOMParser HTML extension
- -/* - * DOMParser HTML extension - * 2012-09-04 - * - * By Eli Grey, http://eligrey.com - * Public domain. - * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. - */ - -/*! @source https://gist.github.com/1129031 */ -/*global document, DOMParser*/ - -(function(DOMParser) { - "use strict"; - - 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); - } - }; -}(DOMParser)); +console.log(doc3.body.firstChild.textContent); +// "Beware of the leopard"Specifications
@@ -228,7 +102,7 @@Browser compatibility
See also
The parseFromString()
method of the {{domxref("DOMParser")}} interface parses a string containing either HTML or XML, returning an {{domxref("HTMLDocument")}} or an {{domxref("XMLDocument")}}.
const doc = domparser.parseFromString(string, mimeType)+ +
string
mimeType
A {{domxref("DOMString")}}. This string determines whether the XML parser or the HTML parser is used to parse the string. Valid values are:
+text/html
text/xml
application/xml
application/xhtml+xml
image/svg+xml
A value of text/html
will invoke the HTML parser, and the method will return an {{domxref("HTMLDocument")}}.
+ Any other valid value will invoke the XML parser, and the method will return an {{domxref("XMLDocument")}}.
Any other value will cause a TypeError
to be thrown.
An {{domxref("HTMLDocument")}} or an {{domxref("XMLDocument")}}, depending on the
+ mimeType
argument.
This example shows how to parse XML, SVG, and HTML. Note that a MIME type of
+text/html
will invoke the HTML parser, and any other valid MIME type will invoke
+the XML parser.
const parser = new DOMParser(); + +const xmlString = "<warning>Beware of the tiger</warning>"; +const doc1 = parser.parseFromString(xmlString, "application/xml"); +// XMLDocument + +const svgString = "<circle cx=\"50\" cy=\"50\" r=\"50\"/>"; +const doc2 = parser.parseFromString(svgString, "image/svg+xml"); +// XMLDocument + +const htmlString = "<strong>Beware of the leopard</strong>"; +const doc3 = parser.parseFromString(htmlString, "text/html"); +// HTMLDocument + +console.log(doc1.documentElement.textContent) +// "Beware of the tiger" + +console.log(doc2.firstChild.tagName); +// "circle" + +console.log(doc3.body.firstChild.textContent); +// "Beware of the leopard" ++ +
Note that if the XML parser is used and parsing fails, the DOMParser
does not throw an
+ exception, but instead returns an error document:
const parser = new DOMParser(); + +const xmlString = "<warning>Beware of the missing closing tag"; +const doc = parser.parseFromString(xmlString, "application/xml"); + +console.log(doc.documentElement.tagName); +// parsererror + +console.log(doc.documentElement.textContent); +// XML Parsing Error: no element found ++ +
The parsing error may also be reported to the browser's JavaScript console.
+ +Specification | +Status | +Comment | +
---|---|---|
{{SpecName('HTML WHATWG', '#dom-domparser-parsefromstring', 'parseFromString()')}} + | +{{Spec2('HTML WHATWG')}} | ++ |
{{Compat("api.DOMParser.parseFromString")}}
+ +