This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
setDraftEditorSelection.js
366 lines (336 loc) · 11.1 KB
/
setDraftEditorSelection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall draft_js
*/
'use strict';
import type {SelectionObject} from 'DraftDOMTypes';
import type SelectionState from 'SelectionState';
const DraftEffects = require('DraftEffects');
const DraftJsDebugLogging = require('DraftJsDebugLogging');
const UserAgent = require('UserAgent');
const containsNode = require('containsNode');
const getActiveElement = require('getActiveElement');
const getCorrectDocumentFromNode = require('getCorrectDocumentFromNode');
const invariant = require('invariant');
const isElement = require('isElement');
const isIE = UserAgent.isBrowser('IE');
function getAnonymizedDOM(
node: Node,
getNodeLabels?: (n: Node) => Array<string>,
): string {
if (!node) {
return '[empty]';
}
const anonymized = anonymizeTextWithin(node, getNodeLabels);
if (anonymized.nodeType === Node.TEXT_NODE) {
return anonymized.textContent;
}
invariant(
isElement(anonymized),
'Node must be an Element if it is not a text node.',
);
const castedElement: Element = (anonymized: any);
return castedElement.outerHTML;
}
function anonymizeTextWithin(
node: Node,
getNodeLabels?: (n: Node) => Array<string>,
): Node {
const labels = getNodeLabels !== undefined ? getNodeLabels(node) : [];
if (node.nodeType === Node.TEXT_NODE) {
const length = node.textContent.length;
return getCorrectDocumentFromNode(node).createTextNode(
'[text ' +
length +
(labels.length ? ' | ' + labels.join(', ') : '') +
']',
);
}
const clone = node.cloneNode();
if (clone.nodeType === 1 && labels.length) {
((clone: any): Element).setAttribute('data-labels', labels.join(', '));
}
const childNodes = node.childNodes;
for (let ii = 0; ii < childNodes.length; ii++) {
clone.appendChild(anonymizeTextWithin(childNodes[ii], getNodeLabels));
}
return clone;
}
function getAnonymizedEditorDOM(
node: Node,
getNodeLabels?: (n: Node) => Array<string>,
): string {
// grabbing the DOM content of the Draft editor
let currentNode: ?Node = node;
// this should only be used after checking with isElement
let castedNode: Element = (currentNode: any);
while (currentNode) {
if (isElement(currentNode) && castedNode.hasAttribute('contenteditable')) {
// found the Draft editor container
return getAnonymizedDOM(currentNode, getNodeLabels);
} else {
currentNode = currentNode.parentNode;
castedNode = (currentNode: any);
}
}
return 'Could not find contentEditable parent of node';
}
function getNodeLength(node: Node): number {
return node.nodeValue === null
? node.childNodes.length
: node.nodeValue.length;
}
/**
* In modern non-IE browsers, we can support both forward and backward
* selections.
*
* Note: IE10+ supports the Selection object, but it does not support
* the `extend` method, which means that even in modern IE, it's not possible
* to programatically create a backward selection. Thus, for all IE
* versions, we use the old IE API to create our selections.
*/
function setDraftEditorSelection(
selectionState: SelectionState,
node: Node,
blockKey: string,
nodeStart: number,
nodeEnd: number,
): void {
// It's possible that the editor has been removed from the DOM but
// our selection code doesn't know it yet. Forcing selection in
// this case may lead to errors, so just bail now.
const documentObject = getCorrectDocumentFromNode(node);
if (!containsNode(documentObject.documentElement, node)) {
return;
}
const selection: SelectionObject = documentObject.defaultView.getSelection();
let anchorKey = selectionState.getAnchorKey();
let anchorOffset = selectionState.getAnchorOffset();
let focusKey = selectionState.getFocusKey();
let focusOffset = selectionState.getFocusOffset();
let isBackward = selectionState.getIsBackward();
// IE doesn't support backward selection. Swap key/offset pairs.
if (!selection.extend && isBackward) {
const tempKey = anchorKey;
const tempOffset = anchorOffset;
anchorKey = focusKey;
anchorOffset = focusOffset;
focusKey = tempKey;
focusOffset = tempOffset;
isBackward = false;
}
const hasAnchor =
anchorKey === blockKey &&
nodeStart <= anchorOffset &&
nodeEnd >= anchorOffset;
const hasFocus =
focusKey === blockKey && nodeStart <= focusOffset && nodeEnd >= focusOffset;
// If the selection is entirely bound within this node, set the selection
// and be done.
if (hasAnchor && hasFocus) {
selection.removeAllRanges();
addPointToSelection(
selection,
node,
anchorOffset - nodeStart,
selectionState,
);
addFocusToSelection(
selection,
node,
focusOffset - nodeStart,
selectionState,
);
return;
}
if (!isBackward) {
// If the anchor is within this node, set the range start.
if (hasAnchor) {
selection.removeAllRanges();
addPointToSelection(
selection,
node,
anchorOffset - nodeStart,
selectionState,
);
}
// If the focus is within this node, we can assume that we have
// already set the appropriate start range on the selection, and
// can simply extend the selection.
if (hasFocus) {
addFocusToSelection(
selection,
node,
focusOffset - nodeStart,
selectionState,
);
}
} else {
// If this node has the focus, set the selection range to be a
// collapsed range beginning here. Later, when we encounter the anchor,
// we'll use this information to extend the selection.
if (hasFocus) {
selection.removeAllRanges();
addPointToSelection(
selection,
node,
focusOffset - nodeStart,
selectionState,
);
}
// If this node has the anchor, we may assume that the correct
// focus information is already stored on the selection object.
// We keep track of it, reset the selection range, and extend it
// back to the focus point.
if (hasAnchor) {
const storedFocusNode = selection.focusNode;
const storedFocusOffset = selection.focusOffset;
selection.removeAllRanges();
addPointToSelection(
selection,
node,
anchorOffset - nodeStart,
selectionState,
);
addFocusToSelection(
selection,
storedFocusNode,
storedFocusOffset,
selectionState,
);
}
}
}
/**
* Extend selection towards focus point.
*/
function addFocusToSelection(
selection: SelectionObject,
node: ?Node,
offset: number,
selectionState: SelectionState,
): void {
const activeElement = getActiveElement();
const extend = selection.extend;
// containsNode returns false if node is null.
// Let's refine the type of this value out here so flow knows.
if (extend && node != null && containsNode(activeElement, node)) {
// If `extend` is called while another element has focus, an error is
// thrown. We therefore disable `extend` if the active element is somewhere
// other than the node we are selecting. This should only occur in Firefox,
// since it is the only browser to support multiple selections.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=921444.
// logging to catch bug that is being reported in t16250795
if (offset > getNodeLength(node)) {
// the call to 'selection.extend' is about to throw
DraftJsDebugLogging.logSelectionStateFailure({
anonymizedDom: getAnonymizedEditorDOM(node),
extraParams: JSON.stringify({offset}),
selectionState: JSON.stringify(selectionState.toJS()),
});
}
// logging to catch bug that is being reported in t18110632
const nodeWasFocus = node === selection.focusNode;
try {
// Fixes some reports of "InvalidStateError: Failed to execute 'extend' on
// 'Selection': This Selection object doesn't have any Ranges."
// Note: selection.extend does not exist in IE.
if (selection.rangeCount > 0 && selection.extend) {
selection.extend(node, offset);
}
} catch (e) {
DraftJsDebugLogging.logSelectionStateFailure({
anonymizedDom: getAnonymizedEditorDOM(node, function (n) {
const labels = [];
if (n === activeElement) {
labels.push('active element');
}
if (n === selection.anchorNode) {
labels.push('selection anchor node');
}
if (n === selection.focusNode) {
labels.push('selection focus node');
}
return labels;
}),
extraParams: JSON.stringify(
{
activeElementName: activeElement ? activeElement.nodeName : null,
nodeIsFocus: node === selection.focusNode,
nodeWasFocus,
selectionRangeCount: selection.rangeCount,
selectionAnchorNodeName: selection.anchorNode
? selection.anchorNode.nodeName
: null,
selectionAnchorOffset: selection.anchorOffset,
selectionFocusNodeName: selection.focusNode
? selection.focusNode.nodeName
: null,
selectionFocusOffset: selection.focusOffset,
message: e ? '' + e : null,
offset,
},
null,
2,
),
selectionState: JSON.stringify(selectionState.toJS(), null, 2),
});
// allow the error to be thrown -
// better than continuing in a broken state
throw e;
}
} else {
// IE doesn't support extend. This will mean no backward selection.
// Extract the existing selection range and add focus to it.
// Additionally, clone the selection range. IE11 throws an
// InvalidStateError when attempting to access selection properties
// after the range is detached.
if (node && selection.rangeCount > 0) {
const range = selection.getRangeAt(0);
range.setEnd(node, offset);
selection.addRange(range.cloneRange());
}
}
}
function addPointToSelection(
selection: SelectionObject,
node: Node,
offset: number,
selectionState: SelectionState,
): void {
const range = getCorrectDocumentFromNode(node).createRange();
// logging to catch bug that is being reported in t16250795
if (offset > getNodeLength(node)) {
// in this case we know that the call to 'range.setStart' is about to throw
DraftJsDebugLogging.logSelectionStateFailure({
anonymizedDom: getAnonymizedEditorDOM(node),
extraParams: JSON.stringify({offset}),
selectionState: JSON.stringify(selectionState.toJS()),
});
DraftEffects.handleExtensionCausedError();
}
range.setStart(node, offset);
// IE sometimes throws Unspecified Error when trying to addRange
if (isIE) {
try {
selection.addRange(range);
} catch (e) {
if (__DEV__) {
/* eslint-disable-next-line no-console */
console.warn('Call to selection.addRange() threw exception: ', e);
}
}
} else {
selection.addRange(range);
}
}
module.exports = {
setDraftEditorSelection,
addFocusToSelection,
};