mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-12-01 17:36:35 +00:00
3002bbb591
ve.ce.Surface * Switched to using getSlice instead of getData in copy and paste handlers * Added try/catch which attempts to build a transaction with the unbalanced data first, but falls back on the balanced data otherwise ve.dm.*Node * Added default style attributes (now used by ve.dm.NodeFactory) ve.dm.Document * Fixed bugs in fixupInsertions where parentType was being set with an object rather than a string * Made use of getDataElement * Added adoption capability so that inserting a</h1><p>b into <p>c[cursor]d</p> results in <p>ca</p><p>bd</p> rather than throwing an exception * Renamed getBalancedData to getSlice, now retuning a ve.dm.DocumentSlice object ve.dm.DocumentSlice * Introduced new container for balanced data and a range of the original context - useful for copy/paste ve.dm.NodeFactory * Added getDataElement method, which uses default attributes to create a boilerplate version of a data element ve.dm.Document.test * Updated getBalancedData test to be a getSlice test demos/ve/index, VisualEditor, test/index * Added references to ve.dm.DocumentSlice Change-Id: Id9269a29e51ca213508de8f155d3feec5e5b0774
178 lines
4.9 KiB
JavaScript
178 lines
4.9 KiB
JavaScript
/**
|
|
* VisualEditor data model NodeFactory class.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* DataModel node factory.
|
|
*
|
|
* @class
|
|
* @extends {ve.Factory}
|
|
* @constructor
|
|
*/
|
|
ve.dm.NodeFactory = function VeDmNodeFactory() {
|
|
// Parent constructor
|
|
ve.Factory.call( this );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.inheritClass( ve.dm.NodeFactory, ve.Factory );
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Gets a data element with fallback attributes.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @param {Object} attributes Node attributes, defaults will be used where needed
|
|
* @returns {Object} Data element
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.getDataElement = function ( type, attributes ) {
|
|
var element = { 'type': type };
|
|
if ( type in this.registry ) {
|
|
if ( this.registry[type].defaultAttributes ) {
|
|
attributes = ve.extendObject( {}, this.registry[type].defaultAttributes, attributes );
|
|
}
|
|
if ( ve.isPlainObject( attributes ) && !ve.isEmptyObject( attributes ) ) {
|
|
element.attributes = ve.copyObject( attributes );
|
|
}
|
|
return element;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Gets a list of allowed child node types for a given node.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {String[]|null} List of node types allowed as children or null if any type is allowed
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.getChildNodeTypes = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.registry[type].rules.childNodeTypes;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Gets a list of allowed parent node types for a given node.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {String[]|null} List of node types allowed as parents or null if any type is allowed
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.getParentNodeTypes = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.registry[type].rules.parentNodeTypes;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Checks if a given node type can have child nodes.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {Boolean} The node can have children
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.canNodeHaveChildren = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
// If childNodeTypes is null any child is allowed, if it's an array of at least one element
|
|
// than at least one kind of node is allowed
|
|
var types = this.registry[type].rules.childNodeTypes;
|
|
return types === null || ( ve.isArray( types ) && types.length > 0 );
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Checks if a given node type can have grandchild nodes.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {Boolean} The node can have grandchildren
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.canNodeHaveGrandchildren = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.canNodeHaveChildren( type ) &&
|
|
!this.registry[type].rules.canContainContent &&
|
|
!this.registry[type].rules.isContent;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Checks if a given node type has a wrapping element.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {Boolean} Whether the node has a wrapping element
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.isNodeWrapped = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.registry[type].rules.isWrapped;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Checks if a given node contains content.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {Boolean} The node contains content
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.canNodeContainContent = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.registry[type].rules.canContainContent;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Checks if a given node is content.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {Boolean} The node is content
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.isNodeContent = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.registry[type].rules.isContent;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/**
|
|
* Checks if a given node has significant whitespace. Can only be true if canContainContent is
|
|
* also true.
|
|
*
|
|
* @method
|
|
* @param {String} type Node type
|
|
* @returns {Boolean} The node has significant whitespace
|
|
* @throws 'Unknown node type: {type}'
|
|
*/
|
|
ve.dm.NodeFactory.prototype.doesNodeHaveSignificantWhitespace = function ( type ) {
|
|
if ( type in this.registry ) {
|
|
return this.registry[type].rules.hasSignificantWhitespace;
|
|
}
|
|
throw new Error( 'Unknown node type: ' + type );
|
|
};
|
|
|
|
/* Initialization */
|
|
|
|
ve.dm.nodeFactory = new ve.dm.NodeFactory();
|