IE11: setDraftEditorSelection crashes with error 800a025e on removeAllRanges when getRangeAt(0).getClientRects().length === 0
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. You can use this jsfiddle to get started: https://jsfiddle.net/stopachka/m6z0xn4r/.
When selection.removeAllRanges() is called in setDraftEditorSelection, IE throws an error JavaScript runtime error: Could not complete the operation due to error 800a025e if selection.rangeCount === 0 || selection.getRangeAt(0).getClientRects().length === 0. The root cause is discussed here: https://stackoverflow.com/questions/22914075/javascript-error-800a025e-using-range-selector/37580789#37580789
What is the expected behavior?
The selection's removeAllRanges() is successfully called.
Which versions of Draft.js, and which browser / OS are affected by this issue? Did this work in previous versions of Draft.js?
0.10.5, IE11. No.
Additional comments
I believe the solution for this is to check for the above scenario and not call removeAllRanges(), which I will be happy to send out a PR for. I suppose it's a safe change given that either rangeCount === 0 or the first range has getClientRects().length === 0, but I'm happy to hear what others have to say.
function removeAllRangesSafe(selection: Object) {
// IE throws Error 800a025e under the below circumstance
if (document.body.createTextRange) {
const willCrashInIE = selection.rangeCount === 0 || selection.getRangeAt(0).getClientRects().length === 0;
if (willCrashInIE) {
return;
}
}
selection.removeAllRanges();
}
Alternatively, we can be extra safe by preventing swallowing all errors from IE?
function removeAllRangesSafe(selection: Object) {
// IE throws Error 800a025e at times
if (document.body.createTextRange) {
try {
selection.removeAllRanges();
} catch (e) {
// Swallow
}
} else {
selection.removeAllRanges();
}
}
My team is experiencing the same 800a025e error but I've tracked it down to using a component in blockRendererFn:
<Editor
(... bunch of props omitted)
blockRendererFn={customBlockRenderer}
/>
import ListItem from "components/ListItem";
export default function customBlockRenderer(contentBlock) {
(... bunch of conditions omitted)
return {
component: ListItem
}
}
Works fine if I remove component: ListItem but obviously doesn't render our component
AND
Its cropping up with our wrapper in a custom block renderer
const blockRenderMap = Immutable.Map({
Table: {
wrapper: <Table />,
}
});
...
const extendedBlockRenderMap = DefaultDraftBlockRenderMap.merge(blockRenderMap);
...
<Editor
(... bunch of props omitted)
blockRenderMap={extendedBlockRenderMap}
/>
Am pretty stumped, and I am unsure if these interact with removeAllRanges?
huh. What version are you on @tbredin?
@mttwc I actually considered opening a PR with your solution. Only problem I ran into, was to "provide the steps to reproduce and if possible a minimal demo of the problem".. I just couldn't find a practical solution for demoing and simply reproducing this issue. Mainly because jsfiddle, codepen and alike are not supporting IE11. For now I forked the repo and published a npm package with the fix https://www.npmjs.com/package/pons-draft-js