mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-15 18:39:52 +00:00
2e36f1542b
This changes the node API to work with multiple elements, so we can support about groups. Instead of passing in and returning single DOM elements, we use arrays of DOM elements. ve.dm.Converter: * Pass modelRegistry into the constructor * Remove onNodeRegister handler and its data * Remove getDataElementFromDomElement() and getDataAnnotationFromDomElement(). Most logic moved into getDataFromDom(), some into createDataElement() * Remove createAlien(), replaced with createDataElement( ve.dm.AlienNode, ... ) * Replace doAboutGrouping() (which wrapped about groups) with getAboutGroup() (which returns an array of the nodes in the group) * Put in a hack so <meta>/<link> elements with an mw: property aren't alienated * Remove about group wrapping behavior in favor of just outputting multiple nodes ve.dm.AlienNode.js: * For multi-element aliens, only choose inline if all elements are inline, not just the first one ve.dm.example.js: * Add html/0 stuff for meta nodes * Fix test case to reflect new alien behavior Change-Id: I40dcc27430f778bc00a44b91b7d824bfb2718be6
112 lines
3.2 KiB
JavaScript
112 lines
3.2 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 {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.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, '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 );
|