mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-29 00:30:44 +00:00
e083afb029
Change-Id: I7b7a2d50637e2009742b290cb1bd0f4fc0395934
109 lines
3.1 KiB
JavaScript
109 lines
3.1 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel MetaNode class.
|
|
*
|
|
* @copyright 2011-2012 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 {string} name Node name
|
|
* @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 VeDmMetaBlockNode( name, length, element ) {
|
|
// Parent constructor
|
|
ve.dm.LeafNode.call( this, name, 0, element );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.dm.MetaNode, ve.dm.LeafNode );
|
|
|
|
/* Static Properties */
|
|
|
|
ve.dm.MetaNode.static.name = 'meta';
|
|
|
|
ve.dm.MetaNode.static.matchTagNames = [ 'meta', 'link' ];
|
|
|
|
ve.dm.MetaNode.static.toDataElement = function ( domElement, context ) {
|
|
var isLink = domElement.nodeName.toLowerCase() === 'link',
|
|
keyAttr = isLink ? 'rel' : 'property',
|
|
valueAttr = isLink ? 'href' : 'content',
|
|
dataElement = {
|
|
'type': context.expectingContent ? 'metaInline' : 'metaBlock',
|
|
'attributes': {
|
|
'style': isLink ? 'link' : 'meta',
|
|
'key': domElement.getAttribute( keyAttr )
|
|
}
|
|
};
|
|
if ( domElement.hasAttribute( valueAttr ) ) {
|
|
dataElement.attributes.value = domElement.getAttribute( valueAttr );
|
|
}
|
|
return dataElement;
|
|
};
|
|
|
|
ve.dm.MetaNode.static.toDomElement = 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, 'metaBlock', 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, 'metaInline', 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 );
|