2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor content editable 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
|
|
|
|
*/
|
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Generic ContentEditable node.
|
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @class
|
|
|
|
* @abstract
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.Node}
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {String} type Symbolic name of node type
|
2012-03-08 12:27:02 +00:00
|
|
|
* @param {ve.dm.Node} model Model to observe
|
2012-06-20 01:20:28 +00:00
|
|
|
* @param {jQuery} [$element] Element to use as a container
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-09-06 23:15:55 +00:00
|
|
|
ve.ce.Node = function VeCeNode( type, model, $element ) {
|
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 );
|
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
// Properties
|
|
|
|
this.model = model;
|
2012-08-28 22:06:34 +00:00
|
|
|
this.$ = $element || $( '<div>' );
|
2012-06-20 01:20:28 +00:00
|
|
|
this.parent = null;
|
|
|
|
|
|
|
|
this.$.data( 'node', this );
|
2012-11-14 22:21:36 +00:00
|
|
|
|
|
|
|
// Walk through node model attributes and pick just the HTML ones,
|
|
|
|
// then apply them to the DOM element
|
|
|
|
var attributes = this.model.getAttributes(),
|
|
|
|
attribute;
|
|
|
|
for ( attribute in attributes ) {
|
|
|
|
if ( attribute.indexOf( 'html/' ) === 0 ) {
|
|
|
|
this.$.attr( attribute.substr( 5 ), attributes[attribute] );
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 12:27:02 +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.ce.Node, ve.Node );
|
|
|
|
|
2012-11-09 22:02:41 +00:00
|
|
|
/* Static Memebers */
|
|
|
|
|
|
|
|
ve.ce.Node.static = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Template for shield elements.
|
|
|
|
*
|
|
|
|
* Uses data URI to inject a 1x1 transparent GIF image into the DOM.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.ce.Node.static.$shieldTemplate = $(
|
|
|
|
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" ' +
|
|
|
|
'class="ve-ce-node-shield">'
|
|
|
|
);
|
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of allowed child node types.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {String[]|null} List of node types allowed as children or null if any type is allowed
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getChildNodeTypes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getChildNodeTypes();
|
2012-04-05 21:31:59 +00:00
|
|
|
};
|
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets a list of allowed parent node types.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
2012-03-08 12:27:02 +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
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getParentNodeTypes = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getParentNodeTypes();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if model is for a node that can have children.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Model node can have children
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveChildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canHaveChildren();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if model is for a node that can have children.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Model node can have children
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveChildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canHaveChildren();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Checks if model is for a node that can have grandchildren.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @method
|
2012-06-20 01:20:28 +00:00
|
|
|
* @returns {Boolean} Model node can have grandchildren
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveGrandchildren = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canHaveGrandchildren();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if model is for a wrapped element.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Model node is a wrapped element
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.isWrapped = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.isWrapped();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this node can contain content.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node can contain content
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canContainContent = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.canContainContent();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this node is content.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node is content
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.isContent = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.isContent();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-07-20 00:45:41 +00:00
|
|
|
* Checks if this node can have a slug before it
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
2012-07-20 00:45:41 +00:00
|
|
|
* @returns {Boolean} Whether the node can have a slug before it
|
2012-06-20 01:20:28 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canHaveSlugBefore = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return !this.canContainContent() && this.getParentNodeTypes() === null && this.type !== 'text';
|
|
|
|
};
|
|
|
|
|
2012-07-20 00:45:41 +00:00
|
|
|
/**
|
|
|
|
* Checks if this node can have a slug after it
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Whether the node can have a slug after it
|
|
|
|
*/
|
|
|
|
ve.ce.Node.prototype.canHaveSlugAfter = ve.ce.Node.prototype.canHaveSlugBefore;
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* Gets model length.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
|
|
|
* @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} Model length
|
2012-06-20 01:20:28 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getLength = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getLength();
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-06-20 01:20:28 +00:00
|
|
|
* Gets model outer length.
|
|
|
|
*
|
|
|
|
* This method passes through to the model.
|
|
|
|
*
|
2012-03-08 12:27:02 +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} Model outer length
|
2012-03-08 12:27:02 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getOuterLength = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return this.model.getOuterLength();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if this node can be split.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Boolean} Node can be split
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.canBeSplit = function () {
|
2012-06-20 01:20:28 +00:00
|
|
|
return ve.ce.nodeFactory.canNodeBeSplit( this.type );
|
2012-03-08 12:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a reference to the model this node observes.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2012-03-08 12:27:02 +00:00
|
|
|
* @method
|
|
|
|
* @returns {ve.dm.Node} Reference to the model this node observes
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.prototype.getModel = function () {
|
2012-03-08 12:27:02 +00:00
|
|
|
return this.model;
|
|
|
|
};
|
|
|
|
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.ce.Node.getSplitableNode = function ( node ) {
|
2012-03-08 12:27:02 +00:00
|
|
|
var splitableNode = null;
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.Node.traverseUpstream( node, function ( node ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if ( node.canBeSplit() ) {
|
|
|
|
splitableNode = node;
|
2012-06-21 06:19:56 +00:00
|
|
|
return true;
|
2012-06-20 01:20:28 +00:00
|
|
|
} else {
|
2012-06-21 06:19:56 +00:00
|
|
|
return false;
|
2012-03-08 12:27:02 +00:00
|
|
|
}
|
|
|
|
} );
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2012-03-08 12:27:02 +00:00
|
|
|
return splitableNode;
|
|
|
|
};
|