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 | 1x 3178x 1x 1x 3179x 3179x 3179x 3444x 3444x 3444x 135x 3309x 3179x 3179x 3179x 9425x 3381x 6044x 6031x 45x 45x 5986x 18x 5968x 4689x 458x 458x 458x 4689x 4689x 4736x 9425x 3179x 6246x 458x 458x 458x 6246x 1557x 4689x 4736x 3179x 1557x 3179x 1x 2314x 2314x 2314x 2380x 2314x 1x 18x 18x 34x 33x 13x 34x 21x 18x 334x 334x 334x 334x 638x 334x 3x 331x 331x 331x 301x 301x 301x 301x 301x 1305x 166x 301x 301x 297x 297x 297x 297x 275x 153x 297x 331x 331x 21x 331x 331x 314x 331x 331x 346x 346x 2x 344x 344x 327x 344x 327x 344x 28x 316x 316x 316x 301x 301x 15x 15x 316x 308x 8x 316x 316x 307x 307x 25x 25x 282x 282x 282x 316x 34x 34x 34x 34x 18x 18x 18x 8x 8x 8x 18x 34x 28x 6x 34x 34x 18x 18x 18x 18x 18x 18x 18x 18x 8x 10x 18x 316x 316x 331x | /*! * VisualEditor annotated text content state class * * @copyright See AUTHORS.txt */ /** * 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 ) { // 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 elements in force (i.e. if their text nodes are DOM siblings). const elementListStack = [ [] ], chunks = []; let stackTop = 0; /** * Add to chunks, merging content with the same elements/type into the same chunk * * @param {string} text Plain text * @param {string} [type="text"] If this is a unicorn then 'unicorn', else 'text' (default) */ function add( text, type ) { type = type || 'text'; const last = chunks[ chunks.length - 1 ]; if ( last && last.elements === elementListStack[ stackTop ] && last.type === type ) { last.text += text; } else { chunks.push( new ve.ce.TextStateChunk( text, elementListStack[ stackTop ], type ) ); } } let view; const annotationStack = []; let node = element; 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( '\u2603'.repeat( 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 ) { Iif ( other === this ) { return true; } Iif ( !other || this.chunks.length !== other.chunks.length ) { return false; } for ( let 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 ) { /** * 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 ) { let count = 0; for ( let i = 0, iLen = newArray.length; i < iLen; i++ ) { let j, jLen; for ( j = 0, jLen = oldArray.length; j < jLen; j++ ) { if ( equals( newArray[ i ], oldArray[ j ] ) ) { break; } } if ( j === jLen ) { count++; } } return count; } const oldChunks = prev.chunks, newChunks = this.chunks, modelData = modelDoc.data, newData = []; // Find first changed chunk at start/end of oldChunks/newChunks const change = ve.countEdgeMatches( oldChunks, newChunks, ( a, b ) => 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. let textStart = 0; let 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 ] ) ) { const oldChunk = oldChunks[ change.start ]; const newChunk = newChunks[ change.start ]; const iLen = Math.min( oldChunk.text.length, newChunk.text.length ); let i; 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 ] ) ) { const oldChunk = oldChunks[ oldChunks.length - 1 - change.end ]; const 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). const 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 ) ); let i; 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 let changeOffset = modelOffset + 1; for ( let i = 0, iLen = change.start; i < iLen; i++ ) { changeOffset += oldChunks[ i ].text.length; } // Calculate range of old content to remove let removed = 0; for ( let i = change.start, iLen = oldChunks.length - change.end; i < iLen; i++ ) { removed += oldChunks[ i ].text.length; } const removeRange = new ve.Range( changeOffset + textStart, changeOffset + removed - textEnd ); // Prepare new content, reusing existing ve.dm.Annotation objects where possible for ( let i = change.start, iLen = newChunks.length - change.end; i < iLen; i++ ) { const newChunk = newChunks[ i ]; if ( newChunk.type === 'unicorn' ) { // Unicorns don't exist in the model continue; } let 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). let annotations = null; let missing = null; // In the old chunks, find the chunks adjacent to the change let jStart; let matchStartOffset; 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; } let jEnd; 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). let matchOffset = matchStartOffset; for ( let j = jStart; j < jEnd; j++ ) { const 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. let leastMissing = newChunk.elements.length; let bestOffset = null; matchOffset = matchStartOffset; for ( let j = jStart; j < jEnd; j++ ) { const 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; } let oldAnnotations; 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 ( let j = 0, jLen = newChunk.elements.length; j < jLen; j++ ) { const 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. const view = $( element ).data( 'view' ); let ann; 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 const 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 ) ); const 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 ); }; |