From ce0371e8ae1c5255bb2b59d2b127b87180a23264 Mon Sep 17 00:00:00 2001 From: Catrope Date: Sat, 7 Jul 2012 10:57:27 -0700 Subject: [PATCH] Fix bug with nested lists in getCoveredSiblingGroups() For nested lists, this function would return multiple groups where one was wholly contained in the other, use offsets to prevent that from happening. Change-Id: Ib03bb1c81712d805cc263c2975cc3942de63d2ed --- modules/ve/ve.Document.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/modules/ve/ve.Document.js b/modules/ve/ve.Document.js index 5288a2206b..1cd7124f38 100644 --- a/modules/ve/ve.Document.js +++ b/modules/ve/ve.Document.js @@ -371,6 +371,14 @@ ve.Document.prototype.selectNodes = function( range, mode ) { return retval; }; +/** + * Return groups of sibling nodes covered by the given range + * @param {ve.Range} selection Range + * @return {Array} Array of objects. Each object has the following keys: + * nodes: Array of sibling nodes covered by a part of range + * parent: Parent of all of these nodes + * grandparent: parent's parent + */ ve.Document.prototype.getCoveredSiblingGroups = function( selection ) { var leaves = this.selectNodes( selection, 'leaves' ), firstCoveredSibling, @@ -378,10 +386,13 @@ ve.Document.prototype.getCoveredSiblingGroups = function( selection ) { node, parentNode, siblingNode, - groups = []; - // Iterate through covered leaf nodes and process either a conversion or wrapping for groups of - // consecutive covered siblings - for conversion, the entire list will be changed + groups = [], + lastEndOffset = 0; for ( var i = 0; i < leaves.length; i++ ) { + if ( leaves[i].nodeOuterRange.end <= lastEndOffset ) { + // This range is contained within a range we've already processed + continue; + } node = leaves[i].node; // Traverse up to a content branch from content elements if ( node.isContent() ) { @@ -411,6 +422,7 @@ ve.Document.prototype.getCoveredSiblingGroups = function( selection ) { siblingNode = siblingNode.getParent(); } } while ( siblingNode.getParent() === parentNode ); + lastEndOffset = parentNode.getOuterRange().end; } return groups; };