From ed32159be7fbd5b904374e1b71460b7f54653d38 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Thu, 16 Nov 2017 13:12:42 -0800 Subject: [PATCH] fix to default window in findDOM* utils --- packages/slate-react/src/utils/find-dom-node.js | 5 +++-- packages/slate-react/src/utils/find-dom-point.js | 10 +++++----- packages/slate-react/src/utils/find-dom-range.js | 9 +++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/slate-react/src/utils/find-dom-node.js b/packages/slate-react/src/utils/find-dom-node.js index 48b7bd9bb..cc4218c34 100644 --- a/packages/slate-react/src/utils/find-dom-node.js +++ b/packages/slate-react/src/utils/find-dom-node.js @@ -5,15 +5,16 @@ import { Node } from 'slate' * Find the DOM node for a `key`. * * @param {String|Node} key + * @param {Window} win (optional) * @return {Element} */ -function findDOMNode(key, window) { +function findDOMNode(key, win = window) { if (Node.isNode(key)) { key = key.key } - const el = window.document.querySelector(`[data-key="${key}"]`) + const el = win.document.querySelector(`[data-key="${key}"]`) if (!el) { throw new Error(`Unable to find a DOM node for "${key}". This is often because of forgetting to add \`props.attributes\` to a custom component.`) diff --git a/packages/slate-react/src/utils/find-dom-point.js b/packages/slate-react/src/utils/find-dom-point.js index 2d5c69844..14a7fa22a 100644 --- a/packages/slate-react/src/utils/find-dom-point.js +++ b/packages/slate-react/src/utils/find-dom-point.js @@ -4,20 +4,20 @@ import findDOMNode from './find-dom-node' /** * Find a native DOM selection point from a Slate `key` and `offset`. * - * @param {Element} root * @param {String} key * @param {Number} offset - * @return {Object} + * @param {Window} win (optional) + * @return {Object|Null} */ -function findDOMPoint(key, offset, window) { - const el = findDOMNode(key, window) +function findDOMPoint(key, offset, win = window) { + const el = findDOMNode(key, win) let start = 0 let n // COMPAT: In IE, this method's arguments are not optional, so we have to // pass in all four even though the last two are defaults. (2017/10/25) - const iterator = window.document.createNodeIterator( + const iterator = win.document.createNodeIterator( el, NodeFilter.SHOW_TEXT, () => NodeFilter.FILTER_ACCEPT, diff --git a/packages/slate-react/src/utils/find-dom-range.js b/packages/slate-react/src/utils/find-dom-range.js index 0a0b9ac3b..9abbd6f3d 100644 --- a/packages/slate-react/src/utils/find-dom-range.js +++ b/packages/slate-react/src/utils/find-dom-range.js @@ -5,16 +5,17 @@ import findDOMPoint from './find-dom-point' * Find a native DOM range Slate `range`. * * @param {Range} range + * @param {Window} win (optional) * @return {Object|Null} */ -function findDOMRange(range, window) { +function findDOMRange(range, win = window) { const { anchorKey, anchorOffset, focusKey, focusOffset, isBackward, isCollapsed } = range - const anchor = findDOMPoint(anchorKey, anchorOffset, window) - const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, window) + const anchor = findDOMPoint(anchorKey, anchorOffset, win) + const focus = isCollapsed ? anchor : findDOMPoint(focusKey, focusOffset, win) if (!anchor || !focus) return null - const r = window.document.createRange() + const r = win.document.createRange() const start = isBackward ? focus : anchor const end = isBackward ? anchor : focus r.setStart(start.node, start.offset)