From 1484b06da61a886a6517c34f0b0b2d610cb557c1 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Thu, 26 Nov 2015 13:54:47 +0000 Subject: [PATCH] Avoid changing the writability of the 'href' attr of Wombat overrides document.baseURI and .href in order to return the original URL rather than the proxied URL. The .href override however ended up making a writable attribute read-only, which could trigger script errors in strict-mode JS. Fix this by avoiding replacing the setter for a DOM property if no replacement setter is provided. Fixes an error loading Hypothesis under Microsoft Edge. --- karma-tests/wombat.spec.js | 97 +++++++++++++++++++++++--------------- pywb/static/wombat.js | 15 ++++-- 2 files changed, 69 insertions(+), 43 deletions(-) diff --git a/karma-tests/wombat.spec.js b/karma-tests/wombat.spec.js index 8bebb8de..62fe9f72 100644 --- a/karma-tests/wombat.spec.js +++ b/karma-tests/wombat.spec.js @@ -121,45 +121,66 @@ describe('WombatJS', function () { }, done); }); - it('should rewrite document.baseURI', function (done) { - runWombatTest({ - initScript: function () { - wbinfo = { - wombat_opts: {}, - prefix: window.location.origin, - wombat_ts: '', - }; - }, - wombatScript: wombatScript, - testScript: function () { - var baseURI = document.baseURI; - if (typeof baseURI !== 'string') { - throw new Error('baseURI is not a string'); - } - if (domTests.areDOMPropertiesConfigurable()) { - assert.equal(baseURI, 'http:///dummy.html'); - } - }, - }, done); + describe('anchor rewriting', function () { + it('should rewrite links in dynamically injected tags', function (done) { + runWombatTest({ + initScript: function () { + wbinfo = { + wombat_opts: {}, + prefix: window.location.origin, + wombat_ts: '', + }; + }, + wombatScript: wombatScript, + html: 'A link', + testScript: function () { + var link = document.getElementById('link'); + if (domTests.areDOMPropertiesConfigurable()) { + assert.equal(link.href, 'http:///foobar.html'); + } + }, + }, done); + }); }); - it('should rewrite links in dynamically injected tags', function (done) { - runWombatTest({ - initScript: function () { - wbinfo = { - wombat_opts: {}, - prefix: window.location.origin, - wombat_ts: '', - }; - }, - wombatScript: wombatScript, - html: 'A link', - testScript: function () { - var link = document.getElementById('link'); - if (domTests.areDOMPropertiesConfigurable()) { - assert.equal(link.href, 'http:///foobar.html'); - } - }, - }, done); + describe('base URL overrides', function () { + it('document.baseURI should return the original URL', function (done) { + runWombatTest({ + initScript: function () { + wbinfo = { + wombat_opts: {}, + prefix: window.location.origin, + wombat_ts: '', + }; + }, + wombatScript: wombatScript, + testScript: function () { + var baseURI = document.baseURI; + if (typeof baseURI !== 'string') { + throw new Error('baseURI is not a string'); + } + if (domTests.areDOMPropertiesConfigurable()) { + assert.equal(baseURI, 'http:///dummy.html'); + } + }, + }, done); + }); + + it('should allow base.href to be assigned', function (done) { + runWombatTest({ + initScript: function () { + wbinfo = { + wombat_opts: {}, + }; + }, + wombatScript: wombatScript, + testScript: function () { + 'use strict'; + var baseElement = document.createElement('base'); + baseElement.href = 'http://foobar.com/base'; + assert.equal(baseElement.href, 'http://foobar.com/base'); + }, + }, done); + }); }); }); diff --git a/pywb/static/wombat.js b/pywb/static/wombat.js index 62f9bbb6..09ad23c4 100644 --- a/pywb/static/wombat.js +++ b/pywb/static/wombat.js @@ -379,11 +379,16 @@ var wombat_internal = function($wbwindow) { } try { - Object.defineProperty(obj, prop, { - configurable: false, - set: set_func, - get: get_func - }); + var descriptor = { + configurable: true, + get: get_func, + }; + + if (set_func) { + descriptor.set = set_func; + } + + Object.defineProperty(obj, prop, descriptor); return true; } catch (e) {