|
1 | 1 | import { getContentPath } from './contentPath'; |
2 | 2 | import Highlighter from './Highlighter'; |
3 | 3 | import { adjacentTextSections } from './injectHighlightWrappers.spec.data'; |
4 | | -import { IData } from './serializationStrategies/XpathRangeSelector'; |
| 4 | +import { IData as TextPositionData } from './serializationStrategies/TextPositionSelector'; |
| 5 | +import { IData as XpathRangeData } from './serializationStrategies/XpathRangeSelector'; |
5 | 6 |
|
6 | 7 | describe('getContentPath', () => { |
7 | | - it('creates a path of indexes', () => { |
8 | | - document.body.innerHTML = `<div id="page">${adjacentTextSections}</div>`; |
9 | | - |
10 | | - const container = document.getElementById('page')!; |
11 | | - const highlighter = new Highlighter(container, { formatMessage: jest.fn() }); |
12 | | - |
13 | | - const result = getContentPath( |
14 | | - { |
15 | | - referenceElementId: 'test-container', |
16 | | - // tslint:disable-next-line quotemark |
17 | | - startContainer: "./*[name()='section'][1]/*[name()='div'][1]/*[name()='p'][1]/text()[1]", |
18 | | - startOffset: 4, |
19 | | - } as IData, |
20 | | - highlighter |
21 | | - ); |
22 | | - |
23 | | - expect(result).toEqual([3, 1, 1, 0, 4]); |
24 | | - }); |
| 8 | + describe('with XpathRangeSelector type', () => { |
| 9 | + it('creates a path of indexes', () => { |
| 10 | + document.body.innerHTML = `<div id="page">${adjacentTextSections}</div>`; |
| 11 | + |
| 12 | + const container = document.getElementById('page')!; |
| 13 | + const highlighter = new Highlighter(container, { formatMessage: jest.fn() }); |
| 14 | + |
| 15 | + const result = getContentPath( |
| 16 | + { |
| 17 | + referenceElementId: 'test-container', |
| 18 | + // tslint:disable-next-line quotemark |
| 19 | + startContainer: "./*[name()='section'][1]/*[name()='div'][1]/*[name()='p'][1]/text()[1]", |
| 20 | + startOffset: 4, |
| 21 | + type: 'XpathRangeSelector', |
| 22 | + } as XpathRangeData, |
| 23 | + highlighter |
| 24 | + ); |
| 25 | + |
| 26 | + expect(result).toEqual([3, 1, 1, 0, 4]); |
| 27 | + }); |
25 | 28 |
|
26 | | - it('handles adjacent text highlights', () => { |
27 | | - document.body.innerHTML = `<div id="page"> |
| 29 | + it('handles adjacent text highlights', () => { |
| 30 | + document.body.innerHTML = `<div id="page"> |
28 | 31 | <div id="test-container"> |
29 | | - <p id="test-p"> |
30 | | - <span data-highlighted>A shortcut called</span> FOIL is sometimes used to find the product of two binomials. |
| 32 | + <p id="test-p"> |
| 33 | + <span data-highlighted>A shortcut called</span> FOIL is sometimes used to find the product of two binomials. |
31 | 34 | </p> |
32 | 35 | </div> |
33 | | - </div>`; |
34 | | - |
35 | | - const container = document.getElementById('test-container')!; |
36 | | - const highlighter = new Highlighter(container, { formatMessage: jest.fn() }); |
37 | | - |
38 | | - const range: any = { |
39 | | - collapse: false, |
40 | | - commonAncestorContainer: container, |
41 | | - endContainer: './text()[1]', |
42 | | - endOffset: 31, |
43 | | - setEndAfter: jest.fn(), |
44 | | - setStartBefore: jest.fn(), |
45 | | - startContainer: './text()[1]', |
46 | | - startOffset: 27, |
47 | | - }; |
48 | | - |
49 | | - const result = getContentPath( |
50 | | - { |
51 | | - referenceElementId: 'test-p', |
52 | | - startContainer: range.startContainer, |
53 | | - startOffset: range.startOffset, |
54 | | - } as IData, |
55 | | - highlighter |
56 | | - ); |
57 | | - |
58 | | - expect(result).toEqual([0, 27]); |
| 36 | + </div>`; |
| 37 | + |
| 38 | + const container = document.getElementById('test-container')!; |
| 39 | + const highlighter = new Highlighter(container, { formatMessage: jest.fn() }); |
| 40 | + |
| 41 | + const range: any = { |
| 42 | + collapse: false, |
| 43 | + commonAncestorContainer: container, |
| 44 | + endContainer: './text()[1]', |
| 45 | + endOffset: 31, |
| 46 | + setEndAfter: jest.fn(), |
| 47 | + setStartBefore: jest.fn(), |
| 48 | + startContainer: './text()[1]', |
| 49 | + startOffset: 27, |
| 50 | + }; |
| 51 | + |
| 52 | + const result = getContentPath( |
| 53 | + { |
| 54 | + referenceElementId: 'test-p', |
| 55 | + startContainer: range.startContainer, |
| 56 | + startOffset: range.startOffset, |
| 57 | + type: 'XpathRangeSelector', |
| 58 | + } as XpathRangeData, |
| 59 | + highlighter |
| 60 | + ); |
| 61 | + |
| 62 | + expect(result).toEqual([0, 27]); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('with TextPositionSelector type', () => { |
| 67 | + it('returns a simplified path and offset', () => { |
| 68 | + document.body.innerHTML = `<div id="page"> |
| 69 | + <div id="test-container"> |
| 70 | + A shortcut called FOIL is sometimes used to find the product of two binomials. |
| 71 | + </div> |
| 72 | + </div>`; |
| 73 | + |
| 74 | + const container = document.getElementById('page')!; |
| 75 | + const highlighter = new Highlighter(container, { formatMessage: jest.fn() }); |
| 76 | + |
| 77 | + const result = getContentPath( |
| 78 | + { |
| 79 | + end: 11, |
| 80 | + referenceElementId: 'test-container', |
| 81 | + start: 3, |
| 82 | + type: 'TextPositionSelector', |
| 83 | + } as TextPositionData, |
| 84 | + highlighter |
| 85 | + ); |
| 86 | + |
| 87 | + expect(result).toEqual([0, 3]); |
| 88 | + }); |
59 | 89 | }); |
60 | 90 | }); |
0 commit comments