Support for FF's buggy location.hash

FF decodes location.hash for you, so decoding is unnecessary and breaks
when the regex contains a "%"

Fixes #12
This commit is contained in:
Jeff Avallone 2015-06-16 20:19:04 -04:00
parent 06a7ffc110
commit e1f5891a3a
2 changed files with 15 additions and 1 deletions

View File

@ -46,6 +46,7 @@ window._gaq = (typeof _gaq !== 'undefined') ? _gaq : {
if (document.body.querySelector('#content .application')) { if (document.body.querySelector('#content .application')) {
var regexper = new Regexper(document.body); var regexper = new Regexper(document.body);
regexper.detectBuggyHash();
regexper.bindListeners(); regexper.bindListeners();
util.tick().then(() => { util.tick().then(() => {

View File

@ -8,6 +8,7 @@ import _ from 'lodash';
export default class Regexper { export default class Regexper {
constructor(root) { constructor(root) {
this.root = root; this.root = root;
this.buggyHash = false;
this.form = root.querySelector('#regexp-form'); this.form = root.querySelector('#regexp-form');
this.field = root.querySelector('#regexp-input'); this.field = root.querySelector('#regexp-input');
this.error = root.querySelector('#error'); this.error = root.querySelector('#error');
@ -82,6 +83,16 @@ export default class Regexper {
window.addEventListener('hashchange', this.hashchangeListener.bind(this)); window.addEventListener('hashchange', this.hashchangeListener.bind(this));
} }
// Detect if https://bugzilla.mozilla.org/show_bug.cgi?id=483304 is in effect
detectBuggyHash() {
var url;
if (typeof window.URL !== 'undefined') {
url = new URL('http://regexper.com/#%25');
this.buggyHash = (url.hash === '#%');
}
}
// Set the URL hash. This method exists to facilitate automated testing // Set the URL hash. This method exists to facilitate automated testing
// (since changing the URL can throw off most JavaScript testing tools). // (since changing the URL can throw off most JavaScript testing tools).
_setHash(hash) { _setHash(hash) {
@ -92,8 +103,10 @@ export default class Regexper {
// automated testing, but also does some basic error handling for malformed // automated testing, but also does some basic error handling for malformed
// URLs. // URLs.
_getHash() { _getHash() {
var hash;
try { try {
return decodeURIComponent(location.hash.slice(1)); hash = location.hash.slice(1)
return this.buggyHash ? hash : decodeURIComponent(hash);
} }
catch(e) { catch(e) {
return e; return e;