2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model Node class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Generic DataModel node.
|
|
|
|
*
|
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
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @param {Number} [length] Length of content data in document
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {Object} [attributes] Reference to map of attribute key/value pairs
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-10-22 21:50:28 +00:00
|
|
|
ve.dm.Node = function VeDmNode( type, length, attributes ) {
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
// Parent constructor
|
2012-06-20 01:20:28 +00:00
|
|
|
ve.Node.call( this, type );
|
2011-11-03 19:01:55 +00:00
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
// Properties
|
2012-06-20 01:20:28 +00:00
|
|
|
this.length = length || 0;
|
|
|
|
this.attributes = attributes || {};
|
|
|
|
this.doc = undefined;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.inheritClass( ve.dm.Node, ve.Node );
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/* Methods */
|
2011-11-02 21:00:55 +00:00
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets a list of allowed child node types.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {String[]|null} List of node types allowed as children or null if any type is allowed
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getChildNodeTypes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.getChildNodeTypes( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-11-03 19:01:55 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets a list of allowed parent node types.
|
|
|
|
*
|
2011-11-03 19:01:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {String[]|null} List of node types allowed as parents or null if any type is allowed
|
2011-11-03 19:01:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getParentNodeTypes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.getParentNodeTypes( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if this node can have child nodes.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Node can have children
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.canHaveChildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.canNodeHaveChildren( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if this node can have child nodes which can also have child nodes.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Node can have grandchildren
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.canHaveGrandchildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.canNodeHaveGrandchildren( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if this node represents a wrapped element in the linear model.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Node represents a wrapped element
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.isWrapped = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.isNodeWrapped( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if this node can contain content.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Node can contain content
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.canContainContent = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.canNodeContainContent( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if this node is content.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Node is content
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.isContent = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.dm.nodeFactory.isNodeContent( this.type );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2012-11-07 20:03:58 +00:00
|
|
|
/**
|
|
|
|
* Checks if this node has significant whitespace. Can only be true if canContainContent is
|
|
|
|
* also true.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node has significant whitespace
|
|
|
|
*/
|
|
|
|
ve.dm.Node.prototype.hasSignificantWhitespace = function () {
|
|
|
|
return ve.dm.nodeFactory.doesNodeHaveSignificantWhitespace( this.type );
|
|
|
|
};
|
|
|
|
|
2012-10-24 21:53:34 +00:00
|
|
|
/**
|
|
|
|
* Checks if this node has an ancestor with given type and attributes.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node is content
|
|
|
|
*/
|
|
|
|
ve.dm.Node.prototype.hasMatchingAncestor = function ( type, attributes ) {
|
|
|
|
var key,
|
|
|
|
node = this;
|
|
|
|
// Traverse up to matching node
|
|
|
|
while ( node && node.getType() !== type ) {
|
|
|
|
node = node.getParent();
|
|
|
|
// Stop at root
|
|
|
|
if ( node === null ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check attributes
|
|
|
|
if ( attributes ) {
|
|
|
|
for ( key in attributes ) {
|
|
|
|
if ( node.getAttribute( key ) !== attributes[key] ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-11-02 21:00:55 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets the inner length.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @returns {Number} Length of the node's contents
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getLength = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.length;
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets the outer length, including any opening/closing elements.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @returns {Number} Length of the entire node
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getOuterLength = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.length + ( this.isWrapped() ? 2 : 0 );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets the range inside the node.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {ve.Range} Inner node range
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getRange = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
var offset = this.getOffset();
|
|
|
|
if ( this.isWrapped() ) {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
return new ve.Range( offset, offset + this.length );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets the range outside the node.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {ve.Range} Outer node range
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getOuterRange = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
var offset = this.getOffset();
|
|
|
|
return new ve.Range( offset, offset + this.getOuterLength() );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Sets the inner length.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @param {Number} length Length of content
|
2012-06-20 01:20:28 +00:00
|
|
|
* @throws Invalid content length error if length is less than 0
|
|
|
|
* @emits lengthChange (diff)
|
|
|
|
* @emits update
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.setLength = function ( length ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( length < 0 ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Length cannot be negative' );
|
2012-06-20 01:20:28 +00:00
|
|
|
}
|
|
|
|
// 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' );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Adjust the length.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @param {Number} adjustment Amount to adjust length by
|
2012-06-20 01:20:28 +00:00
|
|
|
* @throws Invalid adjustment error if resulting length is less than 0
|
|
|
|
* @emits lengthChange (diff)
|
|
|
|
* @emits update
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.adjustLength = function ( adjustment ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
this.setLength( this.length + adjustment );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets the offset of this node within the document.
|
|
|
|
*
|
|
|
|
* If this node has no parent than the result will always be 0.
|
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
Object management: Object create/inherit/clone utilities
* For the most common case:
- replace ve.extendClass with ve.inheritClass (chose slightly
different names to detect usage of the old/new one, and I
like 'inherit' better).
- move it up to below the constructor, see doc block for why.
* Cases where more than 2 arguments were passed to
ve.extendClass are handled differently depending on the case.
In case of a longer inheritance tree, the other arguments
could be omitted (like in "ve.ce.FooBar, ve.FooBar,
ve.Bar". ve.ce.FooBar only needs to inherit from ve.FooBar,
because ve.ce.FooBar inherits from ve.Bar).
In the case of where it previously had two mixins with
ve.extendClass(), either one becomes inheritClass and one
a mixin, both to mixinClass().
No visible changes should come from this commit as the
instances still all have the same visible properties in the
end. No more or less than before.
* Misc.:
- Be consistent in calling parent constructors in the
same order as the inheritance.
- Add missing @extends and @param documentation.
- Replace invalid {Integer} type hint with {Number}.
- Consistent doc comments order:
@class, @abstract, @constructor, @extends, @params.
- Fix indentation errors
A fairly common mistake was a superfluous space before the
identifier on the assignment line directly below the
documentation comment.
$ ack "^ [^*]" --js modules/ve
- Typo "Inhertiance" -> "Inheritance".
- Replacing the other confusing comment "Inheritance" (inside
the constructor) with "Parent constructor".
- Add missing @abstract for ve.ui.Tool.
- Corrected ve.FormatDropdownTool to ve.ui.FormatDropdownTool.js
- Add function names to all @constructor functions. Now that we
have inheritance it is important and useful to have these
functions not be anonymous.
Example of debug shot: http://cl.ly/image/1j3c160w3D45
Makes the difference between
< documentNode;
> ve_dm_DocumentNode
...
: ve_dm_BranchNode
...
: ve_dm_Node
...
: ve_dm_Node
...
: Object
...
without names (current situation):
< documentNode;
> Object
...
: Object
...
: Object
...
: Object
...
: Object
...
though before this commit, it really looks like this
(flattened since ve.extendClass really did a mixin):
< documentNode;
> Object
...
...
...
Pattern in Sublime (case-sensitive) to find nameless
constructor functions:
"^ve\..*\.([A-Z])([^\.]+) = function \("
Change-Id: Iab763954fb8cf375900d7a9a92dec1c755d5407e
2012-09-05 06:07:47 +00:00
|
|
|
* @returns {Number} Offset of node
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getOffset = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.root === this ? 0 : this.root.getOffsetFromNode( this );
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an element attribute value.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-11-02 21:00:55 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Mixed} Value of attribute, or undefined if no such attribute exists
|
2011-11-02 21:00:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getAttribute = function ( key ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.attributes[key];
|
2011-11-02 21:00:55 +00:00
|
|
|
};
|
|
|
|
|
2011-12-05 19:41:04 +00:00
|
|
|
/**
|
2012-10-24 22:22:27 +00:00
|
|
|
* Gets a reference to this node's attributes object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-12-05 19:41:04 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Object} Attributes object (by reference)
|
2011-12-05 19:41:04 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getAttributes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.attributes;
|
2011-12-05 19:41:04 +00:00
|
|
|
};
|
|
|
|
|
2012-10-24 21:53:57 +00:00
|
|
|
/**
|
|
|
|
* Checks if this node has certain attributes.
|
|
|
|
*
|
|
|
|
* If an array of keys is provided only the presence of the attributes will be checked. If an object
|
|
|
|
* with keys and values is provided both the presence of the attributes and their values will be
|
|
|
|
* checked. Comparison of values is done by casting to strings unless the strict argument is used.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {String[]|Object} attributes Array of keys or object of keys and values
|
|
|
|
* @param {Boolean} strict Use strict comparison when checking if values match
|
|
|
|
* @returns {Boolean} Node has attributes
|
|
|
|
*/
|
|
|
|
ve.dm.Node.prototype.hasAttributes = function ( attributes, strict ) {
|
|
|
|
var key, i, len;
|
|
|
|
if ( ve.isPlainObject( attributes ) ) {
|
|
|
|
// Node must have all the required attributes
|
|
|
|
for ( key in attributes ) {
|
|
|
|
if (
|
|
|
|
!( key in this.attributes ) ||
|
|
|
|
( strict ?
|
|
|
|
attributes[key] !== this.attributes[key] :
|
|
|
|
String( attributes[key] ) !== String( this.attributes[key] )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ( ve.isArray( attributes ) ) {
|
|
|
|
for ( i = 0, len = attributes.length; i < len; i++ ) {
|
|
|
|
if ( !( attributes[i] in this.attributes ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2011-12-05 19:41:04 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Get a clone of the linear model element for this node. The attributes object is deep-copied.
|
|
|
|
*
|
|
|
|
* @returns {Object} Element object with 'type' and (optionally) 'attributes' fields
|
2011-12-05 19:41:04 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.getClonedElement = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
var retval = { 'type': this.type };
|
|
|
|
if ( !ve.isEmptyObject( this.attributes ) ) {
|
|
|
|
retval.attributes = ve.copyObject( this.attributes );
|
2011-12-05 19:41:04 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
return retval;
|
2012-07-19 03:40:49 +00:00
|
|
|
};
|
2011-12-05 19:41:04 +00:00
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if this node can be merged with another.
|
|
|
|
*
|
|
|
|
* For two nodes to be mergeable, this node and the given node must either be the same node or:
|
|
|
|
* - Have the same type
|
|
|
|
* - Have the same depth
|
|
|
|
* - Have similar ancestory (each node upstream must have the same type)
|
|
|
|
*
|
2011-12-05 19:41:04 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {ve.dm.Node} node Node to consider merging with
|
|
|
|
* @returns {Boolean} Nodes can be merged
|
2011-12-05 19:41:04 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Node.prototype.canBeMergedWith = function ( node ) {
|
2012-07-20 00:24:54 +00:00
|
|
|
var n1 = this,
|
2012-06-20 01:20:28 +00:00
|
|
|
n2 = node;
|
|
|
|
// Move up from n1 and n2 simultaneously until we find a common ancestor
|
|
|
|
while ( n1 !== n2 ) {
|
|
|
|
if (
|
|
|
|
// Check if we have reached a root (means there's no common ancestor or unequal depth)
|
|
|
|
( n1 === null || n2 === null ) ||
|
|
|
|
// Ensure that types match
|
|
|
|
n1.getType() !== n2.getType()
|
|
|
|
) {
|
|
|
|
return false;
|
2011-12-05 19:41:04 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
// Move up
|
|
|
|
n1 = n1.getParent();
|
|
|
|
n2 = n2.getParent();
|
2011-12-05 19:41:04 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
return true;
|
2011-12-05 19:41:04 +00:00
|
|
|
};
|