| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.dm.Converter = function VeDmConverter( modelRegistry, nodeFactory, annotationFactory ) { |
| 19 |
|
| 20 |
this.modelRegistry = modelRegistry; |
| 21 |
this.nodeFactory = nodeFactory; |
| 22 |
this.annotationFactory = annotationFactory; |
| 23 |
this.doc = null; |
| 24 |
this.documentData = null; |
| 25 |
this.store = null; |
| 26 |
this.internalList = null; |
| 27 |
this.mode = null; |
| 28 |
this.fromClipboard = null; |
| 29 |
this.contextStack = null; |
| 30 |
|
| 31 |
|
| 32 |
const whitespaceList = this.constructor.static.whitespaceList; |
| 33 |
|
| 34 |
this.leadingWhitespaceRegex = new RegExp( '^[' + whitespaceList + ']' ); |
| 35 |
this.leadingWhitespacesRegex = new RegExp( '^[' + whitespaceList + ']+' ); |
| 36 |
this.trailingWhitespaceRegex = new RegExp( '[' + whitespaceList + ']$' ); |
| 37 |
this.trailingWhitespacesRegex = new RegExp( '[' + whitespaceList + ']+$' ); |
| 38 |
this.onlyWhitespaceRegex = new RegExp( '^[' + whitespaceList + ']+$' ); |
| 39 |
this.trimWhitespaceRegex = new RegExp( '^([' + whitespaceList + ']*)([\\s\\S]*?)([' + whitespaceList + ']*)$' ); |
| 40 |
|
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
OO.initClass( ve.dm.Converter ); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
ve.dm.Converter.static.computedAttributes = [ 'href', 'src' ]; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
ve.dm.Converter.static.whitespaceList = ' \\t\\f\\u200b\\r\\n'; |
| 66 |
|
| 67 |
ve.dm.Converter.static.PARSER_MODE = 0; |
| 68 |
ve.dm.Converter.static.CLIPBOARD_MODE = 1; |
| 69 |
ve.dm.Converter.static.PREVIEW_MODE = 2; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
ve.dm.Converter.static.getDataContentFromText = function ( text, annotations ) { |
| 82 |
const characters = text.split( '' ); |
| 83 |
|
| 84 |
if ( !annotations || annotations.isEmpty() ) { |
| 85 |
return characters; |
| 86 |
} |
| 87 |
|
| 88 |
for ( let i = 0, len = characters.length; i < len; i++ ) { |
| 89 |
|
| 90 |
characters[ i ] = [ characters[ i ], annotations.getHashes().slice() ]; |
| 91 |
} |
| 92 |
return characters; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
ve.dm.Converter.static.openAndCloseAnnotations = function ( currentSet, targetSet, open, close ) { |
| 109 |
let hash, i, len; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
if ( currentSet.getLength() ) { |
| 114 |
const targetSetOpen = targetSet.clone(); |
| 115 |
let startClosingAt; |
| 116 |
for ( i = 0, len = currentSet.getLength(); i < len; i++ ) { |
| 117 |
hash = currentSet.getHash( i ); |
| 118 |
|
| 119 |
|
| 120 |
if ( |
| 121 |
targetSetOpen.containsHash( hash ) || |
| 122 |
targetSetOpen.containsComparableForSerialization( currentSet.get( i ) ) |
| 123 |
) { |
| 124 |
targetSetOpen.removeHash( hash ); |
| 125 |
} else { |
| 126 |
startClosingAt = i; |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
if ( startClosingAt !== undefined ) { |
| 131 |
|
| 132 |
|
| 133 |
for ( i = currentSet.getLength() - 1; i >= startClosingAt; i-- ) { |
| 134 |
close( currentSet.get( i ) ); |
| 135 |
|
| 136 |
currentSet.removeAt( i ); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if ( targetSet.getLength() ) { |
| 142 |
const currentSetOpen = currentSet.clone(); |
| 143 |
|
| 144 |
for ( i = 0, len = targetSet.getLength(); i < len; i++ ) { |
| 145 |
hash = targetSet.getHash( i ); |
| 146 |
|
| 147 |
|
| 148 |
if ( |
| 149 |
currentSetOpen.containsHash( hash ) || |
| 150 |
currentSetOpen.containsComparableForSerialization( targetSet.get( i ) ) |
| 151 |
) { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
currentSetOpen.removeHash( hash ); |
| 156 |
} else { |
| 157 |
open( targetSet.get( i ) ); |
| 158 |
|
| 159 |
currentSet.pushHash( hash ); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
ve.dm.Converter.static.renderHtmlAttributeList = function ( originalDomElements, targetDomElements, filter, computed, deep ) { |
| 176 |
if ( filter === undefined ) { |
| 177 |
filter = true; |
| 178 |
} |
| 179 |
if ( filter === false ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
for ( let i = 0, ilen = originalDomElements.length; i < ilen; i++ ) { |
| 184 |
if ( !targetDomElements[ i ] ) { |
| 185 |
continue; |
| 186 |
} |
| 187 |
const attrs = originalDomElements[ i ].attributes; |
| 188 |
if ( !attrs ) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
for ( let j = 0, jlen = attrs.length; j < jlen; j++ ) { |
| 192 |
if ( |
| 193 |
targetDomElements[ i ].nodeType === Node.ELEMENT_NODE && |
| 194 |
!targetDomElements[ i ].hasAttribute( attrs[ j ].name ) && |
| 195 |
( filter === true || filter( attrs[ j ].name ) ) |
| 196 |
) { |
| 197 |
let value; |
| 198 |
if ( computed && this.computedAttributes.indexOf( attrs[ j ].name ) !== -1 ) { |
| 199 |
value = originalDomElements[ i ][ attrs[ j ].name ]; |
| 200 |
} else { |
| 201 |
value = attrs[ j ].value; |
| 202 |
} |
| 203 |
targetDomElements[ i ].setAttribute( attrs[ j ].name, value ); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
if ( deep && !targetDomElements[ i ].veFromDataElement && originalDomElements[ i ].children.length > 0 ) { |
| 209 |
this.renderHtmlAttributeList( |
| 210 |
originalDomElements[ i ].children, |
| 211 |
targetDomElements[ i ].children, |
| 212 |
filter, |
| 213 |
computed, |
| 214 |
true |
| 215 |
); |
| 216 |
} |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
ve.dm.Converter.static.moveInlineMetaItems = function ( data ) { |
| 235 |
const ancestors = [], |
| 236 |
pendingMetaItems = []; |
| 237 |
|
| 238 |
function closestMetaParent() { |
| 239 |
for ( let n = ancestors.length - 1; n >= 0; n-- ) { |
| 240 |
const ancestor = ancestors[ n ]; |
| 241 |
if ( ancestor.isMetaParent ) { |
| 242 |
return ancestor; |
| 243 |
} |
| 244 |
} |
| 245 |
return null; |
| 246 |
} |
| 247 |
|
| 248 |
let metaParent; |
| 249 |
for ( let i = 0; i < data.length; i++ ) { |
| 250 |
let item = data[ i ]; |
| 251 |
if ( Array.isArray( item ) ) { |
| 252 |
|
| 253 |
item = item[ 0 ]; |
| 254 |
} |
| 255 |
if ( !item.type ) { |
| 256 |
|
| 257 |
continue; |
| 258 |
} |
| 259 |
if ( item.type[ 0 ] !== '/' ) { |
| 260 |
|
| 261 |
if ( ve.getProp( item, 'internal', 'isInlineMeta' ) ) { |
| 262 |
|
| 263 |
delete item.internal.isInlineMeta; |
| 264 |
metaParent = closestMetaParent(); |
| 265 |
if ( metaParent ) { |
| 266 |
metaParent.item.internal.metaItems.push( item ); |
| 267 |
pendingMetaItems.push( { |
| 268 |
item: item, |
| 269 |
closeItem: data[ i + 1 ], |
| 270 |
metaParent: metaParent, |
| 271 |
offset: i - metaParent.offset - 1 |
| 272 |
} ); |
| 273 |
|
| 274 |
data.splice( i, 2 ); |
| 275 |
|
| 276 |
i--; |
| 277 |
} else { |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
i++; |
| 282 |
} |
| 283 |
} else { |
| 284 |
ancestors.push( { |
| 285 |
item: item, |
| 286 |
offset: i, |
| 287 |
isMetaParent: !!ve.getProp( item, 'internal', 'metaItems' ) |
| 288 |
} ); |
| 289 |
} |
| 290 |
} else { |
| 291 |
|
| 292 |
metaParent = ancestors.pop(); |
| 293 |
if ( metaParent.isMetaParent ) { |
| 294 |
for ( let j = 0; j < pendingMetaItems.length; j++ ) { |
| 295 |
const pending = pendingMetaItems[ j ]; |
| 296 |
if ( pending.metaParent.item !== metaParent.item ) { |
| 297 |
continue; |
| 298 |
} |
| 299 |
pending.item.internal.loadMetaParentHash = metaParent.item.originalDomElementsHash; |
| 300 |
pending.item.internal.loadMetaParentOffset = pending.offset; |
| 301 |
pendingMetaItems.splice( j, 1 ); |
| 302 |
j--; |
| 303 |
|
| 304 |
data.splice( i + 1, 0, pending.item, pending.closeItem ); |
| 305 |
i += 2; |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
}; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
ve.dm.Converter.prototype.isConverting = function () { |
| 320 |
return this.contextStack !== null; |
| 321 |
}; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
ve.dm.Converter.prototype.getStore = function () { |
| 329 |
return this.store; |
| 330 |
}; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
ve.dm.Converter.prototype.getHtmlDocument = function () { |
| 338 |
return this.doc; |
| 339 |
}; |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
ve.dm.Converter.prototype.getTargetHtmlDocument = function () { |
| 347 |
return this.targetDoc; |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
ve.dm.Converter.prototype.getMode = function () { |
| 356 |
return this.mode; |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
ve.dm.Converter.prototype.doesModeNeedRendering = function () { |
| 365 |
return this.getMode() !== this.constructor.static.PARSER_MODE; |
| 366 |
}; |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
ve.dm.Converter.prototype.isForParser = function () { |
| 374 |
return this.getMode() === this.constructor.static.PARSER_MODE; |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
ve.dm.Converter.prototype.isForClipboard = function () { |
| 383 |
return this.getMode() === this.constructor.static.CLIPBOARD_MODE; |
| 384 |
}; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
ve.dm.Converter.prototype.isForPreview = function () { |
| 392 |
return this.getMode() === this.constructor.static.PREVIEW_MODE; |
| 393 |
}; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
ve.dm.Converter.prototype.isFromClipboard = function () { |
| 401 |
return this.fromClipboard; |
| 402 |
}; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
ve.dm.Converter.prototype.getCurrentContext = function () { |
| 410 |
return this.contextStack === null ? null : this.contextStack[ this.contextStack.length - 1 ]; |
| 411 |
}; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
ve.dm.Converter.prototype.getActiveAnnotations = function () { |
| 420 |
const context = this.getCurrentContext(); |
| 421 |
return context ? context.annotations : null; |
| 422 |
}; |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
ve.dm.Converter.prototype.isExpectingContent = function () { |
| 431 |
const context = this.getCurrentContext(); |
| 432 |
return context ? context.expectingContent : null; |
| 433 |
}; |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
ve.dm.Converter.prototype.isValidChildNodeType = function ( nodeType ) { |
| 442 |
const context = this.getCurrentContext(); |
| 443 |
if ( !context ) { |
| 444 |
return null; |
| 445 |
} |
| 446 |
const childTypes = this.nodeFactory.getChildNodeTypes( context.branchType ); |
| 447 |
return ( childTypes === null || childTypes.indexOf( nodeType ) !== -1 ); |
| 448 |
}; |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
ve.dm.Converter.prototype.isInWrapper = function () { |
| 457 |
const context = this.getCurrentContext(); |
| 458 |
return context ? context.inWrapper : null; |
| 459 |
}; |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
ve.dm.Converter.prototype.canCloseWrapper = function () { |
| 468 |
const context = this.getCurrentContext(); |
| 469 |
return context ? context.canCloseWrapper : null; |
| 470 |
}; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
ve.dm.Converter.prototype.getDomElementsFromDataElement = function ( dataElements, doc, childDomElements ) { |
| 485 |
const dataElement = Array.isArray( dataElements ) ? dataElements[ 0 ] : dataElements, |
| 486 |
nodeClass = this.modelRegistry.lookup( dataElement.type ); |
| 487 |
|
| 488 |
if ( !nodeClass ) { |
| 489 |
throw new Error( 'Attempting to convert unknown data element type ' + dataElement.type ); |
| 490 |
} |
| 491 |
if ( nodeClass.static.isInternal ) { |
| 492 |
return false; |
| 493 |
} |
| 494 |
const domElements = nodeClass.static.toDomElements( dataElements, doc, this, childDomElements ); |
| 495 |
if ( !Array.isArray( domElements ) && !( nodeClass.prototype instanceof ve.dm.Annotation ) ) { |
| 496 |
throw new Error( 'toDomElements() failed to return an array when converting element of type ' + dataElement.type ); |
| 497 |
} |
| 498 |
const originalDomElements = this.store.value( dataElement.originalDomElementsHash ); |
| 499 |
|
| 500 |
if ( originalDomElements && !ve.isEqualDomElements( domElements, originalDomElements ) ) { |
| 501 |
this.constructor.static.renderHtmlAttributeList( |
| 502 |
originalDomElements, |
| 503 |
domElements, |
| 504 |
nodeClass.static.preserveHtmlAttributes, |
| 505 |
|
| 506 |
false, |
| 507 |
|
| 508 |
( |
| 509 |
!this.nodeFactory.lookup( dataElement.type ) || |
| 510 |
!this.nodeFactory.canNodeHaveChildren( dataElement.type ) || |
| 511 |
this.nodeFactory.doesNodeHandleOwnChildren( dataElement.type ) |
| 512 |
) |
| 513 |
); |
| 514 |
} |
| 515 |
|
| 516 |
if ( dataElement.internal && dataElement.internal.diff ) { |
| 517 |
Array.prototype.forEach.call( domElements, ( domElement ) => { |
| 518 |
for ( const key in dataElement.internal.diff ) { |
| 519 |
|
| 520 |
|
| 521 |
if ( domElement.setAttribute ) { |
| 522 |
domElement.setAttribute( key, dataElement.internal.diff[ key ] ); |
| 523 |
} |
| 524 |
} |
| 525 |
} ); |
| 526 |
} |
| 527 |
|
| 528 |
if ( this.nodeFactory.lookup( dataElement.type ) && this.nodeFactory.canNodeHaveChildren( dataElement.type ) ) { |
| 529 |
const hasSignificantWhitespace = this.nodeFactory.doesNodeHaveSignificantWhitespace( dataElement.type ); |
| 530 |
domElements.forEach( ( domElement ) => { |
| 531 |
domElement.veFromDataElement = true; |
| 532 |
if ( hasSignificantWhitespace ) { |
| 533 |
domElement.veHasSignificantWhitespace = true; |
| 534 |
} |
| 535 |
} ); |
| 536 |
} |
| 537 |
return domElements; |
| 538 |
}; |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
ve.dm.Converter.prototype.createDataElements = function ( modelClass, domElements ) { |
| 548 |
let dataElements = modelClass.static.toDataElement( domElements, this ); |
| 549 |
|
| 550 |
if ( !dataElements ) { |
| 551 |
return null; |
| 552 |
} |
| 553 |
if ( !Array.isArray( dataElements ) ) { |
| 554 |
dataElements = [ dataElements ]; |
| 555 |
} |
| 556 |
if ( dataElements.length ) { |
| 557 |
let serializer; |
| 558 |
if ( modelClass.prototype instanceof ve.dm.Annotation ) { |
| 559 |
serializer = function ( node ) { |
| 560 |
|
| 561 |
return node.cloneNode( false ).outerHTML; |
| 562 |
}; |
| 563 |
} else { |
| 564 |
serializer = ve.getNodeHtml; |
| 565 |
} |
| 566 |
dataElements[ 0 ].originalDomElementsHash = this.store.hash( |
| 567 |
domElements, |
| 568 |
domElements.map( serializer ).join( '' ) |
| 569 |
); |
| 570 |
if ( modelClass.prototype instanceof ve.dm.BranchNode && modelClass.static.childNodeTypes === null ) { |
| 571 |
|
| 572 |
ve.setProp( dataElements[ 0 ], 'internal', 'metaItems', [] ); |
| 573 |
ve.setProp( dataElements[ 0 ], 'internal', 'changesSinceLoad', 0 ); |
| 574 |
} |
| 575 |
} |
| 576 |
return dataElements; |
| 577 |
}; |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
ve.dm.Converter.prototype.getDomElementFromDataAnnotation = function ( dataAnnotation, doc ) { |
| 587 |
const htmlData = dataAnnotation.toHtml(), |
| 588 |
domElement = doc.createElement( htmlData.tag ); |
| 589 |
|
| 590 |
ve.setDomAttributes( domElement, htmlData.attributes ); |
| 591 |
return domElement; |
| 592 |
}; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
ve.dm.Converter.prototype.getModelFromDom = function ( doc, options, store ) { |
| 607 |
const tmpDoc = new ve.dm.Document(); |
| 608 |
const internalList = new ve.dm.InternalList( tmpDoc ); |
| 609 |
|
| 610 |
store = store || new ve.dm.HashValueStore(); |
| 611 |
options = options || {}; |
| 612 |
|
| 613 |
|
| 614 |
this.doc = doc; |
| 615 |
this.targetDoc = options.targetDoc || doc; |
| 616 |
this.fromClipboard = options.fromClipboard; |
| 617 |
this.store = store; |
| 618 |
this.internalList = internalList; |
| 619 |
this.contextStack = []; |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
const data = this.getDataFromDomSubtree( doc.body ); |
| 624 |
this.constructor.static.moveInlineMetaItems( data ); |
| 625 |
|
| 626 |
const linearData = new ve.dm.ElementLinearData( store, data ); |
| 627 |
const refData = this.internalList.convertToData( this, doc ); |
| 628 |
linearData.batchSplice( linearData.getLength(), 0, refData ); |
| 629 |
const innerWhitespace = this.getInnerWhitespace( linearData ); |
| 630 |
|
| 631 |
|
| 632 |
this.doc = null; |
| 633 |
this.targetDoc = null; |
| 634 |
this.fromClipboard = null; |
| 635 |
this.store = null; |
| 636 |
this.internalList = null; |
| 637 |
this.contextStack = null; |
| 638 |
|
| 639 |
return new ve.dm.Document( linearData, doc, undefined, internalList, innerWhitespace, options.lang, options.dir, null, null, tmpDoc.getStorage() ); |
| 640 |
}; |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
ve.dm.Converter.prototype.getDataFromDomClean = function ( domElement, wrapperElement, annotationSet ) { |
| 654 |
const contextStack = this.contextStack; |
| 655 |
this.contextStack = []; |
| 656 |
const result = this.getDataFromDomSubtree( domElement, wrapperElement, annotationSet ); |
| 657 |
this.contextStack = contextStack; |
| 658 |
return result; |
| 659 |
}; |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
ve.dm.Converter.prototype.getDataFromDomSubtree = function ( domElement, wrapperElement, annotationSet ) { |
| 671 |
const modelRegistry = this.modelRegistry, |
| 672 |
data = [], |
| 673 |
context = {}, |
| 674 |
prevContext = this.contextStack.length ? |
| 675 |
this.contextStack[ this.contextStack.length - 1 ] : null; |
| 676 |
let wrappingParagraph, |
| 677 |
nextWhitespace = '', |
| 678 |
wrappedWhitespace = '', |
| 679 |
wrappedWhitespaceIndex, |
| 680 |
wrappedMetaItems = []; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
const addWhitespace = ( element, index, whitespace ) => { |
| 691 |
if ( !whitespace ) { |
| 692 |
return; |
| 693 |
} |
| 694 |
if ( !element.internal ) { |
| 695 |
element.internal = {}; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
if ( !element.internal.whitespace ) { |
| 702 |
element.internal.whitespace = []; |
| 703 |
} |
| 704 |
element.internal.whitespace[ index ] = whitespace; |
| 705 |
}; |
| 706 |
const processNextWhitespace = ( element ) => { |
| 707 |
|
| 708 |
|
| 709 |
if ( nextWhitespace !== '' ) { |
| 710 |
addWhitespace( element, 0, nextWhitespace ); |
| 711 |
nextWhitespace = ''; |
| 712 |
} |
| 713 |
}; |
| 714 |
|
| 715 |
const outputWrappedMetaItems = ( whitespaceTreatment ) => { |
| 716 |
const toInsert = []; |
| 717 |
let prev = wrappingParagraph; |
| 718 |
|
| 719 |
for ( let j = 0, len = wrappedMetaItems.length; j < len; j++ ) { |
| 720 |
if ( wrappedMetaItems[ j ].type && wrappedMetaItems[ j ].type.charAt( 0 ) !== '/' ) { |
| 721 |
if ( wrappedMetaItems[ j ].internal && wrappedMetaItems[ j ].internal.whitespace ) { |
| 722 |
if ( whitespaceTreatment === 'restore' ) { |
| 723 |
ve.batchPush( toInsert, this.constructor.static.getDataContentFromText( |
| 724 |
wrappedMetaItems[ j ].internal.whitespace[ 0 ], context.annotations |
| 725 |
) ); |
| 726 |
delete wrappedMetaItems[ j ].internal; |
| 727 |
} else if ( whitespaceTreatment === 'fixup' ) { |
| 728 |
addWhitespace( prev, 3, wrappedMetaItems[ j ].internal.whitespace[ 0 ] ); |
| 729 |
} |
| 730 |
} |
| 731 |
prev = wrappedMetaItems[ j ]; |
| 732 |
} |
| 733 |
toInsert.push( wrappedMetaItems[ j ] ); |
| 734 |
} |
| 735 |
if ( wrappedWhitespace !== '' && whitespaceTreatment === 'restore' ) { |
| 736 |
|
| 737 |
|
| 738 |
ve.batchSplice( data, wrappedWhitespaceIndex, 0, toInsert ); |
| 739 |
} else { |
| 740 |
ve.batchPush( data, toInsert ); |
| 741 |
} |
| 742 |
wrappedMetaItems = []; |
| 743 |
}; |
| 744 |
const startWrapping = () => { |
| 745 |
|
| 746 |
|
| 747 |
wrappingParagraph = { |
| 748 |
type: 'paragraph', |
| 749 |
internal: { generated: 'wrapper', metaItems: [] } |
| 750 |
}; |
| 751 |
data.push( wrappingParagraph ); |
| 752 |
context.inWrapper = true; |
| 753 |
context.canCloseWrapper = true; |
| 754 |
context.expectingContent = true; |
| 755 |
processNextWhitespace( wrappingParagraph ); |
| 756 |
}; |
| 757 |
const stopWrapping = () => { |
| 758 |
if ( wrappedWhitespace !== '' ) { |
| 759 |
|
| 760 |
data.splice( wrappedWhitespaceIndex, wrappedWhitespace.length ); |
| 761 |
|
| 762 |
addWhitespace( wrappedMetaItems.length > 0 ? wrappedMetaItems[ wrappedMetaItems.length - 2 ] : wrappingParagraph, 3, wrappedWhitespace ); |
| 763 |
nextWhitespace = wrappedWhitespace; |
| 764 |
} |
| 765 |
data.push( { type: '/paragraph' } ); |
| 766 |
outputWrappedMetaItems( 'fixup' ); |
| 767 |
wrappingParagraph = undefined; |
| 768 |
context.inWrapper = false; |
| 769 |
context.canCloseWrapper = false; |
| 770 |
context.expectingContent = context.originallyExpectingContent; |
| 771 |
}; |
| 772 |
const getAboutGroup = ( node ) => { |
| 773 |
const group = [ node ]; |
| 774 |
|
| 775 |
if ( node.nodeType !== Node.ELEMENT_NODE || node.getAttribute( 'about' ) === null ) { |
| 776 |
return group; |
| 777 |
} |
| 778 |
const about = node.getAttribute( 'about' ); |
| 779 |
while ( ( node = node.nextSibling ) !== null ) { |
| 780 |
if ( node.nodeType === Node.ELEMENT_NODE && node.getAttribute( 'about' ) === about ) { |
| 781 |
group.push( node ); |
| 782 |
} else { |
| 783 |
break; |
| 784 |
} |
| 785 |
} |
| 786 |
return group; |
| 787 |
}; |
| 788 |
const isAllInstanceOf = ( linearData, targetClass ) => { |
| 789 |
for ( let j = linearData.length - 1; j >= 0; j-- ) { |
| 790 |
const type = ve.dm.LinearData.static.getType( linearData[ j ] ); |
| 791 |
if ( type ) { |
| 792 |
const itemClass = modelRegistry.lookup( type ) || ve.dm.AlienNode; |
| 793 |
if ( !( itemClass === targetClass || itemClass.prototype instanceof targetClass ) ) { |
| 794 |
return false; |
| 795 |
} |
| 796 |
} else { |
| 797 |
return false; |
| 798 |
} |
| 799 |
} |
| 800 |
return true; |
| 801 |
}; |
| 802 |
|
| 803 |
context.annotations = annotationSet || ( |
| 804 |
prevContext ? prevContext.annotations.clone() : new ve.dm.AnnotationSet( this.store ) |
| 805 |
); |
| 806 |
context.branchType = wrapperElement ? wrapperElement.type : ( |
| 807 |
prevContext ? prevContext.branchType : 'document' |
| 808 |
); |
| 809 |
context.branchHasContent = this.nodeFactory.canNodeContainContent( context.branchType ); |
| 810 |
context.originallyExpectingContent = context.branchHasContent || !context.annotations.isEmpty(); |
| 811 |
context.expectingContent = context.originallyExpectingContent; |
| 812 |
context.inWrapper = prevContext ? prevContext.inWrapper : false; |
| 813 |
context.canCloseWrapper = false; |
| 814 |
this.contextStack.push( context ); |
| 815 |
|
| 816 |
|
| 817 |
if ( wrapperElement ) { |
| 818 |
data.push( wrapperElement ); |
| 819 |
} |
| 820 |
|
| 821 |
function setInlineMeta( element ) { |
| 822 |
ve.setProp( element, 'internal', 'isInlineMeta', true ); |
| 823 |
} |
| 824 |
|
| 825 |
let prevElement; |
| 826 |
for ( let i = 0; i < domElement.childNodes.length; i++ ) { |
| 827 |
const childNode = domElement.childNodes[ i ]; |
| 828 |
switch ( childNode.nodeType ) { |
| 829 |
case Node.ELEMENT_NODE: |
| 830 |
case Node.COMMENT_NODE: { |
| 831 |
if ( |
| 832 |
childNode.getAttribute && |
| 833 |
childNode.getAttribute( 'data-ve-ignore' ) |
| 834 |
) { |
| 835 |
continue; |
| 836 |
} |
| 837 |
const aboutGroup = getAboutGroup( childNode ); |
| 838 |
const modelName = this.modelRegistry.matchElement( childNode, aboutGroup.length > 1 ); |
| 839 |
let modelClass = this.modelRegistry.lookup( modelName ) || ve.dm.AlienNode; |
| 840 |
let childNodes; |
| 841 |
if ( modelClass.prototype instanceof ve.dm.Annotation ) { |
| 842 |
childNodes = [ childNode ]; |
| 843 |
} else { |
| 844 |
|
| 845 |
childNodes = modelClass.static.enableAboutGrouping ? |
| 846 |
aboutGroup : [ childNode ]; |
| 847 |
} |
| 848 |
let childDataElements = this.createDataElements( modelClass, childNodes ); |
| 849 |
|
| 850 |
if ( !childDataElements ) { |
| 851 |
|
| 852 |
modelClass = ve.dm.AlienNode; |
| 853 |
childNodes = modelClass.static.enableAboutGrouping ? |
| 854 |
aboutGroup : [ childNode ]; |
| 855 |
childDataElements = this.createDataElements( modelClass, childNodes ); |
| 856 |
} else if ( childDataElements.length ) { |
| 857 |
|
| 858 |
modelClass = this.modelRegistry.lookup( childDataElements[ 0 ].type ); |
| 859 |
} else { |
| 860 |
continue; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
if ( |
| 866 |
!context.inWrapper && !context.expectingContent && |
| 867 |
modelClass.prototype instanceof ve.dm.Annotation && |
| 868 |
!this.isValidChildNodeType( 'paragraph' ) |
| 869 |
) { |
| 870 |
|
| 871 |
modelClass = ve.dm.AlienBlockNode; |
| 872 |
childNodes = modelClass.static.enableAboutGrouping ? |
| 873 |
aboutGroup : [ childNode ]; |
| 874 |
childDataElements = this.createDataElements( modelClass, childNodes ); |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
if ( modelClass.prototype instanceof ve.dm.Annotation ) { |
| 879 |
const annotation = this.annotationFactory.createFromElement( childDataElements[ 0 ], this.store ); |
| 880 |
|
| 881 |
if ( !context.inWrapper && !context.expectingContent ) { |
| 882 |
startWrapping(); |
| 883 |
prevElement = wrappingParagraph; |
| 884 |
} |
| 885 |
|
| 886 |
const childAnnotations = context.annotations.clone(); |
| 887 |
childAnnotations.push( annotation ); |
| 888 |
|
| 889 |
childDataElements = this.getDataFromDomSubtree( childNode, undefined, childAnnotations ); |
| 890 |
if ( !childDataElements.length || isAllInstanceOf( childDataElements, ve.dm.AlienMetaItem ) ) { |
| 891 |
|
| 892 |
if ( !childDataElements.length || isAllInstanceOf( childDataElements, ve.dm.RemovableAlienMetaItem ) ) { |
| 893 |
childDataElements = this.createDataElements( ve.dm.RemovableAlienMetaItem, childNodes ); |
| 894 |
} else { |
| 895 |
childDataElements = this.createDataElements( ve.dm.AlienMetaItem, childNodes ); |
| 896 |
} |
| 897 |
childDataElements.push( { type: '/' + childDataElements[ 0 ].type } ); |
| 898 |
|
| 899 |
if ( !context.annotations.isEmpty() ) { |
| 900 |
childDataElements[ 0 ].annotations = context.annotations.getHashes().slice(); |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
setInlineMeta( childDataElements[ 0 ] ); |
| 905 |
} |
| 906 |
outputWrappedMetaItems( 'restore' ); |
| 907 |
ve.batchPush( data, childDataElements ); |
| 908 |
|
| 909 |
wrappedWhitespace = ''; |
| 910 |
} else { |
| 911 |
|
| 912 |
if ( modelClass.prototype instanceof ve.dm.MetaItem ) { |
| 913 |
if ( context.expectingContent ) { |
| 914 |
|
| 915 |
childDataElements.forEach( setInlineMeta ); |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
if ( childDataElements.length === 1 ) { |
| 921 |
childDataElements.push( { type: '/' + childDataElements[ 0 ].type } ); |
| 922 |
} |
| 923 |
|
| 924 |
if ( !context.annotations.isEmpty() ) { |
| 925 |
childDataElements[ 0 ].annotations = context.annotations.getHashes().slice(); |
| 926 |
} |
| 927 |
|
| 928 |
|
| 929 |
if ( context.inWrapper && context.canCloseWrapper ) { |
| 930 |
ve.batchPush( wrappedMetaItems, childDataElements ); |
| 931 |
if ( wrappedWhitespace !== '' ) { |
| 932 |
data.splice( wrappedWhitespaceIndex, wrappedWhitespace.length ); |
| 933 |
addWhitespace( childDataElements[ 0 ], 0, wrappedWhitespace ); |
| 934 |
nextWhitespace = wrappedWhitespace; |
| 935 |
wrappedWhitespace = ''; |
| 936 |
} |
| 937 |
} else { |
| 938 |
outputWrappedMetaItems( 'restore' ); |
| 939 |
ve.batchPush( data, childDataElements ); |
| 940 |
processNextWhitespace( childDataElements[ 0 ] ); |
| 941 |
prevElement = childDataElements[ 0 ]; |
| 942 |
} |
| 943 |
|
| 944 |
i += childNodes.length - 1; |
| 945 |
break; |
| 946 |
} |
| 947 |
|
| 948 |
let childIsContent = this.nodeFactory.canNodeSerializeAsContent( childDataElements[ 0 ].type ); |
| 949 |
|
| 950 |
|
| 951 |
if ( !context.expectingContent && childIsContent ) { |
| 952 |
startWrapping(); |
| 953 |
prevElement = wrappingParagraph; |
| 954 |
} else if ( context.expectingContent && !childIsContent ) { |
| 955 |
if ( context.inWrapper && context.canCloseWrapper ) { |
| 956 |
stopWrapping(); |
| 957 |
} else { |
| 958 |
|
| 959 |
modelClass = ve.dm.AlienNode; |
| 960 |
childNodes = modelClass.static.enableAboutGrouping ? |
| 961 |
aboutGroup : [ childNode ]; |
| 962 |
childDataElements = this.createDataElements( modelClass, childNodes ); |
| 963 |
childIsContent = this.nodeFactory.canNodeSerializeAsContent( childDataElements[ 0 ].type ); |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
if ( context.inWrapper && childIsContent ) { |
| 970 |
outputWrappedMetaItems( 'restore' ); |
| 971 |
wrappedWhitespace = ''; |
| 972 |
|
| 973 |
nextWhitespace = ''; |
| 974 |
} |
| 975 |
|
| 976 |
|
| 977 |
if ( childIsContent && !context.annotations.isEmpty() ) { |
| 978 |
childDataElements[ 0 ].annotations = context.annotations.getHashes().slice(); |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
if ( |
| 983 |
childDataElements.length === 1 && |
| 984 |
childNodes.length === 1 && |
| 985 |
this.nodeFactory.canNodeHaveChildren( childDataElements[ 0 ].type ) && |
| 986 |
!this.nodeFactory.doesNodeHandleOwnChildren( childDataElements[ 0 ].type ) |
| 987 |
) { |
| 988 |
|
| 989 |
|
| 990 |
outputWrappedMetaItems( 'restore' ); |
| 991 |
ve.batchPush( data, |
| 992 |
this.getDataFromDomSubtree( childNode, childDataElements[ 0 ], |
| 993 |
new ve.dm.AnnotationSet( this.store ) |
| 994 |
) |
| 995 |
); |
| 996 |
} else { |
| 997 |
if ( childDataElements.length === 1 ) { |
| 998 |
childDataElements.push( { type: '/' + childDataElements[ 0 ].type } ); |
| 999 |
} |
| 1000 |
|
| 1001 |
outputWrappedMetaItems( 'restore' ); |
| 1002 |
ve.batchPush( data, childDataElements ); |
| 1003 |
} |
| 1004 |
processNextWhitespace( childDataElements[ 0 ] ); |
| 1005 |
prevElement = childDataElements[ 0 ]; |
| 1006 |
|
| 1007 |
|
| 1008 |
i += childNodes.length - 1; |
| 1009 |
} |
| 1010 |
break; |
| 1011 |
} |
| 1012 |
case Node.TEXT_NODE: { |
| 1013 |
let text = childNode.data; |
| 1014 |
if ( text === '' ) { |
| 1015 |
|
| 1016 |
break; |
| 1017 |
} |
| 1018 |
if ( !context.originallyExpectingContent ) { |
| 1019 |
|
| 1020 |
if ( this.onlyWhitespaceRegex.test( text ) ) { |
| 1021 |
|
| 1022 |
if ( context.inWrapper ) { |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
wrappedWhitespace = text; |
| 1027 |
wrappedWhitespaceIndex = data.length; |
| 1028 |
ve.batchPush( data, |
| 1029 |
this.constructor.static.getDataContentFromText( wrappedWhitespace, context.annotations ) |
| 1030 |
); |
| 1031 |
} else { |
| 1032 |
|
| 1033 |
if ( !prevElement ) { |
| 1034 |
if ( wrapperElement ) { |
| 1035 |
|
| 1036 |
|
| 1037 |
addWhitespace( wrapperElement, 1, text ); |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
} else { |
| 1043 |
addWhitespace( prevElement, 3, text ); |
| 1044 |
} |
| 1045 |
nextWhitespace = text; |
| 1046 |
wrappedWhitespace = ''; |
| 1047 |
outputWrappedMetaItems( 'restore' ); |
| 1048 |
} |
| 1049 |
|
| 1050 |
break; |
| 1051 |
} else { |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
const matches = text.match( this.trimWhitespaceRegex ); |
| 1057 |
if ( !context.inWrapper ) { |
| 1058 |
|
| 1059 |
startWrapping(); |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
if ( matches[ 1 ] !== '' ) { |
| 1064 |
if ( !prevElement ) { |
| 1065 |
if ( wrapperElement ) { |
| 1066 |
|
| 1067 |
|
| 1068 |
addWhitespace( wrapperElement, 1, matches[ 1 ] ); |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
} else { |
| 1074 |
addWhitespace( prevElement, 3, matches[ 1 ] ); |
| 1075 |
} |
| 1076 |
addWhitespace( wrappingParagraph, 0, matches[ 1 ] ); |
| 1077 |
} |
| 1078 |
} else { |
| 1079 |
outputWrappedMetaItems( 'restore' ); |
| 1080 |
|
| 1081 |
|
| 1082 |
ve.batchPush( data, |
| 1083 |
this.constructor.static.getDataContentFromText( matches[ 1 ], context.annotations ) |
| 1084 |
); |
| 1085 |
} |
| 1086 |
|
| 1087 |
ve.batchPush( data, |
| 1088 |
this.constructor.static.getDataContentFromText( matches[ 2 ], context.annotations ) |
| 1089 |
); |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
wrappedWhitespace = matches[ 3 ]; |
| 1099 |
wrappedWhitespaceIndex = data.length; |
| 1100 |
ve.batchPush( data, |
| 1101 |
this.constructor.static.getDataContentFromText( wrappedWhitespace, context.annotations ) |
| 1102 |
); |
| 1103 |
prevElement = wrappingParagraph; |
| 1104 |
break; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
if ( |
| 1112 |
context.annotations.isEmpty() && i === 0 && wrapperElement && |
| 1113 |
!this.nodeFactory.doesNodeHaveSignificantWhitespace( wrapperElement.type ) |
| 1114 |
) { |
| 1115 |
|
| 1116 |
const matches = text.match( this.leadingWhitespacesRegex ); |
| 1117 |
if ( matches && matches[ 0 ] !== '' ) { |
| 1118 |
addWhitespace( wrapperElement, 1, matches[ 0 ] ); |
| 1119 |
text = text.slice( matches[ 0 ].length ); |
| 1120 |
} |
| 1121 |
} |
| 1122 |
if ( |
| 1123 |
context.annotations.isEmpty() && |
| 1124 |
i === domElement.childNodes.length - 1 && |
| 1125 |
wrapperElement && |
| 1126 |
!this.nodeFactory.doesNodeHaveSignificantWhitespace( wrapperElement.type ) |
| 1127 |
) { |
| 1128 |
|
| 1129 |
const matches = text.match( this.trailingWhitespacesRegex ); |
| 1130 |
if ( matches && matches[ 0 ] !== '' ) { |
| 1131 |
addWhitespace( wrapperElement, 2, matches[ 0 ] ); |
| 1132 |
text = text.slice( 0, text.length - matches[ 0 ].length ); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
ve.batchPush( data, |
| 1138 |
this.constructor.static.getDataContentFromText( text, context.annotations ) |
| 1139 |
); |
| 1140 |
break; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
if ( context.inWrapper && context.canCloseWrapper ) { |
| 1146 |
stopWrapping(); |
| 1147 |
|
| 1148 |
context.inWrapper = true; |
| 1149 |
} |
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
if ( context.branchType !== 'paragraph' && wrapperElement && data[ data.length - 1 ] === wrapperElement && |
| 1154 |
!context.inWrapper && !this.nodeFactory.canNodeContainContent( context.branchType ) && |
| 1155 |
!this.nodeFactory.isNodeContent( context.branchType ) && |
| 1156 |
this.isValidChildNodeType( 'paragraph' ) |
| 1157 |
) { |
| 1158 |
const wrapperParagraph = { type: 'paragraph', internal: { generated: 'wrapper' } }; |
| 1159 |
processNextWhitespace( wrapperParagraph ); |
| 1160 |
data.push( wrapperParagraph ); |
| 1161 |
data.push( { type: '/paragraph' } ); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
if ( wrapperElement ) { |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
if ( nextWhitespace !== '' && data[ data.length - 1 ] !== wrapperElement ) { |
| 1170 |
addWhitespace( wrapperElement, 2, nextWhitespace ); |
| 1171 |
nextWhitespace = ''; |
| 1172 |
} |
| 1173 |
data.push( { type: '/' + wrapperElement.type } ); |
| 1174 |
} |
| 1175 |
|
| 1176 |
if ( context.branchType === 'document' && isAllInstanceOf( data, ve.dm.MetaItem ) && !annotationSet ) { |
| 1177 |
const emptyParagraph = { type: 'paragraph', internal: { generated: 'empty' } }; |
| 1178 |
processNextWhitespace( emptyParagraph ); |
| 1179 |
data.push( emptyParagraph ); |
| 1180 |
data.push( { type: '/paragraph' } ); |
| 1181 |
} |
| 1182 |
|
| 1183 |
this.contextStack.pop(); |
| 1184 |
return data; |
| 1185 |
}; |
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
ve.dm.Converter.prototype.getInnerWhitespace = function ( data ) { |
| 1194 |
const innerWhitespace = new Array( 2 ); |
| 1195 |
let stack = 0, |
| 1196 |
last = data.getLength() - 1; |
| 1197 |
|
| 1198 |
let whitespace; |
| 1199 |
if ( data.isOpenElementData( 0 ) ) { |
| 1200 |
whitespace = ve.getProp( data.getData( 0 ), 'internal', 'whitespace' ); |
| 1201 |
innerWhitespace[ 0 ] = whitespace ? whitespace[ 0 ] : undefined; |
| 1202 |
} |
| 1203 |
if ( data.isCloseElementData( last ) ) { |
| 1204 |
|
| 1205 |
stack++; |
| 1206 |
while ( --last ) { |
| 1207 |
if ( data.isCloseElementData( last ) ) { |
| 1208 |
stack++; |
| 1209 |
} else if ( data.isOpenElementData( last ) ) { |
| 1210 |
stack--; |
| 1211 |
if ( stack === 0 && data.getType( last ) !== 'internalList' ) { |
| 1212 |
break; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
whitespace = ve.getProp( data.getData( last ), 'internal', 'whitespace' ); |
| 1217 |
innerWhitespace[ 1 ] = whitespace ? whitespace[ 3 ] : undefined; |
| 1218 |
} |
| 1219 |
return innerWhitespace; |
| 1220 |
}; |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
ve.dm.Converter.prototype.getDomFromModel = function ( model, mode ) { |
| 1230 |
|
| 1231 |
if ( typeof mode === 'boolean' ) { |
| 1232 |
mode = mode ? this.constructor.static.CLIPBOARD_MODE : this.constructor.static.PARSER_MODE; |
| 1233 |
} |
| 1234 |
mode = mode || this.constructor.static.PARSER_MODE; |
| 1235 |
|
| 1236 |
const doc = ve.createDocumentFromHtml( '' ); |
| 1237 |
this.getDomSubtreeFromModel( model, doc.body, mode ); |
| 1238 |
|
| 1239 |
return doc; |
| 1240 |
}; |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
ve.dm.Converter.prototype.getDomFromNode = function ( node, mode ) { |
| 1250 |
|
| 1251 |
if ( typeof mode === 'boolean' ) { |
| 1252 |
mode = mode ? this.constructor.static.CLIPBOARD_MODE : this.constructor.static.PARSER_MODE; |
| 1253 |
} |
| 1254 |
mode = mode || this.constructor.static.PARSER_MODE; |
| 1255 |
return this.getDomFromModel( |
| 1256 |
node.getDocument().shallowCloneFromRange( node.isInternal() ? node.getRange() : node.getOuterRange() ), |
| 1257 |
mode |
| 1258 |
); |
| 1259 |
}; |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
ve.dm.Converter.prototype.getDomSubtreeFromModel = function ( model, container, mode ) { |
| 1269 |
if ( typeof mode === 'boolean' ) { |
| 1270 |
mode = mode ? this.constructor.static.CLIPBOARD_MODE : this.constructor.static.PARSER_MODE; |
| 1271 |
} |
| 1272 |
mode = mode || this.constructor.static.PARSER_MODE; |
| 1273 |
|
| 1274 |
this.documentData = model.getFullData( undefined, 'roundTrip' ); |
| 1275 |
this.store = model.getStore(); |
| 1276 |
this.internalList = model.getInternalList(); |
| 1277 |
|
| 1278 |
this.originalDocInternalList = model.getOriginalDocument() ? model.getOriginalDocument().getInternalList() : this.internalList; |
| 1279 |
this.mode = mode; |
| 1280 |
|
| 1281 |
this.getDomSubtreeFromData( this.documentData, container, model.getInnerWhitespace() ); |
| 1282 |
|
| 1283 |
|
| 1284 |
this.documentData = null; |
| 1285 |
this.store = null; |
| 1286 |
this.internalList = null; |
| 1287 |
this.originalDocInternalList = null; |
| 1288 |
this.mode = null; |
| 1289 |
}; |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
ve.dm.Converter.prototype.getDomSubtreeFromData = function ( data, container, innerWhitespace ) { |
| 1300 |
const whitespaceHtmlChars = ve.visibleWhitespaceCharacters, |
| 1301 |
isForPreview = this.isForPreview(), |
| 1302 |
dataLen = data.length, |
| 1303 |
doc = container.ownerDocument; |
| 1304 |
let text, annotatedDomElements, annotatedDomElementStack, |
| 1305 |
domElement = container; |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
const openAnnotation = () => { |
| 1312 |
|
| 1313 |
if ( text.length > 0 ) { |
| 1314 |
annotatedDomElements.push( doc.createTextNode( text ) ); |
| 1315 |
text = ''; |
| 1316 |
} |
| 1317 |
annotatedDomElements = []; |
| 1318 |
annotatedDomElementStack.push( annotatedDomElements ); |
| 1319 |
}; |
| 1320 |
|
| 1321 |
const closeAnnotation = ( annotation ) => { |
| 1322 |
const originalDomElements = annotation.getOriginalDomElements( this.store ), |
| 1323 |
origElementText = originalDomElements[ 0 ] && |
| 1324 |
originalDomElements[ 0 ].textContent || |
| 1325 |
''; |
| 1326 |
let leading = '', |
| 1327 |
trailing = ''; |
| 1328 |
|
| 1329 |
|
| 1330 |
if ( text.length > 0 ) { |
| 1331 |
annotatedDomElements.push( doc.createTextNode( text ) ); |
| 1332 |
text = ''; |
| 1333 |
} |
| 1334 |
|
| 1335 |
const annotatedChildDomElements = annotatedDomElementStack.pop(); |
| 1336 |
annotatedDomElements = annotatedDomElementStack[ annotatedDomElementStack.length - 1 ]; |
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
if ( annotation.constructor.static.trimWhitespace ) { |
| 1341 |
let matches; |
| 1342 |
let first = annotatedChildDomElements[ 0 ]; |
| 1343 |
while ( |
| 1344 |
first && |
| 1345 |
first.nodeType === Node.TEXT_NODE && |
| 1346 |
( matches = first.data.match( this.leadingWhitespacesRegex ) ) && |
| 1347 |
!this.leadingWhitespaceRegex.test( origElementText ) |
| 1348 |
) { |
| 1349 |
leading += matches[ 0 ]; |
| 1350 |
first.deleteData( 0, matches[ 0 ].length ); |
| 1351 |
if ( first.data.length !== 0 ) { |
| 1352 |
break; |
| 1353 |
} |
| 1354 |
|
| 1355 |
annotatedChildDomElements.shift(); |
| 1356 |
|
| 1357 |
first = annotatedChildDomElements[ 0 ]; |
| 1358 |
} |
| 1359 |
let last = annotatedChildDomElements[ annotatedChildDomElements.length - 1 ]; |
| 1360 |
while ( |
| 1361 |
last && |
| 1362 |
last.nodeType === Node.TEXT_NODE && |
| 1363 |
( matches = last.data.match( this.trailingWhitespacesRegex ) ) && |
| 1364 |
!this.trailingWhitespaceRegex.test( origElementText ) |
| 1365 |
) { |
| 1366 |
trailing = matches[ 0 ] + trailing; |
| 1367 |
last.deleteData( last.data.length - matches[ 0 ].length, matches[ 0 ].length ); |
| 1368 |
if ( last.data.length !== 0 ) { |
| 1369 |
break; |
| 1370 |
} |
| 1371 |
|
| 1372 |
annotatedChildDomElements.pop(); |
| 1373 |
|
| 1374 |
last = annotatedChildDomElements[ annotatedChildDomElements.length - 1 ]; |
| 1375 |
} |
| 1376 |
} |
| 1377 |
|
| 1378 |
let annotationElement; |
| 1379 |
if ( annotatedChildDomElements.length ) { |
| 1380 |
annotationElement = this.getDomElementsFromDataElement( |
| 1381 |
annotation.getElement(), doc, annotatedChildDomElements |
| 1382 |
)[ 0 ]; |
| 1383 |
} |
| 1384 |
|
| 1385 |
if ( leading ) { |
| 1386 |
annotatedDomElements.push( doc.createTextNode( leading ) ); |
| 1387 |
} |
| 1388 |
let n, len; |
| 1389 |
if ( annotationElement ) { |
| 1390 |
for ( n = 0, len = annotatedChildDomElements.length; n < len; n++ ) { |
| 1391 |
annotationElement.appendChild( annotatedChildDomElements[ n ] ); |
| 1392 |
} |
| 1393 |
annotatedDomElements.push( annotationElement ); |
| 1394 |
} else { |
| 1395 |
for ( n = 0, len = annotatedChildDomElements.length; n < len; n++ ) { |
| 1396 |
annotatedDomElements.push( annotatedChildDomElements[ n ] ); |
| 1397 |
} |
| 1398 |
} |
| 1399 |
if ( trailing ) { |
| 1400 |
annotatedDomElements.push( doc.createTextNode( trailing ) ); |
| 1401 |
} |
| 1402 |
}; |
| 1403 |
|
| 1404 |
const findEndOfNode = ( k ) => { |
| 1405 |
let n, depth; |
| 1406 |
for ( n = k + 1, depth = 1; n < dataLen && depth > 0; n++ ) { |
| 1407 |
if ( data[ n ].type ) { |
| 1408 |
depth += data[ n ].type.charAt( 0 ) === '/' ? -1 : 1; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
if ( depth !== 0 ) { |
| 1412 |
throw new Error( 'Unbalanced data: ' + depth + ' element(s) left open.' ); |
| 1413 |
} |
| 1414 |
return n; |
| 1415 |
}; |
| 1416 |
|
| 1417 |
const getDataElementOrSlice = ( i ) => { |
| 1418 |
let dataSlice; |
| 1419 |
if ( |
| 1420 |
ve.dm.nodeFactory.lookup( data[ i ].type ) && |
| 1421 |
ve.dm.nodeFactory.doesNodeHandleOwnChildren( data[ i ].type ) |
| 1422 |
) { |
| 1423 |
dataSlice = data.slice( i, findEndOfNode( i ) ); |
| 1424 |
} else { |
| 1425 |
dataSlice = data[ i ]; |
| 1426 |
} |
| 1427 |
return dataSlice; |
| 1428 |
}; |
| 1429 |
|
| 1430 |
const getChar = ( char ) => { |
| 1431 |
if ( |
| 1432 |
isForPreview && |
| 1433 |
!domElement.veHasSignificantWhitespace && |
| 1434 |
Object.prototype.hasOwnProperty.call( whitespaceHtmlChars, char ) |
| 1435 |
) { |
| 1436 |
char = whitespaceHtmlChars[ char ]; |
| 1437 |
} |
| 1438 |
return char; |
| 1439 |
}; |
| 1440 |
|
| 1441 |
const annotationStack = new ve.dm.AnnotationSet( this.store ); |
| 1442 |
|
| 1443 |
for ( let i = 0; i < dataLen; i++ ) { |
| 1444 |
if ( typeof data[ i ] === 'string' ) { |
| 1445 |
|
| 1446 |
text = ''; |
| 1447 |
let isStart = i > 0 && |
| 1448 |
ve.dm.LinearData.static.isOpenElementData( data[ i - 1 ] ) && |
| 1449 |
!ve.dm.nodeFactory.doesNodeHaveSignificantWhitespace( |
| 1450 |
ve.dm.LinearData.static.getType( data[ i - 1 ] ) |
| 1451 |
); |
| 1452 |
|
| 1453 |
while ( typeof data[ i ] === 'string' ) { |
| 1454 |
|
| 1455 |
|
| 1456 |
if ( !( isStart && this.onlyWhitespaceRegex.test( data[ i ] ) && this.isForParser() ) ) { |
| 1457 |
text += getChar( data[ i ] ); |
| 1458 |
isStart = false; |
| 1459 |
} |
| 1460 |
i++; |
| 1461 |
} |
| 1462 |
|
| 1463 |
i--; |
| 1464 |
|
| 1465 |
if ( text.length > 0 ) { |
| 1466 |
domElement.appendChild( doc.createTextNode( text ) ); |
| 1467 |
} |
| 1468 |
} else if ( |
| 1469 |
Array.isArray( data[ i ] ) || |
| 1470 |
( |
| 1471 |
data[ i ].annotations !== undefined && |
| 1472 |
this.nodeFactory.canNodeSerializeAsContent( data[ i ].type ) |
| 1473 |
) |
| 1474 |
) { |
| 1475 |
|
| 1476 |
text = ''; |
| 1477 |
annotatedDomElements = []; |
| 1478 |
annotatedDomElementStack = [ annotatedDomElements ]; |
| 1479 |
while ( |
| 1480 |
data[ i ] !== undefined && ( |
| 1481 |
Array.isArray( data[ i ] ) || |
| 1482 |
( |
| 1483 |
data[ i ].annotations !== undefined && |
| 1484 |
this.nodeFactory.canNodeSerializeAsContent( data[ i ].type ) |
| 1485 |
) |
| 1486 |
) |
| 1487 |
) { |
| 1488 |
const annotations = new ve.dm.AnnotationSet( |
| 1489 |
this.store, data[ i ].annotations || data[ i ][ 1 ] |
| 1490 |
); |
| 1491 |
this.constructor.static.openAndCloseAnnotations( annotationStack, annotations, |
| 1492 |
openAnnotation, closeAnnotation |
| 1493 |
); |
| 1494 |
|
| 1495 |
if ( data[ i ].annotations === undefined ) { |
| 1496 |
|
| 1497 |
text += getChar( data[ i ][ 0 ] ); |
| 1498 |
} else { |
| 1499 |
|
| 1500 |
|
| 1501 |
if ( text.length > 0 ) { |
| 1502 |
annotatedDomElements.push( doc.createTextNode( text ) ); |
| 1503 |
text = ''; |
| 1504 |
} |
| 1505 |
|
| 1506 |
const dataElementOrSlice = getDataElementOrSlice( i ); |
| 1507 |
const childDomElements = this.getDomElementsFromDataElement( dataElementOrSlice, doc ); |
| 1508 |
for ( let j = 0; j < childDomElements.length; j++ ) { |
| 1509 |
annotatedDomElements.push( childDomElements[ j ] ); |
| 1510 |
} |
| 1511 |
if ( Array.isArray( dataElementOrSlice ) ) { |
| 1512 |
i += dataElementOrSlice.length - 1; |
| 1513 |
} else { |
| 1514 |
i++; |
| 1515 |
} |
| 1516 |
} |
| 1517 |
i++; |
| 1518 |
} |
| 1519 |
|
| 1520 |
i--; |
| 1521 |
|
| 1522 |
|
| 1523 |
if ( text.length > 0 ) { |
| 1524 |
annotatedDomElements.push( doc.createTextNode( text ) ); |
| 1525 |
text = ''; |
| 1526 |
} |
| 1527 |
|
| 1528 |
this.constructor.static.openAndCloseAnnotations( annotationStack, new ve.dm.AnnotationSet( this.store ), |
| 1529 |
openAnnotation, closeAnnotation |
| 1530 |
); |
| 1531 |
|
| 1532 |
for ( let j = 0; j < annotatedDomElements.length; j++ ) { |
| 1533 |
domElement.appendChild( annotatedDomElements[ j ] ); |
| 1534 |
} |
| 1535 |
} else if ( data[ i ].type !== undefined ) { |
| 1536 |
const dataElement = data[ i ]; |
| 1537 |
|
| 1538 |
if ( dataElement.type.charAt( 0 ) === '/' ) { |
| 1539 |
|
| 1540 |
const parentDomElement = domElement.parentNode; |
| 1541 |
const type = data[ i ].type.slice( 1 ); |
| 1542 |
const isContentNode = this.nodeFactory.isNodeContent( type ); |
| 1543 |
|
| 1544 |
|
| 1545 |
const oldLastOuterPost = parentDomElement.lastOuterPost; |
| 1546 |
if ( |
| 1547 |
!isContentNode && |
| 1548 |
domElement.veInternal && |
| 1549 |
domElement.veInternal.whitespace |
| 1550 |
) { |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
const pre = domElement.veInternal.whitespace[ 1 ]; |
| 1555 |
if ( pre ) { |
| 1556 |
if ( |
| 1557 |
domElement.firstChild && |
| 1558 |
domElement.firstChild.nodeType === Node.TEXT_NODE |
| 1559 |
) { |
| 1560 |
|
| 1561 |
domElement.firstChild.insertData( 0, pre ); |
| 1562 |
} else { |
| 1563 |
|
| 1564 |
const textNode = doc.createTextNode( pre ); |
| 1565 |
textNode.veIsWhitespace = true; |
| 1566 |
domElement.insertBefore( |
| 1567 |
textNode, |
| 1568 |
domElement.firstChild |
| 1569 |
); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
const lastChild = domElement.veInternal.childDomElements ? |
| 1573 |
domElement.veInternal |
| 1574 |
.childDomElements[ domElement.veInternal.childDomElements.length - 1 ] |
| 1575 |
.lastChild : |
| 1576 |
domElement.lastChild; |
| 1577 |
const ours = domElement.veInternal.whitespace[ 2 ]; |
| 1578 |
let theirs; |
| 1579 |
if ( domElement.lastOuterPost === undefined ) { |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
theirs = ours; |
| 1584 |
} else { |
| 1585 |
theirs = domElement.lastOuterPost; |
| 1586 |
} |
| 1587 |
if ( ours && ours === theirs ) { |
| 1588 |
if ( lastChild && lastChild.nodeType === Node.TEXT_NODE ) { |
| 1589 |
|
| 1590 |
domElement.lastChild.appendData( ours ); |
| 1591 |
} else { |
| 1592 |
|
| 1593 |
const textNode = doc.createTextNode( ours ); |
| 1594 |
textNode.veIsWhitespace = true; |
| 1595 |
domElement.appendChild( |
| 1596 |
textNode |
| 1597 |
); |
| 1598 |
} |
| 1599 |
} |
| 1600 |
|
| 1601 |
parentDomElement.lastOuterPost = domElement.veInternal.whitespace[ 3 ] || ''; |
| 1602 |
} else if ( !isContentNode ) { |
| 1603 |
|
| 1604 |
|
| 1605 |
parentDomElement.lastOuterPost = ''; |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
let doUnwrap = false; |
| 1614 |
if ( domElement.veInternal ) { |
| 1615 |
switch ( domElement.veInternal.generated ) { |
| 1616 |
case 'slug': |
| 1617 |
|
| 1618 |
if ( domElement.childNodes.length === 0 ) { |
| 1619 |
doUnwrap = true; |
| 1620 |
} |
| 1621 |
break; |
| 1622 |
case 'empty': |
| 1623 |
|
| 1624 |
if ( |
| 1625 |
domElement.childNodes.length === 0 && |
| 1626 |
( |
| 1627 |
|
| 1628 |
|
| 1629 |
data[ i + 1 ] === undefined || |
| 1630 |
data[ i + 1 ].type.charAt( 0 ) === '/' || |
| 1631 |
|
| 1632 |
( |
| 1633 |
data[ i + 1 ].type && |
| 1634 |
this.nodeFactory.isNodeInternal( data[ i + 1 ].type ) |
| 1635 |
) |
| 1636 |
) |
| 1637 |
) { |
| 1638 |
doUnwrap = true; |
| 1639 |
} |
| 1640 |
break; |
| 1641 |
case 'wrapper': { |
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
doUnwrap = true; |
| 1646 |
const previousSiblings = domElement.parentNode.childNodes; |
| 1647 |
|
| 1648 |
|
| 1649 |
for ( let j = previousSiblings.length - 2; j >= 0; j-- ) { |
| 1650 |
const sibling = previousSiblings[ j ]; |
| 1651 |
if ( ve.isBlockElement( sibling ) ) { |
| 1652 |
|
| 1653 |
break; |
| 1654 |
} |
| 1655 |
|
| 1656 |
if ( |
| 1657 |
|
| 1658 |
( sibling.nodeType === Node.TEXT_NODE && !sibling.veIsWhitespace ) || |
| 1659 |
|
| 1660 |
( sibling.nodeType === Node.ELEMENT_NODE && sibling.tagName !== 'META' && sibling.tagName !== 'LINK' ) |
| 1661 |
) { |
| 1662 |
|
| 1663 |
doUnwrap = false; |
| 1664 |
break; |
| 1665 |
} |
| 1666 |
} |
| 1667 |
break; |
| 1668 |
} |
| 1669 |
} |
| 1670 |
} |
| 1671 |
if ( doUnwrap ) { |
| 1672 |
if ( domElement.childNodes.length ) { |
| 1673 |
|
| 1674 |
while ( domElement.firstChild ) { |
| 1675 |
parentDomElement.insertBefore( |
| 1676 |
domElement.firstChild, |
| 1677 |
domElement |
| 1678 |
); |
| 1679 |
} |
| 1680 |
} else { |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
parentDomElement.lastOuterPost = oldLastOuterPost || ''; |
| 1685 |
} |
| 1686 |
parentDomElement.removeChild( domElement ); |
| 1687 |
} |
| 1688 |
|
| 1689 |
delete domElement.veInternal; |
| 1690 |
delete domElement.lastOuterPost; |
| 1691 |
|
| 1692 |
|
| 1693 |
if ( !ve.dm.nodeFactory.lookup( type ) || !ve.dm.nodeFactory.isNodeInternal( type ) ) { |
| 1694 |
domElement = parentDomElement; |
| 1695 |
} |
| 1696 |
} else { |
| 1697 |
|
| 1698 |
if ( this.nodeFactory.isNodeInternal( data[ i ].type ) ) { |
| 1699 |
|
| 1700 |
break; |
| 1701 |
} |
| 1702 |
const isContentNode = this.nodeFactory.isNodeContent( data[ i ].type ); |
| 1703 |
|
| 1704 |
const dataElementOrSlice = getDataElementOrSlice( i ); |
| 1705 |
const childDomElements = this.getDomElementsFromDataElement( dataElementOrSlice, doc ); |
| 1706 |
if ( childDomElements && !childDomElements.length ) { |
| 1707 |
|
| 1708 |
i = findEndOfNode( i ) - 1; |
| 1709 |
continue; |
| 1710 |
} else if ( childDomElements ) { |
| 1711 |
|
| 1712 |
|
| 1713 |
childDomElements[ 0 ].veInternal = ve.extendObject( |
| 1714 |
{ childDomElements: childDomElements }, |
| 1715 |
dataElement.internal ? ve.copy( dataElement.internal ) : {} |
| 1716 |
); |
| 1717 |
|
| 1718 |
for ( let j = 0; j < childDomElements.length; j++ ) { |
| 1719 |
domElement.appendChild( childDomElements[ j ] ); |
| 1720 |
} |
| 1721 |
|
| 1722 |
const parentDomElement = domElement; |
| 1723 |
domElement = childDomElements[ 0 ]; |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
if ( domElement.veInternal && domElement.veInternal.whitespace ) { |
| 1735 |
|
| 1736 |
const ours = domElement.veInternal.whitespace[ 0 ]; |
| 1737 |
let theirs; |
| 1738 |
if ( domElement.previousSibling ) { |
| 1739 |
|
| 1740 |
theirs = parentDomElement.lastOuterPost; |
| 1741 |
} else if ( parentDomElement === container ) { |
| 1742 |
|
| 1743 |
theirs = innerWhitespace ? innerWhitespace[ 0 ] : ours; |
| 1744 |
} else { |
| 1745 |
|
| 1746 |
if ( |
| 1747 |
parentDomElement.veInternal && |
| 1748 |
parentDomElement.veInternal.whitespace |
| 1749 |
) { |
| 1750 |
theirs = parentDomElement.veInternal.whitespace[ 1 ]; |
| 1751 |
|
| 1752 |
parentDomElement.veInternal.whitespace[ 1 ] = undefined; |
| 1753 |
} |
| 1754 |
|
| 1755 |
} |
| 1756 |
if ( ours && ours === theirs ) { |
| 1757 |
|
| 1758 |
const textNode = doc.createTextNode( ours ); |
| 1759 |
textNode.veIsWhitespace = true; |
| 1760 |
parentDomElement.insertBefore( |
| 1761 |
textNode, |
| 1762 |
domElement |
| 1763 |
); |
| 1764 |
} |
| 1765 |
} else if ( |
| 1766 |
!isContentNode && |
| 1767 |
!domElement.previousSibling && |
| 1768 |
parentDomElement.veInternal && |
| 1769 |
parentDomElement.veInternal.whitespace |
| 1770 |
) { |
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
parentDomElement.veInternal.whitespace[ 1 ] = undefined; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
if ( Array.isArray( dataElementOrSlice ) ) { |
| 1780 |
i += dataElementOrSlice.length - 2; |
| 1781 |
} else if ( childDomElements && childDomElements.length && childDomElements[ 0 ].handledOwnChildren ) { |
| 1782 |
i = findEndOfNode( i ) - 2; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
} |
| 1787 |
|
| 1788 |
if ( |
| 1789 |
container.lastOuterPost !== undefined && |
| 1790 |
( !innerWhitespace || container.lastOuterPost === innerWhitespace[ 1 ] ) |
| 1791 |
) { |
| 1792 |
if ( container.lastChild && container.lastChild.nodeType === Node.TEXT_NODE ) { |
| 1793 |
|
| 1794 |
container.lastChild.appendData( container.lastOuterPost ); |
| 1795 |
} else if ( container.lastOuterPost.length > 0 ) { |
| 1796 |
|
| 1797 |
container.appendChild( doc.createTextNode( container.lastOuterPost ) ); |
| 1798 |
} |
| 1799 |
delete container.lastOuterPost; |
| 1800 |
} |
| 1801 |
|
| 1802 |
container.normalize(); |
| 1803 |
}; |
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
ve.dm.converter = new ve.dm.Converter( ve.dm.modelRegistry, ve.dm.nodeFactory, ve.dm.annotationFactory ); |