-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Support iframes (nested browsing contexts) in selection event handling #9184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e1d4110
Use event target doc as getActiveElement context
acusti ed0cc8a
Use node’s window and document when possible
acusti 8f11b2c
Make getting/setting selection check iframe contents
acusti 83cfb55
Add tests for ReactInputSelection
acusti 374d525
Adapt restoreSelection to work for all activeElements
acusti f6f9e35
Tests for getting / restoring selections across iframes
acusti 6ac703f
Add guards for Firefox and Safari compatibility
acusti cec8c3f
Prettier
acusti 0163795
Update test file header
acusti d777546
Avoid causing focus/blur issues from restoreSelection
acusti 7c59a77
Add early return for restoring selection common case
acusti 131357e
Merge remote-tracking branch 'upstream/master' into iframes
acusti 2d4ac46
Prefer active element as element with selection
acusti 5755556
Merge branch 'master' into iframes
acusti b3998eb
Prettier
acusti 174375c
Prevent restoreSelection overwriting active element
acusti 39a9589
Code review-based cleanup and improvements
acusti 82443f7
Detect all valid selection-capable input types
acusti b25279d
Merge branch 'master' into iframes
acusti 026e8f0
Merge branch 'master' into iframes
acusti 689d7c1
Merge branch 'master' into iframes
acusti 074ff51
Guard against null (based on failing test)
acusti 0744710
Linting (var :arrow_right: const)
acusti c71601b
Merge branch 'master' into iframes
acusti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Linting (var ➡️ const)
- Loading branch information
commit 07447109230c452c6e87a656e6d94fcbbfe28ce1
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,19 +9,19 @@ | |
|
|
||
| 'use strict'; | ||
|
|
||
| var React = require('react'); | ||
| var ReactDOM = require('react-dom'); | ||
| var ReactTestUtils = require('react-dom/test-utils'); | ||
| var ReactInputSelection = require('../client/ReactInputSelection'); | ||
| const React = require('react'); | ||
| const ReactDOM = require('react-dom'); | ||
| const ReactTestUtils = require('react-dom/test-utils'); | ||
| const ReactInputSelection = require('../client/ReactInputSelection'); | ||
|
|
||
| describe('ReactInputSelection', () => { | ||
| var textValue = 'the text contents'; | ||
| var createAndMountElement = (type, props, children) => { | ||
| var element = React.createElement(type, props, children); | ||
| var instance = ReactTestUtils.renderIntoDocument(element); | ||
| const textValue = 'the text contents'; | ||
| const createAndMountElement = (type, props, children) => { | ||
| const element = React.createElement(type, props, children); | ||
| const instance = ReactTestUtils.renderIntoDocument(element); | ||
| return ReactDOM.findDOMNode(instance); | ||
| }; | ||
| var makeGetSelection = (win = window) => () => ({ | ||
| const makeGetSelection = (win = window) => () => ({ | ||
| anchorNode: win.document.activeElement, | ||
| focusNode: win.document.activeElement, | ||
| anchorOffset: | ||
|
|
@@ -32,7 +32,7 @@ describe('ReactInputSelection', () => { | |
|
|
||
| describe('hasSelectionCapabilities', () => { | ||
| it('returns true for textareas', () => { | ||
| var textarea = document.createElement('textarea'); | ||
| const textarea = document.createElement('textarea'); | ||
| expect(ReactInputSelection.hasSelectionCapabilities(textarea)).toBe(true); | ||
| }); | ||
|
|
||
|
|
@@ -56,7 +56,7 @@ describe('ReactInputSelection', () => { | |
| expect(ReactInputSelection.hasSelectionCapabilities(input)).toBe(true); | ||
| }); | ||
|
|
||
| var inputReadOnly = document.createElement('input'); | ||
| const inputReadOnly = document.createElement('input'); | ||
| inputReadOnly.readOnly = 'true'; | ||
| expect(ReactInputSelection.hasSelectionCapabilities(inputReadOnly)).toBe( | ||
| true, | ||
|
|
@@ -83,13 +83,13 @@ describe('ReactInputSelection', () => { | |
| }); | ||
|
|
||
| it('returns true for contentEditable elements', () => { | ||
| var div = document.createElement('div'); | ||
| const div = document.createElement('div'); | ||
| div.contentEditable = 'true'; | ||
| var body = document.createElement('body'); | ||
| const body = document.createElement('body'); | ||
| body.contentEditable = 'true'; | ||
| var input = document.createElement('input'); | ||
| const input = document.createElement('input'); | ||
| input.contentEditable = 'true'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh. I didn't know you could do this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| var select = document.createElement('select'); | ||
| const select = document.createElement('select'); | ||
| select.contentEditable = 'true'; | ||
|
|
||
| expect(ReactInputSelection.hasSelectionCapabilities(div)).toBe(true); | ||
|
|
@@ -99,8 +99,8 @@ describe('ReactInputSelection', () => { | |
| }); | ||
|
|
||
| it('returns false for any other type of HTMLElement', () => { | ||
| var select = document.createElement('select'); | ||
| var iframe = document.createElement('iframe'); | ||
| const select = document.createElement('select'); | ||
| const iframe = document.createElement('iframe'); | ||
|
|
||
| expect(ReactInputSelection.hasSelectionCapabilities(select)).toBe(false); | ||
| expect(ReactInputSelection.hasSelectionCapabilities(iframe)).toBe(false); | ||
|
|
@@ -109,14 +109,14 @@ describe('ReactInputSelection', () => { | |
|
|
||
| describe('getSelection', () => { | ||
| it('gets selection offsets from a textarea or input', () => { | ||
| var input = createAndMountElement('input', {defaultValue: textValue}); | ||
| const input = createAndMountElement('input', {defaultValue: textValue}); | ||
| input.setSelectionRange(6, 11); | ||
| expect(ReactInputSelection.getSelection(input)).toEqual({ | ||
| start: 6, | ||
| end: 11, | ||
| }); | ||
|
|
||
| var textarea = createAndMountElement('textarea', { | ||
| const textarea = createAndMountElement('textarea', { | ||
| defaultValue: textValue, | ||
| }); | ||
| textarea.setSelectionRange(6, 11); | ||
|
|
@@ -127,7 +127,7 @@ describe('ReactInputSelection', () => { | |
| }); | ||
|
|
||
| it('gets selection offsets from a contentEditable element', () => { | ||
| var node = createAndMountElement('div', null, textValue); | ||
| const node = createAndMountElement('div', null, textValue); | ||
| node.selectionStart = 6; | ||
| node.selectionEnd = 11; | ||
| expect(ReactInputSelection.getSelection(node)).toEqual({ | ||
|
|
@@ -137,7 +137,7 @@ describe('ReactInputSelection', () => { | |
| }); | ||
|
|
||
| it('gets selection offsets as start: 0, end: 0 if no selection', () => { | ||
| var node = createAndMountElement('select'); | ||
| const node = createAndMountElement('select'); | ||
| expect(ReactInputSelection.getSelection(node)).toEqual({ | ||
| start: 0, | ||
| end: 0, | ||
|
|
@@ -160,12 +160,12 @@ describe('ReactInputSelection', () => { | |
|
|
||
| describe('setSelection', () => { | ||
| it('sets selection offsets on textareas and inputs', () => { | ||
| var input = createAndMountElement('input', {defaultValue: textValue}); | ||
| const input = createAndMountElement('input', {defaultValue: textValue}); | ||
| ReactInputSelection.setSelection(input, {start: 1, end: 10}); | ||
| expect(input.selectionStart).toEqual(1); | ||
| expect(input.selectionEnd).toEqual(10); | ||
|
|
||
| var textarea = createAndMountElement('textarea', { | ||
| const textarea = createAndMountElement('textarea', { | ||
| defaultValue: textValue, | ||
| }); | ||
| ReactInputSelection.setSelection(textarea, {start: 1, end: 10}); | ||
|
|
@@ -190,15 +190,15 @@ describe('ReactInputSelection', () => { | |
| describe('getSelectionInformation/restoreSelection', () => { | ||
| it('gets and restores selection for inputs that get remounted', () => { | ||
| // Mock window getSelection if needed | ||
| var originalGetSelection = window.getSelection; | ||
| const originalGetSelection = window.getSelection; | ||
| window.getSelection = window.getSelection || makeGetSelection(window); | ||
| var input = document.createElement('input'); | ||
| const input = document.createElement('input'); | ||
| input.value = textValue; | ||
| document.body.appendChild(input); | ||
| input.focus(); | ||
| input.selectionStart = 1; | ||
| input.selectionEnd = 10; | ||
| var selectionInfo = ReactInputSelection.getSelectionInformation(); | ||
| const selectionInfo = ReactInputSelection.getSelectionInformation(); | ||
| expect(selectionInfo.activeElement).toBe(input); | ||
| expect(selectionInfo.elementSelections[0].element).toBe(input); | ||
| expect(selectionInfo.elementSelections[0].selectionRange).toEqual({ | ||
|
|
@@ -222,24 +222,24 @@ describe('ReactInputSelection', () => { | |
| }); | ||
|
|
||
| it('gets and restores selection for inputs in an iframe that get remounted', () => { | ||
| var iframe = document.createElement('iframe'); | ||
| const iframe = document.createElement('iframe'); | ||
| document.body.appendChild(iframe); | ||
| var iframeDoc = iframe.contentDocument; | ||
| var iframeWin = iframeDoc.defaultView; | ||
| const iframeDoc = iframe.contentDocument; | ||
| const iframeWin = iframeDoc.defaultView; | ||
| // Mock window and iframe getSelection if needed | ||
| var originalGetSelection = window.getSelection; | ||
| var originalIframeGetSelection = iframeWin.getSelection; | ||
| const originalGetSelection = window.getSelection; | ||
| const originalIframeGetSelection = iframeWin.getSelection; | ||
| window.getSelection = window.getSelection || makeGetSelection(window); | ||
| iframeWin.getSelection = | ||
| iframeWin.getSelection || makeGetSelection(iframeWin); | ||
|
|
||
| var input = document.createElement('input'); | ||
| const input = document.createElement('input'); | ||
| input.value = textValue; | ||
| iframeDoc.body.appendChild(input); | ||
| input.focus(); | ||
| input.selectionStart = 1; | ||
| input.selectionEnd = 10; | ||
| var selectionInfo = ReactInputSelection.getSelectionInformation(); | ||
| const selectionInfo = ReactInputSelection.getSelectionInformation(); | ||
| expect(selectionInfo.activeElement === input).toBe(true); | ||
| expect(selectionInfo.elementSelections[0].selectionRange).toEqual({ | ||
| start: 1, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These feel like they could be separate tests.
Also: Why do
email,password, andnumberreturn false? Is this because they don't support the text selection api?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s a great question! That’s what the existing function enforces, so I figured I’d capture that behavior in the tests:
But I’m not sure it’s desired, because those input types can definitely have selections. The
hasSelectionCapabilitiescheck could be changed to something like: