1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var ReactDOMSelection = require('./ReactDOMSelection');
|
14 |
|
15 | var containsNode = require('fbjs/lib/containsNode');
|
16 | var focusNode = require('fbjs/lib/focusNode');
|
17 | var getActiveElement = require('fbjs/lib/getActiveElement');
|
18 |
|
19 | function isInDocument(node) {
|
20 | return containsNode(document.documentElement, node);
|
21 | }
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var ReactInputSelection = {
|
30 | hasSelectionCapabilities: function (elem) {
|
31 | var nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase();
|
32 | return nodeName && (nodeName === 'input' && elem.type === 'text' || nodeName === 'textarea' || elem.contentEditable === 'true');
|
33 | },
|
34 |
|
35 | getSelectionInformation: function () {
|
36 | var focusedElem = getActiveElement();
|
37 | return {
|
38 | focusedElem: focusedElem,
|
39 | selectionRange: ReactInputSelection.hasSelectionCapabilities(focusedElem) ? ReactInputSelection.getSelection(focusedElem) : null
|
40 | };
|
41 | },
|
42 |
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | restoreSelection: function (priorSelectionInformation) {
|
49 | var curFocusedElem = getActiveElement();
|
50 | var priorFocusedElem = priorSelectionInformation.focusedElem;
|
51 | var priorSelectionRange = priorSelectionInformation.selectionRange;
|
52 | if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
|
53 | if (ReactInputSelection.hasSelectionCapabilities(priorFocusedElem)) {
|
54 | ReactInputSelection.setSelection(priorFocusedElem, priorSelectionRange);
|
55 | }
|
56 | focusNode(priorFocusedElem);
|
57 | }
|
58 | },
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | getSelection: function (input) {
|
67 | var selection;
|
68 |
|
69 | if ('selectionStart' in input) {
|
70 |
|
71 | selection = {
|
72 | start: input.selectionStart,
|
73 | end: input.selectionEnd
|
74 | };
|
75 | } else if (document.selection && input.nodeName && input.nodeName.toLowerCase() === 'input') {
|
76 |
|
77 | var range = document.selection.createRange();
|
78 |
|
79 |
|
80 | if (range.parentElement() === input) {
|
81 | selection = {
|
82 | start: -range.moveStart('character', -input.value.length),
|
83 | end: -range.moveEnd('character', -input.value.length)
|
84 | };
|
85 | }
|
86 | } else {
|
87 |
|
88 | selection = ReactDOMSelection.getOffsets(input);
|
89 | }
|
90 |
|
91 | return selection || { start: 0, end: 0 };
|
92 | },
|
93 |
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | setSelection: function (input, offsets) {
|
101 | var start = offsets.start;
|
102 | var end = offsets.end;
|
103 | if (end === undefined) {
|
104 | end = start;
|
105 | }
|
106 |
|
107 | if ('selectionStart' in input) {
|
108 | input.selectionStart = start;
|
109 | input.selectionEnd = Math.min(end, input.value.length);
|
110 | } else if (document.selection && input.nodeName && input.nodeName.toLowerCase() === 'input') {
|
111 | var range = input.createTextRange();
|
112 | range.collapse(true);
|
113 | range.moveStart('character', start);
|
114 | range.moveEnd('character', end - start);
|
115 | range.select();
|
116 | } else {
|
117 | ReactDOMSelection.setOffsets(input, offsets);
|
118 | }
|
119 | }
|
120 | };
|
121 |
|
122 | module.exports = ReactInputSelection; |
\ | No newline at end of file |