From fbaea888b9d2b195a1bc5568cbf186640e2bb9ce Mon Sep 17 00:00:00 2001 From: Rob Moen Date: Wed, 16 May 2012 15:55:01 -0700 Subject: [PATCH] Rewrite data model methods needed for ui tools getAnnotationRangeFromOffset and offsetContainsAnnotation which deprecated getAnnotationBoundaries, and getIndexOfAnnotation write unit tests for proof Change-Id: I6c0d4e3ca96dd569b1909cd22fce68c3a6fe382c --- modules/ve2/dm/ve.dm.DocumentFragment.js | 48 ++++++++++ tests/ve2/dm/ve.dm.DocumentFragment.test.js | 99 +++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/modules/ve2/dm/ve.dm.DocumentFragment.js b/modules/ve2/dm/ve.dm.DocumentFragment.js index 1244f4409f..1c18d4b1d6 100644 --- a/modules/ve2/dm/ve.dm.DocumentFragment.js +++ b/modules/ve2/dm/ve.dm.DocumentFragment.js @@ -198,6 +198,54 @@ ve.dm.DocumentFragment.prototype.getAnnotationsFromOffset = function( offset ) { return []; }; +/** + * Does this offset contain the specified annotation + * + * @method + * @param {Integer} offset Offset to look at + * @param {Object} annotation Object to look for + * @returns {Boolean} Whether an offset contains the specified annotation + */ +ve.dm.DocumentFragment.prototype.offsetContainsAnnotation = function ( offset, annotation ) { + var annotations = this.getAnnotationsFromOffset( offset ); + for (var i=0;i 0 ) { + start--; + if ( this.offsetContainsAnnotation(start, annotation ) === false ) { + start++; + break; + } + } + while ( end < this.data.length ) { + end++; + if ( this.offsetContainsAnnotation(end, annotation ) === false ) { + end--; + break; + } + } + return new ve.Range( start, end ); +}; + /** * Gets an array of common annnotations across a range. * diff --git a/tests/ve2/dm/ve.dm.DocumentFragment.test.js b/tests/ve2/dm/ve.dm.DocumentFragment.test.js index a2c3ebb29c..41cfbb2866 100644 --- a/tests/ve2/dm/ve.dm.DocumentFragment.test.js +++ b/tests/ve2/dm/ve.dm.DocumentFragment.test.js @@ -310,3 +310,102 @@ test( 'getAnnotationsFromRange', 1, function() { }); +test( 'offsetContainsAnnotation', 1, function(){ + var cases = [ + { + msg: 'contains no annotations', + data: [ + ['a'] + ], + lookFor: {'type': 'bold'}, + expected: false + }, + { + msg: 'contains bold', + data: [ + ['a', { '{"type:"bold"}': { 'type': 'bold' } } ] + ], + lookFor: {'type': 'bold'}, + expected: true + }, + { + msg: 'contains bold', + data: [ + ['a', { + '{"type:"bold"}': { 'type': 'bold' }, + '{"type":"italic"}': { 'type': 'italic'} + } + ] + ], + lookFor: {'type': 'bold'}, + expected: true + } + ], + fragment; + + expect( cases.length ); + + for( var i=0;i