Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.AnnotationSet.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel AnnotationSet class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Annotation set.
9 *
10 * @constructor
11 * @param {ve.dm.HashValueStore} store
12 * @param {string[]} [storeHashes]
13 */
14 ve.dm.AnnotationSet = function VeDmAnnotationSet( store, storeHashes ) {
15 // Parent constructor
16 this.store = store;
17 this.storeHashes = storeHashes || [];
18
19 if ( this.get().indexOf( undefined ) !== -1 ) {
20 throw new Error( 'Annotation with hash ' +
21 this.storeHashes[ this.get().indexOf( undefined ) ] +
22 ' not found in store'
23 );
24 }
25 };
26
27 /* Methods */
28
29 /**
30 * Get the hash-value store.
31 *
32 * @return {ve.dm.HashValueStore} Hash-value store
33 */
34 ve.dm.AnnotationSet.prototype.getStore = function () {
35 return this.store;
36 };
37
38 /**
39 * Get a clone.
40 *
41 * @return {ve.dm.AnnotationSet} Copy of annotation set
42 */
43 ve.dm.AnnotationSet.prototype.clone = function () {
44 return new ve.dm.AnnotationSet( this.getStore(), this.storeHashes.slice() );
45 };
46
47 /**
48 * Get an annotation set containing only annotations within the set with a specific name.
49 *
50 * @param {string} name Type name
51 * @return {ve.dm.AnnotationSet} Copy of annotation set
52 */
53 ve.dm.AnnotationSet.prototype.getAnnotationsByName = function ( name ) {
54 return this.filter( ( annotation ) => annotation.name === name );
55 };
56
57 /**
58 * Get an annotation set containing only annotations within the set which are comparable
59 * to a specific annotation.
60 *
61 * @param {ve.dm.Annotation} annotation Annotation to compare to
62 * @return {ve.dm.AnnotationSet} Copy of annotation set
63 */
64 ve.dm.AnnotationSet.prototype.getComparableAnnotations = function ( annotation ) {
65 return this.filter( ( a ) => ve.compare(
66 annotation.getComparableObject(),
67 a.getComparableObject()
68 ) );
69 };
70
71 /**
72 * Get an annotation set containing only annotations within the set which are comparable
73 * to an annotation from another set.
74 *
75 * @param {ve.dm.AnnotationSet} annotations Annotation set to compare to
76 * @return {ve.dm.AnnotationSet} Copy of annotation set
77 */
78 ve.dm.AnnotationSet.prototype.getComparableAnnotationsFromSet = function ( annotations ) {
79 return this.filter( ( a ) => annotations.containsComparable( a ) );
80 };
81
82 /**
83 * Check if any annotations in the set have a specific name.
84 *
85 * @param {string} name Type name
86 * @return {boolean} Annotation of given type exists in the set
87 */
88 ve.dm.AnnotationSet.prototype.hasAnnotationWithName = function ( name ) {
89 return this.containsMatching( ( annotation ) => annotation.name === name );
90 };
91
92 /**
93 * Get an annotation or all annotations from the set.
94 *
95 * set.get( 5 ) returns the annotation at offset 5, set.get() returns an array with all annotations
96 * in the entire set.
97 *
98 * @param {number} [offset] If set, only get the annotation at the offset
99 * @return {ve.dm.Annotation[]|ve.dm.Annotation|undefined} The annotation at offset, or an array of all
100 * annotations in the set
101 */
102 ve.dm.AnnotationSet.prototype.get = function ( offset ) {
103 if ( offset !== undefined ) {
104 return this.getStore().value( this.getHash( offset ) );
105 } else {
106 return this.getStore().values( this.getHashes() );
107 }
108 };
109
110 /**
111 * Get store hash from offset within annotation set.
112 *
113 * @param {number} offset Offset within annotation set
114 * @return {string} Store hash at specified offset
115 */
116 ve.dm.AnnotationSet.prototype.getHash = function ( offset ) {
117 return this.storeHashes[ offset ];
118 };
119
120 /**
121 * Get all store hashes.
122 *
123 * @return {string[]} Store hashes
124 */
125 ve.dm.AnnotationSet.prototype.getHashes = function () {
126 return this.storeHashes;
127 };
128
129 /**
130 * Get the length of the set.
131 *
132 * @return {number} The number of annotations in the set
133 */
134 ve.dm.AnnotationSet.prototype.getLength = function () {
135 return this.storeHashes.length;
136 };
137
138 /**
139 * Check if the set is empty.
140 *
141 * @return {boolean} The set is empty
142 */
143 ve.dm.AnnotationSet.prototype.isEmpty = function () {
144 return this.getLength() === 0;
145 };
146
147 /**
148 * Check whether a given annotation occurs in the set.
149 *
150 * Annotations are compared by store hash.
151 *
152 * @param {ve.dm.Annotation} annotation
153 * @return {boolean} There is an annotation in the set with the same hash as annotation
154 */
155 ve.dm.AnnotationSet.prototype.contains = function ( annotation ) {
156 return this.offsetOf( annotation ) !== -1;
157 };
158
159 /**
160 * Check whether a given store hash occurs in the set.
161 *
162 * @param {string} storeHash Store hash of annotation
163 * @return {boolean} There is an annotation in the set with this store hash
164 */
165 ve.dm.AnnotationSet.prototype.containsHash = function ( storeHash ) {
166 return this.getHashes().indexOf( storeHash ) !== -1;
167 };
168
169 /**
170 * Check whether the set contains any of the annotations in another set.
171 *
172 * @param {ve.dm.AnnotationSet} set Set to compare the set with
173 * @return {boolean} There is at least one annotation in set that is also in the set
174 */
175 ve.dm.AnnotationSet.prototype.containsAnyOf = function ( set ) {
176 const setHashes = set.getHashes(),
177 thisHashes = this.getHashes();
178 for ( let i = 0, length = setHashes.length; i < length; i++ ) {
179 if ( thisHashes.indexOf( setHashes[ i ] ) !== -1 ) {
180 return true;
181 }
182 }
183 return false;
184 };
185
186 /**
187 * Check whether the set contains all of the annotations in another set.
188 *
189 * @param {ve.dm.AnnotationSet} set Set to compare the set with
190 * @return {boolean} All annotations in set are also in the set
191 */
192 ve.dm.AnnotationSet.prototype.containsAllOf = function ( set ) {
193 const setHashes = set.getHashes(),
194 thisHashes = this.getHashes();
195 for ( let i = 0, length = setHashes.length; i < length; i++ ) {
196 if ( thisHashes.indexOf( setHashes[ i ] ) === -1 ) {
197 return false;
198 }
199 }
200 return true;
201 };
202
203 /**
204 * Get the offset of a given annotation in the set.
205 *
206 * @param {ve.dm.Annotation} annotation Annotation to search for
207 * @return {number} Offset of annotation in the set, or -1 if annotation is not in the set.
208 */
209 ve.dm.AnnotationSet.prototype.offsetOf = function ( annotation ) {
210 return this.offsetOfHash( this.getStore().hashOfValue( annotation ) );
211 };
212
213 /**
214 * Get the offset of a given annotation in the set by store hash.
215 *
216 * @param {string} storeHash Store hash of annotation to search for
217 * @return {number} Offset of annotation in the set, or -1 if annotation is not in the set.
218 */
219 ve.dm.AnnotationSet.prototype.offsetOfHash = function ( storeHash ) {
220 return this.getHashes().indexOf( storeHash );
221 };
222
223 /**
224 * Filter the set by an item property.
225 *
226 * This returns a new set with all annotations in the set for which the callback returned true for.
227 *
228 * @param {Function} callback Function that takes an annotation and returns boolean true to include
229 * @param {boolean} [returnBool] For internal use only
230 * @return {ve.dm.AnnotationSet|boolean} New set containing only the matching annotations
231 */
232 ve.dm.AnnotationSet.prototype.filter = function ( callback, returnBool ) {
233 let result;
234
235 if ( !returnBool ) {
236 result = this.clone();
237 // TODO: Should we be returning this on all methods that modify the original? Might help
238 // with chainability, but perhaps it's also confusing because most chainable methods return
239 // a new hash set.
240 result.removeAll();
241 }
242 for ( let i = 0, length = this.getLength(); i < length; i++ ) {
243 const storeHash = this.getHash( i );
244 const annotation = this.getStore().value( storeHash );
245 if ( callback( annotation ) ) {
246 if ( returnBool ) {
247 return true;
248 } else {
249 result.storeHashes.push( storeHash );
250 }
251 }
252 }
253 return returnBool ? false : result;
254 };
255
256 /**
257 * Check if the set contains an annotation comparable to the specified one.
258 *
259 * getComparableObject is used to compare the annotations, and should return
260 * true if an annotation is found which is mergeable with the specified one.
261 *
262 * @param {ve.dm.Annotation} annotation Annotation to compare to
263 * @return {boolean} At least one comparable annotation found
264 */
265 ve.dm.AnnotationSet.prototype.containsComparable = function ( annotation ) {
266 return this.filter( ( a ) => annotation.compareTo( a ), true );
267 };
268
269 /**
270 * Get the first annotation mergeable with the specified one
271 *
272 * @param {ve.dm.Annotation} annotation Annotation to compare to
273 * @return {ve.dm.Annotation|null} First matching annotation
274 */
275 ve.dm.AnnotationSet.prototype.getComparable = function ( annotation ) {
276 for ( let i = 0, len = this.getLength(); i < len; i++ ) {
277 const ann = this.getStore().value( this.getHash( i ) );
278 if ( ann.compareTo( annotation ) ) {
279 return ann;
280 }
281 }
282 return null;
283 };
284
285 /**
286 * FIXME T126031: Check if the set contains an annotation comparable to the
287 * specified one for the purposes of serialization.
288 *
289 * This method uses getComparableObjectForSerialization which also includes
290 * HTML attributes.
291 *
292 * @param {ve.dm.Annotation} annotation Annotation to compare to
293 * @return {boolean} At least one comparable annotation found
294 */
295 ve.dm.AnnotationSet.prototype.containsComparableForSerialization = function ( annotation ) {
296 return this.filter( ( a ) => annotation.compareToForSerialization( a ), true );
297 };
298
299 /**
300 * Check if the set contains at least one annotation where a given property matches a given filter.
301 *
302 * This is equivalent to (but more efficient than) `!this.filter( .. ).isEmpty()`.
303 *
304 * @see ve.dm.AnnotationSet#filter
305 *
306 * @param {Function} callback Function that takes an annotation and returns boolean true to include
307 * @return {boolean} At least one matching annotation found
308 */
309 ve.dm.AnnotationSet.prototype.containsMatching = function ( callback ) {
310 return this.filter( callback, true );
311 };
312
313 /**
314 * Check if the set contains the same annotations as another set.
315 *
316 * Compares annotations by their comparable object value.
317 *
318 * @param {ve.dm.AnnotationSet} annotationSet The annotationSet to compare this one to
319 * @return {boolean} The annotations are the same
320 */
321 ve.dm.AnnotationSet.prototype.compareTo = function ( annotationSet ) {
322 const length = this.getHashes().length;
323
324 if ( length === annotationSet.getLength() ) {
325 for ( let i = 0; i < length; i++ ) {
326 if ( !annotationSet.containsComparable( this.get( i ) ) ) {
327 return false;
328 }
329 }
330 } else {
331 return false;
332 }
333 return true;
334 };
335
336 /**
337 * Filter out all annotations in this set which are comparable to those in another set
338 *
339 * This returns a new set.
340 *
341 * @param {ve.dm.AnnotationSet} set The AnnotationSet to filter out
342 * @return {ve.dm.AnnotationSet} A new set without the comparable annotations
343 */
344 ve.dm.AnnotationSet.prototype.withoutComparableSet = function ( set ) {
345 return this.filter( ( annotation ) => !set.containsComparable( annotation ) );
346 };
347
348 /**
349 * Strictly compare two annotation sets for equality.
350 *
351 * This method only considers two annotation sets to be equal if they contain exactly the same
352 * annotations (not just comparable, but with the same hash in the HashValueStore)
353 * in exactly the same order.
354 *
355 * @param {ve.dm.AnnotationSet} set The annotation set to compare this one to
356 * @return {boolean} The annotation sets are equal
357 */
358 ve.dm.AnnotationSet.prototype.equalsInOrder = function ( set ) {
359 const ourHashes = this.getHashes(),
360 theirHashes = set.getHashes();
361 if ( ourHashes.length !== theirHashes.length ) {
362 return false;
363 }
364 for ( let i = 0, len = ourHashes.length; i < len; i++ ) {
365 if ( ourHashes[ i ] !== theirHashes[ i ] ) {
366 return false;
367 }
368 }
369 return true;
370 };
371
372 /**
373 * Add an annotation to the set.
374 *
375 * If the annotation is already present in the set, nothing happens.
376 *
377 * The annotation will be inserted before the annotation that is currently at the given offset. If offset is
378 * negative, it will be counted from the end (i.e. offset -1 is the last item, -2 the second-to-last,
379 * etc.). If offset is out of bounds, the annotation will be added to the end of the set.
380 *
381 * @param {ve.dm.Annotation} annotation Annotation to add
382 * @param {number} offset Offset to add the annotation at
383 */
384 ve.dm.AnnotationSet.prototype.add = function ( annotation, offset ) {
385 const storeHash = this.getStore().hash( annotation );
386 // Negative offset
387 if ( offset < 0 ) {
388 offset = this.getLength() + offset;
389 }
390 // Greater than length, add to end
391 if ( offset >= this.getLength() ) {
392 this.push( annotation );
393 return;
394 }
395 // If not in set already, splice in place
396 if ( !this.containsHash( storeHash ) ) {
397 this.storeHashes.splice( offset, 0, storeHash );
398 }
399 };
400
401 /**
402 * Add all annotations in the given set, removing any duplicates (including existing ones).
403 *
404 * The offset calculation happens before duplicates are removed.
405 *
406 * @param {ve.dm.AnnotationSet} set Set to add to the set
407 * @param {number} [offset] Offset at which to insert; defaults to the end of the set
408 */
409 ve.dm.AnnotationSet.prototype.addSet = function ( set, offset ) {
410 const hashes = this.getHashes();
411 if ( offset === undefined ) {
412 offset = hashes.length;
413 }
414 this.storeHashes = OO.simpleArrayUnion(
415 hashes.slice( 0, offset ),
416 set.getHashes(),
417 hashes.slice( offset )
418 );
419 };
420
421 /**
422 * Add an annotation at the end of the set.
423 *
424 * @param {ve.dm.Annotation} annotation Annotation to add
425 */
426 ve.dm.AnnotationSet.prototype.push = function ( annotation ) {
427 this.pushHash( this.getStore().hash( annotation ) );
428 };
429
430 /**
431 * Add an annotation at the end of the set by store hash.
432 *
433 * @param {string} storeHash Store hash of annotation to add
434 */
435 ve.dm.AnnotationSet.prototype.pushHash = function ( storeHash ) {
436 this.storeHashes.push( storeHash );
437 };
438
439 /**
440 * Remove the annotation at a given offset.
441 *
442 * @param {number} offset Offset to remove item at. If negative, the counts from the end, see add()
443 * @throws {Error} Offset out of bounds.
444 */
445 ve.dm.AnnotationSet.prototype.removeAt = function ( offset ) {
446 if ( offset < 0 ) {
447 offset = this.getLength() + offset;
448 }
449 if ( offset >= this.getLength() ) {
450 throw new Error( 'Offset out of bounds' );
451 }
452 this.storeHashes.splice( offset, 1 );
453 };
454
455 /**
456 * Remove a given annotation from the set by store hash.
457 *
458 * If the annotation isn't in the set, nothing happens.
459 *
460 * @param {string} storeHash Store hash of annotation to remove
461 */
462 ve.dm.AnnotationSet.prototype.removeHash = function ( storeHash ) {
463 const offset = this.offsetOfHash( storeHash );
464 if ( offset !== -1 ) {
465 this.storeHashes.splice( offset, 1 );
466 }
467 };
468
469 /**
470 * Remove a given annotation from the set.
471 *
472 * If the annotation isn't in the set, nothing happens.
473 *
474 * @param {ve.dm.Annotation} annotation Annotation to remove
475 */
476 ve.dm.AnnotationSet.prototype.remove = function ( annotation ) {
477 const offset = this.offsetOf( annotation );
478 if ( offset !== -1 ) {
479 this.storeHashes.splice( offset, 1 );
480 }
481 };
482
483 /**
484 * Remove all annotations.
485 */
486 ve.dm.AnnotationSet.prototype.removeAll = function () {
487 this.storeHashes = [];
488 };
489
490 /**
491 * Remove all annotations in a given set from the set.
492 *
493 * Annotations that aren't in the set are ignored.
494 *
495 * @param {ve.dm.AnnotationSet} set Set to remove from the set
496 */
497 ve.dm.AnnotationSet.prototype.removeSet = function ( set ) {
498 this.storeHashes = OO.simpleArrayDifference( this.getHashes(), set.getHashes() );
499 };
500
501 /**
502 * Remove all annotations that are not also in a given other set from the set.
503 *
504 * @param {ve.dm.AnnotationSet} set Set to intersect with the set
505 */
506 ve.dm.AnnotationSet.prototype.removeNotInSet = function ( set ) {
507 this.storeHashes = OO.simpleArrayIntersection( this.getHashes(), set.getHashes() );
508 };
509
510 /**
511 * Reverse the set.
512 *
513 * This returns a copy, the original set is not modified.
514 *
515 * @return {ve.dm.AnnotationSet} Copy of the set with the order reversed.
516 */
517 ve.dm.AnnotationSet.prototype.reversed = function () {
518 const newSet = this.clone();
519 newSet.storeHashes.reverse();
520 return newSet;
521 };
522
523 /**
524 * Merge another set into the set.
525 *
526 * This returns a copy, the original set is not modified.
527 *
528 * @param {ve.dm.AnnotationSet} set Other set
529 * @return {ve.dm.AnnotationSet} Set containing all annotations in the set as well as all annotations in set
530 */
531 ve.dm.AnnotationSet.prototype.mergeWith = function ( set ) {
532 const newSet = this.clone();
533 newSet.addSet( set );
534 return newSet;
535 };
536
537 /**
538 * Get the difference between the set and another set.
539 *
540 * @param {ve.dm.AnnotationSet} set Other set
541 * @return {ve.dm.AnnotationSet} New set containing all annotations that are in the set but not in set
542 */
543 ve.dm.AnnotationSet.prototype.diffWith = function ( set ) {
544 const newSet = this.clone();
545 newSet.removeSet( set );
546 return newSet;
547 };
548
549 /**
550 * Get the intersection of the set with another set.
551 *
552 * This requires annotations to be strictly equal. To find annotations which are comparable
553 * use the slower #getComparableAnnotationsFromSet with #addSet.
554 *
555 * @param {ve.dm.AnnotationSet} set Other set
556 * @return {ve.dm.AnnotationSet} New set containing all annotations that are both in the set and in set
557 */
558 ve.dm.AnnotationSet.prototype.intersectWith = function ( set ) {
559 const newSet = this.clone();
560 newSet.removeNotInSet( set );
561 return newSet;
562 };