Divec has uploaded a new change for review. https://gerrit.wikimedia.org/r/204985
Change subject: WIP POC: walkDom function for document-order traversal ...................................................................... WIP POC: walkDom function for document-order traversal Change-Id: Ie9f4ed984763409feea2a41703943758f731bd0d --- M src/ve.utils.js 1 file changed, 46 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/85/204985/1 diff --git a/src/ve.utils.js b/src/ve.utils.js index 3377050..3193781 100644 --- a/src/ve.utils.js +++ b/src/ve.utils.js @@ -1345,3 +1345,49 @@ ve.getSystemPlatform = function () { return ( ve.init.platform && ve.init.platform.constructor || ve.init.Platform ).static.getSystemPlatform(); }; + +/** + * Walk DOM nodes in document order (forward or reverse) + * + * @param {number} direction Positive for forward, negative for reverse + * @param {Node} node Starting node + * @param {Function} onStart Callback (return true to stop walking, defined to stop descending) + * @param {Function} [onEnd] Callback (return true to stop walking) + */ +ve.walkDom = function ( direction, node, onStart, onEnd ) { + var stop, + forward = direction > 0; + while ( true ) { + // Descend as far as possible (calling onStart) + // Then ascend while no siblings (calling onEnd) + // Then step to sibling + // If onStart or onEnd returns a true value, stop walking + // Else if onStart returns a defined value, stop descending + while ( true ) { + stop = onStart( node ); + if ( stop ) { + return; + } + if ( stop !== undefined && !node.firstChild ) { + break; + } + node = forward ? node.firstChild : node.lastChild; + } + while ( true ) { + if ( onEnd && onEnd( node ) ) { + return; + } + if ( forward ? node.nextSibling : node.previousSibling ) { + break; + } + node = node.parentNode; + if ( !node ) { + return; + } + } + if ( onEnd && onEnd( node ) ) { + return; + } + node = forward ? node.nextSibling : node.previousSibling; + } +}; -- To view, visit https://gerrit.wikimedia.org/r/204985 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie9f4ed984763409feea2a41703943758f731bd0d Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Divec <da...@sheetmusic.org.uk> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits