2 * VisualEditor DataModel MWReferenceNode class.
4 * @copyright 2011-2017 Cite VisualEditor Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
9 * DataModel MediaWiki reference node.
12 * @extends ve.dm.LeafNode
13 * @mixins ve.dm.FocusableNode
16 * @param {Object} [element] Reference to element in linear model
18 ve.dm.MWReferenceNode = function VeDmMWReferenceNode() {
20 ve.dm.MWReferenceNode.super.apply( this, arguments );
23 ve.dm.FocusableNode.call( this );
29 attributeChange: 'onAttributeChange'
35 OO.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode );
37 OO.mixinClass( ve.dm.MWReferenceNode, ve.dm.FocusableNode );
41 ve.dm.MWReferenceNode.static.name = 'mwReference';
43 ve.dm.MWReferenceNode.static.matchTagNames = null;
45 ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ];
47 ve.dm.MWReferenceNode.static.allowedRdfaTypes = [ 'dc:references' ];
49 ve.dm.MWReferenceNode.static.isContent = true;
51 ve.dm.MWReferenceNode.static.blacklistedAnnotationTypes = [ 'link' ];
54 * Regular expression for parsing the listKey attribute
59 ve.dm.MWReferenceNode.static.listKeyRegex = /^(auto|literal)\/(.*)$/;
61 ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
62 var dataElement, mwDataJSON, mwData, reflistItemId, body, refGroup, listGroup, autoKeyed, listKey, queueResult, listIndex, contentsUsed;
64 function getReflistItemHtml( id ) {
65 var elem = converter.getHtmlDocument().getElementById( id );
66 return elem && elem.innerHTML || '';
69 mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' );
70 mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
71 reflistItemId = mwData.body && mwData.body.id;
72 body = ( mwData.body && mwData.body.html ) ||
73 ( reflistItemId && getReflistItemHtml( reflistItemId ) ) ||
75 refGroup = mwData.attrs && mwData.attrs.group || '';
76 listGroup = this.name + '/' + refGroup;
77 autoKeyed = !mwData.attrs || mwData.attrs.name === undefined;
79 'auto/' + converter.internalList.getNextUniqueNumber() :
80 'literal/' + mwData.attrs.name;
81 queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body );
82 listIndex = queueResult.index;
83 contentsUsed = ( body !== '' && queueResult.isNew );
89 originalMw: mwDataJSON,
94 contentsUsed: contentsUsed
97 if ( reflistItemId ) {
98 dataElement.attributes.refListItemId = reflistItemId;
103 ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) {
104 var itemNodeHtml, originalHtml, mwData, i, iLen, keyedNodes, setContents, contentsAlreadySet,
105 originalMw, listKeyParts, name, group, $link,
106 isForClipboard = converter.isForClipboard(),
107 el = doc.createElement( 'span' ),
108 itemNodeWrapper = doc.createElement( 'div' ),
109 originalHtmlWrapper = doc.createElement( 'div' ),
110 itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex ),
111 itemNodeRange = itemNode.getRange();
113 el.setAttribute( 'typeof', 'mw:Extension/ref' );
115 mwData = dataElement.attributes.mw ? ve.copy( dataElement.attributes.mw ) : {};
118 setContents = dataElement.attributes.contentsUsed;
120 keyedNodes = converter.internalList
121 .getNodeGroup( dataElement.attributes.listGroup )
122 .keyedNodes[ dataElement.attributes.listKey ];
125 // Check if a previous node has already set the content. If so, we don't overwrite this
127 contentsAlreadySet = false;
129 for ( i = 0, iLen = keyedNodes.length; i < iLen; i++ ) {
130 if ( keyedNodes[ i ].element === dataElement ) {
133 if ( keyedNodes[ i ].element.attributes.contentsUsed ) {
134 contentsAlreadySet = true;
140 // Check if any other nodes with this key provided content. If not
141 // then we attach the contents to the first reference with this key
143 // Check that this is the first reference with its key
144 if ( keyedNodes && dataElement === keyedNodes[ 0 ].element ) {
146 // Check no other reference originally defined the contents
147 // As this is keyedNodes[0] we can start at 1
148 for ( i = 1, iLen = keyedNodes.length; i < iLen; i++ ) {
149 if ( keyedNodes[ i ].element.attributes.contentsUsed ) {
157 if ( setContents && !contentsAlreadySet ) {
158 converter.getDomSubtreeFromData(
159 itemNode.getDocument().getFullData( itemNodeRange, true ),
162 itemNodeHtml = itemNodeWrapper.innerHTML; // Returns '' if itemNodeWrapper is empty
163 originalHtml = ve.getProp( mwData, 'body', 'html' ) ||
164 ( ve.getProp( mwData, 'body', 'id' ) !== undefined && itemNode.getAttribute( 'originalHtml' ) ) ||
166 originalHtmlWrapper.innerHTML = originalHtml;
167 // Only set body.html if itemNodeHtml and originalHtml are actually different,
168 // or we are writing the clipboard for use in another VE instance
169 if ( isForClipboard || !originalHtmlWrapper.isEqualNode( itemNodeWrapper ) ) {
170 ve.setProp( mwData, 'body', 'html', itemNodeHtml );
174 // If we have no internal item data for this reference, don't let it get pasted into
175 // another VE document. T110479
176 if ( isForClipboard && itemNodeRange.isCollapsed() ) {
177 el.setAttribute( 'data-ve-ignore', 'true' );
181 listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
182 if ( listKeyParts[ 1 ] === 'auto' ) {
183 // Only render a name if this key was reused
184 if ( keyedNodes.length > 1 ) {
185 // Allocate a unique list key, then strip the 'literal/'' prefix
186 name = converter.internalList.getUniqueListKey(
187 dataElement.attributes.listGroup,
188 dataElement.attributes.listKey,
189 // Generate a name starting with ':' to distinguish it from normal names
191 ).slice( 'literal/'.length );
197 name = listKeyParts[ 2 ];
200 if ( name !== undefined ) {
201 ve.setProp( mwData, 'attrs', 'name', name );
204 // Set or clear group
205 if ( dataElement.attributes.refGroup !== '' ) {
206 ve.setProp( mwData, 'attrs', 'group', dataElement.attributes.refGroup );
207 } else if ( mwData.attrs ) {
208 delete mwData.attrs.refGroup;
211 // If mwAttr and originalMw are the same, use originalMw to prevent reserialization,
212 // unless we are writing the clipboard for use in another VE instance
213 // Reserialization has the potential to reorder keys and so change the DOM unnecessarily
214 originalMw = dataElement.attributes.originalMw;
215 if ( !isForClipboard && originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
216 el.setAttribute( 'data-mw', originalMw );
218 // Return the original DOM elements if possible
219 if ( dataElement.originalDomElementsIndex !== undefined ) {
220 return ve.copyDomElements( converter.getStore().value( dataElement.originalDomElementsIndex ), doc );
223 el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
225 // HTML for the external clipboard, it will be ignored by the converter
226 group = this.getGroup( dataElement );
227 $link = $( '<a>', doc ).css(
228 'counterReset', 'mw-Ref ' + this.getIndex( dataElement, converter.internalList )
231 $link.attr( 'data-mw-group', this.getGroup( dataElement ) );
233 $( el ).addClass( 'mw-ref' ).append(
235 $( '<span>', doc ).addClass( 'mw-reflink-text' ).text( this.getIndexLabel( dataElement, converter.internalList ) )
243 ve.dm.MWReferenceNode.static.remapInternalListIndexes = function ( dataElement, mapping, internalList ) {
246 dataElement.attributes.listIndex = mapping[ dataElement.attributes.listIndex ];
248 // Remap listKey if it was automatically generated
249 listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
250 if ( listKeyParts[ 1 ] === 'auto' ) {
251 dataElement.attributes.listKey = 'auto/' + internalList.getNextUniqueNumber();
255 ve.dm.MWReferenceNode.static.remapInternalListKeys = function ( dataElement, internalList ) {
257 // Try name, name2, name3, ... until unique
258 while ( internalList.keys.indexOf( dataElement.attributes.listKey + suffix ) !== -1 ) {
259 suffix = suffix ? suffix + 1 : 2;
262 dataElement.attributes.listKey = dataElement.attributes.listKey + suffix;
267 * Gets the index for the reference
270 * @param {Object} dataElement Element data
271 * @param {ve.dm.InternalList} internalList Internal list
272 * @return {number} Index
274 ve.dm.MWReferenceNode.static.getIndex = function ( dataElement, internalList ) {
275 var listIndex, listGroup, position,
276 overrideIndex = ve.getProp( dataElement, 'internal', 'overrideIndex' );
278 if ( overrideIndex ) {
279 return overrideIndex;
282 listIndex = dataElement.attributes.listIndex;
283 listGroup = dataElement.attributes.listGroup;
284 position = internalList.getIndexPosition( listGroup, listIndex );
290 * Gets the group for the reference
293 * @param {Object} dataElement Element data
294 * @return {string} Group
296 ve.dm.MWReferenceNode.static.getGroup = function ( dataElement ) {
297 return dataElement.attributes.refGroup;
301 * Gets the index label for the reference
304 * @param {Object} dataElement Element data
305 * @param {ve.dm.InternalList} internalList Internal list
306 * @return {string} Reference label
308 ve.dm.MWReferenceNode.static.getIndexLabel = function ( dataElement, internalList ) {
309 var refGroup = dataElement.attributes.refGroup,
310 index = ve.dm.MWReferenceNode.static.getIndex( dataElement, internalList );
312 return '[' + ( refGroup ? refGroup + ' ' : '' ) + index + ']';
318 ve.dm.MWReferenceNode.static.cloneElement = function () {
319 var clone = ve.dm.MWReferenceNode.super.static.cloneElement.apply( this, arguments );
320 delete clone.attributes.contentsUsed;
321 delete clone.attributes.mw;
322 delete clone.attributes.originalMw;
329 ve.dm.MWReferenceNode.static.describeChange = function ( key, change ) {
330 if ( key === 'refGroup' ) {
333 return ve.msg( 'cite-ve-changedesc-reflist-group-both', change.from, change.to );
335 return ve.msg( 'cite-ve-changedesc-reflist-group-from', change.from );
338 return ve.msg( 'cite-ve-changedesc-reflist-group-to', change.to );
340 if ( key === 'refListItemId' ) {
341 return ve.msg( 'cite-ve-changedesc-reflist-item-id' );
348 * Don't allow reference nodes to be edited if we can't find their contents.
352 ve.dm.MWReferenceNode.prototype.isEditable = function () {
353 var internalItem = this.getInternalItem();
354 return internalItem && internalItem.getLength() > 0;
358 * Gets the internal item node associated with this node
360 * @return {ve.dm.InternalItemNode} Item node
362 ve.dm.MWReferenceNode.prototype.getInternalItem = function () {
363 return this.getDocument().getInternalList().getItemNode( this.getAttribute( 'listIndex' ) );
367 * Gets the index for the reference
369 * @return {number} Index
371 ve.dm.MWReferenceNode.prototype.getIndex = function () {
372 return this.constructor.static.getIndex( this.element, this.getDocument().getInternalList() );
376 * Gets the group for the reference
378 * @return {string} Group
380 ve.dm.MWReferenceNode.prototype.getGroup = function () {
381 return this.constructor.static.getGroup( this.element );
385 * Gets the index label for the reference
387 * @return {string} Reference label
389 ve.dm.MWReferenceNode.prototype.getIndexLabel = function () {
390 return this.constructor.static.getIndexLabel( this.element, this.getDocument().getInternalList() );
394 * Handle the node being attached to the root
396 ve.dm.MWReferenceNode.prototype.onRoot = function () {
397 this.addToInternalList();
401 * Handle the node being detached from the root
403 * @param {ve.dm.DocumentNode} oldRoot Old document root
405 ve.dm.MWReferenceNode.prototype.onUnroot = function ( oldRoot ) {
406 if ( this.getDocument().getDocumentNode() === oldRoot ) {
407 this.removeFromInternalList();
412 * Register the node with the internal list
414 ve.dm.MWReferenceNode.prototype.addToInternalList = function () {
415 if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
416 this.registeredListGroup = this.element.attributes.listGroup;
417 this.registeredListKey = this.element.attributes.listKey;
418 this.registeredListIndex = this.element.attributes.listIndex;
419 this.getDocument().getInternalList().addNode(
420 this.registeredListGroup,
421 this.registeredListKey,
422 this.registeredListIndex,
429 * Unregister the node from the internal list
431 ve.dm.MWReferenceNode.prototype.removeFromInternalList = function () {
432 if ( !this.registeredListGroup ) {
433 // Don't try to remove if we haven't been added in the first place.
436 this.getDocument().getInternalList().removeNode(
437 this.registeredListGroup,
438 this.registeredListKey,
439 this.registeredListIndex,
444 ve.dm.MWReferenceNode.prototype.onAttributeChange = function ( key, from, to ) {
446 ( key !== 'listGroup' && key !== 'listKey' ) ||
447 ( key === 'listGroup' && this.registeredListGroup === to ) ||
448 ( key === 'listKey' && this.registeredListKey === to )
453 // Need the old list keys and indexes, so we register them in addToInternalList
454 // They've already been updated in this.element.attributes before this code runs
455 this.removeFromInternalList();
456 this.addToInternalList();
461 ve.dm.modelRegistry.register( ve.dm.MWReferenceNode );