mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
edcaaf9edc
Add a static.name property to ce nodes and make sure both ce and dm nodes always use the static.name property in constructors and registration calls. The result of this is that any given node type should now only appear once in the code as a string. Bug: 45701 Change-Id: Ibf31de16ab28ad58209c1443cd74f93dda278998
111 lines
3.1 KiB
JavaScript
111 lines
3.1 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel MetaNode class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* DataModel meta node.
|
|
*
|
|
* @class
|
|
* @abstract
|
|
* @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.MetaNode = function VeDmMetaNode( length, element ) {
|
|
// Parent constructor
|
|
ve.dm.LeafNode.call( this, 0, element );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.dm.MetaNode, ve.dm.LeafNode );
|
|
|
|
/* Static Properties */
|
|
|
|
ve.dm.MetaNode.static.name = 'meta';
|
|
|
|
ve.dm.MetaNode.static.isMeta = true;
|
|
|
|
ve.dm.MetaNode.static.matchTagNames = [ 'meta', 'link' ];
|
|
|
|
ve.dm.MetaNode.static.toDataElement = function ( domElements, context ) {
|
|
var firstDomElement = domElements[0],
|
|
isLink = firstDomElement.nodeName.toLowerCase() === 'link',
|
|
keyAttr = isLink ? 'rel' : 'property',
|
|
valueAttr = isLink ? 'href' : 'content',
|
|
dataElement = {
|
|
'type': context.expectingContent ? 'metaInline' : 'metaBlock',
|
|
'attributes': {
|
|
'style': isLink ? 'link' : 'meta',
|
|
'key': firstDomElement.getAttribute( keyAttr )
|
|
}
|
|
};
|
|
if ( firstDomElement.hasAttribute( valueAttr ) ) {
|
|
dataElement.attributes.value = firstDomElement.getAttribute( valueAttr );
|
|
}
|
|
return dataElement;
|
|
};
|
|
|
|
ve.dm.MetaNode.static.toDomElements = function ( dataElement ) {
|
|
var style = dataElement.attributes && dataElement.attributes.style || 'meta',
|
|
isLink = style === 'link',
|
|
tag = isLink ? 'link' : 'meta',
|
|
keyAttr = isLink ? 'rel' : 'property',
|
|
valueAttr = isLink ? 'href' : 'content',
|
|
domElement;
|
|
if ( style === 'comment' ) {
|
|
return [ document.createComment( dataElement.attributes && dataElement.attributes.text || '' ) ];
|
|
}
|
|
domElement = document.createElement( tag );
|
|
if ( dataElement.attributes && dataElement.attributes.key !== null ) {
|
|
domElement.setAttribute( keyAttr, dataElement.attributes.key );
|
|
}
|
|
if ( dataElement.attributes && dataElement.attributes.value ) {
|
|
domElement.setAttribute( valueAttr, dataElement.attributes.value );
|
|
}
|
|
return [ domElement ];
|
|
};
|
|
|
|
/* Concrete subclasses */
|
|
|
|
/**
|
|
* DataModel metaBlock node.
|
|
*
|
|
* @class
|
|
* @abstract
|
|
* @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.MetaBlockNode = function VeDmMetaBlockNode( length, element ) {
|
|
// Parent constructor
|
|
ve.dm.MetaNode.call( this, length, element );
|
|
};
|
|
|
|
ve.inheritClass( ve.dm.MetaBlockNode, ve.dm.MetaNode );
|
|
|
|
ve.dm.MetaBlockNode.static.name = 'metaBlock';
|
|
|
|
|
|
ve.dm.MetaInlineNode = function VeDmMetaInlineNode( length, element ) {
|
|
// Parent constructor
|
|
ve.dm.MetaNode.call( this, length, element );
|
|
};
|
|
|
|
ve.inheritClass( ve.dm.MetaInlineNode, ve.dm.MetaNode );
|
|
|
|
ve.dm.MetaInlineNode.static.name = 'metaInline';
|
|
|
|
ve.dm.MetaInlineNode.static.isContent = true;
|
|
|
|
/* Registration */
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MetaNode );
|
|
ve.dm.modelRegistry.register( ve.dm.MetaBlockNode );
|
|
ve.dm.modelRegistry.register( ve.dm.MetaInlineNode );
|