diff --git a/lib/models/node.js b/lib/models/node.js index 15afb406c..2097950aa 100644 --- a/lib/models/node.js +++ b/lib/models/node.js @@ -95,19 +95,12 @@ const Node = { // Then remove any nodes in between the top-most start and end nodes... let startParent = node.getParent(startKey) let endParent = node.getParent(endKey) - - const startGrandestParent = node.nodes.find((child) => { - return child == startParent || child.hasDescendant(startParent) - }) - - const endGrandestParent = node.nodes.find((child) => { - return child == endParent || child.hasDescendant(endParent) - }) - + const startAncestor = node.getHighestChild(startParent) + const endAncestor = node.getHighestChild(endParent) const nodes = node.nodes - .takeUntil(child => child == startGrandestParent) - .push(startGrandestParent) - .concat(node.nodes.skipUntil(child => child == endGrandestParent)) + .takeUntil(child => child == startAncestor) + .push(startAncestor) + .concat(node.nodes.skipUntil(child => child == endAncestor)) node = node.merge({ nodes }) @@ -320,6 +313,22 @@ const Node = { return this.nodes.find(node => node.key == key) }, + /** + * Get the highest child ancestor of a node by `key`. + * + * @param {String or Node} key + * @return {Node or Null} node + */ + + getHighestChild(key) { + key = normalizeKey(key) + return this.nodes.find(node => { + if (node.key == key) return true + if (node.kind == 'text') return false + return node.hasDescendant(key) + }) + }, + /** * Get a descendant node by `key`. *