Skip to content
Snippets Groups Projects
Commit af0e54e1 authored by Chris Beer's avatar Chris Beer
Browse files

Guard against annotation targets without fragments

parent 49f4af63
No related branches found
No related tags found
No related merge requests found
...@@ -118,6 +118,10 @@ describe('AnnotationItem', () => { ...@@ -118,6 +118,10 @@ describe('AnnotationItem', () => {
expect(new AnnotationItem({ target: 'www.example.com/#xywh=10,10,100,200' }) expect(new AnnotationItem({ target: 'www.example.com/#xywh=10,10,100,200' })
.fragmentSelector).toEqual([10, 10, 100, 200]); .fragmentSelector).toEqual([10, 10, 100, 200]);
}); });
it('url without a fragment', () => {
expect(new AnnotationItem({ target: 'www.example.com' })
.fragmentSelector).toEqual(null);
});
}); });
describe('svgSelector', () => { describe('svgSelector', () => {
it('simple string', () => { it('simple string', () => {
... ...
......
...@@ -151,6 +151,11 @@ describe('AnnotationResource', () => { ...@@ -151,6 +151,11 @@ describe('AnnotationResource', () => {
expect(new AnnotationResource({ on: { selector: { '@type': 'oa:Choice', default: { value: 'www.example.com/#xywh=10,10,100,200' } } } }) expect(new AnnotationResource({ on: { selector: { '@type': 'oa:Choice', default: { value: 'www.example.com/#xywh=10,10,100,200' } } } })
.fragmentSelector).toEqual([10, 10, 100, 200]); .fragmentSelector).toEqual([10, 10, 100, 200]);
}); });
it('url without a fragment', () => {
expect(new AnnotationResource({ on: { selector: { value: 'www.example.com' } } })
.fragmentSelector).toEqual(null);
});
}); });
describe('svgSelector', () => { describe('svgSelector', () => {
it('simple string', () => { it('simple string', () => {
... ...
......
...@@ -104,13 +104,19 @@ export default class AnnotationItem { ...@@ -104,13 +104,19 @@ export default class AnnotationItem {
get fragmentSelector() { get fragmentSelector() {
const { selector } = this; const { selector } = this;
let match;
switch (typeof selector) { switch (typeof selector) {
case 'string': case 'string':
return selector.match(/xywh=(.*)$/)[1].split(',').map(str => parseInt(str, 10)); match = selector.match(/xywh=(.*)$/);
break;
case 'object': case 'object':
return selector.value.match(/xywh=(.*)$/)[1].split(',').map(str => parseInt(str, 10)); match = selector.value.match(/xywh=(.*)$/);
break;
default: default:
return null; return null;
} }
return match && match[1].split(',').map(str => parseInt(str, 10));
} }
} }
...@@ -105,13 +105,19 @@ export default class AnnotationResource { ...@@ -105,13 +105,19 @@ export default class AnnotationResource {
get fragmentSelector() { get fragmentSelector() {
const { selector } = this; const { selector } = this;
let match;
switch (typeof selector) { switch (typeof selector) {
case 'string': case 'string':
return selector.match(/xywh=(.*)$/)[1].split(',').map(str => parseInt(str, 10)); match = selector.match(/xywh=(.*)$/);
break;
case 'object': case 'object':
return selector.value.match(/xywh=(.*)$/)[1].split(',').map(str => parseInt(str, 10)); match = selector.value.match(/xywh=(.*)$/);
break;
default: default:
return null; return null;
} }
return match && match[1].split(',').map(str => parseInt(str, 10));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment