From 3e7116dce1729663fddb8190419210d8c741bd8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rge=20N=C3=A6ss?= <bjoerge@gmail.com> Date: Tue, 18 Sep 2018 03:38:49 +0200 Subject: [PATCH] Gracefully handle permission denied error when checking for element.nodeType (#2178) --- .../slate-react/src/components/content.js | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/slate-react/src/components/content.js b/packages/slate-react/src/components/content.js index 3f3b99ca5..b97eccc9a 100644 --- a/packages/slate-react/src/components/content.js +++ b/packages/slate-react/src/components/content.js @@ -14,6 +14,8 @@ import getChildrenDecorations from '../utils/get-children-decorations' import scrollToSelection from '../utils/scroll-to-selection' import removeAllRanges from '../utils/remove-all-ranges' +const FIREFOX_NODE_TYPE_ACCESS_ERROR = /Permission denied to access property "nodeType"/ + /** * Debug. * @@ -264,9 +266,24 @@ class Content extends React.Component { isInEditor = target => { const { element } = this - // COMPAT: Text nodes don't have `isContentEditable` property. So, when - // `target` is a text node use its parent node for check. - const el = target.nodeType === 3 ? target.parentNode : target + + let el + + try { + // COMPAT: Text nodes don't have `isContentEditable` property. So, when + // `target` is a text node use its parent node for check. + el = target.nodeType === 3 ? target.parentNode : target + } catch (err) { + // COMPAT: In Firefox, `target.nodeType` will throw an error if target is + // originating from an internal "restricted" element (e.g. a stepper + // arrow on a number input) + // see github.com/ianstormtaylor/slate/issues/1819 + if (IS_FIREFOX && FIREFOX_NODE_TYPE_ACCESS_ERROR.test(err.message)) { + return false + } + + throw err + } return ( el.isContentEditable && (el === element || el.closest('[data-slate-editor]') === element)