Handle DOMParser exception #175

Merged
merged 2 commits into from Feb 3, 2013

Conversation

Projects
None yet
3 participants
@reed
Collaborator

reed commented Jan 29, 2013

  • Fixes #168
  • Alternative to #174
  • Implemented without overloading DOMParser
  • Tested on:
    • Firefox 9
    • Firefox 18.0.1
    • Opera 12.12
    • Chrome 24.0.1312.56
    • Safari 6.0
@dhh

This comment has been minimized.

Show comment Hide comment
@dhh

dhh Jan 29, 2013

Contributor

Looking good but let's get some comments in there for the various tests explaining what they're intended for.

Contributor

dhh commented Jan 29, 2013

Looking good but let's get some comments in there for the various tests explaining what they're intended for.

@reed

This comment has been minimized.

Show comment Hide comment
@reed

reed Jan 29, 2013

Collaborator

Updated with comments.

Collaborator

reed commented Jan 29, 2013

Updated with comments.

dhh added a commit that referenced this pull request Feb 3, 2013

@dhh dhh merged commit 16d3c85 into turbolinks:master Feb 3, 2013

@cmer

This comment has been minimized.

Show comment Hide comment
@cmer

cmer Feb 18, 2013

Not sure if this helps, but here's the workaround we've been using to fix the DOMParser in Opera (sorry for pure JS):

// Fix DOMParser (used in Turbolink) under Opera.
// Source: http://stackoverflow.com/questions/9500318/troubles-trying-to-parse-an-html-string-with-domparser
if (typeof(DOMParser) !== "undefined") {
    (function(DOMParser) {  
        "use strict";  
        var DOMParser_proto = DOMParser.prototype  
          , real_parseFromString = DOMParser_proto.parseFromString;

        // Test if parsing will throw an error. If so, it's DAMN broken!
        var broken = false;
        try {  
            (new DOMParser).parseFromString("", "text/html");
        } catch (ex) {
            broken = true;
        }

        if (broken) {
            // Patching DOMParser

            DOMParser_proto.parseFromString = function(markup, type) {  
                if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {  
                    var doc = document.implementation.createHTMLDocument("")
                      , doc_elt = doc.documentElement
                      , first_elt;

                    doc_elt.innerHTML = markup;
                    first_elt = doc_elt.firstElementChild;

                    if (doc_elt.childElementCount === 1
                        && first_elt.localName.toLowerCase() === "html") {  
                        doc.replaceChild(first_elt, doc_elt);  
                    }  

                    return doc;  
                } else {  
                    return real_parseFromString.apply(this, arguments);  
                }  
            };  
        }
    }(DOMParser));
}

cmer commented on b853169 Feb 18, 2013

Not sure if this helps, but here's the workaround we've been using to fix the DOMParser in Opera (sorry for pure JS):

// Fix DOMParser (used in Turbolink) under Opera.
// Source: http://stackoverflow.com/questions/9500318/troubles-trying-to-parse-an-html-string-with-domparser
if (typeof(DOMParser) !== "undefined") {
    (function(DOMParser) {  
        "use strict";  
        var DOMParser_proto = DOMParser.prototype  
          , real_parseFromString = DOMParser_proto.parseFromString;

        // Test if parsing will throw an error. If so, it's DAMN broken!
        var broken = false;
        try {  
            (new DOMParser).parseFromString("", "text/html");
        } catch (ex) {
            broken = true;
        }

        if (broken) {
            // Patching DOMParser

            DOMParser_proto.parseFromString = function(markup, type) {  
                if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {  
                    var doc = document.implementation.createHTMLDocument("")
                      , doc_elt = doc.documentElement
                      , first_elt;

                    doc_elt.innerHTML = markup;
                    first_elt = doc_elt.firstElementChild;

                    if (doc_elt.childElementCount === 1
                        && first_elt.localName.toLowerCase() === "html") {  
                        doc.replaceChild(first_elt, doc_elt);  
                    }  

                    return doc;  
                } else {  
                    return real_parseFromString.apply(this, arguments);  
                }  
            };  
        }
    }(DOMParser));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment