From f8061a97ebd88fd43dd00b90630df25f7c25739d Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Tue, 11 Dec 2012 11:10:22 -0800 Subject: [PATCH] Improve truncate and truncateRange to allow negative values Positive values will move the end, negative values will move the start Change-Id: I566653664bf2a8a74781f1059a51873ab72560f9 --- modules/ve/dm/ve.dm.SurfaceFragment.js | 2 +- modules/ve/ve.Range.js | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/modules/ve/dm/ve.dm.SurfaceFragment.js b/modules/ve/dm/ve.dm.SurfaceFragment.js index 9a1026f724..44b53c9ee9 100644 --- a/modules/ve/dm/ve.dm.SurfaceFragment.js +++ b/modules/ve/dm/ve.dm.SurfaceFragment.js @@ -126,7 +126,7 @@ ve.dm.SurfaceFragment.prototype.adjustRange = function ( start, end ) { * Gets a new fragment with a truncated length. * * @method - * @param {Number} limit Maximum length of range + * @param {Number} limit Maximum length of range (negative for left-side truncation) * @returns {ve.dm.SurfaceFragment} Truncated fragment */ ve.dm.SurfaceFragment.prototype.truncateRange = function ( limit ) { diff --git a/modules/ve/ve.Range.js b/modules/ve/ve.Range.js index 1b987d0e38..67eb00ac5d 100644 --- a/modules/ve/ve.Range.js +++ b/modules/ve/ve.Range.js @@ -141,14 +141,20 @@ ve.Range.prototype.equals = function ( other ) { * Creates a new ve.Range object. * * @method - * @param {Number} Length of the new range. + * @param {Number} Length of the new range (negative for left-side truncation) * @returns {ve.Range} A new range. */ ve.Range.prototype.truncate = function ( length ) { this.normalize(); - return new ve.Range( - this.start, Math.max( this.start, Math.min( this.start + length, this.end ) ) - ); + if ( length >= 0 ) { + return new ve.Range( + this.start, Math.min( this.start + length, this.end ) + ); + } else { + return new ve.Range( + Math.max( this.end + length, this.start ), this.end + ); + } }; /**