All files / src ve.Range.js

100% Statements 91/91
96% Branches 48/50
100% Functions 25/25
100% Lines 87/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366                          1x         45633x 45633x 45633x 45633x         1x                                                       1x 2x                     1x 226x                     1x 17x 1x   16x 16x 16x 32x 16x   16x 3x   16x 12x     16x                           1x 12x                 1x 1386x                             1x 7x                         1x 7x               1x 1981x               1x 32x                 1x 5x                 1x 2605x                 1x 5314x                 1x 15x 11x       4x                       1x 7x                   1x 29921x               1x 4555x                 1x 1212x                         1x 44x 44x 197x   44x               1x 2924x 19320x                   1x 2x 2x 1x     1x                 1x 82x 347x 12x     70x                       1x 20x 20x   20x 19x     20x 8x     20x                       1x 39x 39x   39x 33x     39x 22x     39x    
/*!
 * VisualEditor Range class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * @class
 *
 * @constructor
 * @param {number} [from=0] Anchor offset
 * @param {number} [to=from] Focus offset
 */
ve.Range = function VeRange( from, to ) {
	// For ease of debugging, check arguments.length when applying defaults, to preserve
	// invalid arguments such as undefined and NaN that indicate a programming error.
	// Range calculation errors can often propagate quite far before surfacing, so the
	// indication is important.
	this.from = arguments.length >= 1 ? from : 0;
	this.to = arguments.length >= 2 ? to : this.from;
	this.start = this.from < this.to ? this.from : this.to;
	this.end = this.from < this.to ? this.to : this.from;
};
 
/* Inheritance */
 
OO.initClass( ve.Range );
 
/**
 * @property {number} from Starting offset
 */
 
/**
 * @property {number} to Ending offset
 */
 
/**
 * @property {number} start Starting offset (the lesser of #to and #from)
 */
 
/**
 * @property {number} end Ending offset (the greater of #to and #from)
 */
 
/* Static Methods */
 
/**
 * Create a new range from a JSON serialization of a range
 *
 * @see ve.Range#toJSON
 *
 * @param {string} json JSON serialization
 * @return {ve.Range} New range
 */
ve.Range.static.newFromJSON = function ( json ) {
	return this.newFromHash( JSON.parse( json ) );
};
 
/**
 * Create a new range from a range hash object
 *
 * @see ve.Range#toJSON
 *
 * @param {Object} hash
 * @return {ve.Range} New range
 */
ve.Range.static.newFromHash = function ( hash ) {
	return new ve.Range( hash.from, hash.to );
};
 
/**
 * Create a range object that covers all of the given ranges.
 *
 * @static
 * @param {ve.Range[]} ranges Array of ve.Range objects (at least one)
 * @param {boolean} backwards Return a backwards range
 * @return {ve.Range} Range that spans all of the given ranges
 */
ve.Range.static.newCoveringRange = function ( ranges, backwards ) {
	if ( ranges.length === 0 ) {
		throw new Error( 'newCoveringRange() requires at least one range' );
	}
	let minStart = ranges[ 0 ].start;
	let maxEnd = ranges[ 0 ].end;
	ranges.forEach( ( range, i ) => {
		if ( i === 0 ) {
			return;
		}
		if ( range.start < minStart ) {
			minStart = range.start;
		}
		if ( range.end > maxEnd ) {
			maxEnd = range.end;
		}
	} );
	return backwards ? new ve.Range( maxEnd, minStart ) : new ve.Range( minStart, maxEnd );
};
 
/* Methods */
 
/**
 * Check if an offset is within the range.
 *
 * Specifically we mean the whole element at a specific offset, so in effect
 * this is the same as #containsRange( new ve.Range( offset, offset + 1 ) ).
 *
 * @param {number} offset Offset to check
 * @return {boolean} If offset is within the range
 */
ve.Range.prototype.containsOffset = function ( offset ) {
	return offset >= this.start && offset < this.end;
};
 
/**
 * Check if another range is within the range.
 *
 * @param {ve.Range} range Range to check
 * @return {boolean} If other range is within the range
 */
ve.Range.prototype.containsRange = function ( range ) {
	return range.start >= this.start && range.end <= this.end;
};
 
/**
 * Check if another range is touching this one
 *
 * This includes ranges which touch this one, e.g. [1,3] & [3,5],
 * ranges which overlap this one, and ranges which cover
 * this one completely, e.g. [1,3] & [0,5].
 *
 * Useful for testing if two ranges can be joined (using #expand)
 *
 * @param {ve.Range} range Range to check
 * @return {boolean} If other range touches this range
 */
ve.Range.prototype.touchesRange = function ( range ) {
	return range.end >= this.start && range.start <= this.end;
};
 
/**
 * Check if another range overlaps this one
 *
 * This includes ranges which intersect this one, e.g. [1,3] & [2,4],
 * and ranges which cover this one completely, e.g. [1,3] & [0,5],
 * but *not* ranges which only touch, e.g. [0,2] & [2,4].
 *
 * @param {ve.Range} range Range to check
 * @return {boolean} If other range overlaps this range
 */
ve.Range.prototype.overlapsRange = function ( range ) {
	return range.end > this.start && range.start < this.end;
};
 
/**
 * Get the length of the range.
 *
 * @return {number} Length of range
 */
ve.Range.prototype.getLength = function () {
	return this.end - this.start;
};
 
/**
 * Gets a range with reversed direction.
 *
 * @return {ve.Range} A new range
 */
ve.Range.prototype.flip = function () {
	return new ve.Range( this.to, this.from );
};
 
/**
 * Get a range that's a translated version of this one.
 *
 * @param {number} distance Distance to move range by
 * @return {ve.Range} New translated range
 */
ve.Range.prototype.translate = function ( distance ) {
	return new ve.Range( this.from + distance, this.to + distance );
};
 
/**
 * Check if two ranges are equal, taking direction into account.
 *
 * @param {ve.Range|null} other
 * @return {boolean}
 */
ve.Range.prototype.equals = function ( other ) {
	return other && this.from === other.from && this.to === other.to;
};
 
/**
 * Check if two ranges are equal, ignoring direction.
 *
 * @param {ve.Range|null} other
 * @return {boolean}
 */
ve.Range.prototype.equalsSelection = function ( other ) {
	return other && this.end === other.end && this.start === other.start;
};
 
/**
 * Create a new range with a truncated length.
 *
 * @param {number} limit Maximum length of new range (negative for left-side truncation)
 * @return {ve.Range} A new range
 */
ve.Range.prototype.truncate = function ( limit ) {
	if ( limit >= 0 ) {
		return new ve.Range(
			this.start, Math.min( this.start + limit, this.end )
		);
	} else {
		return new ve.Range(
			Math.max( this.end + limit, this.start ), this.end
		);
	}
};
 
/**
 * Expand a range to include another range, preserving direction.
 *
 * @param {ve.Range} other Range to expand to include
 * @return {ve.Range} Range covering this range and other
 */
ve.Range.prototype.expand = function ( other ) {
	return ve.Range.static.newCoveringRange( [ this, other ], this.isBackwards() );
};
 
/**
 * Check if the range is collapsed.
 *
 * A collapsed range has equal start and end values making its length zero.
 *
 * @return {boolean} Range is collapsed
 */
ve.Range.prototype.isCollapsed = function () {
	return this.from === this.to;
};
 
/**
 * Check if the range is backwards, i.e. from > to
 *
 * @return {boolean} Range is backwards
 */
ve.Range.prototype.isBackwards = function () {
	return this.from > this.to;
};
 
/**
 * Get a object summarizing the range for JSON serialization
 *
 * @param {string} [key] Key in parent object
 * @return {Object} Object for JSON serialization
 */
ve.Range.prototype.toJSON = function () {
	return {
		type: 'range',
		from: this.from,
		to: this.to
	};
};
 
/**
 * Iterate over the range, calling a function for each index, to build an array.
 *
 * @param {function(number): any} callback Function called for each index
 * @return {Array} Array of results
 */
ve.Range.prototype.map = function ( callback ) {
	const result = [];
	for ( let i = this.start; i < this.end; i++ ) {
		result.push( callback( i ) );
	}
	return result;
};
 
/**
 * Iterate over the range, calling a function for each index
 *
 * @param {function(number): void} callback Function called for each index
 */
ve.Range.prototype.forEach = function ( callback ) {
	for ( let i = this.start; i < this.end; i++ ) {
		callback( i );
	}
};
 
/**
 * Test whether at least one index in the range passes the test implemented by the provided function.
 *
 * @param {function(number): boolean} callback Function called for each index
 * @return {boolean} True if the callback returns true for any index
 */
ve.Range.prototype.some = function ( callback ) {
	for ( let i = this.start; i < this.end; i++ ) {
		if ( callback( i ) ) {
			return true;
		}
	}
	return false;
};
 
/**
 * Test whether all indices in the range pass the test implemented by the provided function.
 *
 * @param {function(number): boolean} callback Function called for each index
 * @return {boolean} True if the callback returns true for all indices
 */
ve.Range.prototype.every = function ( callback ) {
	for ( let i = this.start; i < this.end; i++ ) {
		if ( !callback( i ) ) {
			return false;
		}
	}
	return true;
};
 
/**
 * Trim elements from both ends of the range using a callback.
 *
 * Walks from start forwards and end backwards, removing elements for which callback returns true.
 * Stops trimming at each end when callback returns false. Returns a new trimmed range.
 *
 * @param {function(number): boolean} callback Called with index
 * @return {ve.Range} New trimmed range
 */
ve.Range.prototype.trimCallback = function ( callback ) {
	let newStart = this.start;
	let newEnd = this.end;
 
	while ( newEnd > newStart && callback( newEnd - 1 ) ) {
		newEnd--;
	}
 
	while ( newStart < newEnd && callback( newStart ) ) {
		newStart++;
	}
 
	return this.isBackwards() ? new ve.Range( newEnd, newStart ) : new ve.Range( newStart, newEnd );
};
 
/**
 * Expand the range outwards from both ends while callback returns true.
 * Walks from start-1 backwards and end forwards, expanding as long as callback returns true.
 * Stops expanding at each end when callback returns false. Returns a new expanded range.
 *
 * @param {function(number, number): boolean} callback Called with (index, iFromStartOrEnd)
 * @param {ve.Range} maxRange Maximum range to expand to
 * @return {ve.Range} New expanded range
 */
ve.Range.prototype.expandCallback = function ( callback, maxRange ) {
	let newStart = this.start;
	let newEnd = this.end;
 
	while ( newEnd < maxRange.end && callback( newEnd ) ) {
		newEnd++;
	}
 
	while ( newStart > maxRange.start && callback( newStart - 1 ) ) {
		newStart--;
	}
 
	return this.isBackwards() ? new ve.Range( newEnd, newStart ) : new ve.Range( newStart, newEnd );
};