summaryrefslogtreecommitdiffstats
path: root/toolkit/components/microformats/test/lib/domparser.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/components/microformats/test/lib/domparser.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/components/microformats/test/lib/domparser.js')
-rw-r--r--toolkit/components/microformats/test/lib/domparser.js103
1 files changed, 103 insertions, 0 deletions
diff --git a/toolkit/components/microformats/test/lib/domparser.js b/toolkit/components/microformats/test/lib/domparser.js
new file mode 100644
index 000000000..bc342634a
--- /dev/null
+++ b/toolkit/components/microformats/test/lib/domparser.js
@@ -0,0 +1,103 @@
+
+// Based on https://gist.github.com/1129031 By Eli Grey, http://eligrey.com - Public domain.
+
+// DO NOT use https://developer.mozilla.org/en-US/docs/Web/API/DOMParser example polyfill
+// as it does not work with earlier versions of Chrome
+
+
+(function(DOMParser) {
+ 'use strict';
+
+ var DOMParser_proto;
+ var real_parseFromString;
+ var textHTML; // Flag for text/html support
+ var textXML; // Flag for text/xml support
+ var htmlElInnerHTML; // Flag for support for setting html element's innerHTML
+
+ // Stop here if DOMParser not defined
+ if (!DOMParser) {
+ return;
+ }
+
+ // Firefox, Opera and IE throw errors on unsupported types
+ try {
+ // WebKit returns null on unsupported types
+ textHTML = !!(new DOMParser()).parseFromString('', 'text/html');
+
+ } catch (er) {
+ textHTML = false;
+ }
+
+ // If text/html supported, don't need to do anything.
+ if (textHTML) {
+ return;
+ }
+
+ // Next try setting innerHTML of a created document
+ // IE 9 and lower will throw an error (can't set innerHTML of its HTML element)
+ try {
+ var doc = document.implementation.createHTMLDocument('');
+ doc.documentElement.innerHTML = '<title></title><div></div>';
+ htmlElInnerHTML = true;
+
+ } catch (er) {
+ htmlElInnerHTML = false;
+ }
+
+ // If if that failed, try text/xml
+ if (!htmlElInnerHTML) {
+
+ try {
+ textXML = !!(new DOMParser()).parseFromString('', 'text/xml');
+
+ } catch (er) {
+ textHTML = false;
+ }
+ }
+
+ // Mess with DOMParser.prototype (less than optimal...) if one of the above worked
+ // Assume can write to the prototype, if not, make this a stand alone function
+ if (DOMParser.prototype && (htmlElInnerHTML || textXML)) {
+ DOMParser_proto = DOMParser.prototype;
+ real_parseFromString = DOMParser_proto.parseFromString;
+
+ DOMParser_proto.parseFromString = function (markup, type) {
+
+ // Only do this if type is text/html
+ if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
+ var doc, doc_el, first_el;
+
+ // Use innerHTML if supported
+ if (htmlElInnerHTML) {
+ doc = document.implementation.createHTMLDocument('');
+ doc_el = doc.documentElement;
+ doc_el.innerHTML = markup;
+ first_el = doc_el.firstElementChild;
+
+ // Otherwise use XML method
+ } else if (textXML) {
+
+ // Make sure markup is wrapped in HTML tags
+ // Should probably allow for a DOCTYPE
+ if (!(/^<html.*html>$/i.test(markup))) {
+ markup = '<html>' + markup + '<\/html>';
+ }
+ doc = (new DOMParser()).parseFromString(markup, 'text/xml');
+ doc_el = doc.documentElement;
+ first_el = doc_el.firstElementChild;
+ }
+
+ // Is this an entire document or a fragment?
+ if (doc_el.childElementCount === 1 && first_el.localName.toLowerCase() === 'html') {
+ doc.replaceChild(first_el, doc_el);
+ }
+
+ return doc;
+
+ // If not text/html, send as-is to host method
+ } else {
+ return real_parseFromString.apply(this, arguments);
+ }
+ };
+ }
+}(DOMParser));