2013-06-24 14:41:39 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWMathNode class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki math node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.LeafNode
|
2013-07-19 22:30:29 +00:00
|
|
|
* @mixins ve.dm.GeneratedContentNode
|
|
|
|
*
|
2013-06-24 14:41:39 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.dm.MWMathNode = function VeDmMWMathNode( length, element ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.dm.LeafNode.call( this, 0, element );
|
2013-07-19 22:30:29 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.GeneratedContentNode.call( this );
|
2013-06-24 14:41:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.dm.MWMathNode, ve.dm.LeafNode );
|
|
|
|
|
2013-07-19 22:30:29 +00:00
|
|
|
ve.mixinClass( ve.dm.MWMathNode, ve.dm.GeneratedContentNode );
|
|
|
|
|
2013-06-24 14:41:39 +00:00
|
|
|
/* Static members */
|
|
|
|
|
|
|
|
ve.dm.MWMathNode.static.name = 'mwMath';
|
|
|
|
|
|
|
|
ve.dm.MWMathNode.static.enableAboutGrouping = true;
|
|
|
|
|
|
|
|
ve.dm.MWMathNode.static.matchTagNames = null;
|
|
|
|
|
|
|
|
ve.dm.MWMathNode.static.matchRdfaTypes = [ 'mw:Extension/math' ];
|
|
|
|
|
|
|
|
ve.dm.MWMathNode.static.isContent = true;
|
|
|
|
|
2013-07-19 22:30:29 +00:00
|
|
|
ve.dm.MWMathNode.static.toDataElement = function ( domElements, converter ) {
|
|
|
|
var dataElement, index,
|
2013-06-24 14:41:39 +00:00
|
|
|
mwDataJSON = domElements[0].getAttribute( 'data-mw' ),
|
2013-07-19 22:30:29 +00:00
|
|
|
mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
|
2013-06-24 14:41:39 +00:00
|
|
|
|
|
|
|
dataElement = {
|
|
|
|
'type': 'mwMath',
|
|
|
|
'attributes': {
|
|
|
|
'mw': mwData,
|
2013-07-19 22:30:29 +00:00
|
|
|
'originalDomElements': ve.copyArray( domElements ),
|
|
|
|
'originalMw': mwDataJSON
|
2013-06-24 14:41:39 +00:00
|
|
|
}
|
|
|
|
};
|
2013-07-19 22:30:29 +00:00
|
|
|
|
|
|
|
index = this.storeDomElements( dataElement, domElements, converter.getStore() );
|
|
|
|
dataElement.attributes.originalIndex = index;
|
|
|
|
|
2013-06-24 14:41:39 +00:00
|
|
|
return dataElement;
|
|
|
|
};
|
|
|
|
|
2013-07-19 22:30:29 +00:00
|
|
|
ve.dm.MWMathNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
|
|
|
var el,
|
|
|
|
index = converter.getStore().indexOfHash( ve.getHash( this.getHashObject( dataElement ) ) ),
|
2013-07-03 21:44:15 +00:00
|
|
|
originalMw = dataElement.attributes.originalMw;
|
|
|
|
|
2013-07-19 22:30:29 +00:00
|
|
|
// If the transclusion is unchanged just send back the
|
|
|
|
// original DOM elements so selser can skip over it
|
|
|
|
if (
|
|
|
|
index === dataElement.attributes.originalIndex ||
|
|
|
|
( originalMw && ve.compare( dataElement.attributes.mw, JSON.parse( originalMw ) ) )
|
|
|
|
) {
|
|
|
|
// The object in the store is also used for CE rendering so return a copy
|
|
|
|
return ve.copyDomElements( dataElement.attributes.originalDomElements, doc );
|
2013-07-03 21:44:15 +00:00
|
|
|
} else {
|
2013-07-19 22:30:29 +00:00
|
|
|
el = doc.createElement( 'img' );
|
|
|
|
el.setAttribute( 'typeof', 'mw:Extension/Math' );
|
|
|
|
el.setAttribute( 'data-mw', JSON.stringify( dataElement.attributes.mw ) );
|
|
|
|
return [ el ];
|
2013-07-03 21:44:15 +00:00
|
|
|
}
|
2013-07-19 22:30:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWMathNode.static.getHashObject = function ( dataElement ) {
|
|
|
|
return {
|
|
|
|
type: dataElement.type,
|
|
|
|
mw: dataElement.attributes.mw
|
|
|
|
};
|
2013-06-24 14:41:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWMathNode );
|