diff --git a/modules/parser.js b/modules/parser.js index f73496153..42b1131cd 100644 --- a/modules/parser.js +++ b/modules/parser.js @@ -754,13 +754,13 @@ function getComments( rootNode ) { match = timestamps[ nextTimestamp ][ 1 ]; range = { startContainer: startNode.parentNode, - startOffset: Array.prototype.indexOf.call( startNode.parentNode.childNodes, startNode ), + startOffset: utils.childIndexOf( startNode ), endContainer: node, endOffset: match.index + match[ 0 ].length }; sigRange = { startContainer: firstSigNode.parentNode, - startOffset: Array.prototype.indexOf.call( firstSigNode.parentNode.childNodes, firstSigNode ), + startOffset: utils.childIndexOf( firstSigNode ), endContainer: node, endOffset: match.index + match[ 0 ].length }; diff --git a/modules/utils.js b/modules/utils.js index 31135b943..2963886a4 100644 --- a/modules/utils.js +++ b/modules/utils.js @@ -15,6 +15,20 @@ function getNativeRange( comment ) { return nativeRange; } +/** + * Get the index of a node in its parentNode's childNode list + * + * @param {Node} child + * @return {number} Index in parentNode's childNode list + */ +function childIndexOf( child ) { + var i = 0; + while ( ( child = child.previousSibling ) ) { + i++; + } + return i; +} + /** * Find closest ancestor element using one of the given tag names. * @@ -48,6 +62,7 @@ function htmlTrim( str ) { module.exports = { getNativeRange: getNativeRange, + childIndexOf: childIndexOf, closestElement: closestElement, htmlTrim: htmlTrim }; diff --git a/tests/qunit/testUtils.js b/tests/qunit/testUtils.js index 31d496005..ea1147418 100644 --- a/tests/qunit/testUtils.js +++ b/tests/qunit/testUtils.js @@ -1,3 +1,6 @@ +var + utils = require( 'ext.discussionTools.init' ).utils; + module.exports = {}; /* eslint-disable qunit/no-commented-tests */ @@ -46,18 +49,6 @@ module.exports.overrideMwConfig = function ( config ) { ); }; -/** - * Get the index of a node in its parentNode's childNode list - * - * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org - * - * @param {Node} node The node - * @return {number} Index in parentNode's childNode list - */ -function parentIndex( node ) { - return Array.prototype.indexOf.call( node.parentNode.childNodes, node ); -} - /** * Get the offset path from ancestor to offset in descendant * @@ -76,7 +67,7 @@ function getOffsetPath( ancestor, node, nodeOffset ) { console.log( node, 'is not a descendant of', ancestor ); throw new Error( 'Not a descendant' ); } - path.unshift( parentIndex( node ) ); + path.unshift( utils.childIndexOf( node ) ); node = node.parentNode; } return path;