2013-04-17 17:53:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWReferenceNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki reference node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.LeafNode
|
|
|
|
* @constructor
|
|
|
|
* @param {number} [length] Length of content data in document; ignored and overridden to 0
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode = function VeDmMWReferenceNode( length, element ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.dm.LeafNode.call( this, 0, element );
|
2013-05-29 14:46:52 +00:00
|
|
|
|
|
|
|
// Event handlers
|
|
|
|
this.connect( this, {
|
|
|
|
'root': 'onRoot',
|
|
|
|
'unroot': 'onUnroot'
|
|
|
|
} );
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
/* Static members */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.MWReferenceNode.static.name = 'mwReference';
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.matchTagNames = null;
|
|
|
|
|
2013-06-03 20:22:35 +00:00
|
|
|
ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ];
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.isContent = true;
|
|
|
|
|
2013-11-19 10:56:37 +00:00
|
|
|
ve.dm.MWReferenceNode.static.blacklistedAnnotationTypes = [ 'link' ];
|
|
|
|
|
2013-11-12 23:34:50 +00:00
|
|
|
/**
|
|
|
|
* Regular expression for parsing the listKey attribute
|
|
|
|
* @static
|
|
|
|
* @property {RegExp}
|
|
|
|
* @inheritable
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.listKeyRegex = /^(auto|literal)\/(.*)$/;
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
|
2013-06-03 20:22:35 +00:00
|
|
|
var dataElement,
|
2013-06-21 02:55:16 +00:00
|
|
|
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
|
|
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
|
|
|
|
body = mwData.body ? mwData.body.html : '',
|
|
|
|
refGroup = mwData.attrs && mwData.attrs.group || '',
|
2013-06-03 20:22:35 +00:00
|
|
|
listGroup = this.name + '/' + refGroup,
|
2013-09-19 22:57:08 +00:00
|
|
|
autoKeyed = !mwData.attrs || mwData.attrs.name === undefined,
|
|
|
|
listKey = autoKeyed ? 'auto/' + converter.internalList.getNextUniqueNumber() : 'literal/' + mwData.attrs.name,
|
2013-06-06 15:42:55 +00:00
|
|
|
queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body ),
|
|
|
|
listIndex = queueResult.index,
|
|
|
|
contentsUsed = ( body !== '' && queueResult.isNew );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
|
|
|
dataElement = {
|
|
|
|
'type': this.name,
|
|
|
|
'attributes': {
|
2013-06-21 02:55:16 +00:00
|
|
|
'mw': mwData,
|
2013-06-24 10:24:28 +00:00
|
|
|
'originalMw': mwDataJSON,
|
2013-07-28 20:51:32 +00:00
|
|
|
'childDomElements': ve.copy( Array.prototype.slice.apply( domElements[0].childNodes ) ),
|
2013-05-29 14:46:52 +00:00
|
|
|
'listIndex': listIndex,
|
|
|
|
'listGroup': listGroup,
|
2013-06-03 20:22:35 +00:00
|
|
|
'listKey': listKey,
|
2013-06-06 15:42:55 +00:00
|
|
|
'refGroup': refGroup,
|
|
|
|
'contentsUsed': contentsUsed
|
2013-04-17 17:53:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return dataElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
2013-06-30 08:43:56 +00:00
|
|
|
var itemNodeHtml, originalHtml, mwData, i, iLen, keyedNodes, setContents, contentsAlreadySet,
|
2013-09-19 22:57:08 +00:00
|
|
|
originalMw, childDomElements, listKeyParts, name,
|
2013-06-24 02:16:17 +00:00
|
|
|
el = doc.createElement( 'span' ),
|
2013-04-17 17:53:26 +00:00
|
|
|
itemNodeWrapper = doc.createElement( 'div' ),
|
|
|
|
itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex ),
|
|
|
|
itemNodeRange = itemNode.getRange();
|
|
|
|
|
2013-06-24 02:16:17 +00:00
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/ref' );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-07-28 20:51:32 +00:00
|
|
|
mwData = dataElement.attributes.mw ? ve.copy( dataElement.attributes.mw ) : {};
|
2013-06-27 23:17:59 +00:00
|
|
|
mwData.name = 'ref';
|
2013-06-06 15:42:55 +00:00
|
|
|
|
2013-08-27 20:51:48 +00:00
|
|
|
setContents = dataElement.attributes.contentsUsed;
|
2013-06-06 15:42:55 +00:00
|
|
|
|
2013-06-30 08:43:56 +00:00
|
|
|
keyedNodes = converter.internalList
|
|
|
|
.getNodeGroup( dataElement.attributes.listGroup )
|
|
|
|
.keyedNodes[dataElement.attributes.listKey];
|
2013-09-30 13:26:33 +00:00
|
|
|
|
2013-06-30 08:43:56 +00:00
|
|
|
if ( setContents ) {
|
|
|
|
// Check if a previous node has already set the content. If so, we don't overwrite this
|
|
|
|
// node's contents.
|
|
|
|
contentsAlreadySet = false;
|
|
|
|
if ( keyedNodes ) {
|
|
|
|
for ( i = 0, iLen = keyedNodes.length; i < iLen; i++ ) {
|
|
|
|
if ( keyedNodes[i].element === dataElement ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( keyedNodes[i].element.attributes.contentsUsed ) {
|
|
|
|
contentsAlreadySet = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-06 15:42:55 +00:00
|
|
|
// Check if any other nodes with this key provided content. If not
|
|
|
|
// then we attach the contents to the first reference with this key
|
2013-06-30 08:43:56 +00:00
|
|
|
|
2013-09-30 13:26:33 +00:00
|
|
|
// Check that this is the first reference with its key
|
2013-06-30 08:43:56 +00:00
|
|
|
if ( keyedNodes && dataElement === keyedNodes[0].element ) {
|
2013-06-06 15:42:55 +00:00
|
|
|
setContents = true;
|
|
|
|
// Check no other reference originally defined the contents
|
2013-06-07 16:58:34 +00:00
|
|
|
// As this is keyedNodes[0] we can start at 1
|
|
|
|
for ( i = 1, iLen = keyedNodes.length; i < iLen; i++ ) {
|
|
|
|
if ( keyedNodes[i].element.attributes.contentsUsed ) {
|
2013-06-06 15:42:55 +00:00
|
|
|
setContents = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 08:43:56 +00:00
|
|
|
if ( setContents && !contentsAlreadySet ) {
|
2013-06-06 15:42:55 +00:00
|
|
|
converter.getDomSubtreeFromData(
|
2013-06-23 17:21:52 +00:00
|
|
|
itemNode.getDocument().getFullData( new ve.Range( itemNodeRange.start, itemNodeRange.end ), true ),
|
2013-06-06 15:42:55 +00:00
|
|
|
itemNodeWrapper
|
2013-06-26 02:18:59 +00:00
|
|
|
);
|
|
|
|
itemNodeHtml = $( itemNodeWrapper ).html(); // Returns '' if itemNodeWrapper is empty
|
|
|
|
originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
|
|
|
|
// Only set body.html if itemNodeHtml and originalHtml are actually different
|
|
|
|
if ( !$( '<div>' ).html( originalHtml ).get( 0 ).isEqualNode( itemNodeWrapper ) ) {
|
|
|
|
ve.setProp( mwData, 'body', 'html', itemNodeHtml );
|
|
|
|
}
|
2013-06-06 15:42:55 +00:00
|
|
|
}
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-09-19 22:57:08 +00:00
|
|
|
// Generate name
|
2013-11-12 23:34:50 +00:00
|
|
|
listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
|
2013-09-19 22:57:08 +00:00
|
|
|
if ( listKeyParts[1] === 'auto' ) {
|
|
|
|
// Only render a name if this key was reused
|
|
|
|
if ( keyedNodes.length > 1 ) {
|
|
|
|
// Allocate a unique list key, then strip the 'literal/'' prefix
|
|
|
|
name = converter.internalList.getUniqueListKey(
|
|
|
|
dataElement.attributes.listGroup,
|
|
|
|
dataElement.attributes.listKey,
|
|
|
|
// Generate a name starting with ':' to distinguish it from normal names
|
|
|
|
'literal/:'
|
|
|
|
).substr( 'literal/'.length );
|
|
|
|
} else {
|
|
|
|
name = undefined;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Use literal name
|
|
|
|
name = listKeyParts[2];
|
2013-06-14 19:07:55 +00:00
|
|
|
}
|
2013-09-19 22:57:08 +00:00
|
|
|
// Set name
|
|
|
|
if ( name !== undefined ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'name', name );
|
|
|
|
}
|
|
|
|
|
2013-06-14 19:07:55 +00:00
|
|
|
// Set or clear group
|
|
|
|
if ( dataElement.attributes.refGroup !== '' ) {
|
2013-06-24 02:16:17 +00:00
|
|
|
ve.setProp( mwData, 'attrs', 'group', dataElement.attributes.refGroup );
|
|
|
|
} else if ( mwData.attrs ) {
|
|
|
|
delete mwData.attrs.refGroup;
|
2013-06-14 19:07:55 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 10:24:28 +00:00
|
|
|
// If mwAttr and originalMw are the same, use originalMw to prevent reserialization.
|
2013-06-24 00:00:42 +00:00
|
|
|
// Reserialization has the potential to reorder keys and so change the DOM unnecessarily
|
2013-06-24 10:24:28 +00:00
|
|
|
originalMw = dataElement.attributes.originalMw;
|
|
|
|
if ( originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
|
|
|
|
el.setAttribute( 'data-mw', originalMw );
|
2013-06-24 18:57:27 +00:00
|
|
|
|
|
|
|
// Restore the span's childNodes to prevent unnecessary DOM diffs
|
|
|
|
childDomElements = ve.copyDomElements( dataElement.attributes.childDomElements, doc );
|
|
|
|
for ( i = 0, iLen = childDomElements.length; i < iLen; i++ ) {
|
|
|
|
el.appendChild( childDomElements[i] );
|
|
|
|
}
|
2013-06-24 00:00:42 +00:00
|
|
|
} else {
|
2013-06-24 02:16:17 +00:00
|
|
|
el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
|
2013-06-24 00:00:42 +00:00
|
|
|
}
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2013-06-24 02:16:17 +00:00
|
|
|
return [ el ];
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
2013-11-12 23:34:50 +00:00
|
|
|
ve.dm.MWReferenceNode.static.remapInternalListIndexes = function ( dataElement, mapping, internalList ) {
|
|
|
|
var listKeyParts;
|
|
|
|
// Remap listIndex
|
2013-05-22 14:58:21 +00:00
|
|
|
dataElement.attributes.listIndex = mapping[dataElement.attributes.listIndex];
|
2013-11-12 23:34:50 +00:00
|
|
|
|
|
|
|
// Remap listKey if it was automatically generated
|
|
|
|
listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
|
|
|
|
if ( listKeyParts[1] === 'auto' ) {
|
|
|
|
dataElement.attributes.listKey = 'auto/' + internalList.getNextUniqueNumber();
|
|
|
|
}
|
2013-05-22 14:58:21 +00:00
|
|
|
};
|
|
|
|
|
2013-09-30 13:26:33 +00:00
|
|
|
ve.dm.MWReferenceNode.static.remapInternalListKeys = function ( dataElement, internalList ) {
|
|
|
|
var suffix = '';
|
|
|
|
// Try name, name2, name3, ... until unique
|
|
|
|
while ( ve.indexOf( dataElement.attributes.listKey + suffix, internalList.keys ) !== -1 ) {
|
|
|
|
suffix = suffix ? suffix + 1 : 2;
|
|
|
|
}
|
|
|
|
if ( suffix ) {
|
|
|
|
dataElement.attributes.listKey = dataElement.attributes.listKey + suffix;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-13 16:51:07 +00:00
|
|
|
/**
|
|
|
|
* Gets the index label for the reference
|
|
|
|
* @static
|
|
|
|
* @param {Object} dataElement Element data
|
|
|
|
* @param {ve.dm.InternalList} internalList Internal list
|
|
|
|
* @returns {string} Reference label
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.static.getIndexLabel = function ( dataElement, internalList ) {
|
|
|
|
var listIndex = dataElement.attributes.listIndex,
|
|
|
|
listGroup = dataElement.attributes.listGroup,
|
|
|
|
refGroup = dataElement.attributes.refGroup,
|
|
|
|
position = internalList.getIndexPosition( listGroup, listIndex );
|
|
|
|
|
|
|
|
return '[' + ( refGroup ? refGroup + ' ' : '' ) + ( position + 1 ) + ']';
|
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2013-06-13 08:30:13 +00:00
|
|
|
/**
|
|
|
|
* Don't allow reference nodes to be inspected if we can't find their contents.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.isInspectable = function () {
|
|
|
|
var internalItem = this.getInternalItem();
|
|
|
|
return internalItem && internalItem.getLength() > 0;
|
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/**
|
|
|
|
* Gets the internal item node associated with this node
|
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.InternalItemNode} Item node
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getInternalItem = function () {
|
2013-05-29 14:46:52 +00:00
|
|
|
return this.getDocument().getInternalList().getItemNode( this.getAttribute( 'listIndex' ) );
|
|
|
|
};
|
|
|
|
|
2013-12-13 16:51:07 +00:00
|
|
|
/**
|
|
|
|
* Gets the index label for the reference
|
|
|
|
* @method
|
|
|
|
* @returns {string} Reference label
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.getIndexLabel = function () {
|
|
|
|
return this.constructor.static.getIndexLabel( this.element, this.getDocument().getInternalList() );
|
|
|
|
};
|
|
|
|
|
2013-05-29 14:46:52 +00:00
|
|
|
/**
|
|
|
|
* Handle the node being attached to the root
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.onRoot = function () {
|
2013-06-14 19:07:55 +00:00
|
|
|
this.addToInternalList();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the node being detatched from the root
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.onUnroot = function () {
|
|
|
|
this.removeFromInternalList();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register the node with the internal list
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.dm.MWReferenceNode.prototype.addToInternalList = function () {
|
2013-05-29 14:46:52 +00:00
|
|
|
if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
|
|
|
|
this.getDocument().getInternalList().addNode(
|
2013-06-07 16:58:34 +00:00
|
|
|
this.element.attributes.listGroup,
|
|
|
|
this.element.attributes.listKey,
|
|
|
|
this.element.attributes.listIndex,
|
|
|
|
this
|
2013-05-29 14:46:52 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-06-14 19:07:55 +00:00
|
|
|
* Unregister the node from the internal list
|
2013-05-29 14:46:52 +00:00
|
|
|
* @method
|
|
|
|
*/
|
2013-06-14 19:07:55 +00:00
|
|
|
ve.dm.MWReferenceNode.prototype.removeFromInternalList = function () {
|
2013-05-29 14:46:52 +00:00
|
|
|
this.getDocument().getInternalList().removeNode(
|
2013-06-07 16:58:34 +00:00
|
|
|
this.element.attributes.listGroup,
|
|
|
|
this.element.attributes.listKey,
|
|
|
|
this.element.attributes.listIndex,
|
|
|
|
this
|
2013-05-29 14:46:52 +00:00
|
|
|
);
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
2013-07-31 22:53:29 +00:00
|
|
|
/** */
|
2013-06-06 15:42:55 +00:00
|
|
|
ve.dm.MWReferenceNode.prototype.getClonedElement = function () {
|
|
|
|
var clone = ve.dm.LeafNode.prototype.getClonedElement.call( this );
|
2013-06-27 22:45:09 +00:00
|
|
|
delete clone.attributes.contentsUsed;
|
2013-08-27 20:51:48 +00:00
|
|
|
delete clone.attributes.mw;
|
2013-06-27 23:04:26 +00:00
|
|
|
delete clone.attributes.originalMw;
|
2013-06-06 15:42:55 +00:00
|
|
|
return clone;
|
|
|
|
};
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2013-05-28 11:31:41 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferenceNode );
|