2011-11-02 21:00:55 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.dm.Node object.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @class
|
2011-11-03 19:01:55 +00:00
|
|
|
* @abstract
|
2011-11-02 21:00:55 +00:00
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @extends {ve.Node}
|
2011-11-03 21:48:40 +00:00
|
|
|
* @param {String} type Symbolic name of node type
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {Object} element Element object in document data
|
|
|
|
* @param {Integer} [length] Length of content data in document
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node = function( type, element, length ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.Node.call( this );
|
2011-11-03 19:01:55 +00:00
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
// Properties
|
2011-11-03 19:01:55 +00:00
|
|
|
this.type = type;
|
2011-11-02 21:00:55 +00:00
|
|
|
this.parent = null;
|
|
|
|
this.root = this;
|
|
|
|
this.element = element || null;
|
2011-11-04 17:07:44 +00:00
|
|
|
this.contentLength = length || 0;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Abstract Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a view for this node.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @method
|
2012-03-06 22:39:43 +00:00
|
|
|
* @returns {ve.ce.Node} New item view associated with this model
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.createView = function() {
|
2011-11-02 21:00:55 +00:00
|
|
|
throw 'DocumentModelNode.createView not implemented in this subclass:' + this.constructor;
|
|
|
|
};
|
|
|
|
|
2011-11-03 19:01:55 +00:00
|
|
|
/**
|
|
|
|
* Gets a plain object representation of the document's data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Object} Plain object representation
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getPlainObject = function() {
|
2011-11-03 21:48:40 +00:00
|
|
|
throw 'DocumentModelNode.getPlainObject not implemented in this subclass:' + this.constructor;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-11-03 21:48:40 +00:00
|
|
|
/* Methods */
|
2011-11-02 21:00:55 +00:00
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets the content length.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @see {ve.Node.prototype.getContentLength}
|
2011-11-04 17:07:44 +00:00
|
|
|
* @returns {Integer} Length of content
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getContentLength = function() {
|
2011-11-04 17:07:44 +00:00
|
|
|
return this.contentLength;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets the element length.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @see {ve.Node.prototype.getElementLength}
|
2011-11-04 17:07:44 +00:00
|
|
|
* @returns {Integer} Length of content
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getElementLength = function() {
|
2011-11-04 17:07:44 +00:00
|
|
|
return this.contentLength + 2;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Sets the content length.
|
2011-11-03 21:48:40 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {Integer} contentLength Length of content
|
|
|
|
* @throws Invalid content length error if contentLength is less than 0
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.setContentLength = function( contentLength ) {
|
2011-11-04 17:07:44 +00:00
|
|
|
if ( contentLength < 0 ) {
|
|
|
|
throw 'Invalid content length error. Content length can not be less than 0.';
|
|
|
|
}
|
|
|
|
var diff = contentLength - this.contentLength;
|
|
|
|
this.contentLength = contentLength;
|
|
|
|
if ( this.parent ) {
|
|
|
|
this.parent.adjustContentLength( diff );
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Adjust the content length.
|
2011-11-03 21:48:40 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2011-11-04 17:07:44 +00:00
|
|
|
* @param {Integer} adjustment Amount to adjust content length by
|
|
|
|
* @throws Invalid adjustment error if resulting length is less than 0
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.adjustContentLength = function( adjustment, quiet ) {
|
2011-11-04 17:07:44 +00:00
|
|
|
this.contentLength += adjustment;
|
|
|
|
// Make sure the adjustment was sane
|
|
|
|
if ( this.contentLength < 0 ) {
|
|
|
|
// Reverse the adjustment
|
|
|
|
this.contentLength -= adjustment;
|
|
|
|
// Complain about it
|
|
|
|
throw 'Invalid adjustment error. Content length can not be less than 0.';
|
|
|
|
}
|
|
|
|
if ( this.parent ) {
|
|
|
|
this.parent.adjustContentLength( adjustment, true );
|
|
|
|
}
|
|
|
|
if ( !quiet ) {
|
|
|
|
this.emit( 'update' );
|
|
|
|
}
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attaches this node to another as a child.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.Node} parent Node to attach to
|
2011-11-02 21:00:55 +00:00
|
|
|
* @emits attach (parent)
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.attach = function( parent ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.emit( 'beforeAttach', parent );
|
|
|
|
this.parent = parent;
|
|
|
|
this.setRoot( parent.getRoot() );
|
|
|
|
this.emit( 'afterAttach', parent );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detaches this node from it's parent.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @emits detach
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.detach = function() {
|
2011-11-02 21:00:55 +00:00
|
|
|
this.emit( 'beforeDetach' );
|
|
|
|
this.parent = null;
|
|
|
|
this.clearRoot();
|
|
|
|
this.emit( 'afterDetach' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets a reference to this node's parent.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.dm.Node} Reference to this node's parent
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getParent = function() {
|
2011-11-04 17:07:44 +00:00
|
|
|
return this.parent;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Gets the root node in the tree this node is currently attached to.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.dm.Node} Root node
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getRoot = function() {
|
2011-11-04 17:07:44 +00:00
|
|
|
return this.root;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Sets the root node to this and all of it's children.
|
|
|
|
*
|
|
|
|
* This method is overridden by nodes with children.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.dm.Node} root Node to use as root
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.setRoot = function( root ) {
|
2011-11-04 17:07:44 +00:00
|
|
|
this.root = root;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-11-04 17:07:44 +00:00
|
|
|
* Clears the root node from this and all of it's children.
|
|
|
|
*
|
|
|
|
* This method is overridden by nodes with children.
|
2011-11-02 21:00:55 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.clearRoot = function() {
|
2011-11-04 17:07:44 +00:00
|
|
|
this.root = null;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the element object.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Object} Element object in linear data model
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getElement = function() {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.element;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the symbolic element type name.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {String} Symbolic name of element type
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getElementType = function() {
|
2011-11-10 15:13:57 +00:00
|
|
|
//return this.element.type;
|
|
|
|
// We can't use this.element.type because this.element may be null
|
|
|
|
// So this function now returns this.type and should really be called
|
|
|
|
// getType()
|
|
|
|
// TODO: Do we care?
|
|
|
|
return this.type;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an element attribute value.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Mixed} Value of attribute, or null if no such attribute exists
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getElementAttribute = function( key ) {
|
2011-11-03 19:01:55 +00:00
|
|
|
if ( this.element && this.element.attributes && key in this.element.attributes ) {
|
2011-11-02 21:00:55 +00:00
|
|
|
return this.element.attributes[key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2011-12-05 19:41:04 +00:00
|
|
|
/**
|
|
|
|
* Gets all element data, including the element opening, closing and it's contents.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Array} Element data
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getElementData = function() {
|
2011-12-05 19:41:04 +00:00
|
|
|
// Get reference to the document, which might be this node but otherwise should be this.root
|
|
|
|
var root = this.type === 'document' ?
|
|
|
|
this : ( this.root && this.root.type === 'document' ? this.root : null );
|
|
|
|
if ( root ) {
|
|
|
|
return root.getElementDataFromNode( this );
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets content data within a given range.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Range} [range] Range of content to get
|
2011-12-05 19:41:04 +00:00
|
|
|
* @returns {Array} Content data
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getContentData = function( range ) {
|
2011-12-05 19:41:04 +00:00
|
|
|
// Get reference to the document, which might be this node but otherwise should be this.root
|
|
|
|
var root = this.type === 'document' ?
|
|
|
|
this : ( this.root && this.root.type === 'document' ? this.root : null );
|
|
|
|
if ( root ) {
|
|
|
|
return root.getContentDataFromNode( this, range );
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets plain text version of the content within a specific range.
|
|
|
|
*
|
|
|
|
* Two newlines are inserted between leaf nodes.
|
|
|
|
*
|
|
|
|
* TODO: Maybe do something more adaptive with newlines
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Range} [range] Range of text to get
|
2011-12-05 19:41:04 +00:00
|
|
|
* @returns {String} Text within given range
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.Node.prototype.getContentText = function( range ) {
|
2011-12-05 19:41:04 +00:00
|
|
|
var content = this.getContentData( range );
|
|
|
|
// Copy characters
|
|
|
|
var text = '',
|
|
|
|
element = false;
|
|
|
|
for ( var i = 0, length = content.length; i < length; i++ ) {
|
2011-12-05 20:10:50 +00:00
|
|
|
if ( typeof content[i].type === 'string' ) {
|
2011-12-05 19:41:04 +00:00
|
|
|
if ( i ) {
|
|
|
|
element = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( element ) {
|
|
|
|
text += '\n\n';
|
|
|
|
element = false;
|
|
|
|
}
|
|
|
|
text += typeof content[i] === 'string' ? content[i] : content[i][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.dm.Node, ve.Node );
|