(bug 42404) Errors passing "incorrect" offsets to ve.ce.Surface.getNodeAndOffset

* Adjust offsets to correct positions before showing selection with them
* Throw an error inside of getNodeAndOffset if we can't find a match rather than mysteriously not retuning anything

Change-Id: Ia7347527e6466262e819f456404b32926fd95e34
This commit is contained in:
Trevor Parscal 2012-11-26 15:01:35 -08:00
parent 1ba75b7ea9
commit ee80af5f96

View file

@ -950,6 +950,13 @@ ve.ce.Surface.prototype.showSelection = function ( range ) {
rangySel = rangy.getSelection(),
rangyRange = rangy.createRange();
// Ensure the range we are asking to select is from and to correct offsets - failure to do so
// may cause getNodeAndOffset to throw an exception
range = new ve.Range(
this.getNearestCorrectOffset( range.start ),
this.getNearestCorrectOffset( range.end )
);
if ( range.start !== range.end ) {
start = this.getNodeAndOffset( range.start );
end = this.getNodeAndOffset( range.end );
@ -1061,6 +1068,7 @@ ve.ce.Surface.prototype.hasSlugAtOffset = function ( offset ) {
* @param {Number} offset Linear model offset
* @returns {Object} Object containing a node and offset property where node is an HTML element and
* offset is the position within the element
* @throws {Error} Offset could not be translated to a DOM element and offset
*/
ve.ce.Surface.prototype.getNodeAndOffset = function ( offset ) {
var node, startOffset, current, stack, item, $item, length,
@ -1116,10 +1124,10 @@ ve.ce.Surface.prototype.getNodeAndOffset = function ( offset ) {
current = stack[stack.length-1];
continue;
}
}
current[1]++;
}
throw new Error( 'Offset could not be translated to a DOM element and offset: ' + offset );
};
/**