Commit 70f69edc authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #427 from hypothesis/dont-throw-on-invalid-json

Don't throw on invalid JSON
parents a4e57347 a6869f48
...@@ -36,14 +36,7 @@ function configFrom(window_) { ...@@ -36,14 +36,7 @@ function configFrom(window_) {
return config; return config;
} }
// Parse config from `<script class="js-hypothesis-config">` tags Object.assign(config, sharedSettings.jsonConfigsFrom(window_.document));
try {
Object.assign(config, sharedSettings.jsonConfigsFrom(window_.document));
} catch (err) {
console.warn('Could not parse settings from js-hypothesis-config tags',
err);
}
Object.assign(config, settings.configFuncSettingsFrom(window_)); Object.assign(config, settings.configFuncSettingsFrom(window_));
// Convert legacy keys/values in config to corresponding current // Convert legacy keys/values in config to corresponding current
......
...@@ -10,7 +10,6 @@ var configFrom = proxyquire('../index', util.noCallThru({ ...@@ -10,7 +10,6 @@ var configFrom = proxyquire('../index', util.noCallThru({
'./settings': fakeSettings, './settings': fakeSettings,
'../../shared/settings': fakeSharedSettings, '../../shared/settings': fakeSharedSettings,
})); }));
var sandbox = sinon.sandbox.create();
function fakeWindow() { function fakeWindow() {
return { return {
...@@ -20,10 +19,6 @@ function fakeWindow() { ...@@ -20,10 +19,6 @@ function fakeWindow() {
} }
describe('annotator.config', function() { describe('annotator.config', function() {
beforeEach('stub console.warn()', function() {
sandbox.stub(console, 'warn');
});
beforeEach('reset fakeSharedSettings', function() { beforeEach('reset fakeSharedSettings', function() {
fakeSharedSettings.jsonConfigsFrom = sinon.stub().returns({}); fakeSharedSettings.jsonConfigsFrom = sinon.stub().returns({});
}); });
...@@ -35,10 +30,6 @@ describe('annotator.config', function() { ...@@ -35,10 +30,6 @@ describe('annotator.config', function() {
fakeSettings.configFuncSettingsFrom = sinon.stub().returns({}); fakeSettings.configFuncSettingsFrom = sinon.stub().returns({});
}); });
afterEach('reset the sandbox', function() {
sandbox.restore();
});
it('gets the config.app setting', function() { it('gets the config.app setting', function() {
var window_ = fakeWindow(); var window_ = fakeWindow();
...@@ -89,22 +80,6 @@ describe('annotator.config', function() { ...@@ -89,22 +80,6 @@ describe('annotator.config', function() {
}); });
}); });
context('when jsonConfigsFrom() throws an error', function() {
beforeEach(function() {
fakeSharedSettings.jsonConfigsFrom.throws();
});
it('catches the error', function() {
configFrom(fakeWindow());
});
it('logs a warning', function() {
configFrom(fakeWindow());
assert.called(console.warn);
});
});
it('gets config settings from window.hypothesisConfig()', function() { it('gets config settings from window.hypothesisConfig()', function() {
var window_ = fakeWindow(); var window_ = fakeWindow();
......
...@@ -28,13 +28,21 @@ function assign(dest, src) { ...@@ -28,13 +28,21 @@ function assign(dest, src) {
* @param {Document|Element} document - The root element to search. * @param {Document|Element} document - The root element to search.
*/ */
function jsonConfigsFrom(document) { function jsonConfigsFrom(document) {
var config = {};
var settingsElements = var settingsElements =
document.querySelectorAll('script.js-hypothesis-config'); document.querySelectorAll('script.js-hypothesis-config');
var config = {};
for (var i=0; i < settingsElements.length; i++) { for (var i=0; i < settingsElements.length; i++) {
assign(config, JSON.parse(settingsElements[i].textContent)); var settings;
try {
settings = JSON.parse(settingsElements[i].textContent);
} catch (err) {
console.warn('Could not parse settings from js-hypothesis-config tags', err);
settings = {};
}
assign(config, settings);
} }
return config; return config;
} }
......
...@@ -2,7 +2,14 @@ ...@@ -2,7 +2,14 @@
var settings = require('../settings'); var settings = require('../settings');
var sandbox = sinon.sandbox.create();
describe('settings', function () { describe('settings', function () {
afterEach('reset the sandbox', function() {
sandbox.restore();
});
describe('#jsonConfigsFrom', function() { describe('#jsonConfigsFrom', function() {
var jsonConfigsFrom = settings.jsonConfigsFrom; var jsonConfigsFrom = settings.jsonConfigsFrom;
...@@ -64,15 +71,28 @@ describe('settings', function () { ...@@ -64,15 +71,28 @@ describe('settings', function () {
}); });
context("when there's a JSON script containing invalid JSON", function() { context("when there's a JSON script containing invalid JSON", function() {
beforeEach('stub console.warn()', function() {
sandbox.stub(console, 'warn');
});
beforeEach('add a JSON script containing invalid JSON', function() { beforeEach('add a JSON script containing invalid JSON', function() {
appendJSHypothesisConfig(document, 'this is not valid json'); appendJSHypothesisConfig(document, 'this is not valid json');
}); });
it('throws a SyntaxError', function() { it('logs a warning', function() {
assert.throws( jsonConfigsFrom(document);
function() { jsonConfigsFrom(document); },
SyntaxError assert.called(console.warn);
); });
it('returns {}', function() {
assert.deepEqual(jsonConfigsFrom(document), {});
});
it('still returns settings from other JSON scripts', function() {
appendJSHypothesisConfig(document, '{"foo": "FOO", "bar": "BAR"}');
assert.deepEqual(jsonConfigsFrom(document), {foo: 'FOO', bar: 'BAR'});
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment