all files / src/ce/ ve.ce.TextState.js

93.08% Statements 148/159
87.76% Branches 86/98
100% Functions 7/7
93.08% Lines 148/159
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                                  2936×                           2936×                               3129×         3121×                 2936×         8679× 3082× 5597×               5589×         29×   29×   5560× 18× 5542× 4337×   377× 377× 377×   4337× 4337×         4342× 8679× 2936×   5743× 377× 377× 377×   5743× 1406×   4337×   4342× 2936×   1406×   2936×                     2173× 2173×     2173×     2173× 2268×       2173×                             334×                                     17×   17× 32× 31× 12×     32× 20×     17×       334× 642×   334×           331× 331× 331×         300× 300× 300× 300× 300× 1299× 166×     300×               300×     296× 296×             296×           296× 270×     153×     296×         331× 331× 23×       331× 331× 313×   331×     331× 343× 343×     341× 341× 325×   341× 325×   341×     25×               316× 316×   316× 300× 300×     16× 16×   316× 307×             316× 316× 308× 308× 25× 25×   283×             283×       283×   316×               33× 33× 33× 33× 17× 17×         17×       17×   33× 28×                       33× 33× 16×       16× 16×           16×     16×       16×     16× 16×         16×     316× 316×     331×    
/*!
 * VisualEditor annotated text content state class
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * Annotated text content state (a snapshot of node content)
 *
 * @class
 *
 * @constructor
 * @param {HTMLElement} element Element whose content is to be snapshotted
 */
ve.ce.TextState = function VeCeTextState( element ) {
	/**
	 * @property {ve.ce.TextStateChunk[]|null} chunks Linearized annotated text content
	 */
	this.chunks = this.constructor.static.getChunks( element );
};
 
/* Inheritance */
 
OO.initClass( ve.ce.TextState );
 
/* Static methods */
 
/**
 * Saves a snapshot of the current text content state
 *
 * @param {HTMLElement} element Element whose content is to be snapshotted
 * @return {ve.ce.TextStateChunk[]} chunks
 */
ve.ce.TextState.static.getChunks = function ( element ) {
	var view,
		node = element,
		// Stack of element-lists in force; each element list is equal to its predecessor extended
		// by one element. This means two chunks have object-equal element lists if they have the
		// same element elements in force (i.e. if their text nodes are DOM siblings).
		elementListStack = [ [] ],
		stackTop = 0,
		annotationStack = [],
		chunks = [];
 
	/**
	 * Add to chunks, merging content with the same elements/type into the same chunk
	 *
	 * @param {string} text Plain text
	 * @param {string} [type] If this is a unicorn then 'unicorn', else 'text' (default)
	 */
	function add( text, type ) {
		if (
			!chunks.length ||
			chunks[ chunks.length - 1 ].elements !== elementListStack[ stackTop ] ||
			chunks[ chunks.length - 1 ].type !== type
		) {
			chunks.push( new ve.ce.TextStateChunk(
				text,
				elementListStack[ stackTop ],
				type || 'text'
			) );
		} else {
			chunks[ chunks.length - 1 ].text += text;
		}
	}
 
	while ( true ) {
		// Process node
		// If appropriate, step into first child and loop
		// If no next sibling, step out until there is (breaking if we leave element)
		// Step to next sibling and loop
		if ( node.nodeType === Node.TEXT_NODE ) {
			add( node.data.replace( /\u00A0/g, ' ' ) );
		} else if (
			// Node types that don't appear in the model
			// TODO: what about comments?
			node.nodeType !== Node.ELEMENT_NODE ||
			node.classList.contains( 've-ce-branchNode-blockSlug' ) ||
			node.classList.contains( 've-ce-cursorHolder' )
		) {
			// Do nothing
		} else if ( ( view = $( node ).data( 'view' ) ) && view instanceof ve.ce.LeafNode ) {
			// Don't return the content, but return placeholder characters so the
			// offsets match up.
			// Only return placeholders for the first element in a sibling group;
			// otherwise we'll double count this node
			Eif ( node === view.$element[ 0 ] ) {
				// \u2603 is the snowman character: ☃
				add( ve.repeatString( '\u2603', view.getOuterLength() ) );
			}
		} else if ( node.classList.contains( 've-ce-unicorn' ) ) {
			add( '', 'unicorn' );
		} else if ( node.firstChild ) {
			if ( ve.ce.isAnnotationElement( node ) ) {
				// Push a new element stack state
				elementListStack.push( elementListStack[ stackTop ].concat( node ) );
				annotationStack.push( node );
				stackTop++;
			}
			node = node.firstChild;
			continue;
		}
		// else no child nodes; do nothing
 
		// Step out of this node, then keep stepping outwards until there is a next sibling
		while ( true ) {
			if ( node === element ) {
				break;
			}
			if ( node === annotationStack[ annotationStack.length - 1 ] ) {
				annotationStack.pop();
				elementListStack.pop();
				stackTop--;
			}
			if ( node.nextSibling ) {
				break;
			}
			node = node.parentNode;
		}
		if ( node === element ) {
			break;
		}
		node = node.nextSibling;
	}
	return chunks;
};
 
/* Methods */
 
/**
 * Test whether the text state is equal to another.
 *
 * @param {ve.ce.TextState} other The other text state
 * @return {boolean} Whether the states are equal
 */
ve.ce.TextState.prototype.isEqual = function ( other ) {
	var i, len;
	Iif ( other === this ) {
		return true;
	}
	Iif ( !other || this.chunks.length !== other.chunks.length ) {
		return false;
	}
	for ( i = 0, len = this.chunks.length; i < len; i++ ) {
		Iif ( !( this.chunks[ i ].isEqual( other.chunks[ i ] ) ) ) {
			return false;
		}
	}
	return true;
};
 
/**
 * Create a model transaction from a change in text state.
 * This must be fast enough to cope with the typical typing scenario (either with or
 * without an IME) where the contents of a single text node get modified several times
 * per second.
 *
 * @param {ve.ce.TextState} prev Previous text state (must be for the same node)
 * @param {ve.dm.Document} modelDoc The model document
 * @param {number} modelOffset The offset of the node in the model
 * @param {ve.dm.AnnotationSet} [unicornAnnotations] The annotations at the unicorn, if any
 * @return {ve.dm.Transaction|null} Transaction corresponding to the text state change
 */
ve.ce.TextState.prototype.getChangeTransaction = function ( prev, modelDoc, modelOffset, unicornAnnotations ) {
	var change, i, iLen, textStart, textEnd, changeOffset, removed, removeRange,
		annotations, missing, oldChunk, newChunk, j, jStart, jEnd, leastMissing,
		matchStartOffset, matchOffset, bestOffset, jLen, oldAnnotations, element,
		modelClass, ann, oldAnn, data, view,
		oldChunks = prev.chunks,
		newChunks = this.chunks,
		modelData = modelDoc.data,
		newData = [];
 
	/**
	 * Calculates the size of newArray - oldArray (asymmetric difference)
	 * This is O(n^2) but the lengths should be fairly low, and this doesn't
	 * happen during typical typing.
	 *
	 * @param {Array} newArray
	 * @param {Array} oldArray
	 * @param {Function} equals Two-argument, boolean valued equivalence test
	 * @return {number} Number of elements of newArray not in oldArray
	 */
	function countMissing( newArray, oldArray, equals ) {
		var i, iLen, j, jLen,
			count = 0;
		for ( i = 0, iLen = newArray.length; i < iLen; i++ ) {
			for ( j = 0, jLen = oldArray.length; j < jLen; j++ ) {
				if ( equals( newArray[ i ], oldArray[ j ] ) ) {
					break;
				}
			}
			if ( j === jLen ) {
				count++;
			}
		}
		return count;
	}
 
	// Find first changed chunk at start/end of oldChunks/newChunks
	change = ve.countEdgeMatches( oldChunks, newChunks, function ( a, b ) {
		return a.isEqual( b );
	} );
	if ( change === null ) {
		// No change
		return null;
	}
 
	// Count matching characters with matching annotations at start/end of the changed chunks.
	// During typical typing, there is a single changed chunk with matching start/end chars.
	textStart = 0;
	textEnd = 0;
	if ( change.start + change.end < Math.min( oldChunks.length, newChunks.length ) ) {
		// Both oldChunks and newChunks include a changed chunk. Therefore the first changed
		// chunk of oldChunks and newChunks is respectively oldChunks[ change.start ] and
		// newChunks[ change.start ] . If they have matching annotations, then matching
		// characters at their start are also part of the unchanged start region.
		Eif ( oldChunks[ change.start ].hasEqualElements( newChunks[ change.start ] ) ) {
			oldChunk = oldChunks[ change.start ];
			newChunk = newChunks[ change.start ];
			iLen = Math.min( oldChunk.text.length, newChunk.text.length );
			for ( i = 0; i < iLen; i++ ) {
				if ( oldChunk.text[ i ] !== newChunk.text[ i ] ) {
					break;
				}
			}
			textStart = i;
		}
 
		// Likewise, the last changed chunk of oldChunks and newChunks is respectively
		// oldChunks[ oldChunks.length - 1 - change.end ] and
		// newChunks[ newChunks.length - 1 - change.end ] , and if they have matching
		// annotations, then matching characters at their end potentially form part of
		// the unchanged end region.
		if ( oldChunks[ oldChunks.length - 1 - change.end ].hasEqualElements(
			newChunks[ newChunks.length - 1 - change.end ]
		) ) {
			oldChunk = oldChunks[ oldChunks.length - 1 - change.end ];
			newChunk = newChunks[ newChunks.length - 1 - change.end ];
			// However, if only one chunk has changed in oldChunks/newChunks, then
			// oldChunk/newChunk is also the *first* changed chunk, in which case
			// textStart has already eaten into that chunk; so take care not to
			// overlap it. (For example, for 'ana'->'anna', textStart will be 2 so
			// we want to limit textEnd to 1, else the 'n' of 'ana' will be counted
			// twice).
			iLen = Math.min(
				oldChunk.text.length -
				( change.start + change.end === oldChunks.length - 1 ? textStart : 0 ),
				newChunk.text.length -
				( change.start + change.end === newChunks.length - 1 ? textStart : 0 )
			);
			for ( i = 0; i < iLen; i++ ) {
				if ( newChunk.text[ newChunk.text.length - 1 - i ] !==
					oldChunk.text[ oldChunk.text.length - 1 - i ]
				) {
					break;
				}
			}
			textEnd = i;
		}
	}
 
	// Starting just inside the node, skip past matching chunks at the array starts
	changeOffset = modelOffset + 1;
	for ( i = 0, iLen = change.start; i < iLen; i++ ) {
		changeOffset += oldChunks[ i ].text.length;
	}
 
	// Calculate range of old content to remove
	removed = 0;
	for ( i = change.start, iLen = oldChunks.length - change.end; i < iLen; i++ ) {
		removed += oldChunks[ i ].text.length;
	}
	removeRange = new ve.Range( changeOffset + textStart, changeOffset + removed - textEnd );
 
	// Prepare new content, reusing existing ve.dm.Annotation objects where possible
	for ( i = change.start, iLen = newChunks.length - change.end; i < iLen; i++ ) {
		newChunk = newChunks[ i ];
		if ( newChunk.type === 'unicorn' ) {
			// Unicorns don't exist in the model
			continue;
		}
		data = newChunk.text.split( '' );
		if ( i === change.start ) {
			data = data.slice( textStart );
		}
		if ( i === iLen - 1 ) {
			data = data.slice( 0, data.length - textEnd );
		}
		if ( data.length === 0 ) {
			// There is nothing to add, because textStart/textEnd causes all the
			// content in this chunk to be retained,
			continue;
		}
 
		// Search for matching elements in old chunks adjacent to the change (i.e. removed
		// chunks or the first chunk before/after the removal). O(n^2) is fine here
		// because during typical typing there is only one changed chunk, and the worst
		// case is three new chunks (e.g. when the interior of an existing chunk is
		// annotated).
		annotations = null;
		missing = null;
		// In the old chunks, find the chunks adjacent to the change
		if ( change.start === 0 ) {
			jStart = 0;
			matchStartOffset = changeOffset;
		} else {
			// Include the last chunk before the change
			jStart = change.start - 1;
			matchStartOffset = changeOffset - oldChunks[ jStart ].text.length;
		}
		if ( change.end === 0 ) {
			jEnd = oldChunks.length;
		} else {
			// Include the first chunk after the change
			jEnd = oldChunks.length - change.end + 1;
		}
 
		// Search for exact match first. During typical typing there is an exact
		// match at j=1 (or j=0 if there is no previous chunk).
		matchOffset = matchStartOffset;
		for ( j = jStart; j < jEnd; j++ ) {
			oldChunk = oldChunks[ j ];
			if ( !oldChunk.hasEqualElements( newChunk ) ) {
				matchOffset += oldChunk.text.length;
				continue;
			}
			Iif ( oldChunk.type === 'unicorn' ) {
				if ( !unicornAnnotations ) {
					throw new Error( 'No unicorn annotations' );
				}
				annotations = unicornAnnotations;
				break;
			}
			annotations = modelData.getInsertionAnnotationsFromRange(
				new ve.Range( matchOffset ),
				true
			);
			break;
		}
		if ( annotations === null ) {
			// No exact match: search for the old chunk whose element list covers best
			// (choosing the startmost of any tying chunks). There may be no missing
			// elements even though the match is not exact (e.g. because of removed
			// annotations and reordering).
			//
			// This block doesn't happen during typical typing, so performance is
			// less critical.
			leastMissing = newChunk.elements.length;
			bestOffset = null;
			matchOffset = matchStartOffset;
			for ( j = jStart; j < jEnd; j++ ) {
				oldChunk = oldChunks[ j ];
				missing = countMissing(
					newChunk.elements,
					oldChunk.elements,
					ve.ce.TextStateChunk.static.compareElements
				);
				if ( missing < leastMissing ) {
					leastMissing = missing;
					bestOffset = matchOffset;
					Iif ( missing === 0 ) {
						break;
					}
				}
				matchOffset += oldChunk.text.length;
			}
			if ( bestOffset === null ) {
				oldAnnotations = new ve.dm.AnnotationSet( modelData.getStore() );
			} else {
				oldAnnotations = modelData.getInsertionAnnotationsFromRange(
					new ve.Range( bestOffset ),
					true
				);
			}
			// For each element in new order, add applicable old annotation, or
			// (if whitelisted) newly-created annotation.
			// TODO: this can potentially duplicate existing non-adjacent
			// annotations. Sometimes this could be required behaviour, e.g. for
			// directionality spans; in other situations it would be cleaner to
			// duplicate.
			annotations = new ve.dm.AnnotationSet( modelData.getStore() );
			for ( j = 0, jLen = newChunk.elements.length; j < jLen; j++ ) {
				element = newChunk.elements[ j ];
				// Recover the node from jQuery data store. This can only break if the browser
				// completely rebuilds the node, but should work in cases like typing into
				// collapsed links because nails ensure the link is never completely empty.
				view = $( element ).data( 'view' );
				Iif ( view ) {
					ann = view.getModel();
				} else {
					// No view: new annotation element (or replacement one):
					// see https://phabricator.wikimedia.org/T116269 and
					// https://code.google.com/p/chromium/issues/detail?id=546461
					modelClass = ve.dm.modelRegistry.lookup(
						ve.dm.modelRegistry.matchElement( element )
					);
					Iif ( !( modelClass && modelClass.prototype instanceof ve.dm.Annotation ) ) {
						// Erroneous element; nothing we can do with it
						continue;
					}
					ann = ve.dm.annotationFactory.createFromElement(
						modelClass.static.toDataElement( [ element ], ve.dm.converter )
					);
					oldAnn = oldAnnotations.getComparable( ann );
					if ( oldAnn ) {
						ann = oldAnn;
					} else Iif ( !ann.constructor.static.inferFromView ) {
						// New and un-whitelisted: drop the annotation
						continue;
					}
				}
				annotations.add( ann, annotations.getLength() );
			}
		}
		ve.dm.Document.static.addAnnotationsToData( data, annotations );
		ve.batchPush( newData, data );
	}
 
	return ve.dm.TransactionBuilder.static.newFromReplacement( modelDoc, removeRange, newData );
};