Commit 30160267 authored by Sean Hammond's avatar Sean Hammond Committed by Robert Knight

Read query from the host page in settings.query()

Add code to the `settings.query()` method so that, instead of reading
the setting from the URL only, it reads the query setting from the
`js-hypothesis-config` scripts in the host page or, failing that, tries
to read it from the URL.

In `index.js`, after `settings.query()` is called, there are two
`Object.assign()` calls that overwrite values already in the `config`
objects with any returned from `js-hypothesis-config` scripts or the
`window.hypothesisConfig()` function in the host page. So a
`config.query` setting from `settings.query()` already gets overwritten
by a `query` setting in a `js-hypothesis-config` script or
`hypothesisConfig()` function.

This commit makes that overwriting explicit, encapsulates it in the
`query()` method, and unit tests it.

Note that the two `Object.assign()`s are still present in `index.js` so
the `config.query` setting returned by the `query()` method still gets
overwritten by them, but this now has no effect as the `query()` method
will already have set `config.query` to the same value that it will be
overwritten with.

In a future commit the `Object.assign()`s will be removed and there
won't be any overwriting.

Note that **this will change the behaviour of the code** once the
`Object.assign()`s have been removed: the `query()` method reads the
query setting from `js-hypothesis-config` scripts but does not read it
from `window.hypothesisConfig()` any longer. I don't believe it's
necessary to read `query` from `hypothesisConfig()`.
parent 5a2181e3
......@@ -56,24 +56,34 @@ function settingsFrom(window_) {
}
/**
* Return the `#annotations:query:*` query from the given URL's fragment.
* Return the config.query setting from the host page or from the URL.
*
* If the URL contains a `#annotations:query:*` (or `#annotatons:q:*`) fragment
* then return a the query part extracted from the fragment.
* Otherwise return `null`.
* If the host page contains a js-hypothesis-config script containing a
* query setting then return that.
*
* @return {string|null} - The extracted query, or null.
* Otherwise if the host page's URL has a `#annotations:query:*` (or
* `#annotations:q:*`) fragment then return the query value from that.
*
* Otherwise return null.
*
* @return {string|null} - The config.query setting, or null.
*/
function query() {
var queryFragmentMatch = window_.location.href.match(/#annotations:(query|q):(.+)$/i);
if (queryFragmentMatch) {
try {
return decodeURI(queryFragmentMatch[2]);
} catch (err) {
// URI Error should return the page unfiltered.
/** Return the query from the URL, or null. */
function queryFromURL() {
var queryFragmentMatch = window_.location.href.match(/#annotations:(query|q):(.+)$/i);
if (queryFragmentMatch) {
try {
return decodeURI(queryFragmentMatch[2]);
} catch (err) {
// URI Error should return the page unfiltered.
}
}
return null;
}
return null;
return jsonConfigs.query || queryFromURL();
}
function hostPageSetting(name) {
......
......@@ -156,6 +156,24 @@ describe('annotator.config.settingsFrom', function() {
});
describe('#query', function() {
context('when the host page has a js-hypothesis-config with a query setting', function() {
beforeEach('add a js-hypothesis-config query setting', function() {
fakeSharedSettings.jsonConfigsFrom.returns({query: 'queryFromJSON'});
});
it('returns the query from the js-hypothesis-config script', function() {
assert.equal(settingsFrom(fakeWindow()).query, 'queryFromJSON');
});
context("when there's also a query in the URL fragment", function() {
specify('js-hypothesis-config queries override URL ones', function() {
var window_ = fakeWindow('http://localhost:3000#annotations:query:queryFromUrl');
assert.equal(settingsFrom(window_).query, 'queryFromJSON');
});
});
});
[
{
describe: "when there's a #annotations:query:<QUERY> fragment",
......
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