Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Firefox 9 not implemented exception on DOMParser. #168

Closed
erichmenge opened this issue Jan 17, 2013 · 3 comments · Fixed by #175
Closed

Firefox 9 not implemented exception on DOMParser. #168

erichmenge opened this issue Jan 17, 2013 · 3 comments · Fixed by #175

Comments

@erichmenge
Copy link

https://github.com/rails/turbolinks/blob/master/lib/assets/javascripts/turbolinks.js.coffee#L156
Relates to #39

Throws a not implemented exception on Firefox 9.

I'm using Turbolinks.visit in a part of an application and a Firefox 9 user reported that the button had no effect. Upon investigation I discovered this exception.

http://caniuse.com/#feat=history doesn't show Firefox 9 being able to use pushState, but it appears to be able in my tests of it.

If you look at http://stackoverflow.com/questions/9250545/javascript-domparser-access-innerhtml-and-other-properties/9251106#9251106 there is a "fix" for this issue:

/* 
 * DOMParser HTML extension 
 * 2012-02-02 
 * 
 * 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 DOMParser_proto = DOMParser.prototype  
      , real_parseFromString = DOMParser_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) {}  

    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));

I'm happy to do a PR with this type of implementation if there aren't any better solutions.

@erichmenge
Copy link
Author

The other thought I have is that maybe visit() should be wrapped in a try/catch so that if anything fails we can still get the user on his or her way. This likely happens anyway for a normal link, but if you're calling Turbolinks.visit() within a script the user may end up clicking on something and getting no response.

@erichmenge
Copy link
Author

Heh, don't recall closing this issue. Not sure how that happened.

@msievers
Copy link

I have a similar problem with current Opera 12.12 so I took the MDN code and moved it to CoffeeScript. PR is pending (#174).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants