All files / src/dm ve.dm.AnnotationSet.js

97.59% Statements 162/166
86.36% Branches 38/44
100% Functions 47/47
97.41% Lines 151/155

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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577                          1x   37554x 37554x   37554x                             1x 52057x               1x 7814x                 1x 18x 11x                     1x 2x 4x                           1x 124x 130x                   1x 2x 3x                           1x 42118x 3281x   38837x                   1x 9028x               1x 56481x               1x 40629x               1x 13883x                     1x 118x                 1x 4613x                 1x 2x 2x 2x 1x 1x     1x                 1x 21x 21x 21x 23x 7x     14x                 1x 159x                 1x 3273x                       1x     5146x 3087x       3087x   5146x 1123x 1123x 1123x 788x 255x   533x       4891x                       1x 455x 288x                   1x 18x 18x 18x 8x     10x                         1x 1512x 228x                           1x 92x                     1x 2740x   2740x 2543x 113x 23x       197x   2520x                     1x 5x 5x                           1x 2029x 2029x 2029x 100x   1929x 69x 9x     1920x                             1x 200x   200x       200x 195x 195x     5x 4x                       1x 417x 417x 413x   417x                       1x 585x               1x 1501x                 1x 646x     646x     646x                   1x 3114x 3114x 3094x                     1x 36x 36x 36x             1x 3088x                   1x 18x               1x 2x                   1x 1x 1x 1x                     1x 1x 1x 1x                 1x 1x 1x 1x                       1x 1x 1x 1x    
/*!
 * VisualEditor DataModel AnnotationSet class.
 *
 * @copyright See AUTHORS.txt
 */
 
/**
 * Annotation set.
 *
 * @constructor
 * @param {ve.dm.HashValueStore} store
 * @param {string[]} [storeHashes]
 */
ve.dm.AnnotationSet = function VeDmAnnotationSet( store, storeHashes ) {
	// Parent constructor
	this.store = store;
	this.storeHashes = storeHashes || [];
 
	Iif ( this.get().indexOf( undefined ) !== -1 ) {
		throw new Error( 'Annotation with hash ' +
			this.storeHashes[ this.get().indexOf( undefined ) ] +
			' not found in store'
		);
	}
};
 
/* Methods */
 
/**
 * Get the hash-value store.
 *
 * @return {ve.dm.HashValueStore} Hash-value store
 */
ve.dm.AnnotationSet.prototype.getStore = function () {
	return this.store;
};
 
/**
 * Get a clone.
 *
 * @return {ve.dm.AnnotationSet} Copy of annotation set
 */
ve.dm.AnnotationSet.prototype.clone = function () {
	return new ve.dm.AnnotationSet( this.getStore(), this.storeHashes.slice() );
};
 
/**
 * Get an annotation set containing only annotations within the set with a specific name.
 *
 * @param {string} name Type name
 * @return {ve.dm.AnnotationSet} Copy of annotation set
 */
ve.dm.AnnotationSet.prototype.getAnnotationsByName = function ( name ) {
	return this.filter( function ( annotation ) {
		return annotation.name === name;
	} );
};
 
/**
 * Get an annotation set containing only annotations within the set which are comparable
 * to a specific annotation.
 *
 * @param {ve.dm.Annotation} annotation Annotation to compare to
 * @return {ve.dm.AnnotationSet} Copy of annotation set
 */
ve.dm.AnnotationSet.prototype.getComparableAnnotations = function ( annotation ) {
	return this.filter( function ( a ) {
		return ve.compare(
			annotation.getComparableObject(),
			a.getComparableObject()
		);
	} );
};
 
/**
 * Get an annotation set containing only annotations within the set which are comparable
 * to an annotation from another set.
 *
 * @param {ve.dm.AnnotationSet} annotations Annotation set to compare to
 * @return {ve.dm.AnnotationSet} Copy of annotation set
 */
ve.dm.AnnotationSet.prototype.getComparableAnnotationsFromSet = function ( annotations ) {
	return this.filter( function ( a ) {
		return annotations.containsComparable( a );
	} );
};
 
/**
 * Check if any annotations in the set have a specific name.
 *
 * @param {string} name Type name
 * @return {boolean} Annotation of given type exists in the set
 */
ve.dm.AnnotationSet.prototype.hasAnnotationWithName = function ( name ) {
	return this.containsMatching( function ( annotation ) {
		return annotation.name === name;
	} );
};
 
/**
 * Get an annotation or all annotations from the set.
 *
 * set.get( 5 ) returns the annotation at offset 5, set.get() returns an array with all annotations
 * in the entire set.
 *
 * @param {number} [offset] If set, only get the annotation at the offset
 * @return {ve.dm.Annotation[]|ve.dm.Annotation|undefined} The annotation at offset, or an array of all
 *  annotations in the set
 */
ve.dm.AnnotationSet.prototype.get = function ( offset ) {
	if ( offset !== undefined ) {
		return this.getStore().value( this.getHash( offset ) );
	} else {
		return this.getStore().values( this.getHashes() );
	}
};
 
/**
 * Get store hash from offset within annotation set.
 *
 * @param {number} offset Offset within annotation set
 * @return {string} Store hash at specified offset
 */
ve.dm.AnnotationSet.prototype.getHash = function ( offset ) {
	return this.storeHashes[ offset ];
};
 
/**
 * Get all store hashes.
 *
 * @return {string[]} Store hashes
 */
ve.dm.AnnotationSet.prototype.getHashes = function () {
	return this.storeHashes;
};
 
/**
 * Get the length of the set.
 *
 * @return {number} The number of annotations in the set
 */
ve.dm.AnnotationSet.prototype.getLength = function () {
	return this.storeHashes.length;
};
 
/**
 * Check if the set is empty.
 *
 * @return {boolean} The set is empty
 */
ve.dm.AnnotationSet.prototype.isEmpty = function () {
	return this.getLength() === 0;
};
 
/**
 * Check whether a given annotation occurs in the set.
 *
 * Annotations are compared by store hash.
 *
 * @param {ve.dm.Annotation} annotation
 * @return {boolean} There is an annotation in the set with the same hash as annotation
 */
ve.dm.AnnotationSet.prototype.contains = function ( annotation ) {
	return this.offsetOf( annotation ) !== -1;
};
 
/**
 * Check whether a given store hash occurs in the set.
 *
 * @param {string} storeHash Store hash of annotation
 * @return {boolean} There is an annotation in the set with this store hash
 */
ve.dm.AnnotationSet.prototype.containsHash = function ( storeHash ) {
	return this.getHashes().indexOf( storeHash ) !== -1;
};
 
/**
 * Check whether the set contains any of the annotations in another set.
 *
 * @param {ve.dm.AnnotationSet} set Set to compare the set with
 * @return {boolean} There is at least one annotation in set that is also in the set
 */
ve.dm.AnnotationSet.prototype.containsAnyOf = function ( set ) {
	var setHashes = set.getHashes(),
		thisHashes = this.getHashes();
	for ( var i = 0, length = setHashes.length; i < length; i++ ) {
		Eif ( thisHashes.indexOf( setHashes[ i ] ) !== -1 ) {
			return true;
		}
	}
	return false;
};
 
/**
 * Check whether the set contains all of the annotations in another set.
 *
 * @param {ve.dm.AnnotationSet} set Set to compare the set with
 * @return {boolean} All annotations in set are also in the set
 */
ve.dm.AnnotationSet.prototype.containsAllOf = function ( set ) {
	var setHashes = set.getHashes(),
		thisHashes = this.getHashes();
	for ( var i = 0, length = setHashes.length; i < length; i++ ) {
		if ( thisHashes.indexOf( setHashes[ i ] ) === -1 ) {
			return false;
		}
	}
	return true;
};
 
/**
 * Get the offset of a given annotation in the set.
 *
 * @param {ve.dm.Annotation} annotation Annotation to search for
 * @return {number} Offset of annotation in the set, or -1 if annotation is not in the set.
 */
ve.dm.AnnotationSet.prototype.offsetOf = function ( annotation ) {
	return this.offsetOfHash( this.getStore().hashOfValue( annotation ) );
};
 
/**
 * Get the offset of a given annotation in the set by store hash.
 *
 * @param {string} storeHash Store hash of annotation to search for
 * @return {number} Offset of annotation in the set, or -1 if annotation is not in the set.
 */
ve.dm.AnnotationSet.prototype.offsetOfHash = function ( storeHash ) {
	return this.getHashes().indexOf( storeHash );
};
 
/**
 * Filter the set by an item property.
 *
 * This returns a new set with all annotations in the set for which the callback returned true for.
 *
 * @param {Function} callback Function that takes an annotation and returns boolean true to include
 * @param {boolean} [returnBool] For internal use only
 * @return {ve.dm.AnnotationSet|boolean} New set containing only the matching annotations
 */
ve.dm.AnnotationSet.prototype.filter = function ( callback, returnBool ) {
	var result;
 
	if ( !returnBool ) {
		result = this.clone();
		// TODO: Should we be returning this on all methods that modify the original? Might help
		// with chainability, but perhaps it's also confusing because most chainable methods return
		// a new hash set.
		result.removeAll();
	}
	for ( var i = 0, length = this.getLength(); i < length; i++ ) {
		var storeHash = this.getHash( i );
		var annotation = this.getStore().value( storeHash );
		if ( callback( annotation ) ) {
			if ( returnBool ) {
				return true;
			} else {
				result.storeHashes.push( storeHash );
			}
		}
	}
	return returnBool ? false : result;
};
 
/**
 * Check if the set contains an annotation comparable to the specified one.
 *
 * getComparableObject is used to compare the annotations, and should return
 * true if an annotation is found which is mergeable with the specified one.
 *
 * @param {ve.dm.Annotation} annotation Annotation to compare to
 * @return {boolean} At least one comparable annotation found
 */
ve.dm.AnnotationSet.prototype.containsComparable = function ( annotation ) {
	return this.filter( function ( a ) {
		return annotation.compareTo( a );
	}, true );
};
 
/**
 * Get the first annotation mergeable with the specified one
 *
 * @param {ve.dm.Annotation} annotation Annotation to compare to
 * @return {ve.dm.Annotation|null} First matching annotation
 */
ve.dm.AnnotationSet.prototype.getComparable = function ( annotation ) {
	for ( var i = 0, len = this.getLength(); i < len; i++ ) {
		var ann = this.getStore().value( this.getHash( i ) );
		if ( ann.compareTo( annotation ) ) {
			return ann;
		}
	}
	return null;
};
 
/**
 * FIXME T126031: Check if the set contains an annotation comparable to the
 * specified one for the purposes of serialization.
 *
 * This method uses getComparableObjectForSerialization which also includes
 * HTML attributes.
 *
 * @param {ve.dm.Annotation} annotation Annotation to compare to
 * @return {boolean} At least one comparable annotation found
 */
ve.dm.AnnotationSet.prototype.containsComparableForSerialization = function ( annotation ) {
	return this.filter( function ( a ) {
		return annotation.compareToForSerialization( a );
	}, true );
};
 
/**
 * Check if the set contains at least one annotation where a given property matches a given filter.
 *
 * This is equivalent to (but more efficient than) `!this.filter( .. ).isEmpty()`.
 *
 * @see ve.dm.AnnotationSet#filter
 *
 * @param {Function} callback Function that takes an annotation and returns boolean true to include
 * @return {boolean} At least one matching annotation found
 */
ve.dm.AnnotationSet.prototype.containsMatching = function ( callback ) {
	return this.filter( callback, true );
};
 
/**
 * Check if the set contains the same annotations as another set.
 *
 * Compares annotations by their comparable object value.
 *
 * @param {ve.dm.AnnotationSet} annotationSet The annotationSet to compare this one to
 * @return {boolean} The annotations are the same
 */
ve.dm.AnnotationSet.prototype.compareTo = function ( annotationSet ) {
	var length = this.getHashes().length;
 
	if ( length === annotationSet.getLength() ) {
		for ( var i = 0; i < length; i++ ) {
			if ( !annotationSet.containsComparable( this.get( i ) ) ) {
				return false;
			}
		}
	} else {
		return false;
	}
	return true;
};
 
/**
 * Filter out all annotations in this set which are comparable to those in another set
 *
 * This returns a new set.
 *
 * @param  {ve.dm.AnnotationSet} set The AnnotationSet to filter out
 * @return {ve.dm.AnnotationSet} A new set without the comparable annotations
 */
ve.dm.AnnotationSet.prototype.withoutComparableSet = function ( set ) {
	return this.filter( function ( annotation ) {
		return !set.containsComparable( annotation );
	} );
};
 
/**
 * Strictly compare two annotation sets for equality.
 *
 * This method only considers two annotation sets to be equal if they contain exactly the same
 * annotations (not just comparable, but with the same hash in the HashValueStore)
 * in exactly the same order.
 *
 * @param {ve.dm.AnnotationSet} set The annotation set to compare this one to
 * @return {boolean} The annotation sets are equal
 */
ve.dm.AnnotationSet.prototype.equalsInOrder = function ( set ) {
	var ourHashes = this.getHashes(),
		theirHashes = set.getHashes();
	if ( ourHashes.length !== theirHashes.length ) {
		return false;
	}
	for ( var i = 0, len = ourHashes.length; i < len; i++ ) {
		if ( ourHashes[ i ] !== theirHashes[ i ] ) {
			return false;
		}
	}
	return true;
};
 
/**
 * Add an annotation to the set.
 *
 * If the annotation is already present in the set, nothing happens.
 *
 * The annotation will be inserted before the annotation that is currently at the given offset. If offset is
 * negative, it will be counted from the end (i.e. offset -1 is the last item, -2 the second-to-last,
 * etc.). If offset is out of bounds, the annotation will be added to the end of the set.
 *
 * @param {ve.dm.Annotation} annotation Annotation to add
 * @param {number} offset Offset to add the annotation at
 */
ve.dm.AnnotationSet.prototype.add = function ( annotation, offset ) {
	var storeHash = this.getStore().hash( annotation );
	// Negative offset
	Iif ( offset < 0 ) {
		offset = this.getLength() + offset;
	}
	// Greater than length, add to end
	if ( offset >= this.getLength() ) {
		this.push( annotation );
		return;
	}
	// If not in set already, splice in place
	if ( !this.containsHash( storeHash ) ) {
		this.storeHashes.splice( offset, 0, storeHash );
	}
};
 
/**
 * Add all annotations in the given set, removing any duplicates (including existing ones).
 *
 * The offset calculation happens before duplicates are removed.
 *
 * @param {ve.dm.AnnotationSet} set Set to add to the set
 * @param {number} [offset] Offset at which to insert; defaults to the end of the set
 */
ve.dm.AnnotationSet.prototype.addSet = function ( set, offset ) {
	var hashes = this.getHashes();
	if ( offset === undefined ) {
		offset = hashes.length;
	}
	this.storeHashes = OO.simpleArrayUnion(
		hashes.slice( 0, offset ),
		set.getHashes(),
		hashes.slice( offset )
	);
};
 
/**
 * Add an annotation at the end of the set.
 *
 * @param {ve.dm.Annotation} annotation Annotation to add
 */
ve.dm.AnnotationSet.prototype.push = function ( annotation ) {
	this.pushHash( this.getStore().hash( annotation ) );
};
 
/**
 * Add an annotation at the end of the set by store hash.
 *
 * @param {string} storeHash Store hash of annotation to add
 */
ve.dm.AnnotationSet.prototype.pushHash = function ( storeHash ) {
	this.storeHashes.push( storeHash );
};
 
/**
 * Remove the annotation at a given offset.
 *
 * @param {number} offset Offset to remove item at. If negative, the counts from the end, see add()
 * @throws {Error} Offset out of bounds.
 */
ve.dm.AnnotationSet.prototype.removeAt = function ( offset ) {
	Iif ( offset < 0 ) {
		offset = this.getLength() + offset;
	}
	Iif ( offset >= this.getLength() ) {
		throw new Error( 'Offset out of bounds' );
	}
	this.storeHashes.splice( offset, 1 );
};
 
/**
 * Remove a given annotation from the set by store hash.
 *
 * If the annotation isn't in the set, nothing happens.
 *
 * @param {string} storeHash Store hash of annotation to remove
 */
ve.dm.AnnotationSet.prototype.removeHash = function ( storeHash ) {
	var offset = this.offsetOfHash( storeHash );
	if ( offset !== -1 ) {
		this.storeHashes.splice( offset, 1 );
	}
};
 
/**
 * Remove a given annotation from the set.
 *
 * If the annotation isn't in the set, nothing happens.
 *
 * @param {ve.dm.Annotation} annotation Annotation to remove
 */
ve.dm.AnnotationSet.prototype.remove = function ( annotation ) {
	var offset = this.offsetOf( annotation );
	Eif ( offset !== -1 ) {
		this.storeHashes.splice( offset, 1 );
	}
};
 
/**
 * Remove all annotations.
 */
ve.dm.AnnotationSet.prototype.removeAll = function () {
	this.storeHashes = [];
};
 
/**
 * Remove all annotations in a given set from the set.
 *
 * Annotations that aren't in the set are ignored.
 *
 * @param {ve.dm.AnnotationSet} set Set to remove from the set
 */
ve.dm.AnnotationSet.prototype.removeSet = function ( set ) {
	this.storeHashes = OO.simpleArrayDifference( this.getHashes(), set.getHashes() );
};
 
/**
 * Remove all annotations that are not also in a given other set from the set.
 *
 * @param {ve.dm.AnnotationSet} set Set to intersect with the set
 */
ve.dm.AnnotationSet.prototype.removeNotInSet = function ( set ) {
	this.storeHashes = OO.simpleArrayIntersection( this.getHashes(), set.getHashes() );
};
 
/**
 * Reverse the set.
 *
 * This returns a copy, the original set is not modified.
 *
 * @return {ve.dm.AnnotationSet} Copy of the set with the order reversed.
 */
ve.dm.AnnotationSet.prototype.reversed = function () {
	var newSet = this.clone();
	newSet.storeHashes.reverse();
	return newSet;
};
 
/**
 * Merge another set into the set.
 *
 * This returns a copy, the original set is not modified.
 *
 * @param {ve.dm.AnnotationSet} set Other set
 * @return {ve.dm.AnnotationSet} Set containing all annotations in the set as well as all annotations in set
 */
ve.dm.AnnotationSet.prototype.mergeWith = function ( set ) {
	var newSet = this.clone();
	newSet.addSet( set );
	return newSet;
};
 
/**
 * Get the difference between the set and another set.
 *
 * @param {ve.dm.AnnotationSet} set Other set
 * @return {ve.dm.AnnotationSet} New set containing all annotations that are in the set but not in set
 */
ve.dm.AnnotationSet.prototype.diffWith = function ( set ) {
	var newSet = this.clone();
	newSet.removeSet( set );
	return newSet;
};
 
/**
 * Get the intersection of the set with another set.
 *
 * This requires annotations to be strictly equal. To find annotations which are comparable
 * use the slower #getComparableAnnotationsFromSet with #addSet.
 *
 * @param {ve.dm.AnnotationSet} set Other set
 * @return {ve.dm.AnnotationSet} New set containing all annotations that are both in the set and in set
 */
ve.dm.AnnotationSet.prototype.intersectWith = function ( set ) {
	var newSet = this.clone();
	newSet.removeNotInSet( set );
	return newSet;
};