1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-18 13:11:17 +02:00

fix: prevent IE11 from throwing if selection contains tables (#1825)

* fix: prevent IE11 from throwing if selection contains tables

* fix: run prettier

* fix: lint
This commit is contained in:
Ben Southgate
2018-05-10 22:07:11 -04:00
committed by Ian Storm Taylor
parent 7684fff545
commit f37c3a4776
3 changed files with 33 additions and 4 deletions

View File

@@ -16,6 +16,7 @@ import Node from './node'
import findDOMRange from '../utils/find-dom-range' import findDOMRange from '../utils/find-dom-range'
import findRange from '../utils/find-range' import findRange from '../utils/find-range'
import scrollToSelection from '../utils/scroll-to-selection' import scrollToSelection from '../utils/scroll-to-selection'
import removeAllRanges from '../utils/remove-all-ranges'
/** /**
* Debug. * Debug.
@@ -151,7 +152,7 @@ class Content extends React.Component {
// DOM, blur it manually. // DOM, blur it manually.
if (selection.isBlurred) { if (selection.isBlurred) {
if (!this.isInEditor(anchorNode)) return if (!this.isInEditor(anchorNode)) return
native.removeAllRanges() removeAllRanges(native)
this.element.blur() this.element.blur()
debug('updateSelection', { selection, native }) debug('updateSelection', { selection, native })
return return
@@ -195,7 +196,7 @@ class Content extends React.Component {
// Otherwise, set the `isUpdatingSelection` flag and update the selection. // Otherwise, set the `isUpdatingSelection` flag and update the selection.
this.tmp.isUpdatingSelection = true this.tmp.isUpdatingSelection = true
native.removeAllRanges() removeAllRanges(native)
// COMPAT: IE 11 does not support Selection.setBaseAndExtent // COMPAT: IE 11 does not support Selection.setBaseAndExtent
if (native.setBaseAndExtent) { if (native.setBaseAndExtent) {

View File

@@ -4,6 +4,7 @@ import { IS_CHROME, IS_SAFARI, IS_OPERA } from 'slate-dev-environment'
import getWindow from 'get-window' import getWindow from 'get-window'
import findDOMNode from './find-dom-node' import findDOMNode from './find-dom-node'
import { ZERO_WIDTH_SELECTOR, ZERO_WIDTH_ATTRIBUTE } from './find-point' import { ZERO_WIDTH_SELECTOR, ZERO_WIDTH_ATTRIBUTE } from './find-point'
import removeAllRanges from './remove-all-ranges'
/** /**
* Prepares a Slate document fragment to be copied to the clipboard. * Prepares a Slate document fragment to be copied to the clipboard.
@@ -130,13 +131,13 @@ function cloneFragment(event, value, fragment = value.fragment) {
// throws an error, so we use the older `range` equivalent. (2016/06/21) // throws an error, so we use the older `range` equivalent. (2016/06/21)
const r = window.document.createRange() const r = window.document.createRange()
r.selectNodeContents(div) r.selectNodeContents(div)
native.removeAllRanges() removeAllRanges(native)
native.addRange(r) native.addRange(r)
// Revert to the previous selection right after copying. // Revert to the previous selection right after copying.
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
editor.removeChild(div) editor.removeChild(div)
native.removeAllRanges() removeAllRanges(native)
native.addRange(range) native.addRange(range)
}) })
} }

View File

@@ -0,0 +1,27 @@
/**
* COMPAT: if we are in <= IE11 and the selection contains
* tables, `removeAllRanges()` will throw
* "unable to complete the operation due to error 800a025e"
*
* @param {Selection} selection document selection
*/
function removeAllRanges(selection) {
const doc = window.document
if (doc && doc.body.createTextRange) {
// All IE but Edge
const range = doc.body.createTextRange()
range.collapse()
range.select()
} else {
selection.removeAllRanges()
}
}
/**
* Export.
*
* @type {Function}
*/
export default removeAllRanges