| 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 |
ve.dm.Document = function VeDmDocument( data, htmlDocument, parentDocument, internalList, innerWhitespace, lang, dir, originalDocument, sourceMode, persistentStorage ) { |
| 32 |
|
| 33 |
ve.dm.Document.super.call( this, new ve.dm.DocumentNode() ); |
| 34 |
|
| 35 |
|
| 36 |
const doc = parentDocument || this; |
| 37 |
const root = this.documentNode; |
| 38 |
|
| 39 |
this.lang = lang || 'en'; |
| 40 |
this.dir = dir || 'ltr'; |
| 41 |
|
| 42 |
this.sourceMode = !!sourceMode; |
| 43 |
|
| 44 |
this.documentNode.setRoot( root ); |
| 45 |
|
| 46 |
|
| 47 |
this.documentNode.setDocument( doc ); |
| 48 |
this.internalList = internalList ? internalList.clone( this ) : new ve.dm.InternalList( this ); |
| 49 |
this.innerWhitespace = innerWhitespace ? ve.copy( innerWhitespace ) : new Array( 2 ); |
| 50 |
this.metaList = new ve.dm.MetaList( this ); |
| 51 |
|
| 52 |
|
| 53 |
this.parentDocument = parentDocument || null; |
| 54 |
this.originalDocument = originalDocument || null; |
| 55 |
this.nodesByType = {}; |
| 56 |
this.origInternalListLength = null; |
| 57 |
this.readOnly = false; |
| 58 |
this.attachedRoot = this.documentNode; |
| 59 |
|
| 60 |
|
| 61 |
this.branchNodeFromOffsetCache = []; |
| 62 |
|
| 63 |
if ( data instanceof ve.dm.ElementLinearData ) { |
| 64 |
this.data = data; |
| 65 |
} else if ( data instanceof ve.dm.FlatLinearData ) { |
| 66 |
this.data = new ve.dm.ElementLinearData( data.getStore(), data.getData() ); |
| 67 |
} else { |
| 68 |
this.data = new ve.dm.ElementLinearData( |
| 69 |
new ve.dm.HashValueStore(), |
| 70 |
Array.isArray( data ) ? data : [] |
| 71 |
); |
| 72 |
} |
| 73 |
this.store = this.data.getStore(); |
| 74 |
this.completeHistory = new ve.dm.Change(); |
| 75 |
|
| 76 |
this.completeHistory.store = this.store; |
| 77 |
if ( this.store.getLength() > 0 ) { |
| 78 |
|
| 79 |
this.completeHistory.transactions.push( new ve.dm.Transaction( [ { |
| 80 |
type: 'retain', |
| 81 |
length: this.data.data.length |
| 82 |
} ] ) ); |
| 83 |
this.completeHistory.storeLengthAtTransaction.push( this.store.getLength() ); |
| 84 |
} |
| 85 |
this.htmlDocument = htmlDocument || ve.createDocumentFromHtml( '' ); |
| 86 |
this.persistentStorage = persistentStorage || {}; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
OO.inheritClass( ve.dm.Document, ve.Document ); |
| 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 |
ve.dm.Document.static.addAnnotationsToData = function ( data, annotationSet, replaceComparable, store, prepend ) { |
| 130 |
const offset = prepend ? 0 : undefined; |
| 131 |
|
| 132 |
let ignoreChildrenDepth = 0; |
| 133 |
if ( annotationSet.isEmpty() ) { |
| 134 |
|
| 135 |
return; |
| 136 |
} |
| 137 |
if ( store ) { |
| 138 |
store.merge( annotationSet.getStore() ); |
| 139 |
} else { |
| 140 |
store = annotationSet.getStore(); |
| 141 |
} |
| 142 |
if ( !( data instanceof ve.dm.ElementLinearData ) ) { |
| 143 |
data = new ve.dm.ElementLinearData( store, data ); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
for ( let i = 0, length = data.getLength(); i < length; i++ ) { |
| 148 |
if ( data.isElementData( i ) && ve.dm.nodeFactory.shouldIgnoreChildren( data.getType( i ) ) ) { |
| 149 |
ignoreChildrenDepth += data.isOpenElementData( i ) ? 1 : -1; |
| 150 |
} |
| 151 |
if ( ignoreChildrenDepth ) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
const allowedAnnotations = annotationSet.filter( ( ann ) => data.canTakeAnnotationAtOffset( i, ann, true ) ); |
| 155 |
const existingAnnotations = data.getAnnotationsFromOffset( i, true ); |
| 156 |
let newAnnotationSet; |
| 157 |
if ( !existingAnnotations.isEmpty() ) { |
| 158 |
newAnnotationSet = existingAnnotations; |
| 159 |
if ( replaceComparable ) { |
| 160 |
newAnnotationSet = newAnnotationSet.withoutComparableSet( allowedAnnotations ); |
| 161 |
} |
| 162 |
newAnnotationSet.addSet( allowedAnnotations, offset ); |
| 163 |
} else { |
| 164 |
newAnnotationSet = allowedAnnotations; |
| 165 |
} |
| 166 |
data.setAnnotationsAtOffset( i, newAnnotationSet ); |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
ve.dm.Document.static.newBlankDocument = function ( paragraphType ) { |
| 177 |
const paragraph = { type: 'paragraph' }; |
| 178 |
|
| 179 |
paragraphType = paragraphType === undefined ? 'empty' : paragraphType; |
| 180 |
|
| 181 |
if ( paragraphType ) { |
| 182 |
ve.setProp( paragraph, 'internal', 'generated', paragraphType ); |
| 183 |
} |
| 184 |
|
| 185 |
return new ve.dm.Document( [ |
| 186 |
paragraph, |
| 187 |
{ type: '/paragraph' }, |
| 188 |
{ type: 'internalList' }, |
| 189 |
{ type: '/internalList' } |
| 190 |
] ); |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
ve.dm.Document.prototype.getDocumentNode = function () { |
| 199 |
if ( !this.documentNode.length && !this.documentNode.getDocument().buildingNodeTree ) { |
| 200 |
this.buildNodeTree(); |
| 201 |
} |
| 202 |
return this.documentNode; |
| 203 |
}; |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
ve.dm.Document.prototype.setAttachedRoot = function ( attachedRoot ) { |
| 211 |
this.attachedRoot = attachedRoot; |
| 212 |
}; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
ve.dm.Document.prototype.getAttachedRoot = function () { |
| 220 |
return this.attachedRoot; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
ve.dm.Document.prototype.getDocumentRange = function () { |
| 229 |
return new ve.Range( 0, this.getInternalList().getListNode().getOuterRange().start ); |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ve.dm.Document.prototype.getAttachedRootRange = function () { |
| 238 |
return this.attachedRoot === this.documentNode ? this.getDocumentRange() : this.attachedRoot.getRange(); |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
ve.dm.Document.prototype.buildNodeTree = function () { |
| 245 |
let textLength = 0, |
| 246 |
inTextNode = false; |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
let currentStack = []; |
| 252 |
let parentStack = [ this.documentNode ]; |
| 253 |
|
| 254 |
const nodeStack = [ parentStack, currentStack ]; |
| 255 |
let currentNode = this.documentNode; |
| 256 |
const doc = this.documentNode.getDocument(); |
| 257 |
|
| 258 |
for ( let i = 0, len = this.data.getLength(); i < len; i++ ) { |
| 259 |
if ( !this.data.isElementData( i ) ) { |
| 260 |
|
| 261 |
if ( !inTextNode ) { |
| 262 |
|
| 263 |
const node = new ve.dm.TextNode(); |
| 264 |
|
| 265 |
currentStack.push( node ); |
| 266 |
currentNode = node; |
| 267 |
|
| 268 |
inTextNode = true; |
| 269 |
} |
| 270 |
|
| 271 |
textLength++; |
| 272 |
} else { |
| 273 |
|
| 274 |
if ( inTextNode ) { |
| 275 |
|
| 276 |
currentNode.setLength( textLength ); |
| 277 |
|
| 278 |
currentNode = parentStack[ parentStack.length - 1 ]; |
| 279 |
inTextNode = false; |
| 280 |
textLength = 0; |
| 281 |
} |
| 282 |
|
| 283 |
if ( this.data.isOpenElementData( i ) ) { |
| 284 |
|
| 285 |
|
| 286 |
const node = ve.dm.nodeFactory.createFromElement( this.data.getData( i ) ); |
| 287 |
|
| 288 |
currentStack.push( node ); |
| 289 |
if ( ve.dm.nodeFactory.canNodeHaveChildren( node.getType() ) ) { |
| 290 |
|
| 291 |
parentStack = currentStack; |
| 292 |
currentStack = []; |
| 293 |
nodeStack.push( currentStack ); |
| 294 |
currentNode = node; |
| 295 |
} else { |
| 296 |
|
| 297 |
|
| 298 |
if ( |
| 299 |
!this.data.isCloseElementData( i + 1 ) || |
| 300 |
this.data.getType( i + 1 ) !== this.data.getType( i ) |
| 301 |
) { |
| 302 |
throw new Error( 'Opening element for node that cannot have children must be followed by closing element' ); |
| 303 |
} |
| 304 |
i++; |
| 305 |
} |
| 306 |
} else { |
| 307 |
|
| 308 |
if ( this.data.getType( i ) !== currentNode.getType() ) { |
| 309 |
throw new Error( 'Expected closing for ' + currentNode.getType() + |
| 310 |
' but got closing for ' + this.data.getType( i ) ); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
const children = nodeStack.pop(); |
| 315 |
currentStack = parentStack; |
| 316 |
parentStack = nodeStack[ nodeStack.length - 2 ]; |
| 317 |
if ( !parentStack ) { |
| 318 |
|
| 319 |
throw new Error( 'Unbalanced input passed to document' ); |
| 320 |
} |
| 321 |
|
| 322 |
ve.batchSplice( currentNode, 0, 0, children ); |
| 323 |
currentNode = parentStack[ parentStack.length - 1 ]; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if ( inTextNode ) { |
| 329 |
|
| 330 |
currentNode.setLength( textLength ); |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
doc.buildingNodeTree = true; |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
if ( nodeStack.length > 2 ) { |
| 341 |
|
| 342 |
throw new Error( 'Unbalanced input passed to document' ); |
| 343 |
} |
| 344 |
ve.batchSplice( this.documentNode, 0, 0, currentStack ); |
| 345 |
this.updateNodesByType( [ this.documentNode ], [ this.documentNode ] ); |
| 346 |
|
| 347 |
doc.buildingNodeTree = false; |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
ve.dm.Document.prototype.getLength = function () { |
| 356 |
return this.data.getLength(); |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
ve.dm.Document.prototype.getMetaList = function () { |
| 365 |
|
| 366 |
this.getDocumentNode(); |
| 367 |
return this.metaList; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
ve.dm.Document.prototype.setReadOnly = function ( readOnly ) { |
| 384 |
this.readOnly = !!readOnly; |
| 385 |
if ( !this.readOnly ) { |
| 386 |
|
| 387 |
this.getDocumentNode().traverse( ( node ) => { |
| 388 |
node.offset = null; |
| 389 |
} ); |
| 390 |
} |
| 391 |
}; |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
ve.dm.Document.prototype.isReadOnly = function () { |
| 399 |
return this.readOnly; |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
ve.dm.Document.prototype.commit = function ( transaction, isStaging ) { |
| 412 |
if ( transaction.hasBeenApplied() ) { |
| 413 |
throw new Error( 'Cannot commit a transaction that has already been committed' ); |
| 414 |
} |
| 415 |
this.emit( 'precommit', transaction ); |
| 416 |
this.branchNodeFromOffsetCache = []; |
| 417 |
new ve.dm.TransactionProcessor( this, transaction, isStaging ).process(); |
| 418 |
this.completeHistory.pushTransaction( transaction, this.store.getLength() ); |
| 419 |
this.emit( 'transact', transaction ); |
| 420 |
}; |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
ve.dm.Document.prototype.getData = function ( range, deep ) { |
| 430 |
return this.data.getDataSlice( range, deep ); |
| 431 |
}; |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
ve.dm.Document.prototype.getMetadata = function ( range ) { |
| 443 |
if ( arguments.length > 1 ) { |
| 444 |
throw new Error( 'Argument "deep" is no longer supported' ); |
| 445 |
} |
| 446 |
const documentNode = this.getDocumentNode(); |
| 447 |
if ( !range ) { |
| 448 |
range = new ve.Range( 0, documentNode.length ); |
| 449 |
} |
| 450 |
const data = []; |
| 451 |
documentNode.traverse( ( node ) => { |
| 452 |
let offset; |
| 453 |
if ( node instanceof ve.dm.MetaItem ) { |
| 454 |
offset = node.getOffset(); |
| 455 |
if ( range.start <= offset && offset < range.end ) { |
| 456 |
data[ offset - range.start ] = node; |
| 457 |
} |
| 458 |
} |
| 459 |
} ); |
| 460 |
data.length = range.end - range.start; |
| 461 |
return data; |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
ve.dm.Document.prototype.getHtmlDocument = function () { |
| 470 |
return this.htmlDocument; |
| 471 |
}; |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
ve.dm.Document.prototype.getOriginalDocument = function () { |
| 479 |
return this.originalDocument; |
| 480 |
}; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
ve.dm.Document.prototype.getStore = function () { |
| 488 |
return this.store; |
| 489 |
}; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
ve.dm.Document.prototype.getInternalList = function () { |
| 497 |
return this.internalList; |
| 498 |
}; |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
ve.dm.Document.prototype.getInnerWhitespace = function () { |
| 506 |
return this.innerWhitespace; |
| 507 |
}; |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
ve.dm.Document.prototype.shallowCloneFromSelection = function ( selection ) { |
| 518 |
if ( selection instanceof ve.dm.LinearSelection ) { |
| 519 |
return this.shallowCloneFromRange( selection.getRange() ); |
| 520 |
} else if ( selection instanceof ve.dm.TableSelection ) { |
| 521 |
const data = []; |
| 522 |
const ranges = selection.getTableSliceRanges( this ); |
| 523 |
for ( let i = 0, l = ranges.length; i < l; i++ ) { |
| 524 |
ve.batchPush( data, this.data.slice( ranges[ i ].start, ranges[ i ].end ) ); |
| 525 |
} |
| 526 |
const linearData = new ve.dm.ElementLinearData( this.getStore(), data ); |
| 527 |
|
| 528 |
const tableRange = new ve.Range( 0, data.length ); |
| 529 |
|
| 530 |
|
| 531 |
ve.batchSplice( |
| 532 |
linearData.data, linearData.getLength(), 0, |
| 533 |
this.getData( this.getInternalList().getListNode().getOuterRange(), true ) |
| 534 |
); |
| 535 |
|
| 536 |
|
| 537 |
return new ve.dm.TableSlice( |
| 538 |
linearData, this.getHtmlDocument(), undefined, this.getInternalList(), tableRange, this |
| 539 |
); |
| 540 |
} else { |
| 541 |
return this.shallowCloneFromRange( new ve.Range( 0 ) ); |
| 542 |
} |
| 543 |
}; |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
ve.dm.Document.prototype.shallowCloneFromRange = function ( range ) { |
| 554 |
const balanceOpenings = [], |
| 555 |
balanceClosings = [], |
| 556 |
contextOpenings = [], |
| 557 |
contextClosings = []; |
| 558 |
|
| 559 |
let linearData, originalRange, balancedRange; |
| 560 |
if ( !range ) { |
| 561 |
|
| 562 |
linearData = this.data.sliceObject(); |
| 563 |
originalRange = balancedRange = this.getDocumentRange(); |
| 564 |
} else { |
| 565 |
const selection = this.selectNodes( range, 'siblings' ); |
| 566 |
const first = selection[ 0 ]; |
| 567 |
const last = selection[ selection.length - 1 ]; |
| 568 |
let firstNode = first.node; |
| 569 |
let lastNode = last.node; |
| 570 |
|
| 571 |
|
| 572 |
let startNode = !firstNode.hasChildren() && !firstNode.isContent() ? firstNode : this.getBranchNodeFromOffset( range.start ); |
| 573 |
let endNode = !lastNode.hasChildren() && !lastNode.isContent() ? lastNode : this.getBranchNodeFromOffset( range.end ); |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
while ( selection[ 0 ] && selection[ 0 ].range && selection[ 0 ].range.isCollapsed() && !selection[ 0 ].node.isWrapped() ) { |
| 578 |
selection.shift(); |
| 579 |
} |
| 580 |
|
| 581 |
let i = selection.length - 1; |
| 582 |
while ( selection[ i ] && selection[ i ].range && selection[ i ].range.isCollapsed() && !selection[ i ].node.isWrapped() ) { |
| 583 |
selection.pop(); |
| 584 |
i--; |
| 585 |
} |
| 586 |
|
| 587 |
let balancedNodes; |
| 588 |
if ( selection.length === 0 || range.isCollapsed() ) { |
| 589 |
|
| 590 |
linearData = new ve.dm.ElementLinearData( this.getStore(), [ |
| 591 |
{ type: 'paragraph', internal: { generated: 'empty' } }, |
| 592 |
{ type: '/paragraph' } |
| 593 |
] ); |
| 594 |
originalRange = balancedRange = new ve.Range( 1 ); |
| 595 |
} else if ( startNode === endNode ) { |
| 596 |
|
| 597 |
balancedNodes = selection; |
| 598 |
} else { |
| 599 |
|
| 600 |
while ( !firstNode.isWrapped() ) { |
| 601 |
firstNode = firstNode.getParent(); |
| 602 |
} |
| 603 |
while ( !lastNode.isWrapped() ) { |
| 604 |
lastNode = lastNode.getParent(); |
| 605 |
} |
| 606 |
|
| 607 |
if ( first.range ) { |
| 608 |
while ( true ) { |
| 609 |
while ( !startNode.isWrapped() ) { |
| 610 |
startNode = startNode.getParent(); |
| 611 |
} |
| 612 |
balanceOpenings.push( startNode.getClonedElement() ); |
| 613 |
if ( startNode === firstNode ) { |
| 614 |
break; |
| 615 |
} |
| 616 |
startNode = startNode.getParent(); |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
if ( last !== first && last.range ) { |
| 621 |
while ( true ) { |
| 622 |
while ( !endNode.isWrapped() ) { |
| 623 |
endNode = endNode.getParent(); |
| 624 |
} |
| 625 |
balanceClosings.push( { type: '/' + endNode.getType() } ); |
| 626 |
if ( endNode === lastNode ) { |
| 627 |
break; |
| 628 |
} |
| 629 |
endNode = endNode.getParent(); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
balancedNodes = this.selectNodes( |
| 634 |
new ve.Range( firstNode.getOuterRange().start, lastNode.getOuterRange().end ), |
| 635 |
'covered' |
| 636 |
); |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
function nodeNeedsContext( node ) { |
| 641 |
return node.getParentNodeTypes() !== null || node.isContent(); |
| 642 |
} |
| 643 |
|
| 644 |
if ( !balancedRange ) { |
| 645 |
|
| 646 |
let needsContext = false; |
| 647 |
for ( i = balancedNodes.length - 1; i >= 0; i-- ) { |
| 648 |
if ( nodeNeedsContext( balancedNodes[ i ].node ) ) { |
| 649 |
needsContext = true; |
| 650 |
break; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
if ( needsContext ) { |
| 655 |
startNode = balancedNodes[ 0 ].node; |
| 656 |
|
| 657 |
while ( startNode.getParent() && nodeNeedsContext( startNode ) ) { |
| 658 |
const isContent = startNode.isContent(); |
| 659 |
startNode = startNode.getParent(); |
| 660 |
const contextElement = startNode.getClonedElement(); |
| 661 |
if ( isContent ) { |
| 662 |
ve.setProp( contextElement, 'internal', 'generated', 'wrapper' ); |
| 663 |
} |
| 664 |
contextOpenings.push( contextElement ); |
| 665 |
contextClosings.push( { type: '/' + contextElement.type } ); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
linearData = new ve.dm.ElementLinearData( |
| 672 |
this.getStore(), |
| 673 |
[].concat( |
| 674 |
contextOpenings.reverse(), |
| 675 |
balanceOpenings.reverse(), |
| 676 |
this.data.slice( range.start, range.end ), |
| 677 |
balanceClosings, |
| 678 |
contextClosings |
| 679 |
) |
| 680 |
); |
| 681 |
originalRange = new ve.Range( |
| 682 |
contextOpenings.length + balanceOpenings.length, |
| 683 |
contextOpenings.length + balanceOpenings.length + range.getLength() |
| 684 |
); |
| 685 |
balancedRange = new ve.Range( |
| 686 |
contextOpenings.length, |
| 687 |
contextOpenings.length + balanceOpenings.length + range.getLength() + balanceClosings.length |
| 688 |
); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
ve.batchSplice( |
| 693 |
linearData.data, linearData.getLength(), 0, |
| 694 |
this.getData( this.getInternalList().getListNode().getOuterRange() ) |
| 695 |
); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
const slice = new ve.dm.DocumentSlice( |
| 700 |
linearData, this.getHtmlDocument(), undefined, this.getInternalList(), originalRange, balancedRange, this |
| 701 |
); |
| 702 |
return slice; |
| 703 |
}; |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
ve.dm.Document.prototype.cloneFromRange = function ( range, detachedCopy, mode ) { |
| 715 |
const listRange = this.getInternalList().getListNode().getOuterRange(); |
| 716 |
const data = ve.copy( this.getFullData( range, mode || 'roundTrip' ) ); |
| 717 |
if ( range && ( range.start > listRange.start || range.end < listRange.end ) ) { |
| 718 |
|
| 719 |
ve.batchPush( data, this.getFullData( listRange ) ); |
| 720 |
} |
| 721 |
return this.cloneWithData( data, true, detachedCopy ); |
| 722 |
}; |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
ve.dm.Document.prototype.cloneWithData = function ( data, copyInternalList, detachedCopy ) { |
| 734 |
if ( Array.isArray( data ) ) { |
| 735 |
data = new ve.dm.ElementLinearData( this.getStore().slice(), data ); |
| 736 |
} |
| 737 |
|
| 738 |
const newDoc = new this.constructor( |
| 739 |
data, |
| 740 |
|
| 741 |
this.getHtmlDocument(), |
| 742 |
|
| 743 |
undefined, |
| 744 |
|
| 745 |
copyInternalList ? this.getInternalList() : undefined, |
| 746 |
|
| 747 |
undefined, |
| 748 |
|
| 749 |
this.getLang(), this.getDir(), |
| 750 |
|
| 751 |
this, |
| 752 |
|
| 753 |
this.sourceMode, |
| 754 |
|
| 755 |
this.getStorage() |
| 756 |
); |
| 757 |
if ( copyInternalList && !detachedCopy ) { |
| 758 |
|
| 759 |
|
| 760 |
newDoc.origInternalListLength = this.internalList.getItemNodeCount(); |
| 761 |
} |
| 762 |
return newDoc; |
| 763 |
}; |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
ve.dm.Document.prototype.getFullData = function ( range, mode ) { |
| 774 |
const insertedMetaItems = [], |
| 775 |
insertions = {}, |
| 776 |
iLen = range ? range.end : this.data.getLength(), |
| 777 |
result = []; |
| 778 |
|
| 779 |
function stripMetaLoadInfo( element ) { |
| 780 |
if ( !element || !element.internal ) { |
| 781 |
return element; |
| 782 |
} |
| 783 |
element = ve.copy( element ); |
| 784 |
delete element.internal.changesSinceLoad; |
| 785 |
delete element.internal.metaItems; |
| 786 |
delete element.internal.loadMetaParentHash; |
| 787 |
delete element.internal.loadMetaParentOffset; |
| 788 |
if ( Object.keys( element.internal ).length === 0 ) { |
| 789 |
delete element.internal; |
| 790 |
} |
| 791 |
return element; |
| 792 |
} |
| 793 |
|
| 794 |
for ( let i = range ? range.start : 0; i < iLen; i++ ) { |
| 795 |
const item = this.data.getData( i ); |
| 796 |
if ( |
| 797 |
ve.dm.LinearData.static.isOpenElementData( item ) && |
| 798 |
ve.dm.nodeFactory.isMetaData( item.type ) && |
| 799 |
( |
| 800 |
mode === 'noMetadata' || |
| 801 |
mode === 'roundTrip' && ( |
| 802 |
|
| 803 |
insertedMetaItems.indexOf( item.originalDomElementsHash ) !== -1 || |
| 804 |
|
| 805 |
|
| 806 |
ve.dm.nodeFactory.isRemovableMetaData( item.type ) && ve.getProp( item, 'internal', 'loadMetaParentOffset' ) |
| 807 |
) |
| 808 |
) |
| 809 |
) { |
| 810 |
|
| 811 |
i += 1; |
| 812 |
continue; |
| 813 |
} |
| 814 |
let metaItem, metaItems, internal; |
| 815 |
if ( |
| 816 |
mode === 'roundTrip' && |
| 817 |
( internal = item.internal ) && |
| 818 |
( metaItems = internal.metaItems ) |
| 819 |
) { |
| 820 |
if ( !internal.changesSinceLoad ) { |
| 821 |
this.data.modifyData( i, ( dataItem ) => { |
| 822 |
|
| 823 |
metaItems = dataItem.internal.metaItems; |
| 824 |
|
| 825 |
for ( let j = 0, jLen = metaItems.length; j < jLen; j++ ) { |
| 826 |
metaItem = metaItems[ j ]; |
| 827 |
const offset = i + metaItem.internal.loadMetaParentOffset; |
| 828 |
if ( !insertions[ offset ] ) { |
| 829 |
insertions[ offset ] = []; |
| 830 |
} |
| 831 |
|
| 832 |
delete metaItem.internal.loadBranchNodeHash; |
| 833 |
delete metaItem.internal.loadBranchNodeOffset; |
| 834 |
if ( Object.keys( metaItem.internal ).length === 0 ) { |
| 835 |
delete metaItem.internal; |
| 836 |
} |
| 837 |
insertions[ offset ].push( stripMetaLoadInfo( metaItem ) ); |
| 838 |
insertedMetaItems.push( metaItem.originalDomElementsHash ); |
| 839 |
} |
| 840 |
} ); |
| 841 |
} else { |
| 842 |
|
| 843 |
for ( let j = 0, jLen = metaItems.length; j < jLen; j++ ) { |
| 844 |
metaItem = metaItems[ j ]; |
| 845 |
if ( ve.dm.nodeFactory.isRemovableMetaData( metaItem.type ) ) { |
| 846 |
insertedMetaItems.push( metaItem.originalDomElementsHash ); |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
result.push( stripMetaLoadInfo( item ) ); |
| 852 |
if ( mode === 'roundTrip' && insertions[ i ] ) { |
| 853 |
for ( let j = 0, jLen = insertions[ i ].length; j < jLen; j++ ) { |
| 854 |
metaItem = insertions[ i ][ j ]; |
| 855 |
result.push( metaItem ); |
| 856 |
result.push( { type: '/' + metaItem.type } ); |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
return result; |
| 861 |
}; |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
ve.dm.Document.prototype.getSiblingWordBoundary = function ( offset, direction ) { |
| 871 |
const dataString = new ve.dm.DataString( this.getData() ); |
| 872 |
return unicodeJS.wordbreak.moveBreakOffset( direction, dataString, offset, true ); |
| 873 |
}; |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
ve.dm.Document.prototype.getRelativeOffset = function ( offset, direction, unit ) { |
| 884 |
const data = this.data; |
| 885 |
if ( unit === 'word' ) { |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
let newOffset = this.getSiblingWordBoundary( offset, direction ); |
| 890 |
if ( offset === newOffset ) { |
| 891 |
newOffset = this.getRelativeOffset( offset, direction, 'character' ); |
| 892 |
} |
| 893 |
return newOffset; |
| 894 |
} else { |
| 895 |
|
| 896 |
const adjacentDataOffset = offset + ( direction > 0 ? 0 : -1 ); |
| 897 |
if ( |
| 898 |
data.isElementData( adjacentDataOffset ) && |
| 899 |
ve.dm.nodeFactory.isNodeFocusable( data.getType( adjacentDataOffset ) ) |
| 900 |
) { |
| 901 |
|
| 902 |
return offset + direction; |
| 903 |
} |
| 904 |
const relativeContentOffset = data.getRelativeContentOffset( offset, direction ); |
| 905 |
let relativeStructuralOffset = data.getRelativeStructuralOffset( offset, direction, true ); |
| 906 |
let isFocusable; |
| 907 |
|
| 908 |
if ( ( relativeStructuralOffset - offset < 0 ? -1 : 1 ) !== direction ) { |
| 909 |
relativeStructuralOffset = offset; |
| 910 |
} else { |
| 911 |
isFocusable = ( relativeStructuralOffset - offset < 0 ? -1 : 1 ) === direction && |
| 912 |
data.isElementData( relativeStructuralOffset + direction ) && |
| 913 |
ve.dm.nodeFactory.isNodeFocusable( data.getType( relativeStructuralOffset + direction ) ); |
| 914 |
} |
| 915 |
|
| 916 |
if ( isFocusable || this.hasSlugAtOffset( relativeStructuralOffset ) ) { |
| 917 |
if ( isFocusable ) { |
| 918 |
relativeStructuralOffset += direction; |
| 919 |
} |
| 920 |
|
| 921 |
if ( |
| 922 |
relativeContentOffset === offset || |
| 923 |
( relativeContentOffset - offset < 0 ? -1 : 1 ) !== direction |
| 924 |
) { |
| 925 |
return relativeStructuralOffset; |
| 926 |
} |
| 927 |
|
| 928 |
return direction > 0 ? |
| 929 |
Math.min( relativeContentOffset, relativeStructuralOffset ) : |
| 930 |
Math.max( relativeContentOffset, relativeStructuralOffset ); |
| 931 |
} else { |
| 932 |
|
| 933 |
return direction > 0 ? |
| 934 |
Math.max( relativeContentOffset, offset ) : |
| 935 |
Math.min( relativeContentOffset, offset ); |
| 936 |
} |
| 937 |
} |
| 938 |
}; |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
ve.dm.Document.prototype.getRelativeRange = function ( range, direction, unit, expand, limit ) { |
| 952 |
let to = range.to; |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
if ( !range.isCollapsed() && !expand ) { |
| 957 |
const newOffset = direction > 0 ? range.end : range.start; |
| 958 |
if ( this.data.isContentOffset( newOffset ) || this.hasSlugAtOffset( newOffset ) ) { |
| 959 |
return new ve.Range( newOffset ); |
| 960 |
} else { |
| 961 |
to = newOffset; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
const contentOrSlugOffset = this.getRelativeOffset( to, direction, unit ); |
| 966 |
|
| 967 |
const focusableNode = this.getNearestFocusableNode( to, direction, contentOrSlugOffset ); |
| 968 |
let newRange; |
| 969 |
if ( focusableNode ) { |
| 970 |
newRange = focusableNode.getOuterRange( direction === -1 ); |
| 971 |
} else { |
| 972 |
newRange = new ve.Range( contentOrSlugOffset ); |
| 973 |
} |
| 974 |
if ( limit && !limit.containsRange( newRange ) ) { |
| 975 |
return range; |
| 976 |
} |
| 977 |
if ( expand ) { |
| 978 |
return new ve.Range( range.from, newRange.to ); |
| 979 |
} else { |
| 980 |
return newRange; |
| 981 |
} |
| 982 |
}; |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
ve.dm.Document.prototype.getNearestNodeMatching = function ( test, offset, direction, limit ) { |
| 994 |
|
| 995 |
|
| 996 |
let coveredOffset; |
| 997 |
this.data.getRelativeOffset( |
| 998 |
offset, |
| 999 |
direction === 1 ? 0 : -1, |
| 1000 |
function ( index, lim ) { |
| 1001 |
|
| 1002 |
if ( index >= Math.max( offset, lim ) || index < Math.min( offset, lim ) ) { |
| 1003 |
return true; |
| 1004 |
} |
| 1005 |
if ( |
| 1006 |
this.isOpenElementData( index ) && |
| 1007 |
test( this.getType( index ) ) |
| 1008 |
) { |
| 1009 |
coveredOffset = index + 1; |
| 1010 |
return true; |
| 1011 |
} |
| 1012 |
if ( |
| 1013 |
this.isCloseElementData( index ) && |
| 1014 |
test( this.getType( index ) ) |
| 1015 |
) { |
| 1016 |
coveredOffset = index; |
| 1017 |
return true; |
| 1018 |
} |
| 1019 |
}, |
| 1020 |
limit |
| 1021 |
); |
| 1022 |
if ( coveredOffset ) { |
| 1023 |
return this.getDocumentNode().getNodeFromOffset( coveredOffset ); |
| 1024 |
} else { |
| 1025 |
return null; |
| 1026 |
} |
| 1027 |
}; |
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
ve.dm.Document.prototype.getNearestFocusableNode = function ( offset, direction, limit ) { |
| 1038 |
return this.getNearestNodeMatching( ( nodeType ) => ve.dm.nodeFactory.isNodeFocusable( nodeType ), offset, direction, limit ); |
| 1039 |
}; |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
ve.dm.Document.prototype.getNearestCursorOffset = function ( offset, direction ) { |
| 1053 |
if ( direction === 0 ) { |
| 1054 |
const left = this.getNearestCursorOffset( offset, -1 ); |
| 1055 |
const right = this.getNearestCursorOffset( offset, 1 ); |
| 1056 |
|
| 1057 |
|
| 1058 |
if ( right === -1 ) { |
| 1059 |
return left; |
| 1060 |
} else if ( left === -1 ) { |
| 1061 |
return right; |
| 1062 |
} |
| 1063 |
return offset - left < right - offset ? left : right; |
| 1064 |
} |
| 1065 |
|
| 1066 |
direction = direction > 0 ? 1 : -1; |
| 1067 |
if ( |
| 1068 |
this.data.isContentOffset( offset ) || |
| 1069 |
this.hasSlugAtOffset( offset ) |
| 1070 |
) { |
| 1071 |
return offset; |
| 1072 |
} |
| 1073 |
|
| 1074 |
const contentOffset = this.data.getNearestContentOffset( offset, direction ); |
| 1075 |
const structuralOffset = this.data.getNearestStructuralOffset( offset, direction, true ); |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
if ( structuralOffset === -1 || !this.hasSlugAtOffset( structuralOffset ) ) { |
| 1080 |
return contentOffset; |
| 1081 |
} else if ( contentOffset === -1 ) { |
| 1082 |
return structuralOffset; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if ( direction === 1 ) { |
| 1086 |
if ( contentOffset < offset ) { |
| 1087 |
return structuralOffset; |
| 1088 |
} else { |
| 1089 |
return Math.min( contentOffset, structuralOffset ); |
| 1090 |
} |
| 1091 |
} else { |
| 1092 |
if ( contentOffset > offset ) { |
| 1093 |
return structuralOffset; |
| 1094 |
} else { |
| 1095 |
return Math.max( contentOffset, structuralOffset ); |
| 1096 |
} |
| 1097 |
} |
| 1098 |
}; |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
ve.dm.Document.prototype.getBranchNodeFromOffset = function ( offset ) { |
| 1104 |
if ( offset < 0 || offset > this.data.getLength() ) { |
| 1105 |
throw new Error( 've.dm.Document.getBranchNodeFromOffset(): offset ' + offset + ' is out of bounds' ); |
| 1106 |
} |
| 1107 |
if ( !this.branchNodeFromOffsetCache[ offset ] ) { |
| 1108 |
this.branchNodeFromOffsetCache[ offset ] = ve.Document.prototype.getBranchNodeFromOffset.call( this, offset ); |
| 1109 |
} |
| 1110 |
return this.branchNodeFromOffsetCache[ offset ]; |
| 1111 |
}; |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
ve.dm.Document.prototype.hasSlugAtOffset = function ( offset ) { |
| 1120 |
let node; |
| 1121 |
try { |
| 1122 |
node = this.getBranchNodeFromOffset( offset ); |
| 1123 |
} catch ( e ) { |
| 1124 |
|
| 1125 |
} |
| 1126 |
return node ? node.hasSlugAtOffset( offset ) : false; |
| 1127 |
}; |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
ve.dm.Document.prototype.getDataFromNode = function ( node ) { |
| 1136 |
let offset = node.getOffset(); |
| 1137 |
if ( offset >= 0 ) { |
| 1138 |
|
| 1139 |
|
| 1140 |
if ( node.isWrapped() ) { |
| 1141 |
offset++; |
| 1142 |
} |
| 1143 |
return this.data.slice( offset, offset + node.getLength() ); |
| 1144 |
} |
| 1145 |
return null; |
| 1146 |
}; |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
ve.dm.Document.prototype.rebuildTree = function () { |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
const attachedRoot = this.getAttachedRoot(); |
| 1158 |
this.rebuildTreeNode( attachedRoot ); |
| 1159 |
}; |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
ve.dm.Document.prototype.rebuildTreeNode = function ( rootNode ) { |
| 1167 |
if ( !rootNode.length ) { |
| 1168 |
this.buildNodeTree(); |
| 1169 |
} |
| 1170 |
const range = rootNode.getRange(); |
| 1171 |
const data = this.data.sliceObject( range.start, range.end ); |
| 1172 |
|
| 1173 |
|
| 1174 |
const documentFragment = new ve.dm.Document( data, this.htmlDocument, this ); |
| 1175 |
|
| 1176 |
const addedNodes = documentFragment.getDocumentNode().getChildren(); |
| 1177 |
|
| 1178 |
const removedNodes = ve.batchSplice( rootNode, 0, rootNode.getChildren().length, addedNodes ); |
| 1179 |
|
| 1180 |
this.updateNodesByType( addedNodes, removedNodes ); |
| 1181 |
|
| 1182 |
|
| 1183 |
this.branchNodeFromOffsetCache = []; |
| 1184 |
}; |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
ve.dm.Document.prototype.updateNodesByType = function ( addedNodes, removedNodes ) { |
| 1193 |
const remove = ( node ) => { |
| 1194 |
const type = node.getType(), |
| 1195 |
nodes = this.nodesByType[ type ] || [], |
| 1196 |
index = nodes.indexOf( node ); |
| 1197 |
|
| 1198 |
if ( index !== -1 ) { |
| 1199 |
nodes.splice( index, 1 ); |
| 1200 |
if ( !nodes.length ) { |
| 1201 |
delete this.nodesByType[ type ]; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
}; |
| 1205 |
|
| 1206 |
const add = ( node ) => { |
| 1207 |
const type = node.getType(), |
| 1208 |
nodes = this.nodesByType[ type ] = this.nodesByType[ type ] || []; |
| 1209 |
|
| 1210 |
nodes.push( node ); |
| 1211 |
}; |
| 1212 |
|
| 1213 |
const traverse = ( nodes, action ) => { |
| 1214 |
nodes.forEach( ( node ) => { |
| 1215 |
if ( node.hasChildren() ) { |
| 1216 |
node.traverse( action ); |
| 1217 |
} |
| 1218 |
action( node ); |
| 1219 |
} ); |
| 1220 |
}; |
| 1221 |
|
| 1222 |
traverse( removedNodes, remove ); |
| 1223 |
traverse( addedNodes, add ); |
| 1224 |
}; |
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
ve.dm.Document.prototype.getNodesByType = function ( type, sort ) { |
| 1239 |
if ( !this.documentNode.length && !this.documentNode.getDocument().buildingNodeTree ) { |
| 1240 |
this.buildNodeTree(); |
| 1241 |
} |
| 1242 |
let nodes; |
| 1243 |
if ( type instanceof Function ) { |
| 1244 |
nodes = []; |
| 1245 |
for ( const t in this.nodesByType ) { |
| 1246 |
const nodeType = ve.dm.nodeFactory.lookup( t ); |
| 1247 |
if ( nodeType === type || nodeType.prototype instanceof type ) { |
| 1248 |
ve.batchPush( nodes, this.getNodesByType( t ) ); |
| 1249 |
} |
| 1250 |
} |
| 1251 |
} else { |
| 1252 |
nodes = this.nodesByType[ type ] || []; |
| 1253 |
} |
| 1254 |
|
| 1255 |
if ( sort ) { |
| 1256 |
nodes.sort( ( a, b ) => a.getOffset() - b.getOffset() ); |
| 1257 |
} |
| 1258 |
return nodes; |
| 1259 |
}; |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
ve.dm.Document.prototype.fixupInsertion = function ( data, offset ) { |
| 1281 |
const |
| 1282 |
|
| 1283 |
newData = [], |
| 1284 |
|
| 1285 |
|
| 1286 |
remove = 0, |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
openingStack = [], |
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
closingStack = []; |
| 1296 |
|
| 1297 |
|
| 1298 |
let insertedDataOffset = 0, |
| 1299 |
insertedDataLength = data.length, |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
parentNode, |
| 1305 |
|
| 1306 |
parentType, |
| 1307 |
|
| 1308 |
inTextNode, |
| 1309 |
|
| 1310 |
|
| 1311 |
isFirstChild = null; |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
function writeElement( element, index ) { |
| 1323 |
if ( element.type !== undefined ) { |
| 1324 |
|
| 1325 |
if ( element.type.charAt( 0 ) !== '/' ) { |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
if ( openingStack.length === 0 && closingStack.length > 0 && |
| 1332 |
closingStack[ closingStack.length - 1 ].getType() === element.type |
| 1333 |
) { |
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
parentNode = closingStack.pop(); |
| 1339 |
} else { |
| 1340 |
|
| 1341 |
openingStack.push( element ); |
| 1342 |
} |
| 1343 |
parentType = element.type; |
| 1344 |
} else { |
| 1345 |
let expectedType; |
| 1346 |
|
| 1347 |
|
| 1348 |
if ( openingStack.length > 0 ) { |
| 1349 |
|
| 1350 |
|
| 1351 |
expectedType = openingStack.pop().type; |
| 1352 |
} else { |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
expectedType = parentNode.getType(); |
| 1357 |
closingStack.push( parentNode ); |
| 1358 |
parentNode = parentNode.getParent(); |
| 1359 |
if ( !parentNode ) { |
| 1360 |
throw new Error( 'Inserted data is trying to close the root node ' + |
| 1361 |
'(at index ' + index + ')' ); |
| 1362 |
} |
| 1363 |
parentType = expectedType; |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
if ( |
| 1372 |
element.type !== '/' + expectedType && |
| 1373 |
( |
| 1374 |
|
| 1375 |
|
| 1376 |
!ve.dm.nodeFactory.canNodeContainContent( element.type.slice( 1 ) ) || |
| 1377 |
!ve.dm.nodeFactory.canNodeContainContent( expectedType ) |
| 1378 |
) |
| 1379 |
) { |
| 1380 |
throw new Error( 'Cannot adopt content from ' + element.type + |
| 1381 |
' nodes into ' + expectedType + ' nodes (at index ' + index + ')' ); |
| 1382 |
} |
| 1383 |
} |
| 1384 |
} |
| 1385 |
} |
| 1386 |
newData.push( element ); |
| 1387 |
} |
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
function closeElement( closings, reopenElements, type, index ) { |
| 1401 |
|
| 1402 |
closings.push( { type: '/' + parentType } ); |
| 1403 |
if ( openingStack.length > 0 ) { |
| 1404 |
const element = openingStack.pop(); |
| 1405 |
parentType = element.type; |
| 1406 |
reopenElements.push( ve.copy( element ) ); |
| 1407 |
|
| 1408 |
|
| 1409 |
} else { |
| 1410 |
if ( !parentNode.getParent() ) { |
| 1411 |
throw new Error( 'Cannot insert ' + type + ' even after closing ' + |
| 1412 |
'all containing nodes (at index ' + index + ')' ); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
closingStack.push( parentNode ); |
| 1418 |
reopenElements.push( parentNode.getClonedElement() ); |
| 1419 |
parentNode = parentNode.getParent(); |
| 1420 |
parentType = parentNode.getType(); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
parentNode = this.getBranchNodeFromOffset( offset ); |
| 1425 |
parentType = parentNode.getType(); |
| 1426 |
inTextNode = false; |
| 1427 |
isFirstChild = this.data.isOpenElementData( offset - 1 ); |
| 1428 |
|
| 1429 |
for ( let i = 0; i < data.length; i++ ) { |
| 1430 |
if ( inTextNode && data[ i ].type !== undefined ) { |
| 1431 |
parentType = openingStack.length > 0 ? |
| 1432 |
openingStack[ openingStack.length - 1 ].type : parentNode.getType(); |
| 1433 |
} |
| 1434 |
if ( data[ i ].type === undefined || data[ i ].type.charAt( 0 ) !== '/' ) { |
| 1435 |
|
| 1436 |
|
| 1437 |
let childType = data[ i ].type || 'text'; |
| 1438 |
|
| 1439 |
const openings = []; |
| 1440 |
|
| 1441 |
const closings = []; |
| 1442 |
|
| 1443 |
const reopenElements = []; |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
if ( ve.dm.nodeFactory.isNodeContent( childType ) && |
| 1451 |
!ve.dm.nodeFactory.canNodeContainContent( parentType ) |
| 1452 |
) { |
| 1453 |
childType = 'paragraph'; |
| 1454 |
const wrapper = ve.dm.nodeFactory.getDataElement( childType ); |
| 1455 |
ve.setProp( wrapper, 'internal', 'generated', 'wrapper' ); |
| 1456 |
openings.unshift( wrapper ); |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
let parentsOK; |
| 1462 |
do { |
| 1463 |
const allowedParents = ve.dm.nodeFactory.getParentNodeTypes( childType ); |
| 1464 |
parentsOK = allowedParents === null || |
| 1465 |
allowedParents.indexOf( parentType ) !== -1; |
| 1466 |
if ( !parentsOK ) { |
| 1467 |
|
| 1468 |
if ( allowedParents.length === 0 ) { |
| 1469 |
throw new Error( 'Cannot insert ' + childType + ' because it ' + |
| 1470 |
' cannot have a parent (at index ' + i + ')' ); |
| 1471 |
} |
| 1472 |
|
| 1473 |
childType = allowedParents[ 0 ]; |
| 1474 |
openings.unshift( ve.dm.nodeFactory.getDataElement( childType ) ); |
| 1475 |
} |
| 1476 |
} while ( !parentsOK ); |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
const suggestedParents = ve.dm.nodeFactory.getSuggestedParentNodeTypes( childType ); |
| 1482 |
let suggestedParentsOK; |
| 1483 |
do { |
| 1484 |
suggestedParentsOK = suggestedParents === null || |
| 1485 |
suggestedParents.indexOf( parentType ) !== -1; |
| 1486 |
if ( !suggestedParentsOK ) { |
| 1487 |
closeElement( closings, reopenElements, childType, i ); |
| 1488 |
} |
| 1489 |
} while ( !suggestedParentsOK ); |
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
let childrenOK; |
| 1494 |
do { |
| 1495 |
const allowedChildren = ve.dm.nodeFactory.getChildNodeTypes( parentType ); |
| 1496 |
childrenOK = allowedChildren === null || |
| 1497 |
allowedChildren.indexOf( childType ) !== -1; |
| 1498 |
|
| 1499 |
|
| 1500 |
childrenOK = childrenOK && !( |
| 1501 |
!ve.dm.nodeFactory.isNodeContent( childType ) && |
| 1502 |
ve.dm.nodeFactory.canNodeContainContent( parentType ) |
| 1503 |
); |
| 1504 |
if ( !childrenOK ) { |
| 1505 |
|
| 1506 |
if ( isFirstChild ) { |
| 1507 |
|
| 1508 |
|
| 1509 |
const insertion = this.fixupInsertion( data, offset - 1 ); |
| 1510 |
if ( this.data.isCloseElementData( offset ) && !ve.dm.nodeFactory.isNodeInternal( parentType ) ) { |
| 1511 |
|
| 1512 |
|
| 1513 |
insertion.remove += 2; |
| 1514 |
} |
| 1515 |
return insertion; |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
closeElement( closings, reopenElements, childType, i ); |
| 1520 |
} |
| 1521 |
} while ( !childrenOK ); |
| 1522 |
|
| 1523 |
for ( let j = 0; j < closings.length; j++ ) { |
| 1524 |
|
| 1525 |
|
| 1526 |
if ( i === 0 ) { |
| 1527 |
insertedDataOffset++; |
| 1528 |
} else { |
| 1529 |
insertedDataLength++; |
| 1530 |
} |
| 1531 |
newData.push( closings[ j ] ); |
| 1532 |
} |
| 1533 |
for ( let j = 0; j < openings.length; j++ ) { |
| 1534 |
if ( i === 0 ) { |
| 1535 |
insertedDataOffset++; |
| 1536 |
} else { |
| 1537 |
insertedDataLength++; |
| 1538 |
} |
| 1539 |
writeElement( openings[ j ], i ); |
| 1540 |
} |
| 1541 |
writeElement( data[ i ], i ); |
| 1542 |
if ( data[ i ].type === undefined ) { |
| 1543 |
|
| 1544 |
inTextNode = true; |
| 1545 |
if ( openings.length > 0 ) { |
| 1546 |
|
| 1547 |
parentType = childType; |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
} else { |
| 1552 |
parentType = data[ i ].type; |
| 1553 |
} |
| 1554 |
} else { |
| 1555 |
|
| 1556 |
writeElement( data[ i ], i ); |
| 1557 |
parentType = openingStack.length > 0 ? |
| 1558 |
openingStack[ openingStack.length - 1 ].type : parentNode.getType(); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
if ( closingStack.length > 0 && this.data.isCloseElementData( offset ) ) { |
| 1563 |
|
| 1564 |
|
| 1565 |
return this.fixupInsertion( data, offset + 1 ); |
| 1566 |
} |
| 1567 |
|
| 1568 |
if ( inTextNode ) { |
| 1569 |
parentType = openingStack.length > 0 ? |
| 1570 |
openingStack[ openingStack.length - 1 ].type : parentNode.getType(); |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
while ( openingStack.length > 0 ) { |
| 1575 |
const popped = openingStack[ openingStack.length - 1 ]; |
| 1576 |
|
| 1577 |
|
| 1578 |
writeElement( { type: '/' + popped.type }, data.length ); |
| 1579 |
} |
| 1580 |
|
| 1581 |
while ( closingStack.length > 0 ) { |
| 1582 |
const popped = closingStack[ closingStack.length - 1 ]; |
| 1583 |
|
| 1584 |
|
| 1585 |
writeElement( popped.getClonedElement(), data.length ); |
| 1586 |
} |
| 1587 |
|
| 1588 |
return { |
| 1589 |
offset: offset, |
| 1590 |
data: newData, |
| 1591 |
remove: remove, |
| 1592 |
insertedDataOffset: insertedDataOffset !== 0 ? insertedDataOffset : undefined, |
| 1593 |
insertedDataLength: insertedDataLength !== newData.length ? insertedDataLength : undefined |
| 1594 |
}; |
| 1595 |
}; |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
ve.dm.Document.prototype.newFromHtml = function ( html, importRules ) { |
| 1605 |
const htmlDoc = typeof html === 'string' ? ve.createDocumentFromHtml( html ) : html, |
| 1606 |
doc = ve.dm.converter.getModelFromDom( htmlDoc, { |
| 1607 |
targetDoc: this.getHtmlDocument(), |
| 1608 |
fromClipboard: !!importRules |
| 1609 |
} ), |
| 1610 |
data = doc.data; |
| 1611 |
|
| 1612 |
if ( importRules ) { |
| 1613 |
data.sanitize( importRules.external || {} ); |
| 1614 |
data.sanitize( importRules.all || {} ); |
| 1615 |
} |
| 1616 |
|
| 1617 |
data.remapInternalListKeys( this.getInternalList() ); |
| 1618 |
|
| 1619 |
|
| 1620 |
doc.buildNodeTree(); |
| 1621 |
|
| 1622 |
return doc; |
| 1623 |
}; |
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
ve.dm.Document.prototype.findText = function ( query, options ) { |
| 1639 |
options = options || {}; |
| 1640 |
|
| 1641 |
const data = this.data, |
| 1642 |
searchRange = options.searchRange || this.getAttachedRootRange(); |
| 1643 |
let ranges = []; |
| 1644 |
|
| 1645 |
if ( query instanceof RegExp ) { |
| 1646 |
|
| 1647 |
data.forEachRunOfContent( searchRange, ( off, line ) => { |
| 1648 |
query.lastIndex = 0; |
| 1649 |
let match; |
| 1650 |
while ( ( match = query.exec( line ) ) !== null ) { |
| 1651 |
let matchText = match[ 0 ]; |
| 1652 |
|
| 1653 |
|
| 1654 |
if ( matchText.length === 0 ) { |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
query.lastIndex = match.index + 1; |
| 1660 |
continue; |
| 1661 |
} |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
if ( |
| 1672 |
matchText[ matchText.length - 1 ] === '\uFFFC' && |
| 1673 |
data.isOpenElementData( off + match.index + matchText.length - 1 ) && |
| 1674 |
data.isCloseElementData( off + match.index + matchText.length ) |
| 1675 |
) { |
| 1676 |
matchText += '\uFFFC'; |
| 1677 |
query.lastIndex += 1; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
if ( |
| 1683 |
matchText[ 0 ] === '\uFFFC' && |
| 1684 |
data.isOpenElementData( off + match.index - 1 ) && |
| 1685 |
data.isCloseElementData( off + match.index ) |
| 1686 |
) { |
| 1687 |
|
| 1688 |
query.lastIndex = match.index + 1; |
| 1689 |
continue; |
| 1690 |
} |
| 1691 |
|
| 1692 |
ranges.push( new ve.Range( |
| 1693 |
off + match.index, |
| 1694 |
off + match.index + matchText.length |
| 1695 |
) ); |
| 1696 |
if ( !options.noOverlaps ) { |
| 1697 |
query.lastIndex = match.index + 1; |
| 1698 |
} |
| 1699 |
} |
| 1700 |
} ); |
| 1701 |
} else { |
| 1702 |
const qLen = query.length; |
| 1703 |
let sensitivity; |
| 1704 |
if ( options.diacriticInsensitiveString ) { |
| 1705 |
sensitivity = options.caseSensitiveString ? 'case' : 'base'; |
| 1706 |
} else { |
| 1707 |
sensitivity = options.caseSensitiveString ? 'variant' : 'accent'; |
| 1708 |
} |
| 1709 |
|
| 1710 |
const compare = new Intl.Collator( this.lang, { sensitivity: sensitivity } ).compare; |
| 1711 |
|
| 1712 |
|
| 1713 |
for ( let offset = searchRange.start, l = searchRange.end - qLen; offset <= l; offset++ ) { |
| 1714 |
let j = 0; |
| 1715 |
while ( compare( data.getCharacterData( offset + j ), query[ j ] ) === 0 ) { |
| 1716 |
j++; |
| 1717 |
if ( j === qLen ) { |
| 1718 |
ranges.push( new ve.Range( offset, offset + qLen ) ); |
| 1719 |
offset += options.noOverlaps ? qLen - 1 : 0; |
| 1720 |
break; |
| 1721 |
} |
| 1722 |
} |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
if ( options.wholeWord ) { |
| 1727 |
const dataString = new ve.dm.DataString( this.getData() ); |
| 1728 |
ranges = ranges.filter( ( range ) => unicodeJS.wordbreak.isBreak( dataString, range.start ) && |
| 1729 |
unicodeJS.wordbreak.isBreak( dataString, range.end ) ); |
| 1730 |
} |
| 1731 |
|
| 1732 |
return ranges; |
| 1733 |
}; |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
ve.dm.Document.prototype.getCompleteHistoryLength = function () { |
| 1741 |
return this.completeHistory.getLength(); |
| 1742 |
}; |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
ve.dm.Document.prototype.getCompleteHistorySince = function ( start ) { |
| 1751 |
return this.completeHistory.transactions.slice( start ); |
| 1752 |
}; |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
ve.dm.Document.prototype.getChangeSince = function ( start ) { |
| 1761 |
const change = this.completeHistory.mostRecent( start ); |
| 1762 |
|
| 1763 |
change.selections = {}; |
| 1764 |
return change; |
| 1765 |
}; |
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
ve.dm.Document.prototype.getLang = function () { |
| 1773 |
return this.lang; |
| 1774 |
}; |
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
ve.dm.Document.prototype.getDir = function () { |
| 1782 |
return this.dir; |
| 1783 |
}; |
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
ve.dm.Document.prototype.setStorage = function ( keyOrStorage, value ) { |
| 1796 |
if ( typeof keyOrStorage === 'string' ) { |
| 1797 |
this.persistentStorage[ keyOrStorage ] = value; |
| 1798 |
this.emit( 'storage' ); |
| 1799 |
} else { |
| 1800 |
this.persistentStorage = keyOrStorage; |
| 1801 |
} |
| 1802 |
}; |
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
ve.dm.Document.prototype.getStorage = function ( key ) { |
| 1811 |
if ( key ) { |
| 1812 |
return this.persistentStorage[ key ]; |
| 1813 |
} else { |
| 1814 |
return this.persistentStorage; |
| 1815 |
} |
| 1816 |
}; |