| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.dm.ElementLinearData = function VeDmElementLinearData() { |
| 19 |
|
| 20 |
ve.dm.ElementLinearData.super.apply( this, arguments ); |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
OO.inheritClass( ve.dm.ElementLinearData, ve.dm.FlatLinearData ); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
ve.dm.ElementLinearData.static.startWordRegExp = new RegExp( |
| 30 |
'^(' + unicodeJS.characterclass.patterns.word + ')' |
| 31 |
); |
| 32 |
|
| 33 |
ve.dm.ElementLinearData.static.endWordRegExp = new RegExp( |
| 34 |
'(' + unicodeJS.characterclass.patterns.word + ')$' |
| 35 |
); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
ve.dm.ElementLinearData.static.compareElementsUnannotated = function ( a, b ) { |
| 54 |
let aPlain = a, |
| 55 |
bPlain = b; |
| 56 |
|
| 57 |
if ( a === b ) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
if ( Array.isArray( a ) ) { |
| 62 |
aPlain = a[ 0 ]; |
| 63 |
} |
| 64 |
if ( Array.isArray( b ) ) { |
| 65 |
bPlain = b[ 0 ]; |
| 66 |
} |
| 67 |
if ( typeof aPlain === 'string' && typeof bPlain === 'string' ) { |
| 68 |
return aPlain === bPlain; |
| 69 |
} |
| 70 |
|
| 71 |
if ( typeof a !== typeof b ) { |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
if ( a.type !== b.type ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
if ( ve.dm.LinearData.static.isOpenElementData( a ) ) { |
| 84 |
|
| 85 |
aPlain = ve.dm.modelRegistry.lookup( a.type ).static.getHashObject( a ); |
| 86 |
delete aPlain.originalDomElementsHash; |
| 87 |
|
| 88 |
bPlain = ve.dm.modelRegistry.lookup( b.type ).static.getHashObject( b ); |
| 89 |
delete bPlain.originalDomElementsHash; |
| 90 |
|
| 91 |
return ve.compare( aPlain, bPlain ); |
| 92 |
} else { |
| 93 |
|
| 94 |
return true; |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
ve.dm.ElementLinearData.static.compareElements = function ( a, b, aStore, bStore ) { |
| 112 |
if ( a === b ) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
const typeofA = typeof a; |
| 117 |
|
| 118 |
if ( typeofA !== typeof b ) { |
| 119 |
|
| 120 |
return false; |
| 121 |
} |
| 122 |
if ( typeofA === 'string' ) { |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
if ( !this.compareElementsUnannotated( a, b ) ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
let aAnnotations, bAnnotations; |
| 130 |
|
| 131 |
if ( Array.isArray( a ) ) { |
| 132 |
aAnnotations = a[ 1 ]; |
| 133 |
} |
| 134 |
if ( Array.isArray( b ) ) { |
| 135 |
bAnnotations = b[ 1 ]; |
| 136 |
} |
| 137 |
if ( a && a.type ) { |
| 138 |
aAnnotations = a.annotations; |
| 139 |
} |
| 140 |
if ( b && b.type ) { |
| 141 |
bAnnotations = b.annotations; |
| 142 |
} |
| 143 |
|
| 144 |
const aSet = new ve.dm.AnnotationSet( aStore, aAnnotations || [] ); |
| 145 |
const bSet = new ve.dm.AnnotationSet( bStore || aStore, bAnnotations || [] ); |
| 146 |
|
| 147 |
return aSet.compareTo( bSet ); |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
ve.dm.ElementLinearData.static.getAnnotationHashesFromItem = function ( item ) { |
| 157 |
if ( typeof item === 'string' ) { |
| 158 |
return []; |
| 159 |
} else if ( item.annotations ) { |
| 160 |
return item.annotations.slice(); |
| 161 |
} else if ( item[ 1 ] ) { |
| 162 |
return item[ 1 ].slice(); |
| 163 |
} else { |
| 164 |
return []; |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
ve.dm.ElementLinearData.static.replaceAnnotationHashesForItem = function ( item, hashes ) { |
| 178 |
const isElement = ve.dm.LinearData.static.isElementData( item ); |
| 179 |
item = ve.copy( item ); |
| 180 |
hashes = hashes.slice(); |
| 181 |
if ( hashes.length > 0 ) { |
| 182 |
if ( isElement ) { |
| 183 |
|
| 184 |
item.annotations = hashes; |
| 185 |
} else { |
| 186 |
|
| 187 |
const character = ve.dm.ElementLinearData.static.getCharacterDataFromItem( item ); |
| 188 |
item = [ character, hashes ]; |
| 189 |
} |
| 190 |
} else { |
| 191 |
if ( isElement ) { |
| 192 |
|
| 193 |
delete item.annotations; |
| 194 |
} else { |
| 195 |
|
| 196 |
item = ve.dm.ElementLinearData.static.getCharacterDataFromItem( item ); |
| 197 |
} |
| 198 |
} |
| 199 |
return item; |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
ve.dm.ElementLinearData.static.getCharacterDataFromItem = function ( item ) { |
| 209 |
const data = Array.isArray( item ) ? item[ 0 ] : item; |
| 210 |
return typeof data === 'string' ? data : ''; |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
ve.dm.ElementLinearData.prototype.isContentOffset = function ( offset ) { |
| 234 |
|
| 235 |
if ( offset === 0 || offset === this.getLength() ) { |
| 236 |
return false; |
| 237 |
} |
| 238 |
const left = this.getData( offset - 1 ); |
| 239 |
const right = this.getData( offset ); |
| 240 |
const factory = ve.dm.nodeFactory; |
| 241 |
return ( |
| 242 |
|
| 243 |
( left !== undefined && right !== undefined ) && |
| 244 |
( |
| 245 |
|
| 246 |
|
| 247 |
( typeof left === 'string' || typeof right === 'string' ) || |
| 248 |
|
| 249 |
( Array.isArray( left ) || Array.isArray( right ) ) || |
| 250 |
|
| 251 |
( |
| 252 |
|
| 253 |
|
| 254 |
( |
| 255 |
|
| 256 |
typeof left.type === 'string' && |
| 257 |
|
| 258 |
left.type.charAt( 0 ) === '/' && |
| 259 |
|
| 260 |
factory.isNodeContent( left.type.slice( 1 ) ) |
| 261 |
) || |
| 262 |
|
| 263 |
|
| 264 |
( |
| 265 |
|
| 266 |
typeof right.type === 'string' && |
| 267 |
|
| 268 |
right.type.charAt( 0 ) !== '/' && |
| 269 |
|
| 270 |
factory.isNodeContent( right.type ) |
| 271 |
) || |
| 272 |
|
| 273 |
|
| 274 |
( |
| 275 |
|
| 276 |
'/' + left.type === right.type && |
| 277 |
|
| 278 |
factory.canNodeContainContent( left.type ) |
| 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 |
ve.dm.ElementLinearData.prototype.isStructuralOffset = function ( offset, unrestricted ) { |
| 319 |
|
| 320 |
if ( offset === 0 || offset === this.getLength() ) { |
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
const left = this.getData( offset - 1 ); |
| 325 |
const right = this.getData( offset ); |
| 326 |
const factory = ve.dm.nodeFactory; |
| 327 |
return ( |
| 328 |
( |
| 329 |
left !== undefined && |
| 330 |
right !== undefined && |
| 331 |
typeof left.type === 'string' && |
| 332 |
typeof right.type === 'string' |
| 333 |
) && |
| 334 |
( |
| 335 |
|
| 336 |
|
| 337 |
( |
| 338 |
|
| 339 |
left.type.charAt( 0 ) === '/' && |
| 340 |
|
| 341 |
( |
| 342 |
factory.canNodeHaveChildren( left.type.slice( 1 ) ) || |
| 343 |
!factory.isNodeContent( left.type.slice( 1 ) ) |
| 344 |
) && |
| 345 |
( |
| 346 |
|
| 347 |
!unrestricted || |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
factory.getParentNodeTypes( left.type.slice( 1 ) ) === null |
| 352 |
) |
| 353 |
) || |
| 354 |
|
| 355 |
|
| 356 |
( |
| 357 |
|
| 358 |
right.type.charAt( 0 ) !== '/' && |
| 359 |
|
| 360 |
( |
| 361 |
factory.canNodeHaveChildren( right.type ) || |
| 362 |
!factory.isNodeContent( right.type ) |
| 363 |
) && |
| 364 |
( |
| 365 |
|
| 366 |
!unrestricted || |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
factory.getParentNodeTypes( right.type ) === null |
| 371 |
) |
| 372 |
) || |
| 373 |
|
| 374 |
|
| 375 |
( |
| 376 |
|
| 377 |
'/' + left.type === right.type && |
| 378 |
|
| 379 |
factory.canNodeHaveChildrenNotContent( left.type ) && |
| 380 |
( |
| 381 |
|
| 382 |
!unrestricted || |
| 383 |
|
| 384 |
factory.getChildNodeTypes( left.type ) === null |
| 385 |
) |
| 386 |
) |
| 387 |
) |
| 388 |
); |
| 389 |
}; |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
ve.dm.ElementLinearData.prototype.isContentData = function () { |
| 400 |
let i = this.getLength(); |
| 401 |
while ( i-- ) { |
| 402 |
const item = this.getData( i ); |
| 403 |
if ( item.type !== undefined && |
| 404 |
item.type.charAt( 0 ) !== '/' && |
| 405 |
!ve.dm.nodeFactory.isNodeContent( item.type ) |
| 406 |
) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
} |
| 410 |
return true; |
| 411 |
}; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
ve.dm.ElementLinearData.prototype.canTakeAnnotationAtOffset = function ( offset, annotation, ignoreClose ) { |
| 422 |
if ( this.isElementData( offset ) ) { |
| 423 |
if ( ignoreClose && this.isCloseElementData( offset ) ) { |
| 424 |
return false; |
| 425 |
} |
| 426 |
const type = this.getType( offset ); |
| 427 |
|
| 428 |
|
| 429 |
return ve.dm.nodeFactory.isNodeContent( type ) && ve.dm.nodeFactory.canNodeTakeAnnotation( type, annotation ); |
| 430 |
} else { |
| 431 |
|
| 432 |
return true; |
| 433 |
} |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
ve.dm.ElementLinearData.prototype.getAnnotationHashesFromOffset = function ( offset, ignoreClose ) { |
| 445 |
if ( offset < 0 || offset > this.getLength() ) { |
| 446 |
throw new Error( 'offset ' + offset + ' out of bounds' ); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
if ( |
| 452 |
!ignoreClose && |
| 453 |
this.isCloseElementData( offset ) && |
| 454 |
!ve.dm.nodeFactory.canNodeHaveChildren( this.getType( offset ) ) |
| 455 |
) { |
| 456 |
offset = this.getRelativeContentOffset( offset, -1 ); |
| 457 |
if ( offset === -1 ) { |
| 458 |
return []; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
const item = this.getData( offset ); |
| 463 |
return this.constructor.static.getAnnotationHashesFromItem( item ) || []; |
| 464 |
}; |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
ve.dm.ElementLinearData.prototype.getAnnotationsFromOffset = function ( offset, ignoreClose ) { |
| 477 |
return new ve.dm.AnnotationSet( this.getStore(), this.getAnnotationHashesFromOffset( offset, ignoreClose ) ); |
| 478 |
}; |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
ve.dm.ElementLinearData.prototype.setAnnotationsAtOffset = function ( offset, annotations ) { |
| 489 |
this.setAnnotationHashesAtOffset( offset, this.getStore().hashAll( annotations.get() ) ); |
| 490 |
}; |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
ve.dm.ElementLinearData.prototype.setAnnotationHashesAtOffset = function ( offset, hashes ) { |
| 501 |
let item = this.getData( offset ); |
| 502 |
item = this.constructor.static.replaceAnnotationHashesForItem( item, hashes ); |
| 503 |
this.setData( offset, item ); |
| 504 |
}; |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
ve.dm.ElementLinearData.prototype.setAttributeAtOffset = function ( offset, key, value ) { |
| 514 |
if ( !this.isElementData( offset ) ) { |
| 515 |
return; |
| 516 |
} |
| 517 |
this.modifyData( offset, ( item ) => { |
| 518 |
if ( value === undefined ) { |
| 519 |
|
| 520 |
if ( item.attributes ) { |
| 521 |
delete item.attributes[ key ]; |
| 522 |
} |
| 523 |
} else { |
| 524 |
|
| 525 |
if ( !item.attributes ) { |
| 526 |
item.attributes = {}; |
| 527 |
} |
| 528 |
|
| 529 |
item.attributes[ key ] = value; |
| 530 |
} |
| 531 |
} ); |
| 532 |
}; |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
ve.dm.ElementLinearData.prototype.getCharacterData = function ( offset ) { |
| 541 |
const item = this.getData( offset ); |
| 542 |
return ve.dm.ElementLinearData.static.getCharacterDataFromItem( item ); |
| 543 |
}; |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
ve.dm.ElementLinearData.prototype.getAnnotatedRangeFromOffset = function ( offset, annotation ) { |
| 553 |
let start = offset, |
| 554 |
end = offset; |
| 555 |
if ( this.getAnnotationsFromOffset( offset ).contains( annotation ) === false ) { |
| 556 |
return null; |
| 557 |
} |
| 558 |
while ( start > 0 ) { |
| 559 |
start--; |
| 560 |
if ( this.getAnnotationsFromOffset( start ).contains( annotation ) === false ) { |
| 561 |
start++; |
| 562 |
break; |
| 563 |
} |
| 564 |
} |
| 565 |
while ( end < this.getLength() ) { |
| 566 |
if ( this.getAnnotationsFromOffset( end ).contains( annotation ) === false ) { |
| 567 |
break; |
| 568 |
} |
| 569 |
end++; |
| 570 |
} |
| 571 |
return new ve.Range( start, end ); |
| 572 |
}; |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
ve.dm.ElementLinearData.prototype.getAnnotatedRangeFromSelection = function ( range, annotation ) { |
| 582 |
let start = range.start, |
| 583 |
end = range.end; |
| 584 |
while ( start > 0 ) { |
| 585 |
start--; |
| 586 |
if ( this.getAnnotationsFromOffset( start ).contains( annotation ) === false ) { |
| 587 |
start++; |
| 588 |
break; |
| 589 |
} |
| 590 |
} |
| 591 |
while ( end < this.getLength() ) { |
| 592 |
if ( this.getAnnotationsFromOffset( end ).contains( annotation ) === false ) { |
| 593 |
break; |
| 594 |
} |
| 595 |
end++; |
| 596 |
} |
| 597 |
return new ve.Range( start, end ); |
| 598 |
}; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
ve.dm.ElementLinearData.prototype.getAnnotationsFromRange = function ( range, all, nullIfContentEmpty ) { |
| 610 |
let ignoreChildrenDepth = 0; |
| 611 |
let left, right; |
| 612 |
|
| 613 |
for ( let i = range.start; i < range.end; i++ ) { |
| 614 |
if ( this.isElementData( i ) ) { |
| 615 |
if ( ve.dm.nodeFactory.shouldIgnoreChildren( this.getType( i ) ) ) { |
| 616 |
ignoreChildrenDepth += this.isOpenElementData( i ) ? 1 : -1; |
| 617 |
} |
| 618 |
|
| 619 |
if ( !ve.dm.nodeFactory.isNodeContent( this.getType( i ) ) ) { |
| 620 |
continue; |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
if ( ignoreChildrenDepth > 0 ) { |
| 625 |
continue; |
| 626 |
} |
| 627 |
if ( !left ) { |
| 628 |
|
| 629 |
left = this.getAnnotationsFromOffset( i ); |
| 630 |
|
| 631 |
if ( range.getLength() === 0 || range.getLength() === 1 ) { |
| 632 |
return left; |
| 633 |
} |
| 634 |
continue; |
| 635 |
} |
| 636 |
|
| 637 |
right = this.getAnnotationsFromOffset( i ); |
| 638 |
if ( all && !right.isEmpty() ) { |
| 639 |
left.addSet( right ); |
| 640 |
} else if ( !all ) { |
| 641 |
|
| 642 |
if ( right.isEmpty() ) { |
| 643 |
return new ve.dm.AnnotationSet( this.getStore() ); |
| 644 |
} |
| 645 |
|
| 646 |
left = left.getComparableAnnotationsFromSet( right ); |
| 647 |
|
| 648 |
if ( left.isEmpty() ) { |
| 649 |
break; |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
return left || ( nullIfContentEmpty ? null : new ve.dm.AnnotationSet( this.getStore() ) ); |
| 654 |
}; |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
ve.dm.ElementLinearData.prototype.getInsertionAnnotationsFromRange = function ( range, startAfterAnnotations ) { |
| 669 |
let start; |
| 670 |
|
| 671 |
if ( range.isCollapsed() && !startAfterAnnotations ) { |
| 672 |
|
| 673 |
start = Math.max( 0, range.start - 1 ); |
| 674 |
} else { |
| 675 |
|
| 676 |
|
| 677 |
start = range.start; |
| 678 |
} |
| 679 |
|
| 680 |
let startAnnotations; |
| 681 |
|
| 682 |
if ( this.isContentOffset( start ) ) { |
| 683 |
startAnnotations = this.getAnnotationsFromOffset( start ); |
| 684 |
} else { |
| 685 |
startAnnotations = new ve.dm.AnnotationSet( this.getStore() ); |
| 686 |
} |
| 687 |
|
| 688 |
let afterAnnotations; |
| 689 |
|
| 690 |
if ( this.isContentOffset( range.end ) ) { |
| 691 |
afterAnnotations = this.getAnnotationsFromOffset( range.end ); |
| 692 |
} else { |
| 693 |
|
| 694 |
afterAnnotations = new ve.dm.AnnotationSet( this.getStore() ); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
return startAnnotations.filter( ( annotation ) => annotation.constructor.static.applyToAppendedContent || |
| 700 |
afterAnnotations.containsComparable( annotation ) ); |
| 701 |
}; |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
ve.dm.ElementLinearData.prototype.hasAnnotationsInRange = function ( range ) { |
| 710 |
for ( let i = range.start; i < range.end; i++ ) { |
| 711 |
if ( this.getAnnotationHashesFromOffset( i, true ).length ) { |
| 712 |
return true; |
| 713 |
} |
| 714 |
} |
| 715 |
return false; |
| 716 |
}; |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
ve.dm.ElementLinearData.prototype.trimOuterSpaceFromRange = function ( range ) { |
| 725 |
let start = range.start, |
| 726 |
end = range.end; |
| 727 |
while ( /^\s+$/.test( this.getCharacterData( end - 1 ) ) ) { |
| 728 |
end--; |
| 729 |
} |
| 730 |
while ( start < end && /^\s+$/.test( this.getCharacterData( start ) ) ) { |
| 731 |
start++; |
| 732 |
} |
| 733 |
return range.to < range.end ? new ve.Range( end, start ) : new ve.Range( start, end ); |
| 734 |
}; |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
ve.dm.ElementLinearData.prototype.isPlainText = function ( range, ignoreNonContentNodes, ignoredTypes, ignoreCoveringAnnotations, ignoreAllAnnotations ) { |
| 747 |
range = range || new ve.Range( 0, this.getLength() ); |
| 748 |
|
| 749 |
let annotations; |
| 750 |
if ( ignoreCoveringAnnotations ) { |
| 751 |
annotations = this.getAnnotationsFromRange( range ); |
| 752 |
} |
| 753 |
|
| 754 |
for ( let i = range.start; i < range.end; i++ ) { |
| 755 |
if ( typeof this.data[ i ] === 'string' ) { |
| 756 |
|
| 757 |
continue; |
| 758 |
} else if ( Array.isArray( this.data[ i ] ) ) { |
| 759 |
|
| 760 |
if ( ignoreAllAnnotations ) { |
| 761 |
continue; |
| 762 |
} |
| 763 |
if ( |
| 764 |
ignoreCoveringAnnotations && |
| 765 |
annotations.containsAllOf( this.getAnnotationsFromOffset( i ) ) |
| 766 |
) { |
| 767 |
continue; |
| 768 |
} |
| 769 |
} else if ( ignoreNonContentNodes || ignoredTypes ) { |
| 770 |
|
| 771 |
const type = this.getType( i ); |
| 772 |
if ( ignoredTypes && ignoredTypes.indexOf( type ) !== -1 ) { |
| 773 |
continue; |
| 774 |
} |
| 775 |
if ( ignoreNonContentNodes && !ve.dm.nodeFactory.isNodeContent( type ) ) { |
| 776 |
continue; |
| 777 |
} |
| 778 |
} |
| 779 |
return false; |
| 780 |
} |
| 781 |
return true; |
| 782 |
}; |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
ve.dm.ElementLinearData.prototype.forEachRunOfContent = function ( range, callback ) { |
| 794 |
let text = ''; |
| 795 |
for ( let i = range.start; i < range.end; i++ ) { |
| 796 |
if ( !this.isElementData( i ) ) { |
| 797 |
text += this.getCharacterData( i ); |
| 798 |
} else if ( ve.dm.nodeFactory.isNodeContent( this.getType( i ) ) ) { |
| 799 |
text += '\uFFFC'; |
| 800 |
} else { |
| 801 |
if ( text ) { |
| 802 |
callback( i - text.length, text ); |
| 803 |
} |
| 804 |
text = ''; |
| 805 |
} |
| 806 |
} |
| 807 |
if ( text ) { |
| 808 |
callback( range.end - text.length, text ); |
| 809 |
} |
| 810 |
}; |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
ve.dm.ElementLinearData.prototype.getText = function ( maintainIndices, range ) { |
| 820 |
range = range || new ve.Range( 0, this.getLength() ); |
| 821 |
|
| 822 |
let text = ''; |
| 823 |
for ( let i = range.start; i < range.end; i++ ) { |
| 824 |
if ( !this.isElementData( i ) ) { |
| 825 |
text += this.getCharacterData( i ); |
| 826 |
} else if ( maintainIndices ) { |
| 827 |
text += '\n'; |
| 828 |
} |
| 829 |
} |
| 830 |
return text; |
| 831 |
}; |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
ve.dm.ElementLinearData.prototype.getSourceText = function ( range ) { |
| 843 |
return ve.dm.sourceConverter.getSourceTextFromDataRange( this.data, range ); |
| 844 |
}; |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
ve.dm.ElementLinearData.prototype.getRelativeOffset = function ( offset, distance, callback, ...args ) { |
| 871 |
|
| 872 |
|
| 873 |
if ( distance === 0 ) { |
| 874 |
if ( callback.call( this, offset, ...args ) ) { |
| 875 |
return offset; |
| 876 |
} else { |
| 877 |
distance = 1; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
let direction = ( |
| 882 |
offset <= 0 ? 1 : ( |
| 883 |
offset >= this.getLength() ? -1 : ( |
| 884 |
distance > 0 ? 1 : -1 |
| 885 |
) |
| 886 |
) |
| 887 |
); |
| 888 |
distance = Math.abs( distance ); |
| 889 |
const start = offset; |
| 890 |
let i = start + direction; |
| 891 |
offset = -1; |
| 892 |
let steps = 0; |
| 893 |
let ignoreChildrenDepth = 0; |
| 894 |
let turnedAround = false; |
| 895 |
|
| 896 |
while ( i >= 0 && i <= this.getLength() ) { |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
const dataOffset = i + ( direction > 0 ? -1 : 0 ); |
| 901 |
if ( |
| 902 |
this.isElementData( dataOffset ) && |
| 903 |
ve.dm.nodeFactory.shouldIgnoreChildren( this.getType( dataOffset ) ) |
| 904 |
) { |
| 905 |
const isOpen = this.isOpenElementData( dataOffset ); |
| 906 |
|
| 907 |
|
| 908 |
if ( ( direction > 0 && isOpen ) || ( direction < 0 && !isOpen ) ) { |
| 909 |
ignoreChildrenDepth++; |
| 910 |
} else { |
| 911 |
ignoreChildrenDepth--; |
| 912 |
if ( ignoreChildrenDepth < 0 ) { |
| 913 |
return -1; |
| 914 |
} |
| 915 |
} |
| 916 |
} |
| 917 |
if ( callback.call( this, i, ...args ) ) { |
| 918 |
if ( !ignoreChildrenDepth ) { |
| 919 |
steps++; |
| 920 |
offset = i; |
| 921 |
if ( distance === steps ) { |
| 922 |
return offset; |
| 923 |
} |
| 924 |
} |
| 925 |
} else if ( |
| 926 |
|
| 927 |
!turnedAround && |
| 928 |
|
| 929 |
steps === 0 && |
| 930 |
|
| 931 |
( ( direction < 0 && i === 0 ) || ( direction > 0 && ( i === this.getLength() || this.getType( i - 1 ) === 'internalList' ) ) ) |
| 932 |
) { |
| 933 |
|
| 934 |
if ( callback.call( this, start, ...args ) ) { |
| 935 |
|
| 936 |
return start; |
| 937 |
} |
| 938 |
|
| 939 |
direction *= -1; |
| 940 |
i = start; |
| 941 |
distance = 1; |
| 942 |
turnedAround = true; |
| 943 |
ignoreChildrenDepth = 0; |
| 944 |
} |
| 945 |
i += direction; |
| 946 |
} |
| 947 |
return offset; |
| 948 |
}; |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
ve.dm.ElementLinearData.prototype.getRelativeContentOffset = function ( offset, distance ) { |
| 961 |
return this.getRelativeOffset( offset, distance, this.constructor.prototype.isContentOffset ); |
| 962 |
}; |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
ve.dm.ElementLinearData.prototype.getNearestContentOffset = function ( offset, direction ) { |
| 978 |
if ( this.isContentOffset( offset ) ) { |
| 979 |
return offset; |
| 980 |
} |
| 981 |
if ( direction === undefined ) { |
| 982 |
const left = this.getRelativeContentOffset( offset, -1 ); |
| 983 |
const right = this.getRelativeContentOffset( offset, 1 ); |
| 984 |
return offset - left < right - offset ? left : right; |
| 985 |
} else { |
| 986 |
return this.getRelativeContentOffset( offset, direction > 0 ? 1 : -1 ); |
| 987 |
} |
| 988 |
}; |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
ve.dm.ElementLinearData.prototype.getRelativeStructuralOffset = function ( offset, distance, unrestricted ) { |
| 1002 |
|
| 1003 |
if ( distance === 0 && ( offset === 0 || offset === this.getLength() ) ) { |
| 1004 |
return offset; |
| 1005 |
} |
| 1006 |
return this.getRelativeOffset( |
| 1007 |
offset, distance, this.constructor.prototype.isStructuralOffset, unrestricted |
| 1008 |
); |
| 1009 |
}; |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
ve.dm.ElementLinearData.prototype.getNearestStructuralOffset = function ( offset, direction, unrestricted ) { |
| 1026 |
if ( this.isStructuralOffset( offset, unrestricted ) ) { |
| 1027 |
return offset; |
| 1028 |
} |
| 1029 |
if ( !direction ) { |
| 1030 |
const left = this.getRelativeStructuralOffset( offset, -1, unrestricted ); |
| 1031 |
const right = this.getRelativeStructuralOffset( offset, 1, unrestricted ); |
| 1032 |
return offset - left < right - offset ? left : right; |
| 1033 |
} else { |
| 1034 |
return this.getRelativeStructuralOffset( offset, direction > 0 ? 1 : -1, unrestricted ); |
| 1035 |
} |
| 1036 |
}; |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
ve.dm.ElementLinearData.prototype.getWordRange = function ( offset ) { |
| 1051 |
const dataString = new ve.dm.DataString( this.getData() ); |
| 1052 |
|
| 1053 |
offset = this.getNearestContentOffset( offset ); |
| 1054 |
|
| 1055 |
let range; |
| 1056 |
if ( unicodeJS.wordbreak.isBreak( dataString, offset ) ) { |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
if ( this.constructor.static.endWordRegExp.exec( |
| 1063 |
( dataString.read( offset - 2 ) || ' ' ) + |
| 1064 |
( dataString.read( offset - 1 ) || ' ' ) |
| 1065 |
) ) { |
| 1066 |
|
| 1067 |
range = new ve.Range( |
| 1068 |
unicodeJS.wordbreak.prevBreakOffset( dataString, offset ), |
| 1069 |
offset |
| 1070 |
); |
| 1071 |
} else if ( this.constructor.static.startWordRegExp.exec( |
| 1072 |
( dataString.read( offset ) || ' ' ) + |
| 1073 |
( dataString.read( offset + 1 ) || ' ' ) |
| 1074 |
) ) { |
| 1075 |
|
| 1076 |
range = new ve.Range( |
| 1077 |
offset, |
| 1078 |
unicodeJS.wordbreak.nextBreakOffset( dataString, offset ) |
| 1079 |
); |
| 1080 |
} else { |
| 1081 |
|
| 1082 |
return new ve.Range( offset ); |
| 1083 |
} |
| 1084 |
} else { |
| 1085 |
|
| 1086 |
range = new ve.Range( |
| 1087 |
unicodeJS.wordbreak.prevBreakOffset( dataString, offset ), |
| 1088 |
unicodeJS.wordbreak.nextBreakOffset( dataString, offset ) |
| 1089 |
); |
| 1090 |
} |
| 1091 |
|
| 1092 |
if ( this.getText( false, range ).trim().length === 0 ) { |
| 1093 |
return new ve.Range( offset ); |
| 1094 |
} |
| 1095 |
return range; |
| 1096 |
}; |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
ve.dm.ElementLinearData.prototype.getUsedStoreValues = function ( range ) { |
| 1107 |
const store = this.getStore(), |
| 1108 |
valueStore = {}; |
| 1109 |
|
| 1110 |
range = range || new ve.Range( 0, this.data.length ); |
| 1111 |
|
| 1112 |
for ( let i = range.start; i < range.end; i++ ) { |
| 1113 |
|
| 1114 |
|
| 1115 |
const hashes = this.getAnnotationHashesFromOffset( i, true ); |
| 1116 |
let j = hashes.length; |
| 1117 |
while ( j-- ) { |
| 1118 |
const hash = hashes[ j ]; |
| 1119 |
if ( !Object.prototype.hasOwnProperty.call( valueStore, hash ) ) { |
| 1120 |
valueStore[ hash ] = store.value( hash ); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
if ( this.data[ i ].originalDomElementsHash !== undefined ) { |
| 1124 |
valueStore[ this.data[ i ].originalDomElementsHash ] = store.value( this.data[ i ].originalDomElementsHash ); |
| 1125 |
} |
| 1126 |
} |
| 1127 |
return valueStore; |
| 1128 |
}; |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
ve.dm.ElementLinearData.prototype.remapInternalListIndexes = function ( mapping, internalList ) { |
| 1140 |
for ( let i = 0, ilen = this.data.length; i < ilen; i++ ) { |
| 1141 |
if ( this.isOpenElementData( i ) ) { |
| 1142 |
const nodeClass = ve.dm.nodeFactory.lookup( this.getType( i ) ); |
| 1143 |
this.modifyData( i, ( item ) => { |
| 1144 |
nodeClass.static.remapInternalListIndexes( item, mapping, internalList ); |
| 1145 |
} ); |
| 1146 |
} |
| 1147 |
} |
| 1148 |
}; |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
ve.dm.ElementLinearData.prototype.remapInternalListKeys = function ( internalList ) { |
| 1158 |
for ( let i = 0, ilen = this.data.length; i < ilen; i++ ) { |
| 1159 |
if ( this.isOpenElementData( i ) ) { |
| 1160 |
const nodeClass = ve.dm.nodeFactory.lookup( this.getType( i ) ); |
| 1161 |
this.modifyData( i, ( item ) => { |
| 1162 |
nodeClass.static.remapInternalListKeys( item, internalList ); |
| 1163 |
} ); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
}; |
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
ve.dm.ElementLinearData.prototype.remapAnnotationHash = function ( oldHash, newHash ) { |
| 1175 |
function remap( annotations ) { |
| 1176 |
let spliceAt; |
| 1177 |
while ( ( spliceAt = annotations.indexOf( oldHash ) ) !== -1 ) { |
| 1178 |
if ( annotations.indexOf( newHash ) === -1 ) { |
| 1179 |
annotations.splice( spliceAt, 1, newHash ); |
| 1180 |
} else { |
| 1181 |
annotations.splice( spliceAt, 1 ); |
| 1182 |
} |
| 1183 |
} |
| 1184 |
} |
| 1185 |
for ( let i = 0, ilen = this.data.length; i < ilen; i++ ) { |
| 1186 |
if ( this.data[ i ] === undefined || typeof this.data[ i ] === 'string' ) { |
| 1187 |
|
| 1188 |
continue; |
| 1189 |
} else { |
| 1190 |
this.modifyData( i, ( item ) => { |
| 1191 |
if ( Array.isArray( item ) ) { |
| 1192 |
remap( item[ 1 ] ); |
| 1193 |
} else if ( item.annotations !== undefined ) { |
| 1194 |
remap( item.annotations ); |
| 1195 |
} |
| 1196 |
if ( ve.getProp( item, 'internal', 'metaItems' ) ) { |
| 1197 |
const data = ve.getProp( item, 'internal', 'metaItems' ); |
| 1198 |
for ( let j = 0, jlen = data.length; j < jlen; j++ ) { |
| 1199 |
if ( data[ j ].annotations !== undefined ) { |
| 1200 |
remap( data[ j ].annotations ); |
| 1201 |
} |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} ); |
| 1205 |
} |
| 1206 |
} |
| 1207 |
}; |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
ve.dm.ElementLinearData.prototype.sanitize = function ( rules ) { |
| 1225 |
const elementStack = [], |
| 1226 |
store = this.getStore(), |
| 1227 |
allAnnotations = this.getAnnotationsFromRange( new ve.Range( 0, this.getLength() ), true ); |
| 1228 |
|
| 1229 |
let emptySet, setToRemove; |
| 1230 |
if ( rules.plainText ) { |
| 1231 |
emptySet = new ve.dm.AnnotationSet( store ); |
| 1232 |
} else { |
| 1233 |
if ( rules.removeOriginalDomElements ) { |
| 1234 |
|
| 1235 |
for ( let i = 0, len = allAnnotations.getLength(); i < len; i++ ) { |
| 1236 |
const ann = allAnnotations.get( i ); |
| 1237 |
if ( ann.element.originalDomElementsHash !== undefined ) { |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
const oldHash = store.hashOfValue( ann ); |
| 1243 |
delete allAnnotations.get( i ).element.originalDomElementsHash; |
| 1244 |
const newHash = store.replaceHash( oldHash, ann ); |
| 1245 |
this.remapAnnotationHash( oldHash, newHash ); |
| 1246 |
if ( allAnnotations.storeHashes.indexOf( newHash ) !== -1 ) { |
| 1247 |
|
| 1248 |
|
| 1249 |
allAnnotations.storeHashes.splice( i, 1 ); |
| 1250 |
i--; |
| 1251 |
len--; |
| 1252 |
} else { |
| 1253 |
allAnnotations.storeHashes.splice( i, 1, newHash ); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
} |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
setToRemove = allAnnotations.filter( ( annotation ) => ( |
| 1261 |
rules.blacklist && rules.blacklist[ annotation.name ] |
| 1262 |
) || ( |
| 1263 |
|
| 1264 |
annotation.name === 'textStyle/span' && rules.removeOriginalDomElements |
| 1265 |
) ); |
| 1266 |
} |
| 1267 |
|
| 1268 |
let contentElement; |
| 1269 |
for ( let i = 0, len = this.getLength(); i < len; i++ ) { |
| 1270 |
if ( this.isElementData( i ) ) { |
| 1271 |
let type = this.getType( i ); |
| 1272 |
const canContainContent = ve.dm.nodeFactory.canNodeContainContent( type ); |
| 1273 |
const isOpen = this.isOpenElementData( i ); |
| 1274 |
|
| 1275 |
if ( isOpen ) { |
| 1276 |
elementStack.push( this.getData( i ) ); |
| 1277 |
} else { |
| 1278 |
elementStack.pop(); |
| 1279 |
} |
| 1280 |
|
| 1281 |
if ( rules.conversions && rules.conversions[ type ] ) { |
| 1282 |
type = rules.conversions[ type ]; |
| 1283 |
this.modifyData( i, ( item ) => { |
| 1284 |
item.type = ( !isOpen ? '/' : '' ) + type; |
| 1285 |
} ); |
| 1286 |
} |
| 1287 |
|
| 1288 |
|
| 1289 |
if ( rules.plainText && type !== 'paragraph' && canContainContent ) { |
| 1290 |
type = 'paragraph'; |
| 1291 |
this.setData( i, { |
| 1292 |
type: ( this.isCloseElementData( i ) ? '/' : '' ) + type |
| 1293 |
} ); |
| 1294 |
} |
| 1295 |
|
| 1296 |
|
| 1297 |
if ( |
| 1298 |
( rules.blacklist && rules.blacklist[ type ] ) || |
| 1299 |
( rules.plainText && type !== 'paragraph' && type !== 'internalList' ) || |
| 1300 |
( !rules.allowMetadata && ve.dm.nodeFactory.isMetaData( type ) ) |
| 1301 |
) { |
| 1302 |
this.splice( i, 1 ); |
| 1303 |
len--; |
| 1304 |
|
| 1305 |
if ( isOpen ) { |
| 1306 |
this.modifyData( i, ( item ) => { |
| 1307 |
ve.deleteProp( item, 'internal', 'generated' ); |
| 1308 |
} ); |
| 1309 |
} |
| 1310 |
|
| 1311 |
i--; |
| 1312 |
continue; |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
if ( !rules.allowBreaks && type === 'break' && contentElement ) { |
| 1317 |
if ( this.isOpenElementData( i - 1 ) && this.isCloseElementData( i + 2 ) ) { |
| 1318 |
|
| 1319 |
|
| 1320 |
this.splice( i, 2 ); |
| 1321 |
len -= 2; |
| 1322 |
} else { |
| 1323 |
this.splice( i, 2, { type: '/' + contentElement.type }, ve.copy( contentElement ) ); |
| 1324 |
} |
| 1325 |
|
| 1326 |
i--; |
| 1327 |
continue; |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
if ( |
| 1332 |
!rules.keepEmptyContentBranches && |
| 1333 |
isOpen && this.isCloseElementData( i + 1 ) && |
| 1334 |
!ve.getProp( this.getData( i ), 'internal', 'generated' ) && |
| 1335 |
canContainContent |
| 1336 |
) { |
| 1337 |
this.splice( i, 2 ); |
| 1338 |
len -= 2; |
| 1339 |
|
| 1340 |
i--; |
| 1341 |
continue; |
| 1342 |
} |
| 1343 |
|
| 1344 |
if ( !rules.preserveHtmlWhitespace ) { |
| 1345 |
this.modifyData( i, ( item ) => { |
| 1346 |
ve.deleteProp( item, 'internal', 'whitespace' ); |
| 1347 |
} ); |
| 1348 |
} |
| 1349 |
|
| 1350 |
if ( canContainContent && !isOpen && rules.singleLine ) { |
| 1351 |
i++; |
| 1352 |
const start = i; |
| 1353 |
while ( i < len && !( this.isOpenElementData( i ) && this.getType( i ) === 'internalList' ) ) { |
| 1354 |
i++; |
| 1355 |
} |
| 1356 |
this.splice( start, i - start ); |
| 1357 |
break; |
| 1358 |
} |
| 1359 |
|
| 1360 |
|
| 1361 |
if ( canContainContent ) { |
| 1362 |
contentElement = isOpen ? this.getData( i ) : null; |
| 1363 |
} |
| 1364 |
} else { |
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
if ( |
| 1372 |
this.getCharacterData( i ) === '\n' && |
| 1373 |
|
| 1374 |
!ve.dm.nodeFactory.doesNodeHaveSignificantWhitespace( elementStack[ elementStack.length - 1 ].type ) |
| 1375 |
) { |
| 1376 |
if ( /^\s+$/.test( this.getCharacterData( i + 1 ) ) || |
| 1377 |
/^\s+$/.test( this.getCharacterData( i - 1 ) ) |
| 1378 |
) { |
| 1379 |
|
| 1380 |
this.splice( i, 1 ); |
| 1381 |
len--; |
| 1382 |
|
| 1383 |
i--; |
| 1384 |
continue; |
| 1385 |
} else { |
| 1386 |
ce |
| 1387 |
if ( typeof this.getData( i ) === 'string' ) { |
| 1388 |
this.setData( i, ' ' ); |
| 1389 |
} else { |
| 1390 |
this.modifyData( i, ( item ) => { |
| 1391 |
item[ 0 ] = ' '; |
| 1392 |
} ); |
| 1393 |
} |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
if ( |
| 1400 |
this.getCharacterData( i ) === '\u00a0' && |
| 1401 |
|
| 1402 |
!ve.dm.nodeFactory.doesNodeHaveSignificantWhitespace( elementStack[ elementStack.length - 1 ].type ) |
| 1403 |
) { |
| 1404 |
if ( !( this.getCharacterData( i + 1 ) === ' ' || this.getCharacterData( i - 1 ) === ' ' ) ) { |
| 1405 |
|
| 1406 |
if ( typeof this.getData( i ) === 'string' ) { |
| 1407 |
this.setData( i, ' ' ); |
| 1408 |
} else { |
| 1409 |
this.modifyData( i, ( item ) => { |
| 1410 |
item[ 0 ] = ' '; |
| 1411 |
} ); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
} |
| 1415 |
} |
| 1416 |
const annotations = this.getAnnotationsFromOffset( i, true ); |
| 1417 |
if ( !annotations.isEmpty() ) { |
| 1418 |
if ( rules.plainText ) { |
| 1419 |
this.setAnnotationsAtOffset( i, emptySet ); |
| 1420 |
} else if ( setToRemove.getLength() ) { |
| 1421 |
|
| 1422 |
annotations.removeSet( setToRemove ); |
| 1423 |
this.setAnnotationsAtOffset( i, annotations ); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
if ( this.isOpenElementData( i ) ) { |
| 1427 |
if ( rules.nodeSanitization ) { |
| 1428 |
const nodeClass = ve.dm.modelRegistry.lookup( this.getType( i ) ); |
| 1429 |
|
| 1430 |
this.modifyData( i, ( item ) => { |
| 1431 |
nodeClass.static.sanitize( item, rules ); |
| 1432 |
} ); |
| 1433 |
} |
| 1434 |
if ( rules.removeOriginalDomElements ) { |
| 1435 |
this.modifyData( i, ( item ) => { |
| 1436 |
|
| 1437 |
delete item.originalDomElementsHash; |
| 1438 |
} ); |
| 1439 |
} |
| 1440 |
|
| 1441 |
if ( !rules.allowMetadata ) { |
| 1442 |
this.modifyData( i, ( item ) => { |
| 1443 |
ve.deleteProp( item, 'internal', 'metaItems' ); |
| 1444 |
} ); |
| 1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
}; |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
ve.dm.ElementLinearData.prototype.cloneElements = function ( preserveGenerated ) { |
| 1458 |
const store = this.getStore(); |
| 1459 |
for ( let i = 0, len = this.getLength(); i < len; i++ ) { |
| 1460 |
if ( this.isOpenElementData( i ) ) { |
| 1461 |
const nodeClass = ve.dm.nodeFactory.lookup( this.getType( i ) ); |
| 1462 |
if ( nodeClass ) { |
| 1463 |
this.setData( i, nodeClass.static.cloneElement( this.getData( i ), store, preserveGenerated ) ); |
| 1464 |
} |
| 1465 |
} |
| 1466 |
} |
| 1467 |
}; |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
ve.dm.ElementLinearData.prototype.countNonInternalElements = function ( limit ) { |
| 1476 |
let internalDepth = 0, |
| 1477 |
count = 0; |
| 1478 |
for ( let i = 0, l = this.getLength(); i < l; i++ ) { |
| 1479 |
const type = this.getType( i ); |
| 1480 |
if ( type && ve.dm.nodeFactory.isNodeInternal( type ) ) { |
| 1481 |
if ( this.isOpenElementData( i ) ) { |
| 1482 |
internalDepth++; |
| 1483 |
} else { |
| 1484 |
internalDepth--; |
| 1485 |
} |
| 1486 |
} else if ( !internalDepth ) { |
| 1487 |
count++; |
| 1488 |
if ( limit && count >= limit ) { |
| 1489 |
return count; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
} |
| 1493 |
return count; |
| 1494 |
}; |
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
ve.dm.ElementLinearData.prototype.hasContent = function () { |
| 1502 |
|
| 1503 |
|
| 1504 |
return this.countNonInternalElements( 3 ) > 2 || |
| 1505 |
|
| 1506 |
|
| 1507 |
( |
| 1508 |
this.isElementData( 0 ) && |
| 1509 |
!ve.dm.nodeFactory.canNodeContainContent( this.getType( 0 ) ) && |
| 1510 |
!ve.dm.nodeFactory.isNodeInternal( this.getType( 0 ) ) |
| 1511 |
); |
| 1512 |
}; |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
ve.dm.ElementLinearData.prototype.getCommonAnnotationArrayLength = function ( range ) { |
| 1521 |
const annotationHashesForOffset = []; |
| 1522 |
for ( let i = range.start; i < range.end; i++ ) { |
| 1523 |
annotationHashesForOffset.push( this.getAnnotationHashesFromOffset( i ) ); |
| 1524 |
} |
| 1525 |
return ve.getCommonStartSequenceLength( annotationHashesForOffset ); |
| 1526 |
}; |