Skip to content

Commit

Permalink
Support multiple selectors for WebAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mejackreed committed Jul 24, 2020
1 parent 5638127 commit 56b4409
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
12 changes: 11 additions & 1 deletion __tests__/src/lib/AnnotationItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ describe('AnnotationItem', () => {
it('returns the on string (for simple fragment selector)', () => {
expect(new AnnotationItem({ target: 'yolo' }).selector).toEqual('yolo');
});
it('returns objects wrapped in an array', () => {
expect(new AnnotationItem({ target: { selector: 'yolo' } }).selector).toEqual(['yolo']);
});
it('handles multiple selectors', () => {
expect(new AnnotationItem({ target: { selector: ['yolo', 'foo'] } }).selector).toEqual(['yolo', 'foo']);
});
});
describe('chars', () => {
it('with no resource', () => {
Expand All @@ -124,6 +130,10 @@ describe('AnnotationItem', () => {
expect(new AnnotationItem({ target: 'www.example.com/#xywh=10,10,100,200' })
.fragmentSelector).toEqual([10, 10, 100, 200]);
});
it('multiple selectors', () => {
expect(new AnnotationItem({ target: { selector: [{ type: 'FragmentSelector', value: 'www.example.com/#xywh=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);
Expand All @@ -142,7 +152,7 @@ describe('AnnotationItem', () => {

it('without specified type', () => {
expect(new AnnotationItem({ target: { selector: { } } })
.svgSelector).toEqual(null);
.svgSelector).toEqual(undefined);
});
});
});
11 changes: 5 additions & 6 deletions src/lib/AnnotationItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default class AnnotationItem {
case 'string':
return target;
case 'object':
return target.selector;
return flatten(compact(new Array(target.selector)));
default:
return null;
}
Expand All @@ -91,10 +91,7 @@ export default class AnnotationItem {
case 'string':
return null;
case 'object':
if (selector.type && selector.type === 'SvgSelector') {
return selector;
}
return null;
return selector.find(s => s.type && s.type === 'SvgSelector');
default:
return null;
}
Expand All @@ -105,13 +102,15 @@ export default class AnnotationItem {
const { selector } = this;

let match;
let fragmentSelector;

switch (typeof selector) {
case 'string':
match = selector.match(/xywh=(.*)$/);
break;
case 'object':
match = selector.value.match(/xywh=(.*)$/);
fragmentSelector = selector.find(s => s.type && s.type === 'FragmentSelector');
match = fragmentSelector && fragmentSelector.value.match(/xywh=(.*)$/);
break;
default:
return null;
Expand Down

0 comments on commit 56b4409

Please sign in to comment.