| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.dm.MWReferenceNode = function VeDmMWReferenceNode() { |
| 19 |
|
| 20 |
ve.dm.MWReferenceNode.super.apply( this, arguments ); |
| 21 |
|
| 22 |
|
| 23 |
ve.dm.FocusableNode.call( this ); |
| 24 |
|
| 25 |
|
| 26 |
this.connect( this, { |
| 27 |
root: 'onRoot', |
| 28 |
unroot: 'onUnroot', |
| 29 |
attributeChange: 'onAttributeChange' |
| 30 |
} ); |
| 31 |
}; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
OO.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode ); |
| 36 |
|
| 37 |
OO.mixinClass( ve.dm.MWReferenceNode, ve.dm.FocusableNode ); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ve.dm.MWReferenceNode.static.name = 'mwReference'; |
| 42 |
|
| 43 |
ve.dm.MWReferenceNode.static.matchTagNames = null; |
| 44 |
|
| 45 |
ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ]; |
| 46 |
|
| 47 |
|
| 48 |
ve.dm.MWReferenceNode.static.allowedRdfaTypes = [ 'dc:references', 'mw:Error' ]; |
| 49 |
|
| 50 |
ve.dm.MWReferenceNode.static.isContent = true; |
| 51 |
|
| 52 |
ve.dm.MWReferenceNode.static.disallowedAnnotationTypes = [ 'link' ]; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
ve.dm.MWReferenceNode.static.listKeyRegex = /^(auto|literal)\/([\s\S]*)$/; |
| 64 |
|
| 65 |
ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) { |
| 66 |
function getReflistItemHtml( id ) { |
| 67 |
const elem = converter.getHtmlDocument().getElementById( id ); |
| 68 |
return elem && elem.innerHTML; |
| 69 |
} |
| 70 |
|
| 71 |
const mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' ); |
| 72 |
const mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {}; |
| 73 |
const mwAttrs = mwData.attrs || {}; |
| 74 |
const reflistItemId = ve.getProp( mwData, 'body', 'id' ); |
| 75 |
const body = ve.getProp( mwData, 'body', 'html' ) || |
| 76 |
( reflistItemId && getReflistItemHtml( reflistItemId ) ) || |
| 77 |
''; |
| 78 |
const refGroup = mwAttrs.group || ''; |
| 79 |
const listGroup = this.name + '/' + refGroup; |
| 80 |
const listKey = !mwAttrs.name ? |
| 81 |
'auto/' + converter.internalList.getNextUniqueNumber() : |
| 82 |
'literal/' + mwAttrs.name; |
| 83 |
const queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body ); |
| 84 |
|
| 85 |
const dataElement = { |
| 86 |
type: this.name, |
| 87 |
attributes: { |
| 88 |
mw: mwData, |
| 89 |
originalMw: mwDataJSON, |
| 90 |
listIndex: queueResult.index, |
| 91 |
listGroup: listGroup, |
| 92 |
listKey: listKey, |
| 93 |
refGroup: refGroup, |
| 94 |
contentsUsed: body !== '' && queueResult.isNew |
| 95 |
} |
| 96 |
}; |
| 97 |
if ( mwAttrs.extends && mw.config.get( 'wgCiteSubReferencing' ) ) { |
| 98 |
dataElement.attributes.extendsRef = mwAttrs.extends ? 'literal/' + mwAttrs.extends : null; |
| 99 |
} |
| 100 |
if ( reflistItemId ) { |
| 101 |
dataElement.attributes.refListItemId = reflistItemId; |
| 102 |
} |
| 103 |
return dataElement; |
| 104 |
}; |
| 105 |
|
| 106 |
ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) { |
| 107 |
const isForClipboard = converter.isForClipboard(); |
| 108 |
const el = doc.createElement( 'sup' ); |
| 109 |
|
| 110 |
el.setAttribute( 'typeof', 'mw:Extension/ref' ); |
| 111 |
|
| 112 |
const mwData = dataElement.attributes.mw ? ve.copy( dataElement.attributes.mw ) : {}; |
| 113 |
mwData.name = 'ref'; |
| 114 |
|
| 115 |
if ( isForClipboard || converter.isForParser() ) { |
| 116 |
let setContents = dataElement.attributes.contentsUsed; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
const itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex ); |
| 121 |
const itemNodeRange = itemNode.getRange(); |
| 122 |
|
| 123 |
const keyedNodes = converter.internalList |
| 124 |
.getNodeGroup( dataElement.attributes.listGroup ) |
| 125 |
.keyedNodes[ dataElement.attributes.listKey ]; |
| 126 |
|
| 127 |
const extendsNodes = converter.internalList.getNodeGroup( dataElement.attributes.listGroup ).firstNodes.filter( |
| 128 |
( node ) => node.element.attributes.extendsRef === dataElement.attributes.listKey |
| 129 |
); |
| 130 |
|
| 131 |
let contentsAlreadySet = false; |
| 132 |
if ( setContents ) { |
| 133 |
|
| 134 |
|
| 135 |
if ( keyedNodes ) { |
| 136 |
for ( let i = 0; i < keyedNodes.length; i++ ) { |
| 137 |
if ( |
| 138 |
ve.compare( |
| 139 |
this.getInstanceHashObject( keyedNodes[ i ].element ), |
| 140 |
this.getInstanceHashObject( dataElement ) |
| 141 |
) |
| 142 |
) { |
| 143 |
break; |
| 144 |
} |
| 145 |
if ( keyedNodes[ i ].element.attributes.contentsUsed ) { |
| 146 |
contentsAlreadySet = true; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} else { |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if ( |
| 157 |
keyedNodes && |
| 158 |
ve.compare( |
| 159 |
this.getInstanceHashObject( dataElement ), |
| 160 |
this.getInstanceHashObject( keyedNodes[ 0 ].element ) |
| 161 |
) |
| 162 |
) { |
| 163 |
setContents = true; |
| 164 |
|
| 165 |
|
| 166 |
for ( let i = 1; i < keyedNodes.length; i++ ) { |
| 167 |
if ( keyedNodes[ i ].element.attributes.contentsUsed ) { |
| 168 |
setContents = false; |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if ( setContents && !contentsAlreadySet ) { |
| 177 |
const itemNodeWrapper = doc.createElement( 'div' ); |
| 178 |
const originalHtmlWrapper = doc.createElement( 'div' ); |
| 179 |
converter.getDomSubtreeFromData( |
| 180 |
itemNode.getDocument().getFullData( itemNodeRange, 'roundTrip' ), |
| 181 |
itemNodeWrapper |
| 182 |
); |
| 183 |
|
| 184 |
const itemNodeHtml = itemNodeWrapper.innerHTML; |
| 185 |
const originalHtml = ve.getProp( mwData, 'body', 'html' ) || |
| 186 |
( ve.getProp( mwData, 'body', 'id' ) !== undefined && itemNode.getAttribute( 'originalHtml' ) ) || |
| 187 |
''; |
| 188 |
originalHtmlWrapper.innerHTML = originalHtml; |
| 189 |
|
| 190 |
|
| 191 |
if ( isForClipboard || !originalHtmlWrapper.isEqualNode( itemNodeWrapper ) ) { |
| 192 |
ve.setProp( mwData, 'body', 'html', itemNodeHtml ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if ( isForClipboard && itemNodeRange.isCollapsed() ) { |
| 199 |
el.setAttribute( 'data-ve-ignore', 'true' ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if ( dataElement.attributes.extendsRef ) { |
| 204 |
let extendsAttr; |
| 205 |
const extendsKeyParts = dataElement.attributes.extendsRef.match( this.listKeyRegex ); |
| 206 |
if ( extendsKeyParts[ 1 ] === 'auto' ) { |
| 207 |
|
| 208 |
extendsAttr = converter.internalList.getUniqueListKey( |
| 209 |
dataElement.attributes.listGroup, |
| 210 |
dataElement.attributes.extendsRef, |
| 211 |
|
| 212 |
'literal/:' |
| 213 |
).slice( 'literal/'.length ); |
| 214 |
} else { |
| 215 |
extendsAttr = extendsKeyParts[ 2 ]; |
| 216 |
} |
| 217 |
ve.setProp( mwData, 'attrs', 'extends', extendsAttr ); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
let name; |
| 222 |
const listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex ); |
| 223 |
if ( listKeyParts[ 1 ] === 'auto' ) { |
| 224 |
|
| 225 |
if ( keyedNodes.length > 1 || extendsNodes.length ) { |
| 226 |
|
| 227 |
name = converter.internalList.getUniqueListKey( |
| 228 |
dataElement.attributes.listGroup, |
| 229 |
dataElement.attributes.listKey, |
| 230 |
|
| 231 |
'literal/:' |
| 232 |
).slice( 'literal/'.length ); |
| 233 |
} |
| 234 |
} else { |
| 235 |
|
| 236 |
name = listKeyParts[ 2 ]; |
| 237 |
} |
| 238 |
|
| 239 |
if ( name !== undefined ) { |
| 240 |
ve.setProp( mwData, 'attrs', 'name', name ); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if ( dataElement.attributes.refGroup !== '' ) { |
| 245 |
ve.setProp( mwData, 'attrs', 'group', dataElement.attributes.refGroup ); |
| 246 |
} else if ( mwData.attrs ) { |
| 247 |
delete mwData.attrs.group; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
const originalMw = dataElement.attributes.originalMw; |
| 255 |
if ( converter.isForParser() && originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) { |
| 256 |
el.setAttribute( 'data-mw', originalMw ); |
| 257 |
|
| 258 |
|
| 259 |
if ( dataElement.originalDomElementsHash !== undefined ) { |
| 260 |
return ve.copyDomElements( |
| 261 |
converter.getStore().value( dataElement.originalDomElementsHash ), doc ); |
| 262 |
} |
| 263 |
} else { |
| 264 |
el.setAttribute( 'data-mw', JSON.stringify( mwData ) ); |
| 265 |
|
| 266 |
|
| 267 |
const $link = $( '<a>', doc ) |
| 268 |
.attr( 'data-mw-group', this.getGroup( dataElement ) || null ); |
| 269 |
$( el ).addClass( 'mw-ref reference' ).html( |
| 270 |
$link.append( |
| 271 |
$( '<span>', doc ).addClass( 'mw-reflink-text' ).html( this.getIndexLabel( dataElement, converter.internalList ) ) |
| 272 |
) |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
return [ el ]; |
| 277 |
}; |
| 278 |
|
| 279 |
ve.dm.MWReferenceNode.static.remapInternalListIndexes = function ( |
| 280 |
dataElement, mapping, internalList |
| 281 |
) { |
| 282 |
|
| 283 |
dataElement.attributes.listIndex = mapping[ dataElement.attributes.listIndex ]; |
| 284 |
|
| 285 |
|
| 286 |
const listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex ); |
| 287 |
if ( listKeyParts[ 1 ] === 'auto' ) { |
| 288 |
dataElement.attributes.listKey = 'auto/' + internalList.getNextUniqueNumber(); |
| 289 |
} |
| 290 |
}; |
| 291 |
|
| 292 |
ve.dm.MWReferenceNode.static.remapInternalListKeys = function ( dataElement, internalList ) { |
| 293 |
let suffix = ''; |
| 294 |
|
| 295 |
while ( internalList.keys.indexOf( dataElement.attributes.listKey + suffix ) !== -1 ) { |
| 296 |
suffix = suffix ? suffix + 1 : 2; |
| 297 |
} |
| 298 |
if ( suffix ) { |
| 299 |
dataElement.attributes.listKey += suffix; |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
ve.dm.MWReferenceNode.static.getGroup = function ( dataElement ) { |
| 311 |
return dataElement.attributes.refGroup; |
| 312 |
}; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
ve.dm.MWReferenceNode.static.getIndexLabel = function ( dataElement, internalList ) { |
| 323 |
const refGroup = dataElement.attributes.refGroup; |
| 324 |
const indexNumber = dataElement.attributes.placeholder ? '…' : |
| 325 |
ve.dm.MWReferenceNode.static.findIndexNumber( dataElement, internalList ); |
| 326 |
const label = ( refGroup ? refGroup + ' ' : '' ) + indexNumber; |
| 327 |
|
| 328 |
return `<span class="cite-bracket">[</span>${ label }<span class="cite-bracket">]</span>`; |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
ve.dm.MWReferenceNode.static.findIndexNumber = function ( dataElement, internalList ) { |
| 340 |
return ve.getProp( dataElement, 'internal', 'overrideIndex' ) || |
| 341 |
ve.dm.MWDocumentReferences.static.refsForDoc( internalList.getDocument() ) |
| 342 |
.getIndexLabel( dataElement.attributes.refGroup, dataElement.attributes.listKey ); |
| 343 |
}; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
ve.dm.MWReferenceNode.static.cloneElement = function () { |
| 350 |
const clone = ve.dm.MWReferenceNode.super.static.cloneElement.apply( this, arguments ); |
| 351 |
delete clone.attributes.contentsUsed; |
| 352 |
delete clone.attributes.mw; |
| 353 |
delete clone.attributes.originalMw; |
| 354 |
|
| 355 |
|
| 356 |
clone.originalDomElementsHash = Math.random(); |
| 357 |
return clone; |
| 358 |
}; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
ve.dm.MWReferenceNode.static.getHashObject = function ( dataElement ) { |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
return { |
| 376 |
type: dataElement.type, |
| 377 |
attributes: { |
| 378 |
listGroup: dataElement.attributes.listGroup |
| 379 |
} |
| 380 |
}; |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
ve.dm.MWReferenceNode.static.getInstanceHashObject = function () { |
| 395 |
return ve.dm.MWReferenceNode.super.static.getHashObject.apply( this, arguments ); |
| 396 |
}; |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
ve.dm.MWReferenceNode.static.describeChange = function ( key, change ) { |
| 403 |
if ( key === 'refGroup' ) { |
| 404 |
if ( !change.from ) { |
| 405 |
return ve.htmlMsg( 'cite-ve-changedesc-ref-group-to', this.wrapText( 'ins', change.to ) ); |
| 406 |
} else if ( !change.to ) { |
| 407 |
return ve.htmlMsg( 'cite-ve-changedesc-ref-group-from', this.wrapText( 'del', change.from ) ); |
| 408 |
} else { |
| 409 |
return ve.htmlMsg( 'cite-ve-changedesc-ref-group-both', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) ); |
| 410 |
} |
| 411 |
} |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
ve.dm.MWReferenceNode.prototype.isEditable = function () { |
| 423 |
const internalItem = this.getInternalItem(); |
| 424 |
return internalItem && internalItem.getLength() > 0; |
| 425 |
}; |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
ve.dm.MWReferenceNode.prototype.getInternalItem = function () { |
| 433 |
return this.getDocument().getInternalList().getItemNode( this.getAttribute( 'listIndex' ) ); |
| 434 |
}; |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
ve.dm.MWReferenceNode.prototype.getIndex = function () { |
| 442 |
return this.constructor.static.getIndex( this.element, this.getDocument().getInternalList() ); |
| 443 |
}; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
ve.dm.MWReferenceNode.prototype.getGroup = function () { |
| 451 |
return this.constructor.static.getGroup( this.element ); |
| 452 |
}; |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
ve.dm.MWReferenceNode.prototype.getIndexLabel = function () { |
| 460 |
return this.constructor.static.getIndexLabel( |
| 461 |
this.element, this.getDocument().getInternalList() ); |
| 462 |
}; |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
ve.dm.MWReferenceNode.prototype.getIndexNumber = function () { |
| 470 |
return this.constructor.static.findIndexNumber( this.element, this.getDocument().getInternalList() ); |
| 471 |
}; |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
ve.dm.MWReferenceNode.prototype.onRoot = function () { |
| 477 |
this.addToInternalList(); |
| 478 |
}; |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
ve.dm.MWReferenceNode.prototype.onUnroot = function ( oldRoot ) { |
| 486 |
if ( this.getDocument().getDocumentNode() === oldRoot ) { |
| 487 |
this.removeFromInternalList(); |
| 488 |
} |
| 489 |
}; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
ve.dm.MWReferenceNode.prototype.addToInternalList = function () { |
| 495 |
if ( this.getRoot() === this.getDocument().getDocumentNode() ) { |
| 496 |
const attributes = this.element.attributes; |
| 497 |
this.registeredListGroup = attributes.listGroup; |
| 498 |
this.registeredListKey = attributes.listKey; |
| 499 |
this.registeredListIndex = attributes.listIndex; |
| 500 |
this.getDocument().getInternalList().addNode( |
| 501 |
this.registeredListGroup, |
| 502 |
this.registeredListKey, |
| 503 |
this.registeredListIndex, |
| 504 |
this |
| 505 |
); |
| 506 |
} |
| 507 |
}; |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
ve.dm.MWReferenceNode.prototype.removeFromInternalList = function () { |
| 513 |
if ( !this.registeredListGroup ) { |
| 514 |
|
| 515 |
return; |
| 516 |
} |
| 517 |
this.getDocument().getInternalList().removeNode( |
| 518 |
this.registeredListGroup, |
| 519 |
this.registeredListKey, |
| 520 |
this.registeredListIndex, |
| 521 |
this |
| 522 |
); |
| 523 |
}; |
| 524 |
|
| 525 |
ve.dm.MWReferenceNode.prototype.onAttributeChange = function ( key, from, to ) { |
| 526 |
if ( key === 'placeholder' ) { |
| 527 |
this.getDocument().getInternalList().markGroupAsChanged( this.registeredListGroup ); |
| 528 |
} |
| 529 |
if ( |
| 530 |
( key !== 'listGroup' && key !== 'listKey' ) || |
| 531 |
( key === 'listGroup' && this.registeredListGroup === to ) || |
| 532 |
( key === 'listKey' && this.registeredListKey === to ) |
| 533 |
) { |
| 534 |
return; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
this.removeFromInternalList(); |
| 540 |
this.addToInternalList(); |
| 541 |
}; |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
ve.dm.modelRegistry.register( ve.dm.MWReferenceNode ); |