From 0a72ed2977ac92557303cb6033696922e1e7e716 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 22 Apr 2020 18:43:12 +0200 Subject: [PATCH] Fix cursor recovery in pad --- www/common/cursor-treesome.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/www/common/cursor-treesome.js b/www/common/cursor-treesome.js index c3722e8a3..29cb25646 100644 --- a/www/common/cursor-treesome.js +++ b/www/common/cursor-treesome.js @@ -3,7 +3,7 @@ define([], function () { var indexOfNode = tree.indexOfNode = function (el) { if (!(el && el.parentNode)) { - console.log("No parentNode found!"); + console.log("No parentNode found!", el); throw new Error('No parentNode found!'); } return Array.prototype.indexOf.call(el.parentNode.childNodes, el); @@ -26,6 +26,7 @@ define([], function () { leaf nodes of a tree */ var rightmostNode = tree.rightmostNode = function (el) { + if (!el) { return null; } var childNodeCount = childCount(el); if (!childNodeCount) { // no children return el; // return the element @@ -53,7 +54,7 @@ define([], function () { if (!el.parentNode) { return null; } if (i === 0) { if (root && el.parentNode === root.childNodes[0]) { return null; } - return rightmostNode(previousNode(el.parentNode)); + return rightmostNode(previousNode(el.parentNode, root)); } else { return rightmostNode(el.parentNode.childNodes[i-1]); } @@ -77,6 +78,10 @@ define([], function () { // a and b might be the same element if (a === b) { return 0; } + // If we're selecting an entire node containing a single text node, + // b can be the child of a. Order is correct. + if (b.parentNode && b.parentNode === a) { return 1; } + var cur = b; while (cur) { cur = previousNode(cur, root);