Skip to content
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 Oct 24, 2017
ed0cc8a
Use node’s window and document when possible
acusti Oct 30, 2017
8f11b2c
Make getting/setting selection check iframe contents
acusti Oct 4, 2016
83cfb55
Add tests for ReactInputSelection
acusti Nov 1, 2017
374d525
Adapt restoreSelection to work for all activeElements
acusti Oct 10, 2016
f6f9e35
Tests for getting / restoring selections across iframes
acusti Oct 11, 2016
6ac703f
Add guards for Firefox and Safari compatibility
acusti Mar 15, 2017
cec8c3f
Prettier
acusti Nov 1, 2017
0163795
Update test file header
acusti Nov 1, 2017
d777546
Avoid causing focus/blur issues from restoreSelection
acusti Nov 1, 2017
7c59a77
Add early return for restoring selection common case
acusti Nov 1, 2017
131357e
Merge remote-tracking branch 'upstream/master' into iframes
acusti Nov 3, 2017
2d4ac46
Prefer active element as element with selection
acusti Nov 16, 2017
5755556
Merge branch 'master' into iframes
acusti Nov 16, 2017
b3998eb
Prettier
acusti Nov 16, 2017
174375c
Prevent restoreSelection overwriting active element
acusti Nov 17, 2017
39a9589
Code review-based cleanup and improvements
acusti Nov 17, 2017
82443f7
Detect all valid selection-capable input types
acusti Nov 19, 2017
b25279d
Merge branch 'master' into iframes
acusti Nov 19, 2017
026e8f0
Merge branch 'master' into iframes
acusti Nov 28, 2017
689d7c1
Merge branch 'master' into iframes
acusti Dec 30, 2017
074ff51
Guard against null (based on failing test)
acusti Dec 30, 2017
0744710
Linting (var :arrow_right: const)
acusti Dec 31, 2017
c71601b
Merge branch 'master' into iframes
acusti Jan 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for ReactInputSelection
Had to remove some tests because they rely on DOM functionality that
jsdom doesn’t yet support, like getSelection and iframe activeElement
  • Loading branch information
acusti committed Nov 1, 2017
commit 83cfb558d85070b7eda64e38ea9d047e063bc075
169 changes: 169 additions & 0 deletions packages/react-dom/src/__tests__/ReactInputSelection-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/**
* Copyright 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @emails react-core
*/

'use strict';

var React = require('react');
var ReactDOM = require('react-dom');
var ReactTestUtils = require('react-dom/test-utils');
var 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);
return ReactDOM.findDOMNode(instance);
};

describe('hasSelectionCapabilities', () => {
it('returns true for textareas', () => {
var textarea = document.createElement('textarea');
expect(ReactInputSelection.hasSelectionCapabilities(textarea)).toBe(true);
});

it('returns true for text inputs', () => {
var inputText = document.createElement('input');
var inputReadOnly = document.createElement('input');
inputReadOnly.readOnly = 'true';
var inputNumber = document.createElement('input');
inputNumber.type = 'number';
var inputEmail = document.createElement('input');
inputEmail.type = 'email';
var inputPassword = document.createElement('input');
inputPassword.type = 'password';
var inputHidden = document.createElement('input');
inputHidden.type = 'hidden';

expect(ReactInputSelection.hasSelectionCapabilities(inputText)).toBe(true);
expect(ReactInputSelection.hasSelectionCapabilities(inputReadOnly)).toBe(true);
expect(ReactInputSelection.hasSelectionCapabilities(inputNumber)).toBe(false);
expect(ReactInputSelection.hasSelectionCapabilities(inputEmail)).toBe(false);
expect(ReactInputSelection.hasSelectionCapabilities(inputPassword)).toBe(false);
expect(ReactInputSelection.hasSelectionCapabilities(inputHidden)).toBe(false);
});
Copy link
Contributor

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, and number return false? Is this because they don't support the text selection api?

Copy link
Contributor Author

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:

  return (
    nodeName &&
    ((nodeName === 'input' && elem.type === 'text') ||
      nodeName === 'textarea' ||
      elem.contentEditable === 'true')
  );

But I’m not sure it’s desired, because those input types can definitely have selections. The hasSelectionCapabilities check could be changed to something like:

  return (
    nodeName &&
    ((nodeName === 'input' && ['text', 'email', 'number', 'password'].includes(elem.type)) ||
      nodeName === 'textarea' ||
      elem.contentEditable === 'true')
  );


it('returns true for contentEditable elements', () => {
var div = document.createElement('div');
div.contentEditable = 'true';
var body = document.createElement('body');
body.contentEditable = 'true';
var input = document.createElement('input');
input.contentEditable = 'true';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. I didn't know you could do this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

screen shot 2017-11-16 at 4 39 22 pm

How fun.

var select = document.createElement('select');
select.contentEditable = 'true';

expect(ReactInputSelection.hasSelectionCapabilities(div)).toBe(true);
expect(ReactInputSelection.hasSelectionCapabilities(body)).toBe(true);
expect(ReactInputSelection.hasSelectionCapabilities(input)).toBe(true);
expect(ReactInputSelection.hasSelectionCapabilities(select)).toBe(true);
});

it('returns false for any other type of HTMLElement', () => {
var select = document.createElement('select');
var iframe = document.createElement('iframe');

expect(ReactInputSelection.hasSelectionCapabilities(select)).toBe(false);
expect(ReactInputSelection.hasSelectionCapabilities(iframe)).toBe(false);
});
});

describe('getSelection', () => {
it('gets selection offsets from a textarea or input', () => {
var input = createAndMountElement('input', {defaultValue: textValue});
input.setSelectionRange(6, 11);
expect(ReactInputSelection.getSelection(input)).toEqual({start: 6, end: 11});

var textarea = createAndMountElement('textarea', {defaultValue: textValue});
textarea.setSelectionRange(6, 11);
expect(ReactInputSelection.getSelection(textarea)).toEqual({start: 6, end: 11});
});

it('gets selection offsets from a contentEditable element', () => {
var node = createAndMountElement('div', null, textValue);
node.selectionStart = 6;
node.selectionEnd = 11;
expect(ReactInputSelection.getSelection(node)).toEqual({start: 6, end: 11});
});

it('gets selection offsets as start: 0, end: 0 if no selection', () => {
var node = createAndMountElement('select');
expect(ReactInputSelection.getSelection(node)).toEqual({start: 0, end: 0});
});

it('gets selection on inputs in iframes', () => {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const input = document.createElement('input');
input.value = textValue;
iframe.contentDocument.body.appendChild(input);
input.select();
expect(input.selectionStart).toEqual(0);
expect(input.selectionEnd).toEqual(textValue.length);

document.body.removeChild(iframe);
});
});

describe('setSelection', () => {
it('sets selection offsets on textareas and inputs', () => {
var 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', {defaultValue: textValue});
ReactInputSelection.setSelection(textarea, {start: 1, end: 10});
expect(textarea.selectionStart).toEqual(1);
expect(textarea.selectionEnd).toEqual(10);
});

it('sets selection on inputs in iframes', () => {
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
const input = document.createElement('input');
input.value = textValue;
iframe.contentDocument.body.appendChild(input);
ReactInputSelection.setSelection(input, {start: 1, end: 10});
expect(input.selectionStart).toEqual(1);
expect(input.selectionEnd).toEqual(10);

document.body.removeChild(iframe);
});
});

describe('getSelectionInformation/restoreSelection', () => {
it('gets and restores selection for inputs that get remounted', () => {
var input = document.createElement('input');
input.value = textValue;
document.body.appendChild(input);
input.focus();
input.selectionStart = 1;
input.selectionEnd = 10;
var selectionInfo = ReactInputSelection.getSelectionInformation();
expect(selectionInfo.focusedElem).toBe(input);
expect(selectionInfo.selectionRange).toEqual({start: 1, end: 10});
expect(document.activeElement).toBe(input);
input.setSelectionRange(0, 0);
document.body.removeChild(input);
expect(document.activeElement).not.toBe(input);
expect(input.selectionStart).not.toBe(1);
expect(input.selectionEnd).not.toBe(10);
document.body.appendChild(input);
ReactInputSelection.restoreSelection(selectionInfo);
expect(document.activeElement).toBe(input);
expect(input.selectionStart).toBe(1);
expect(input.selectionEnd).toBe(10);

document.body.removeChild(input);
});
});
});