mediawiki-extensions-Visual.../modules/ve/dm/nodes/ve.dm.AlienNode.js
Catrope 2e36f1542b (bug 45062) Implement the new node API in the converter
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
2013-02-22 15:21:40 -08:00

121 lines
3.5 KiB
JavaScript

/*!
* VisualEditor DataModel AlienNode, AlienBlockNode and AlienInlineNode classes.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel alien 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.AlienNode = function VeDmAlienNode( name, length, element ) {
// Parent constructor
ve.dm.LeafNode.call( this, name, 0, element );
};
/* Inheritance */
ve.inheritClass( ve.dm.AlienNode, ve.dm.LeafNode );
/* Static members */
ve.dm.AlienNode.static.name = 'alien';
ve.dm.AlienNode.static.matchTagNames = [];
ve.dm.AlienNode.static.enableAboutGrouping = true;
ve.dm.AlienNode.static.storeHtmlAttributes = false;
ve.dm.AlienNode.static.toDataElement = function ( domElements, context ) {
var i, isInline, allTagsInline, type, html;
// Check whether all elements are inline elements
allTagsInline = true;
for ( i = 0; i < domElements.length; i++ ) {
if ( ve.isBlockElement( domElements[i] ) ) {
allTagsInline = false;
break;
}
}
// We generate alienBlock elements for block tags and alienInline elements for
// inline tags; unless we're in a content location, in which case we have no choice
// but to generate an alienInline element.
isInline =
// Force inline in content locations (but not wrappers)
( context.expectingContent && !context.inWrapper ) ||
// Also force inline in wrappers that we can't close
( context.inWrapper && !context.canCloseWrapper ) ||
// Look at the tag names otherwise
allTagsInline;
type = isInline ? 'alienInline' : 'alienBlock';
html = $( '<div>', domElements[0].ownerDocument ).append( $( domElements ).clone() ).html();
return {
'type': type,
'attributes': {
'html': html
}
};
};
ve.dm.AlienNode.static.toDomElements = function ( dataElement ) {
var wrapper = document.createElement( 'div' );
wrapper.innerHTML = dataElement.attributes.html;
// Convert wrapper.children to an array
return Array.prototype.slice.call( wrapper.childNodes, 0 );
};
/* Concrete subclasses */
/**
* DataModel alienBlock node.
*
* @class
* @extends ve.dm.AlienNode
* @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.AlienBlockNode = function VeDmAlienBlockNode( length, element ) {
// Parent constructor
ve.dm.AlienNode.call( this, 'alienBlock', length, element );
};
ve.inheritClass( ve.dm.AlienBlockNode, ve.dm.AlienNode );
ve.dm.AlienBlockNode.static.name = 'alienBlock';
/**
* DataModel alienInline node.
*
* @class
* @extends ve.dm.AlienNode
* @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.AlienInlineNode = function VeDmAlienInlineNode( length, element ) {
// Parent constructor
ve.dm.AlienNode.call( this, 'alienInline', length, element );
};
ve.inheritClass( ve.dm.AlienInlineNode, ve.dm.AlienNode );
ve.dm.AlienInlineNode.static.name = 'alienInline';
ve.dm.AlienInlineNode.static.isContent = true;
/* Registration */
ve.dm.modelRegistry.register( ve.dm.AlienNode );
ve.dm.modelRegistry.register( ve.dm.AlienBlockNode );
ve.dm.modelRegistry.register( ve.dm.AlienInlineNode );