mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/DiscussionTools
synced 2024-11-24 00:13:36 +00:00
Merge "Use the faster childIndexOf() approach in JS too"
This commit is contained in:
commit
08e6db455d
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue