Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
70f69edc
Commit
70f69edc
authored
Jun 08, 2017
by
Robert Knight
Committed by
GitHub
Jun 08, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #427 from hypothesis/dont-throw-on-invalid-json
Don't throw on invalid JSON
parents
a4e57347
a6869f48
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
40 deletions
+36
-40
index.js
src/annotator/config/index.js
+1
-8
index-test.js
src/annotator/config/test/index-test.js
+0
-25
settings.js
src/shared/settings.js
+10
-2
settings-test.js
src/shared/test/settings-test.js
+25
-5
No files found.
src/annotator/config/index.js
View file @
70f69edc
...
@@ -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
try
{
Object
.
assign
(
config
,
sharedSettings
.
jsonConfigsFrom
(
window_
.
document
));
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
...
...
src/annotator/config/test/index-test.js
View file @
70f69edc
...
@@ -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
();
...
...
src/shared/settings.js
View file @
70f69edc
...
@@ -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
;
}
}
...
...
src/shared/test/settings-test.js
View file @
70f69edc
...
@@ -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'
});
});
});
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment