Add indexInNode to selectNodes() output

This is for the case where we have a zero-length range in between two
siblings, and we need to know what index that corresponds to in order to
be able to insert nodes there (rebuildNodes() will use it for this
purpose)

Change-Id: I357d1cd665667a76f955a10b8d9d2810976cdbd7
This commit is contained in:
Catrope 2012-05-10 20:08:12 -07:00
parent 379bc8da3c
commit 6652f7573b
2 changed files with 20 additions and 0 deletions

View file

@ -35,6 +35,10 @@ ve.Document.prototype.getDocumentNode = function() {
* 'node': Reference to a ve.dm.Node
* 'range': ve.Range, missing if the entire node is covered
* 'index': Index of the node in its parent, missing if node has no parent
* 'indexInNode': If range is a zero-length range between two children of node,
* this is set to the index of the child following range (or to
* node.children.length+1 if range is between the last child and
* the end). Missing in all other cases
* 'nodeRange': Range covering the inside of the entire node
* @throws 'Invalid start offset' if range.start is out of range
* @throws 'Invalid end offset' if range.end is out of range
@ -108,6 +112,7 @@ ve.Document.prototype.selectNodes = function( range, mode ) {
// Empty range in the parent, outside of any child
retval = [ {
'node': currentFrame.node,
'indexInNode': currentFrame.index + ( endBetween ? 1 : 0 ),
'range': new ve.Range( start, end ),
'nodeRange': new ve.Range( currentFrame.startOffset,
currentFrame.startOffset + currentFrame.node.getLength()

View file

@ -154,6 +154,20 @@ ve.example.getSelectNodesCases = function( doc ) {
'nodeRange': new ve.Range( 1, 4 )
}
]
},
// Zero-length range between two children of the document
{
'actual': doc.selectNodes( new ve.Range( 5, 5 ), 'leaves' ),
'expected': [
// document
{
'node': documentNode,
'range': new ve.Range( 5, 5 ),
// no 'index' because documentNode has no parent
'indexInNode': 1,
'nodeRange': new ve.Range( 0, 53 )
}
]
}
];
};
@ -196,6 +210,7 @@ ve.example.nodeSelectionEqual = function( a, b ) {
strictEqual( 'range' in a[i], 'range' in b[i], 'range existence match' );
}
deepEqual( a[i].index, b[i].index, 'index match' );
deepEqual( a[i].indexInNode, b[i].indexInNode, 'indexInNode match' );
deepEqual( a[i].nodeRange, b[i].nodeRange, 'nodeRange match' );
}
};