Commit 5a2181e3 authored by Sean Hammond's avatar Sean Hammond Committed by Robert Knight

Add hostPageSetting() method (not used yet)

Add a new `annotator.config.settingsFrom#hostPageSetting(settingName)`
method that encapsulates a bunch of logic in one method:

- It always returns null if the client is from a browser extension.
  Browser extensions don't read settings from the host page.
- If the host page's `window.hypothesisConfig()` function returned a
  value for the requested setting, `hostPageSetting(settingName)` returns
  that value.
- If `hypothesisConfig()` doesn't return the setting it looks for it in
  `js-hypothesis-config` scripts in the host page
- If the setting isn't defined in either place it returns `undefined`.
parent 6d9bd921
'use strict';
var configFuncSettingsFrom = require('./config-func-settings-from');
var isBrowserExtension = require('./is-browser-extension');
var sharedSettings = require('../../shared/settings');
function settingsFrom(window_) {
var jsonConfigs = sharedSettings.jsonConfigsFrom(window_.document);
var configFuncSettings = configFuncSettingsFrom(window_);
/**
* Return the href URL of the first annotator link in the given document.
*
......@@ -69,10 +76,23 @@ function settingsFrom(window_) {
return null;
}
function hostPageSetting(name) {
if (isBrowserExtension(app())) {
return null;
}
if (configFuncSettings.hasOwnProperty(name)) {
return configFuncSettings[name];
}
return jsonConfigs[name];
}
return {
get app() { return app(); },
get annotations() { return annotations(); },
get query() { return query(); },
hostPageSetting: hostPageSetting,
};
}
......
'use strict';
var settingsFrom = require('../settings');
var proxyquire = require('proxyquire');
var util = require('../../../shared/test/util');
var fakeConfigFuncSettingsFrom = sinon.stub();
var fakeIsBrowserExtension = sinon.stub();
var fakeSharedSettings = {};
var settingsFrom = proxyquire('../settings', util.noCallThru({
'./config-func-settings-from': fakeConfigFuncSettingsFrom,
'./is-browser-extension': fakeIsBrowserExtension,
'../../shared/settings': fakeSharedSettings,
}));
describe('annotator.config.settingsFrom', function() {
beforeEach('reset fakeConfigFuncSettingsFrom', function() {
fakeConfigFuncSettingsFrom.reset();
fakeConfigFuncSettingsFrom.returns({});
});
beforeEach('reset fakeIsBrowserExtension', function() {
fakeIsBrowserExtension.reset();
fakeIsBrowserExtension.returns(false);
});
beforeEach('reset fakeSharedSettings', function() {
fakeSharedSettings.jsonConfigsFrom = sinon.stub().returns({});
});
describe('#app', function() {
function appendLinkToDocument(href) {
var link = document.createElement('link');
......@@ -88,6 +113,9 @@ describe('annotator.config.settingsFrom', function() {
location: {
href: href,
},
document: {
querySelector: sinon.stub().returns({href: 'hi'}),
},
};
}
......@@ -201,4 +229,71 @@ describe('annotator.config.settingsFrom', function() {
});
});
});
describe('#hostPageSetting', function() {
context('when the client is from a browser extension', function() {
beforeEach('configure a browser extension client', function() {
fakeIsBrowserExtension.returns(true);
});
it('always returns null', function() {
// These settings in the host page should be ignored.
fakeConfigFuncSettingsFrom.returns({foo: 'bar'});
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'});
assert.isNull(settingsFrom(fakeWindow()).hostPageSetting('foo'));
});
});
context('when the client is embedded in a web page', function() {
beforeEach('configure an embedded client', function() {
fakeIsBrowserExtension.returns(false);
});
it('returns setting values from window.hypothesisConfig()', function() {
fakeConfigFuncSettingsFrom.returns({foo: 'bar'});
assert.equal(settingsFrom(fakeWindow()).hostPageSetting('foo'), 'bar');
});
it('returns setting values from js-hypothesis-config scripts', function() {
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'});
assert.equal(settingsFrom(fakeWindow()).hostPageSetting('foo'), 'bar');
});
specify('hypothesisConfig() overrides js-hypothesis-config', function() {
fakeConfigFuncSettingsFrom.returns({
foo: 'fooFromHypothesisConfig',
});
fakeSharedSettings.jsonConfigsFrom.returns({
foo: 'fooFromJsHypothesisConfigScript',
});
assert.equal(
settingsFrom(fakeWindow()).hostPageSetting('foo'),
'fooFromHypothesisConfig'
);
});
[
null,
undefined,
].forEach(function(returnValue) {
specify('even a ' + returnValue + ' from hypothesisConfig() overrides js-hypothesis-configs', function() {
fakeConfigFuncSettingsFrom.returns({foo: returnValue});
fakeSharedSettings.jsonConfigsFrom.returns({foo: 'bar'});
assert.equal(settingsFrom(fakeWindow()).hostPageSetting('foo'), returnValue);
});
});
it("returns undefined if the setting isn't defined anywhere", function() {
fakeConfigFuncSettingsFrom.returns({});
fakeSharedSettings.jsonConfigsFrom.returns({});
assert.isUndefined(settingsFrom(fakeWindow()).hostPageSetting('foo'));
});
});
});
});
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