allow enterNodeCallback to decide whether to decend into a node

This commit is contained in:
Bruno Windels 2019-05-29 14:27:36 +02:00
parent e1d1c8f99c
commit b0d87e7e47

View file

@ -15,22 +15,22 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
function walkDOMDepthFirst(editor, enterNodeCallback, leaveNodeCallback) { export function walkDOMDepthFirst(rootNode, enterNodeCallback, leaveNodeCallback) {
let node = editor.firstChild; let node = rootNode.firstChild;
while (node && node !== editor) { while (node && node !== rootNode) {
enterNodeCallback(node); const shouldDecend = enterNodeCallback(node);
if (node.firstChild) { if (shouldDecend && node.firstChild) {
node = node.firstChild; node = node.firstChild;
} else if (node.nextSibling) { } else if (node.nextSibling) {
node = node.nextSibling; node = node.nextSibling;
} else { } else {
while (!node.nextSibling && node !== editor) { while (!node.nextSibling && node !== rootNode) {
node = node.parentElement; node = node.parentElement;
if (node !== editor) { if (node !== rootNode) {
leaveNodeCallback(node); leaveNodeCallback(node);
} }
} }
if (node !== editor) { if (node !== rootNode) {
node = node.nextSibling; node = node.nextSibling;
} }
} }
@ -62,6 +62,7 @@ export function getCaretOffsetAndText(editor, sel) {
} }
text += nodeText; text += nodeText;
} }
return true;
} }
function leaveNodeCallback(node) { function leaveNodeCallback(node) {