From bd3fa3705122960df5d74021e1f7360b8225be7b Mon Sep 17 00:00:00 2001 From: Jeremy Press Date: Tue, 27 Nov 2018 17:13:34 -0800 Subject: [PATCH] Chore: Tests and updated comment --- src/doc/DocAnnotator.js | 5 ++++- src/doc/__tests__/DocAnnotator-test.js | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/doc/DocAnnotator.js b/src/doc/DocAnnotator.js index e8c446d65..99c23f354 100644 --- a/src/doc/DocAnnotator.js +++ b/src/doc/DocAnnotator.js @@ -535,7 +535,10 @@ class DocAnnotator extends Annotator { onSelectionChange(event: Event) { const selection = window.getSelection(); const selectionNode = selection.anchorNode; - // Check parent node in IE, which will return a text node instead of a div when accessing the anchorNode + // If the selection is not contained within the annotated element, don't do anything to avoid conflicting + // with default selection behavior outside of annotations. + // In IE 11, we need to check the parent of the selection, as the anchor node is a TextElement that cannot + // be found by it's parent divs. if ( !this.annotatedElement.contains(selectionNode) || !this.annotatedElement.contains(selectionNode.parentNode) diff --git a/src/doc/__tests__/DocAnnotator-test.js b/src/doc/__tests__/DocAnnotator-test.js index 634fce3b4..ecdefadb5 100644 --- a/src/doc/__tests__/DocAnnotator-test.js +++ b/src/doc/__tests__/DocAnnotator-test.js @@ -802,18 +802,34 @@ describe('doc/DocAnnotator', () => { stopPropagation: jest.fn() }; + annotator.annotatedElement = { + contains: jest.fn().mockReturnValue(true) + }; + docUtil.isValidSelection = jest.fn().mockReturnValue(true); annotator.lastSelection = {}; annotator.highlighter = { removeAllHighlights: jest.fn() }; annotator.resetHighlightSelection = jest.fn(); - window.getSelection = jest.fn(); + window.getSelection = jest.fn().mockReturnValue({ + anchorNode: { + parentNode: 'Node' + } + }); util.getPageInfo = jest.fn().mockReturnValue({ page: 1 }); util.findClosestElWithClass = jest.fn().mockReturnValue(null); annotator.isCreatingHighlight = false; }); + it('should do nothing if the selection is not contained in the annotatedElement', () => { + annotator.annotatedElement.contains.mockReturnValue(false); + annotator.onSelectionChange(event); + + expect(event.preventDefault).not.toBeCalled(); + expect(event.stopPropagation).not.toBeCalled(); + }); + it('should reset the selectionEndTimeout', () => { annotator.selectionEndTimeout = 1; docUtil.isValidSelection = jest.fn().mockReturnValue(false); @@ -862,6 +878,9 @@ describe('doc/DocAnnotator', () => { const selection = { rangeCount: 10, isCollapsed: false, + anchorNode: { + parentNode: 'Node' + }, toString: () => 'asdf' }; window.getSelection = jest.fn().mockReturnValue(selection); @@ -878,6 +897,9 @@ describe('doc/DocAnnotator', () => { const selection = { rangeCount: 10, isCollapsed: false, + anchorNode: { + parentNode: 'Node' + }, toString: jest.fn().mockReturnValue('asdf') }; annotator.modeControllers = {