2013-04-17 17:53:26 +00:00
|
|
|
/*!
|
2014-08-19 18:32:46 +00:00
|
|
|
* VisualEditor DataModel MWReferencesListNode class.
|
2013-04-17 17:53:26 +00:00
|
|
|
*
|
2015-01-08 23:54:03 +00:00
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
2013-04-17 17:53:26 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-08-19 18:32:46 +00:00
|
|
|
* DataModel MediaWiki references list node.
|
2013-04-17 17:53:26 +00:00
|
|
|
*
|
|
|
|
* @class
|
2013-07-25 17:11:50 +00:00
|
|
|
* @extends ve.dm.BranchNode
|
2014-09-17 00:33:39 +00:00
|
|
|
* @mixins ve.dm.FocusableNode
|
2014-06-07 03:17:26 +00:00
|
|
|
*
|
2013-04-17 17:53:26 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
2014-06-07 03:17:26 +00:00
|
|
|
* @param {ve.dm.Node[]} [children]
|
2013-04-17 17:53:26 +00:00
|
|
|
*/
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode = function VeDmMWReferencesListNode() {
|
2013-04-17 17:53:26 +00:00
|
|
|
// Parent constructor
|
2014-06-07 03:17:26 +00:00
|
|
|
ve.dm.BranchNode.apply( this, arguments );
|
2014-09-17 00:33:39 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.FocusableNode.call( this );
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
OO.inheritClass( ve.dm.MWReferencesListNode, ve.dm.BranchNode );
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2014-09-17 00:33:39 +00:00
|
|
|
OO.mixinClass( ve.dm.MWReferencesListNode, ve.dm.FocusableNode );
|
|
|
|
|
2013-04-17 17:53:26 +00:00
|
|
|
/* Static members */
|
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.name = 'mwReferencesList';
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.handlesOwnChildren = true;
|
2013-07-25 17:11:50 +00:00
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.matchTagNames = null;
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.matchRdfaTypes = [ 'mw:Extension/references' ];
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.storeHtmlAttributes = false;
|
2013-04-17 17:53:26 +00:00
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.toDataElement = function ( domElements, converter ) {
|
2015-01-26 22:06:56 +00:00
|
|
|
var referencesListData, contentsDiv, contentsData,
|
2013-07-25 17:11:50 +00:00
|
|
|
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
|
2013-06-21 02:55:16 +00:00
|
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {},
|
|
|
|
refGroup = mwData.attrs && mwData.attrs.group || '',
|
2013-06-03 20:22:35 +00:00
|
|
|
listGroup = 'mwReference/' + refGroup;
|
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
referencesListData = {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
|
|
|
mw: mwData,
|
|
|
|
originalMw: mwDataJSON,
|
|
|
|
domElements: ve.copy( domElements ),
|
|
|
|
refGroup: refGroup,
|
|
|
|
listGroup: listGroup
|
2013-04-17 17:53:26 +00:00
|
|
|
}
|
|
|
|
};
|
2013-07-25 17:11:50 +00:00
|
|
|
if ( mwData.body && mwData.body.html ) {
|
2015-02-05 00:39:47 +00:00
|
|
|
// Process the nodes in .body.html as if they were this node's children
|
2015-01-26 22:06:56 +00:00
|
|
|
contentsDiv = domElements[0].ownerDocument.createElement( 'div' );
|
|
|
|
contentsDiv.innerHTML = mwData.body.html;
|
|
|
|
contentsData = converter.getDataFromDomClean( contentsDiv );
|
2015-02-05 00:39:47 +00:00
|
|
|
referencesListData = [ referencesListData ]
|
2015-01-31 00:41:37 +00:00
|
|
|
.concat( contentsData )
|
|
|
|
.concat( [ { type: '/' + this.name } ] );
|
2013-07-25 17:11:50 +00:00
|
|
|
}
|
2015-02-05 00:39:47 +00:00
|
|
|
return referencesListData;
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.MWReferencesListNode.static.toDomElements = function ( data, doc, converter ) {
|
2015-01-26 22:06:56 +00:00
|
|
|
var el, els, mwData, originalMw, contentsHtml, originalHtml,
|
|
|
|
wrapper = doc.createElement( 'div' ),
|
|
|
|
originalHtmlWrapper = doc.createElement( 'div' ),
|
2013-07-25 17:11:50 +00:00
|
|
|
dataElement = data[0],
|
|
|
|
attribs = dataElement.attributes,
|
|
|
|
contentsData = data.slice( 1, -1 );
|
2013-06-21 02:55:16 +00:00
|
|
|
|
|
|
|
if ( attribs.domElements ) {
|
|
|
|
// If there's more than 1 element, preserve entire array, not just first element
|
|
|
|
els = ve.copyDomElements( attribs.domElements, doc );
|
|
|
|
el = els[0];
|
|
|
|
} else {
|
|
|
|
el = doc.createElement( 'div' );
|
|
|
|
els = [ el ];
|
|
|
|
}
|
|
|
|
|
2013-07-28 20:51:32 +00:00
|
|
|
mwData = attribs.mw ? ve.copy( attribs.mw ) : {};
|
2013-06-21 02:55:16 +00:00
|
|
|
|
|
|
|
mwData.name = 'references';
|
|
|
|
|
|
|
|
if ( attribs.refGroup ) {
|
|
|
|
ve.setProp( mwData, 'attrs', 'group', attribs.refGroup );
|
|
|
|
} else if ( mwData.attrs ) {
|
|
|
|
delete mwData.attrs.refGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/references' );
|
2013-06-24 00:00:42 +00:00
|
|
|
|
2013-07-25 17:11:50 +00:00
|
|
|
if ( contentsData.length > 2 ) {
|
|
|
|
converter.getDomSubtreeFromData( data.slice( 1, -1 ), wrapper );
|
2015-01-26 22:06:56 +00:00
|
|
|
contentsHtml = wrapper.innerHTML; // Returns '' if wrapper is empty
|
2013-07-25 17:11:50 +00:00
|
|
|
originalHtml = ve.getProp( mwData, 'body', 'html' ) || '';
|
2015-01-26 22:06:56 +00:00
|
|
|
originalHtmlWrapper.innerHTML = originalHtml;
|
2013-07-25 17:11:50 +00:00
|
|
|
// Only set body.html if contentsHtml and originalHtml are actually different
|
2015-01-26 22:06:56 +00:00
|
|
|
if ( !originalHtmlWrapper.isEqualNode( wrapper ) ) {
|
2013-07-25 17:11:50 +00:00
|
|
|
ve.setProp( mwData, 'body', 'html', contentsHtml );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 10:24:28 +00:00
|
|
|
// If mwData 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 = attribs.originalMw;
|
|
|
|
if ( originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
|
|
|
|
el.setAttribute( 'data-mw', originalMw );
|
2013-06-24 00:00:42 +00:00
|
|
|
} else {
|
|
|
|
el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
|
|
|
|
}
|
2013-06-21 02:55:16 +00:00
|
|
|
|
|
|
|
return els;
|
2013-04-17 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
2014-08-19 18:32:46 +00:00
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWReferencesListNode );
|