diff --git a/lib/models/node.js b/lib/models/node.js index 48c702fc4..6e394fb65 100644 --- a/lib/models/node.js +++ b/lib/models/node.js @@ -408,12 +408,12 @@ const Node = { getNextSibling(key) { key = normalizeKey(key) - const shallow = this.nodes - .skipUntil(node => node.key == key) - .rest() - .first() - - if (shallow != null) return shallow + if (this.nodes.has(key)) { + return this.nodes + .skipUntil(node => node.key == key) + .rest() + .first() + } return this.nodes .map(node => node.kind == 'text' ? null : node.getNextSibling(key)) @@ -437,7 +437,7 @@ const Node = { }, /** - * Get the child text node at an `offset`. + * Get the offset for a child text node by `key`. * * @param {String or Node} key * @return {Number} offset @@ -447,7 +447,7 @@ const Node = { this.assertHasDeep(key) const match = this.getDeep(key) - // Get all of the nodes that come before the matching child. + // Find the shallow matching child. const child = this.nodes.find((node) => { if (node == match) return true return node.kind == 'text' @@ -455,6 +455,7 @@ const Node = { : node.hasDeep(match) }) + // Get all of the nodes that come before the matching child. const befores = this.nodes.takeUntil(node => node.key == child.key) // Calculate the offset of the nodes before the matching child. @@ -480,8 +481,8 @@ const Node = { key = normalizeKey(key) if (this.nodes.get(key)) return this - let node = null + let node = null this.nodes.forEach((child) => { if (child.kind == 'text') return const match = child.getParent(key) @@ -501,9 +502,7 @@ const Node = { getPreviousSibling(key) { key = normalizeKey(key) - const matches = this.nodes.get(key) - - if (matches) { + if (this.nodes.has(key)) { return this.nodes .takeUntil(node => node.key == key) .last() @@ -538,7 +537,7 @@ const Node = { getTextAtOffset(offset) { let length = 0 - let texts = this.filterDeep(node => node.kind == 'text') + let texts = this.getTextNodes() let match = texts.find((node) => { length += node.length return length >= offset