allow enterNodeCallback to decide whether to decend into a node

pull/21833/head
Bruno Windels 2019-05-29 14:27:36 +02:00
parent e1d1c8f99c
commit b0d87e7e47
1 changed files with 9 additions and 8 deletions

View File

@ -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) {