From b0d87e7e4731fd59b79f9e0f9d1a7197656db078 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 29 May 2019 14:27:36 +0200 Subject: [PATCH] allow enterNodeCallback to decide whether to decend into a node --- src/editor/dom.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/editor/dom.js b/src/editor/dom.js index ffdb8bed68..3ef1df24c3 100644 --- a/src/editor/dom.js +++ b/src/editor/dom.js @@ -15,22 +15,22 @@ See the License for the specific language governing permissions and limitations under the License. */ -function walkDOMDepthFirst(editor, enterNodeCallback, leaveNodeCallback) { - let node = editor.firstChild; - while (node && node !== editor) { - enterNodeCallback(node); - if (node.firstChild) { +export function walkDOMDepthFirst(rootNode, enterNodeCallback, leaveNodeCallback) { + let node = rootNode.firstChild; + while (node && node !== rootNode) { + const shouldDecend = enterNodeCallback(node); + if (shouldDecend && node.firstChild) { node = node.firstChild; } else if (node.nextSibling) { node = node.nextSibling; } else { - while (!node.nextSibling && node !== editor) { + while (!node.nextSibling && node !== rootNode) { node = node.parentElement; - if (node !== editor) { + if (node !== rootNode) { leaveNodeCallback(node); } } - if (node !== editor) { + if (node !== rootNode) { node = node.nextSibling; } } @@ -62,6 +62,7 @@ export function getCaretOffsetAndText(editor, sel) { } text += nodeText; } + return true; } function leaveNodeCallback(node) {