mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
3ce705313e
Also added more static config getters and fixed some typos in prototype definitions Change-Id: Ib9093a625b944e6a4ca4da50c51fb475b5895b8d
166 lines
3.8 KiB
JavaScript
166 lines
3.8 KiB
JavaScript
/**
|
|
* Generic DataModel node.
|
|
*
|
|
* @class
|
|
* @abstract
|
|
* @constructor
|
|
* @extends {ve.Node}
|
|
* @param {String} type Symbolic name of node type
|
|
* @param {Integer} [length] Length of content data in document
|
|
* @param {Object} [attributes] Reference to map of attribute key/value pairs
|
|
*/
|
|
ve.dm.Node = function( type, length, attributes ) {
|
|
// Inheritance
|
|
ve.Node.call( this, type );
|
|
|
|
// Properties
|
|
this.length = length || 0;
|
|
this.attributes = attributes || {};
|
|
this.doc = undefined;
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Gets a list of allowed child node types.
|
|
*
|
|
* @method
|
|
* @returns {String[]|null} List of node types allowed as children or null if any type is allowed
|
|
*/
|
|
ve.dm.Node.prototype.getChildNodeTypes = function() {
|
|
return ve.dm.factory.getChildNodeTypes( this.type );
|
|
};
|
|
|
|
/**
|
|
* Gets a list of allowed parent node types.
|
|
*
|
|
* @method
|
|
* @returns {String[]|null} List of node types allowed as parents or null if any type is allowed
|
|
*/
|
|
ve.dm.Node.prototype.getParentNodeTypes = function() {
|
|
return ve.dm.factory.getParentNodeTypes( this.type );
|
|
};
|
|
|
|
/**
|
|
* Checks if this node can have child nodes.
|
|
*
|
|
* @method
|
|
* @returns {Boolean} Node can have children
|
|
*/
|
|
ve.dm.Node.prototype.canHaveChildren = function() {
|
|
return ve.dm.factory.canNodeHaveChildren( this.type );
|
|
};
|
|
|
|
/**
|
|
* Checks if this node can have child nodes which can also have child nodes.
|
|
*
|
|
* @method
|
|
* @returns {Boolean} Node can have grandchildren
|
|
*/
|
|
ve.dm.Node.prototype.canHaveGrandchildren = function() {
|
|
return ve.dm.factory.canNodeHaveGrandchildren( this.type );
|
|
};
|
|
|
|
/**
|
|
* Checks if this node represents a wrapped element in the linear model.
|
|
*
|
|
* @method
|
|
* @returns {Boolean} Node represents a wrapped element
|
|
*/
|
|
ve.dm.Node.prototype.isWrapped = function() {
|
|
return ve.dm.factory.isNodeWrapped( this.type );
|
|
};
|
|
|
|
/**
|
|
* Checks if this node can contain content.
|
|
*
|
|
* @method
|
|
* @returns {Boolean} Node can contain content
|
|
*/
|
|
ve.dm.Node.prototype.canContainContent = function() {
|
|
return ve.dm.factory.canNodeContainContent( this.type );
|
|
};
|
|
|
|
/**
|
|
* Checks if this node is content.
|
|
*
|
|
* @method
|
|
* @returns {Boolean} Node is content
|
|
*/
|
|
ve.dm.Node.prototype.isContent = function() {
|
|
return ve.dm.factory.isNodeContent( this.type );
|
|
};
|
|
|
|
/**
|
|
* Gets the inner length.
|
|
*
|
|
* @method
|
|
* @returns {Integer} Length of the node's contents
|
|
*/
|
|
ve.dm.Node.prototype.getLength = function() {
|
|
return this.length;
|
|
};
|
|
|
|
/**
|
|
* Gets the outer length, including any opening/closing elements.
|
|
*
|
|
* @method
|
|
* @returns {Integer} Length of the entire node
|
|
*/
|
|
ve.dm.Node.prototype.getOuterLength = function() {
|
|
return this.length + ( this.isWrapped() ? 2 : 0 );
|
|
};
|
|
|
|
/**
|
|
* Sets the inner length.
|
|
*
|
|
* @method
|
|
* @param {Integer} length Length of content
|
|
* @throws Invalid content length error if length is less than 0
|
|
* @emits lengthChange (diff)
|
|
* @emits update
|
|
*/
|
|
ve.dm.Node.prototype.setLength = function( length ) {
|
|
if ( length < 0 ) {
|
|
throw 'Length cannot be negative';
|
|
}
|
|
// Compute length adjustment from old length
|
|
var diff = length - this.length;
|
|
// Set new length
|
|
this.length = length;
|
|
// Adjust the parent's length
|
|
if ( this.parent ) {
|
|
this.parent.adjustLength( diff );
|
|
}
|
|
// Emit events
|
|
this.emit( 'lengthChange', diff );
|
|
this.emit( 'update' );
|
|
};
|
|
|
|
/**
|
|
* Adjust the length.
|
|
*
|
|
* @method
|
|
* @param {Integer} adjustment Amount to adjust length by
|
|
* @throws Invalid adjustment error if resulting length is less than 0
|
|
* @emits lengthChange (diff)
|
|
* @emits update
|
|
*/
|
|
ve.dm.Node.prototype.adjustLength = function( adjustment ) {
|
|
this.setLength( this.length + adjustment );
|
|
};
|
|
|
|
/**
|
|
* Gets an element attribute value.
|
|
*
|
|
* @method
|
|
* @returns {Mixed} Value of attribute, or undefined if no such attribute exists
|
|
*/
|
|
ve.dm.Node.prototype.getAttribute = function( key ) {
|
|
return this.attributes[key];
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
ve.extendClass( ve.dm.Node, ve.Node );
|