From e1f5891a3aac1cb66c71e3c70afbac743bbb41a0 Mon Sep 17 00:00:00 2001 From: Jeff Avallone Date: Tue, 16 Jun 2015 20:19:04 -0400 Subject: [PATCH] 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 --- src/js/main.js | 1 + src/js/regexper.js | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/js/main.js b/src/js/main.js index 33823b6..fce746b 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -46,6 +46,7 @@ window._gaq = (typeof _gaq !== 'undefined') ? _gaq : { if (document.body.querySelector('#content .application')) { var regexper = new Regexper(document.body); + regexper.detectBuggyHash(); regexper.bindListeners(); util.tick().then(() => { diff --git a/src/js/regexper.js b/src/js/regexper.js index 209620d..3057d81 100644 --- a/src/js/regexper.js +++ b/src/js/regexper.js @@ -8,6 +8,7 @@ import _ from 'lodash'; export default class Regexper { constructor(root) { this.root = root; + this.buggyHash = false; this.form = root.querySelector('#regexp-form'); this.field = root.querySelector('#regexp-input'); this.error = root.querySelector('#error'); @@ -82,6 +83,16 @@ export default class Regexper { 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 // (since changing the URL can throw off most JavaScript testing tools). _setHash(hash) { @@ -92,8 +103,10 @@ export default class Regexper { // automated testing, but also does some basic error handling for malformed // URLs. _getHash() { + var hash; try { - return decodeURIComponent(location.hash.slice(1)); + hash = location.hash.slice(1) + return this.buggyHash ? hash : decodeURIComponent(hash); } catch(e) { return e;