Skip to content

Commit

Permalink
Ignore IndexSizeError and similar when setting editor selection
Browse files Browse the repository at this point in the history
  • Loading branch information
robbertbrak committed Apr 24, 2018
1 parent 87a1435 commit a90a693
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
12 changes: 11 additions & 1 deletion examples/rich/rich.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@
</head>
<body>
<div id="target"></div>
<script src="../../node_modules/react/dist/react.min.js"></script>
<div id="sample">
<ol>
<li>foo</li>
<li>bar</li>
<li>baz</li>
</ol>
<p>
And some more content.
</p>
</div>
<script src="../../node_modules/react/dist/react.js"></script>
<script src="../../node_modules/react-dom/dist/react-dom.js"></script>
<script src="../../node_modules/immutable/dist/immutable.js"></script>
<script src="../../node_modules/es6-shim/es6-shim.js"></script>
Expand Down
52 changes: 30 additions & 22 deletions src/component/selection/setDraftEditorSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,28 @@ function addFocusToSelection(
node: Node,
offset: number
): void {
if (selection.extend && containsNode(getActiveElement(), 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.
selection.extend(node, offset);
} 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 (selection.rangeCount > 0) {
var range = selection.getRangeAt(0);
range.setEnd(node, offset);
selection.addRange(range.cloneRange());
try {
if (selection.extend && containsNode(getActiveElement(), 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.
selection.extend(node, offset);
} 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 (selection.rangeCount > 0) {
var range = selection.getRangeAt(0);
range.setEnd(node, offset);
selection.addRange(range.cloneRange());
}
}
} catch (e) {
console.warn('failed to add focus to selection', e, selection, node, offset);
}
}

Expand All @@ -151,12 +155,16 @@ function addPointToSelection(
node: Node,
offset: number
): void {
var range = document.createRange();
range.setStart(node, offset);
selection.addRange(range);
// Taken from https://github.com/facebook/draft-js/pull/1190
if (selection.rangeCount === 0) {
try {
var range = document.createRange();
range.setStart(node, offset);
selection.addRange(range);
// Taken from https://github.com/facebook/draft-js/pull/1190
if (selection.rangeCount === 0) {
selection.addRange(range);
}
} catch (e) {
console.warn('failed to add point to selection', e, selection, node, offset);
}
}

Expand Down

0 comments on commit a90a693

Please sign in to comment.