2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model Document 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-04-27 02:44:44 +00:00
|
|
|
/**
|
2012-04-30 23:58:41 +00:00
|
|
|
* DataModel document.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-10-22 23:26:39 +00:00
|
|
|
* WARNING: The data parameter is passed by reference. Do not modify a data array after passing
|
|
|
|
* it to this constructor, and do not construct multiple Documents with the same data array. If you
|
|
|
|
* need to do these things, make a deep copy (ve.copyArray()) of the data array and operate on the
|
|
|
|
* copy.
|
2012-08-16 21:18:50 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
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
|
|
|
* @extends {ve.Document}
|
2012-04-27 02:44:44 +00:00
|
|
|
* @param {Array} data Linear model data to start with
|
2012-05-17 18:23:56 +00:00
|
|
|
* @param {ve.dm.Document} [parentDocument] Document to use as root for created nodes
|
2012-04-27 02:44:44 +00:00
|
|
|
*/
|
2012-09-06 23:15:55 +00:00
|
|
|
ve.dm.Document = function VeDmDocument( data, parentDocument ) {
|
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-05-17 18:23:56 +00:00
|
|
|
ve.Document.call( this, new ve.dm.DocumentNode() );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.parentDocument = parentDocument;
|
2012-10-22 23:26:39 +00:00
|
|
|
this.data = ve.isArray( data ) ? data : [];
|
2012-10-30 01:42:12 +00:00
|
|
|
// Sparse array containing the metadata for each offset
|
|
|
|
// Each element is either undefined, or an array of metadata elements
|
|
|
|
// Because the indexes in the metadata array represent offsets in the data array, the
|
|
|
|
// metadata array has one element more than the data array.
|
|
|
|
this.metadata = new Array( this.data.length + 1 );
|
2012-05-17 18:23:56 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
/*
|
|
|
|
* Build a tree of nodes and nodes that will be added to them after a full scan is complete,
|
|
|
|
* then from the bottom up add nodes to their potential parents. This avoids massive length
|
2012-06-13 23:16:07 +00:00
|
|
|
* updates being broadcast upstream constantly while building is underway.
|
2012-05-17 18:23:56 +00:00
|
|
|
*/
|
2012-10-30 01:42:12 +00:00
|
|
|
var i, node, children, meta,
|
2012-08-02 18:46:13 +00:00
|
|
|
doc = parentDocument || this,
|
|
|
|
root = doc.getDocumentNode(),
|
2012-05-17 18:23:56 +00:00
|
|
|
textLength = 0,
|
|
|
|
inTextNode = false,
|
|
|
|
// Stack of stacks, each containing a
|
|
|
|
stack = [[this.documentNode], []],
|
|
|
|
currentStack = stack[1],
|
|
|
|
parentStack = stack[0],
|
|
|
|
currentNode = this.documentNode;
|
2012-08-02 18:46:13 +00:00
|
|
|
this.documentNode.setDocument( doc );
|
|
|
|
this.documentNode.setRoot( root );
|
2012-10-30 01:42:12 +00:00
|
|
|
for ( i = 0; i < this.data.length; i++ ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
// Infer that if an item in the linear model has a type attribute than it must be an element
|
|
|
|
if ( this.data[i].type === undefined ) {
|
|
|
|
// Text node opening
|
|
|
|
if ( !inTextNode ) {
|
|
|
|
// Create a lengthless text node
|
|
|
|
node = new ve.dm.TextNode();
|
|
|
|
// Set the root pointer now, to prevent cascading updates
|
|
|
|
node.setRoot( root );
|
|
|
|
// Put the node on the current inner stack
|
|
|
|
currentStack.push( node );
|
|
|
|
currentNode = node;
|
|
|
|
// Set a flag saying we're inside a text node
|
|
|
|
inTextNode = true;
|
|
|
|
}
|
|
|
|
// Track the length
|
|
|
|
textLength++;
|
|
|
|
} else {
|
2012-10-30 01:42:12 +00:00
|
|
|
if ( this.data[i].type === 'metaInline' || this.data[i].type === 'metaBlock' ) {
|
|
|
|
// Metadata
|
|
|
|
// Splice the meta element and its closing out of the linmod
|
|
|
|
meta = this.data[i];
|
|
|
|
this.spliceData( i, 2 );
|
|
|
|
// Put the metadata in the meta-linmod
|
|
|
|
if ( !this.metadata[i] ) {
|
|
|
|
this.metadata[i] = [];
|
|
|
|
}
|
|
|
|
this.metadata[i].push( meta );
|
|
|
|
// Make sure the loop doesn't skip the next element
|
|
|
|
i--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-17 18:23:56 +00:00
|
|
|
// Text node closing
|
|
|
|
if ( inTextNode ) {
|
|
|
|
// Finish the text node by setting the length
|
|
|
|
currentNode.setLength( textLength );
|
|
|
|
// Put the state variables back as they were
|
|
|
|
currentNode = parentStack[parentStack.length - 1];
|
|
|
|
inTextNode = false;
|
|
|
|
textLength = 0;
|
|
|
|
}
|
|
|
|
// Element open/close
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( this.data[i].type.charAt( 0 ) !== '/' ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
// Branch or leaf node opening
|
|
|
|
// Create a childless node
|
2012-08-16 17:53:33 +00:00
|
|
|
node = ve.dm.nodeFactory.create( this.data[i].type, [],
|
2012-11-23 23:16:08 +00:00
|
|
|
this.data[i]
|
2012-08-16 17:53:33 +00:00
|
|
|
);
|
2012-05-17 18:23:56 +00:00
|
|
|
// Set the root pointer now, to prevent cascading updates
|
|
|
|
node.setRoot( root );
|
|
|
|
// Put the childless node on the current inner stack
|
|
|
|
currentStack.push( node );
|
2012-05-31 22:20:58 +00:00
|
|
|
if ( ve.dm.nodeFactory.canNodeHaveChildren( node.getType() ) ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
// Create a new inner stack for this node
|
|
|
|
parentStack = currentStack;
|
|
|
|
currentStack = [];
|
|
|
|
stack.push( currentStack );
|
|
|
|
}
|
|
|
|
currentNode = node;
|
|
|
|
} else {
|
|
|
|
// Branch or leaf node closing
|
2012-05-31 22:20:58 +00:00
|
|
|
if ( ve.dm.nodeFactory.canNodeHaveChildren( currentNode.getType() ) ) {
|
2012-05-17 18:27:25 +00:00
|
|
|
// Pop this node's inner stack from the outer stack. It'll have all of the
|
|
|
|
// node's child nodes fully constructed
|
2012-05-17 18:23:56 +00:00
|
|
|
children = stack.pop();
|
|
|
|
currentStack = parentStack;
|
|
|
|
parentStack = stack[stack.length - 2];
|
|
|
|
if ( !parentStack ) {
|
|
|
|
// This can only happen if we got unbalanced data
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Unbalanced input passed to document' );
|
2012-05-17 18:23:56 +00:00
|
|
|
}
|
2012-06-06 22:33:42 +00:00
|
|
|
|
2012-10-12 18:04:15 +00:00
|
|
|
if ( children.length === 0 &&
|
|
|
|
ve.dm.nodeFactory.canNodeContainContent(
|
|
|
|
currentNode.getType()
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Content nodes cannot be childless, add a zero-length text node
|
|
|
|
children.push( new ve.dm.TextNode( 0 ) );
|
|
|
|
}
|
2012-05-17 18:23:56 +00:00
|
|
|
// Attach the children to the node
|
|
|
|
ve.batchSplice( currentNode, 0, 0, children );
|
|
|
|
}
|
|
|
|
currentNode = parentStack[parentStack.length - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-31 13:17:23 +00:00
|
|
|
|
|
|
|
if ( inTextNode ) {
|
|
|
|
// Text node ended by end-of-input rather than by an element
|
|
|
|
currentNode.setLength( textLength );
|
|
|
|
// Don't bother updating currentNode et al, we don't use them below
|
|
|
|
}
|
2012-05-17 18:23:56 +00:00
|
|
|
// The end state is stack = [ [this.documentNode] [ array, of, its, children ] ]
|
|
|
|
// so attach all nodes in stack[1] to the root node
|
|
|
|
ve.batchSplice( this.documentNode, 0, 0, stack[1] );
|
2012-04-27 02:44:44 +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.Document, ve.Document );
|
|
|
|
|
2012-05-11 00:20:57 +00:00
|
|
|
/* Static methods */
|
|
|
|
|
2012-11-21 21:22:29 +00:00
|
|
|
/**
|
|
|
|
* Pattern that matches anything that's not considered part of a word.
|
|
|
|
*
|
|
|
|
* This is a very loose definition, it includes some punctuation that can occur around or inside of
|
|
|
|
* a word. This may need to be added to for some locales and perhaps made to be extendable for
|
|
|
|
* better internationalization support.
|
|
|
|
*
|
|
|
|
* Allowed characters:
|
|
|
|
* * Numbers and letters: a-z, A-Z, 0-9
|
|
|
|
* * Underscores and dashes: _, -
|
|
|
|
* * Brackets and parenthesis: (), []
|
|
|
|
* * Apostrophes and double quotes: ', "
|
|
|
|
*
|
|
|
|
* This pattern is tested against one character at a time.
|
|
|
|
*/
|
|
|
|
ve.dm.SurfaceFragment.wordBoundaryPattern = /[^\w'"-\(\)\[\]]+/;
|
|
|
|
|
2012-06-11 22:31:55 +00:00
|
|
|
/**
|
|
|
|
* Applies annotations to content data.
|
2012-06-13 23:06:21 +00:00
|
|
|
*
|
2012-06-11 22:31:55 +00:00
|
|
|
* This method modifies data in place.
|
2012-06-13 23:06:21 +00:00
|
|
|
*
|
2012-06-11 22:31:55 +00:00
|
|
|
* @method
|
|
|
|
* @param {Array} data Data to remove annotations from
|
2012-08-24 02:06:36 +00:00
|
|
|
* @param {ve.AnnotationSet} annotationSet Annotations to apply
|
2012-06-11 22:31:55 +00:00
|
|
|
*/
|
2012-08-24 02:06:36 +00:00
|
|
|
ve.dm.Document.addAnnotationsToData = function ( data, annotationSet ) {
|
2012-11-22 01:05:10 +00:00
|
|
|
if ( annotationSet.isEmpty() ) {
|
|
|
|
// Nothing to do
|
|
|
|
return;
|
|
|
|
}
|
2012-06-11 22:31:55 +00:00
|
|
|
// Apply annotations to data
|
2012-07-19 03:40:49 +00:00
|
|
|
for ( var i = 0; i < data.length; i++ ) {
|
2012-06-11 22:31:55 +00:00
|
|
|
if ( !ve.isArray( data[i] ) ) {
|
2012-08-24 02:06:36 +00:00
|
|
|
data[i] = [data[i], new ve.AnnotationSet()];
|
2012-06-11 22:31:55 +00:00
|
|
|
}
|
2012-08-24 02:06:36 +00:00
|
|
|
data[i][1].addSet( annotationSet );
|
2012-06-11 22:31:55 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-11 00:20:57 +00:00
|
|
|
/**
|
|
|
|
* Checks if content can be inserted at an offset in document data.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* This method assumes that any value that has a type property that's a string is an element object.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @example Content offsets:
|
|
|
|
* <heading> a </heading> <paragraph> b c <img> </img> </paragraph>
|
|
|
|
* . ^ ^ . ^ ^ ^ . ^ .
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @example Content offsets:
|
|
|
|
* <list> <listItem> </listItem> <list>
|
|
|
|
* . . . . .
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Document data
|
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} offset Document offset
|
2012-05-11 00:20:57 +00:00
|
|
|
* @returns {Boolean} Content can be inserted at offset
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.isContentOffset = function ( data, offset ) {
|
2012-05-11 00:20:57 +00:00
|
|
|
// Edges are never content
|
|
|
|
if ( offset === 0 || offset === data.length ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
var left = data[offset - 1],
|
|
|
|
right = data[offset],
|
2012-05-31 22:20:58 +00:00
|
|
|
factory = ve.dm.nodeFactory;
|
2012-05-11 00:20:57 +00:00
|
|
|
return (
|
|
|
|
// Data exists at offsets
|
|
|
|
( left !== undefined && right !== undefined ) &&
|
|
|
|
(
|
|
|
|
// If there's content on the left or the right of the offset than we are good
|
|
|
|
// <paragraph>|a|</paragraph>
|
|
|
|
( typeof left === 'string' || typeof right === 'string' ) ||
|
|
|
|
// Same checks but for annotated characters - isArray is slower, try it next
|
|
|
|
( ve.isArray( left ) || ve.isArray( right ) ) ||
|
|
|
|
// The most expensive test are last, these deal with elements
|
|
|
|
(
|
|
|
|
// Right of a leaf
|
|
|
|
// <paragraph><image></image>|</paragraph>
|
|
|
|
(
|
|
|
|
// Is an element
|
|
|
|
typeof left.type === 'string' &&
|
|
|
|
// Is a closing
|
|
|
|
left.type.charAt( 0 ) === '/' &&
|
|
|
|
// Is a leaf
|
2012-05-22 00:39:03 +00:00
|
|
|
factory.isNodeContent( left.type.substr( 1 ) )
|
2012-05-17 03:25:43 +00:00
|
|
|
) ||
|
2012-05-11 00:20:57 +00:00
|
|
|
// Left of a leaf
|
|
|
|
// <paragraph>|<image></image></paragraph>
|
|
|
|
(
|
|
|
|
// Is an element
|
|
|
|
typeof right.type === 'string' &&
|
|
|
|
// Is not a closing
|
|
|
|
right.type.charAt( 0 ) !== '/' &&
|
|
|
|
// Is a leaf
|
2012-05-22 00:39:03 +00:00
|
|
|
factory.isNodeContent( right.type )
|
2012-05-11 00:20:57 +00:00
|
|
|
) ||
|
|
|
|
// Inside empty content branch
|
|
|
|
// <paragraph>|</paragraph>
|
|
|
|
(
|
|
|
|
// Inside empty element
|
|
|
|
'/' + left.type === right.type &&
|
|
|
|
// Both are content branches (right is the same type)
|
2012-05-22 00:39:03 +00:00
|
|
|
factory.canNodeContainContent( left.type )
|
2012-05-11 00:20:57 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-05-22 00:39:03 +00:00
|
|
|
* Checks if structure can be inserted at an offset in document data.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-23 21:56:33 +00:00
|
|
|
* If the {unrestricted} param is true than only offsets where any kind of element can be inserted
|
|
|
|
* will return true. This can be used to detect the difference between a location that a paragraph
|
|
|
|
* can be inserted, such as between two tables but not direclty inside a table.
|
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* This method assumes that any value that has a type property that's a string is an element object.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-23 21:56:33 +00:00
|
|
|
* @example Structural offsets (unrestricted = false):
|
|
|
|
* <heading> a </heading> <paragraph> b c <img> </img> </paragraph>
|
|
|
|
* ^ . . ^ . . . . . ^
|
|
|
|
*
|
|
|
|
* @example Structural offsets (unrestricted = true):
|
2012-05-11 00:20:57 +00:00
|
|
|
* <heading> a </heading> <paragraph> b c <img> </img> </paragraph>
|
|
|
|
* ^ . . ^ . . . . . ^
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-23 21:56:33 +00:00
|
|
|
* @example Structural offsets (unrestricted = false):
|
|
|
|
* <list> <listItem> </listItem> <list>
|
|
|
|
* ^ ^ ^ ^ ^
|
|
|
|
*
|
|
|
|
* @example Content branch offsets (unrestricted = true):
|
|
|
|
* <list> <listItem> </listItem> <list>
|
|
|
|
* ^ . ^ . ^
|
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Document data
|
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} offset Document offset
|
2012-05-24 00:43:10 +00:00
|
|
|
* @param {Boolean} [unrestricted] Only return true if any kind of element can be inserted at offset
|
2012-05-11 00:20:57 +00:00
|
|
|
* @returns {Boolean} Structure can be inserted at offset
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.isStructuralOffset = function ( data, offset, unrestricted ) {
|
2012-05-11 00:20:57 +00:00
|
|
|
// Edges are always structural
|
|
|
|
if ( offset === 0 || offset === data.length ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Offsets must be within range and both sides must be elements
|
|
|
|
var left = data[offset - 1],
|
|
|
|
right = data[offset],
|
2012-05-31 22:20:58 +00:00
|
|
|
factory = ve.dm.nodeFactory;
|
2012-05-11 00:20:57 +00:00
|
|
|
return (
|
|
|
|
(
|
|
|
|
left !== undefined &&
|
|
|
|
right !== undefined &&
|
|
|
|
typeof left.type === 'string' &&
|
|
|
|
typeof right.type === 'string'
|
|
|
|
) &&
|
|
|
|
(
|
|
|
|
// Right of a branch
|
|
|
|
// <list><listItem><paragraph>a</paragraph>|</listItem>|</list>|
|
|
|
|
(
|
|
|
|
// Is a closing
|
|
|
|
left.type.charAt( 0 ) === '/' &&
|
2012-05-25 20:19:56 +00:00
|
|
|
// Is a branch or non-content leaf
|
|
|
|
(
|
|
|
|
factory.canNodeHaveChildren( left.type.substr( 1 ) ) ||
|
|
|
|
!factory.isNodeContent( left.type.substr( 1 ) )
|
|
|
|
) &&
|
2012-05-23 21:56:33 +00:00
|
|
|
(
|
|
|
|
// Only apply this rule in unrestricted mode
|
|
|
|
!unrestricted ||
|
|
|
|
// Right of an unrestricted branch
|
|
|
|
// <list><listItem><paragraph>a</paragraph>|</listItem></list>|
|
|
|
|
// Both are non-content branches that can have any kind of child
|
|
|
|
factory.getParentNodeTypes( left.type.substr( 1 ) ) === null
|
|
|
|
)
|
2012-05-11 00:20:57 +00:00
|
|
|
) ||
|
2012-05-23 21:56:33 +00:00
|
|
|
// Left of a branch
|
2012-05-11 00:20:57 +00:00
|
|
|
// |<list>|<listItem>|<paragraph>a</paragraph></listItem></list>
|
|
|
|
(
|
|
|
|
// Is not a closing
|
|
|
|
right.type.charAt( 0 ) !== '/' &&
|
2012-05-25 20:19:56 +00:00
|
|
|
// Is a branch or non-content leaf
|
|
|
|
(
|
|
|
|
factory.canNodeHaveChildren( right.type ) ||
|
|
|
|
!factory.isNodeContent( right.type )
|
|
|
|
) &&
|
2012-05-23 21:56:33 +00:00
|
|
|
(
|
|
|
|
// Only apply this rule in unrestricted mode
|
|
|
|
!unrestricted ||
|
|
|
|
// Left of an unrestricted branch
|
|
|
|
// |<list><listItem>|<paragraph>a</paragraph></listItem></list>
|
|
|
|
// Both are non-content branches that can have any kind of child
|
|
|
|
factory.getParentNodeTypes( right.type ) === null
|
|
|
|
)
|
2012-05-11 00:20:57 +00:00
|
|
|
) ||
|
|
|
|
// Inside empty non-content branch
|
|
|
|
// <list>|</list> or <list><listItem>|</listItem></list>
|
|
|
|
(
|
|
|
|
// Inside empty element
|
|
|
|
'/' + left.type === right.type &&
|
|
|
|
// Both are non-content branches (right is the same type)
|
2012-05-23 21:56:33 +00:00
|
|
|
factory.canNodeHaveGrandchildren( left.type ) &&
|
|
|
|
(
|
|
|
|
// Only apply this rule in unrestricted mode
|
|
|
|
!unrestricted ||
|
|
|
|
// Both are non-content branches that can have any kind of child
|
|
|
|
factory.getChildNodeTypes( left.type ) === null
|
|
|
|
)
|
2012-05-11 00:20:57 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a data at a given offset is an element.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* This method assumes that any value that has a type property that's a string is an element object.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @example Element data:
|
|
|
|
* <heading> a </heading> <paragraph> b c <img></img> </paragraph>
|
|
|
|
* ^ . ^ ^ . . ^ ^ ^ .
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Document data
|
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} offset Document offset
|
2012-05-11 00:20:57 +00:00
|
|
|
* @returns {Boolean} Data at offset is an element
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.isElementData = function ( data, offset ) {
|
2012-05-11 00:20:57 +00:00
|
|
|
// Data exists at offset and appears to be an element
|
|
|
|
return data[offset] !== undefined && typeof data[offset].type === 'string';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks for elements in document data.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* This method assumes that any value that has a type property that's a string is an element object.
|
|
|
|
* Elements are discovered by iterating through the entire data array (backwards).
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-11 00:20:57 +00:00
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Document data
|
|
|
|
* @returns {Boolean} At least one elements exists in data
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.containsElementData = function ( data ) {
|
2012-05-11 00:20:57 +00:00
|
|
|
var i = data.length;
|
|
|
|
while ( i-- ) {
|
|
|
|
if ( data[i].type !== undefined ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2012-05-31 13:19:34 +00:00
|
|
|
/**
|
|
|
|
* Checks for non-content elements in document data.
|
|
|
|
*
|
|
|
|
* This method assumes that any value that has a type property that's a string is an element object.
|
|
|
|
* Elements are discovered by iterating through the entire data array.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {Array} data Document data
|
|
|
|
* @returns {Boolean} True if all elements in data are content elements
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.isContentData = function ( data ) {
|
2012-05-31 13:19:34 +00:00
|
|
|
for ( var i = 0, len = data.length; i < len; i++ ) {
|
|
|
|
if ( data[i].type !== undefined &&
|
|
|
|
data[i].type.charAt( 0 ) !== '/' &&
|
2012-05-31 22:20:58 +00:00
|
|
|
!ve.dm.nodeFactory.isNodeContent( data[i].type )
|
2012-05-31 13:19:34 +00:00
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-04-27 02:44:44 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-06-07 03:28:06 +00:00
|
|
|
/**
|
|
|
|
* Reverses a transaction's effects on the content data.
|
2012-06-13 23:06:21 +00:00
|
|
|
*
|
2012-06-07 03:28:06 +00:00
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Transaction}
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.rollback = function ( transaction ) {
|
2012-06-07 03:28:06 +00:00
|
|
|
ve.dm.TransactionProcessor.rollback( this, transaction );
|
|
|
|
};
|
|
|
|
|
2012-06-08 23:21:47 +00:00
|
|
|
/**
|
|
|
|
* Commits a transaction's effects on the content data.
|
2012-06-13 23:06:21 +00:00
|
|
|
*
|
2012-06-08 23:21:47 +00:00
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Transaction}
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.commit = function ( transaction ) {
|
2012-06-08 23:21:47 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( this, transaction );
|
|
|
|
};
|
|
|
|
|
2012-05-17 18:23:56 +00:00
|
|
|
/**
|
|
|
|
* Gets slice or copy of the document data.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Range} [range] Range of data to get, all data will be given by default
|
|
|
|
* @param {Boolean} [deep=false] Whether to return a deep copy (WARNING! This may be very slow)
|
|
|
|
* @returns {Array} Slice or copy of document data
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getData = function ( range, deep ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var end, data,
|
|
|
|
start = 0;
|
2012-05-17 18:23:56 +00:00
|
|
|
if ( range !== undefined ) {
|
|
|
|
range.normalize();
|
|
|
|
start = Math.max( 0, Math.min( this.data.length, range.start ) );
|
|
|
|
end = Math.max( 0, Math.min( this.data.length, range.end ) );
|
|
|
|
}
|
|
|
|
// IE work-around: arr.slice( 0, undefined ) returns [] while arr.slice( 0 ) behaves correctly
|
2012-08-02 18:46:13 +00:00
|
|
|
data = end === undefined ? this.data.slice( start ) : this.data.slice( start, end );
|
2012-05-17 18:23:56 +00:00
|
|
|
// Return either the slice or a deep copy of the slice
|
|
|
|
return deep ? ve.copyArray( data ) : data;
|
|
|
|
};
|
|
|
|
|
2012-08-02 00:59:38 +00:00
|
|
|
/**
|
|
|
|
* Gets the length of the document.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @returns {Number} Document data length
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.getLength = function () {
|
|
|
|
return this.data.length;
|
|
|
|
};
|
|
|
|
|
2012-10-30 01:42:12 +00:00
|
|
|
/**
|
|
|
|
* Splice data into or out of the linear model and update this.metadata accordingly.
|
|
|
|
*
|
|
|
|
* Always use this function, never use this.data.splice() directly, otherwise the linear model
|
|
|
|
* (this.data) and the meta-linmod (this.metadata) can get out of sync. The semantics of the
|
|
|
|
* parameters are identical to those of ve.batchSplice()
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @see {ve.batchSplice}
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.spliceData = function ( offset, remove, insert ) {
|
|
|
|
var spliced, reaped, reapedFlat, i;
|
|
|
|
insert = insert || [];
|
|
|
|
spliced = ve.batchSplice( this.data, offset, remove, insert );
|
|
|
|
reaped = ve.batchSplice( this.metadata, offset, remove, new Array( insert.length ) );
|
|
|
|
// reaped will be an array of arrays, flatten it
|
|
|
|
reapedFlat = [];
|
|
|
|
for ( i = 0; i < reaped.length; i++ ) {
|
|
|
|
if ( reaped[i] !== undefined ) {
|
|
|
|
reapedFlat = reapedFlat.concat( reaped[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add reaped metadata to the metadata that is now at offset (and used to be immediately
|
|
|
|
// after the removed data). Add it to the front, because it came from something that was
|
|
|
|
// before it.
|
|
|
|
if ( reapedFlat.length > 0 ) {
|
|
|
|
this.metadata[offset] = reapedFlat.concat( this.metadata[offset] || [] );
|
|
|
|
}
|
|
|
|
return spliced;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the full data, with the metadata spliced back in.
|
|
|
|
* @returns {Array} Data with metadata interleaved
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.getFullData = function () {
|
|
|
|
var result = [], i, j, len = this.data.length;
|
|
|
|
for ( i = 0; i <= len; i++ ) {
|
|
|
|
if ( this.metadata[i] ) {
|
|
|
|
for ( j = 0; j < this.metadata[i].length; j++ ) {
|
|
|
|
result.push( this.metadata[i][j] );
|
|
|
|
result.push( { 'type': '/' + this.metadata[i][j].type } );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( i < len ) {
|
|
|
|
result.push( this.data[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getNodeFromOffset = function ( offset ) {
|
2012-06-13 23:16:07 +00:00
|
|
|
// FIXME duplicated from ve.ce.Document
|
2012-10-01 18:23:39 +00:00
|
|
|
if ( offset < 0 || offset > this.data.length ) {
|
|
|
|
throw new Error( 've.dm.Document.getNodeFromOffset(): offset ' + offset + ' is out of bounds' );
|
|
|
|
}
|
2012-06-13 23:16:07 +00:00
|
|
|
var node = this.documentNode.getNodeFromOffset( offset );
|
|
|
|
if ( !node.canHaveChildren() ) {
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
return node;
|
2012-05-17 18:23:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the content data of a node.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Node} node Node to get content data for
|
|
|
|
* @returns {Array|null} List of content and elements inside node or null if node is not found
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getDataFromNode = function ( node ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
var length = node.getLength(),
|
|
|
|
offset = this.documentNode.getOffsetFromNode( node );
|
|
|
|
if ( offset >= 0 ) {
|
|
|
|
// XXX: If the node is wrapped in an element than we should increment the offset by one so
|
|
|
|
// we only return the content inside the element.
|
|
|
|
if ( node.isWrapped() ) {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
return this.data.slice( offset, offset + length );
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2012-09-07 01:04:51 +00:00
|
|
|
/**
|
|
|
|
* Returns plain text from a selected range.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Range} [range] Range of data to get the text of.
|
|
|
|
* @returns {String|''} Selected text or an empty string.
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.getText = function ( range ) {
|
|
|
|
var data = this.getData( range ),
|
|
|
|
str = '',
|
|
|
|
i;
|
|
|
|
for ( i = 0; i < data.length; i++ ) {
|
|
|
|
if ( typeof data[i] === 'string' ) {
|
|
|
|
str += data[i];
|
|
|
|
} else if ( ve.isArray( data[i] ) ) {
|
|
|
|
str += data[i][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
|
2012-05-17 18:23:56 +00:00
|
|
|
/**
|
|
|
|
* Gets a list of annotations that a given offset is covered by.
|
|
|
|
*
|
2012-08-24 02:06:36 +00:00
|
|
|
* This method returns a clone of the AnnotationSet in the linear model.
|
|
|
|
*
|
2012-05-17 18:23:56 +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} offset Offset to get annotations for
|
2012-08-24 02:06:36 +00:00
|
|
|
* @returns {ve.AnnotationSet} A set of all annotation objects offset is covered by
|
2012-05-17 18:23:56 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getAnnotationsFromOffset = function ( offset ) {
|
2012-10-01 18:29:46 +00:00
|
|
|
if ( offset < 0 || offset > this.data.length ) {
|
|
|
|
throw new Error( 've.dm.Document.getAnnotationsFromOffset: offset ' + offset + ' out of bounds' );
|
|
|
|
}
|
2012-05-21 19:02:04 +00:00
|
|
|
// Since annotations are not stored on a closing leaf node,
|
|
|
|
// rewind offset by 1 to return annotations for that structure
|
The great inspector and context rewrite of 2012
ve.AnnotationAction
* Added filter to the clearAll method to allow clearing all matching annotations only
ve.dm.Document
* Some variable renaming for consistency
ve.dm.SurfaceFragment
* Added truncateRange method
* Added annotation scope to expandRange method
* Added support for passing an annotation object into annotateContent method
* Switched to using name instead of type in annotateContent method to match the ve.dm.Annotation class
* Fixed logic in annotation mode of expandSelection so that expansion only takes place if the annotation is found
ve.ui.LinkInspector
* Moved most of the functionality elsewhere
* General reorganization
* Changed setOverlayPosition to accept 2 arguments instead of an object with 2 properties and renamed it to positionOverlayBelow
* Check for annotation object before extracting target information from it
* Initialize default target as empty string to avoid undefined being cast to a string and the default target becoming 'undefined'
icons.ai, inspector.png, inspector.svg
* Added generic inspector icon which will be used when a custom icon is not specified in future inspector subclasses
ve.ui.Inspector.Icons
* Added inspector icon
* Renamed clear icon to remove to match it's actual image
ve.ui.Context
* Greatly simplified the interface, reducing the number of methods by inlining a few things and combining others
* Now always listening to resize events on the window rather than only while document is focused
* Not listening to scroll events anymore, they used to affect the top/bottom positioning of the menu which we don't do anymore
* Lots of cleanup and reorganization
* No need to fallback to empty array since getInspectorsForAnnotations does so already
* Only consider fully-covered annotations for inspectors
ve.ui.Frame
* Simplified the constructor by introducing the createFrame method
* General cleanup
* Typo fixes
ve.ui.Inspector
* Generalized lots of functionality previously located in the link inspector class which will be useful to all inspectors (such as title, clear button, saving changes, etc.)
* Added setDisabled and isDisabled methods to manage CSS changes and avoid needing to check the CSS to determine the state of the inspector (storing state in the view is evil)
* Added getMatchingAnnotations method for convenience
* Added prepareSelection stub
* Lots of cleanup and documentation
* Type pattern is now defined in base class
* Added stubs for onOpen and onClose with documentation so that subclass authors know what these methods do
* Removed checks for onOpen or onClose methods since they are now noop stubs and are always there
* Added stub and removed checks for onRemove
* Made esc key close and accept - the illusion is supposed to be that the link changes are applied instantly, even though they are only updated when you close, so all closing except for when removing should apply changes - i.e. esc is now equal to back rather than being a special function that doesn't have an associated affordance
* Only consider fully-covered annotations when getting matching annotations
ve.ui.InspectorFactory
* Depending on type pattern now since it's always there
* Added getInspectorsForAnnotations method
* Return empty array if annotation set is empty
VisualEditor, VisualEditor.i18n
* Added default inspector message
Change-Id: I1cc008445bcbc8cba6754ca4b6ac0397575980d5
2012-11-16 20:40:05 +00:00
|
|
|
var annotations;
|
2012-05-21 19:02:04 +00:00
|
|
|
if (
|
|
|
|
ve.isPlainObject( this.data[offset] ) && // structural offset
|
2012-08-24 02:06:36 +00:00
|
|
|
this.data[offset].hasOwnProperty( 'type' ) && // just in case
|
2012-05-21 19:02:04 +00:00
|
|
|
this.data[offset].type.charAt( 0 ) === '/' && // closing offset
|
2012-05-31 22:20:58 +00:00
|
|
|
ve.dm.nodeFactory.canNodeHaveChildren(
|
2012-05-21 19:02:04 +00:00
|
|
|
this.data[offset].type.substr( 1 )
|
|
|
|
) === false // leaf node
|
2012-08-07 01:50:44 +00:00
|
|
|
) {
|
2012-05-21 19:02:04 +00:00
|
|
|
offset = this.getRelativeContentOffset( offset, -1 );
|
|
|
|
}
|
|
|
|
|
The great inspector and context rewrite of 2012
ve.AnnotationAction
* Added filter to the clearAll method to allow clearing all matching annotations only
ve.dm.Document
* Some variable renaming for consistency
ve.dm.SurfaceFragment
* Added truncateRange method
* Added annotation scope to expandRange method
* Added support for passing an annotation object into annotateContent method
* Switched to using name instead of type in annotateContent method to match the ve.dm.Annotation class
* Fixed logic in annotation mode of expandSelection so that expansion only takes place if the annotation is found
ve.ui.LinkInspector
* Moved most of the functionality elsewhere
* General reorganization
* Changed setOverlayPosition to accept 2 arguments instead of an object with 2 properties and renamed it to positionOverlayBelow
* Check for annotation object before extracting target information from it
* Initialize default target as empty string to avoid undefined being cast to a string and the default target becoming 'undefined'
icons.ai, inspector.png, inspector.svg
* Added generic inspector icon which will be used when a custom icon is not specified in future inspector subclasses
ve.ui.Inspector.Icons
* Added inspector icon
* Renamed clear icon to remove to match it's actual image
ve.ui.Context
* Greatly simplified the interface, reducing the number of methods by inlining a few things and combining others
* Now always listening to resize events on the window rather than only while document is focused
* Not listening to scroll events anymore, they used to affect the top/bottom positioning of the menu which we don't do anymore
* Lots of cleanup and reorganization
* No need to fallback to empty array since getInspectorsForAnnotations does so already
* Only consider fully-covered annotations for inspectors
ve.ui.Frame
* Simplified the constructor by introducing the createFrame method
* General cleanup
* Typo fixes
ve.ui.Inspector
* Generalized lots of functionality previously located in the link inspector class which will be useful to all inspectors (such as title, clear button, saving changes, etc.)
* Added setDisabled and isDisabled methods to manage CSS changes and avoid needing to check the CSS to determine the state of the inspector (storing state in the view is evil)
* Added getMatchingAnnotations method for convenience
* Added prepareSelection stub
* Lots of cleanup and documentation
* Type pattern is now defined in base class
* Added stubs for onOpen and onClose with documentation so that subclass authors know what these methods do
* Removed checks for onOpen or onClose methods since they are now noop stubs and are always there
* Added stub and removed checks for onRemove
* Made esc key close and accept - the illusion is supposed to be that the link changes are applied instantly, even though they are only updated when you close, so all closing except for when removing should apply changes - i.e. esc is now equal to back rather than being a special function that doesn't have an associated affordance
* Only consider fully-covered annotations when getting matching annotations
ve.ui.InspectorFactory
* Depending on type pattern now since it's always there
* Added getInspectorsForAnnotations method
* Return empty array if annotation set is empty
VisualEditor, VisualEditor.i18n
* Added default inspector message
Change-Id: I1cc008445bcbc8cba6754ca4b6ac0397575980d5
2012-11-16 20:40:05 +00:00
|
|
|
annotations = this.data[offset].annotations || this.data[offset][1];
|
|
|
|
return annotations ? annotations.clone() : new ve.AnnotationSet();
|
2012-05-17 18:23:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this offset contain the specified annotation
|
|
|
|
*
|
|
|
|
* @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} offset Offset to look at
|
2012-05-17 18:23:56 +00:00
|
|
|
* @param {Object} annotation Object to look for
|
|
|
|
* @returns {Boolean} Whether an offset contains the specified annotation
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.offsetContainsAnnotation = function ( offset, annotation ) {
|
2012-08-24 02:06:36 +00:00
|
|
|
// TODO inline this
|
|
|
|
return this.getAnnotationsFromOffset( offset ).contains( annotation );
|
2012-05-17 18:23:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the range of content surrounding a given offset that's covered by a given annotation.
|
|
|
|
*
|
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} offset Offset to begin looking forward and backward from
|
2012-05-17 18:23:56 +00:00
|
|
|
* @param {Object} annotation Annotation to test for coverage with
|
|
|
|
* @returns {ve.Range|null} Range of content covered by annotation, or null if offset is not covered
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.getAnnotatedRangeFromOffset = function ( offset, annotation ) {
|
|
|
|
var start = offset,
|
|
|
|
end = offset;
|
2012-05-17 18:27:25 +00:00
|
|
|
if ( this.offsetContainsAnnotation( offset, annotation ) === false ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
while ( start > 0 ) {
|
|
|
|
start--;
|
2012-05-17 18:27:25 +00:00
|
|
|
if ( this.offsetContainsAnnotation( start, annotation ) === false ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
start++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ( end < this.data.length ) {
|
2012-07-24 17:42:29 +00:00
|
|
|
if ( this.offsetContainsAnnotation( end, annotation ) === false ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
return new ve.Range( start, end );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the range of an annotation found in the selection range.
|
|
|
|
*
|
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} offset Offset to begin looking forward and backward from
|
2012-07-24 17:42:29 +00:00
|
|
|
* @param {Object} annotation Annotation to test for coverage with
|
|
|
|
* @returns {ve.Range|null} Range of content covered by annotation, or a copy of the range.
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getAnnotatedRangeFromSelection = function ( range, annotation ) {
|
2012-07-24 17:42:29 +00:00
|
|
|
var start = range.start,
|
|
|
|
end = range.end;
|
|
|
|
while ( start > 0 ) {
|
|
|
|
start--;
|
|
|
|
if ( this.offsetContainsAnnotation( start, annotation ) === false ) {
|
|
|
|
start++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ( end < this.data.length ) {
|
|
|
|
if ( this.offsetContainsAnnotation( end, annotation ) === false ) {
|
2012-05-17 18:23:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-05-17 20:17:18 +00:00
|
|
|
end++;
|
2012-05-17 18:23:56 +00:00
|
|
|
}
|
|
|
|
return new ve.Range( start, end );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-08-24 02:06:36 +00:00
|
|
|
* Gets an array of common annotations across a range.
|
2012-05-17 18:23:56 +00:00
|
|
|
*
|
|
|
|
* @method
|
2012-10-24 22:22:27 +00:00
|
|
|
* @param {ve.Range} range Range to get annotations for
|
2012-06-13 21:31:33 +00:00
|
|
|
* @param {Boolean} [all] Get all annotations found within the range, not just those that cover it
|
2012-10-24 22:22:27 +00:00
|
|
|
* @returns {ve.AnnotationSet} All annotation objects range is covered by
|
2012-05-17 18:23:56 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getAnnotationsFromRange = function ( range, all ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var i,
|
2012-06-13 21:31:33 +00:00
|
|
|
left,
|
2012-09-10 21:31:18 +00:00
|
|
|
right;
|
2012-08-02 18:46:13 +00:00
|
|
|
range.normalize();
|
2012-10-31 22:17:20 +00:00
|
|
|
// Look at left side of range for annotations
|
2012-06-13 21:31:33 +00:00
|
|
|
left = this.getAnnotationsFromOffset( range.start );
|
2012-10-31 22:17:20 +00:00
|
|
|
// Shortcut for single character and zero-length ranges
|
|
|
|
if ( range.getLength() === 0 || range.getLength() === 1 ) {
|
2012-06-13 21:31:33 +00:00
|
|
|
return left;
|
|
|
|
}
|
|
|
|
// Iterator over the range, looking for annotations, starting at the 2nd character
|
2012-08-02 18:46:13 +00:00
|
|
|
for ( i = range.start + 1; i < range.end; i++ ) {
|
2012-06-13 21:31:33 +00:00
|
|
|
// Skip non character data
|
2012-05-17 18:23:56 +00:00
|
|
|
if ( ve.dm.Document.isElementData( this.data, i ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-13 21:31:33 +00:00
|
|
|
// Current character annotations
|
|
|
|
right = this.getAnnotationsFromOffset( i );
|
2012-08-24 02:06:36 +00:00
|
|
|
if ( all && !right.isEmpty() ) {
|
|
|
|
left.addSet( right );
|
2012-06-13 21:31:33 +00:00
|
|
|
} else if ( !all ) {
|
|
|
|
// A non annotated character indicates there's no full coverage
|
2012-08-24 02:06:36 +00:00
|
|
|
if ( right.isEmpty() ) {
|
|
|
|
return new ve.AnnotationSet();
|
2012-06-13 21:31:33 +00:00
|
|
|
}
|
|
|
|
// Exclude annotations that are in left but not right
|
2012-08-24 02:06:36 +00:00
|
|
|
left.removeNotInSet( right );
|
2012-06-13 21:31:33 +00:00
|
|
|
// If we've reduced left down to nothing, just stop looking
|
2012-08-24 02:06:36 +00:00
|
|
|
if ( left.isEmpty() ) {
|
2012-06-13 21:31:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-05-17 18:23:56 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-13 21:31:33 +00:00
|
|
|
return left;
|
2012-05-17 18:23:56 +00:00
|
|
|
};
|
|
|
|
|
2012-07-03 20:28:10 +00:00
|
|
|
/**
|
|
|
|
* Returns ve.Range free of outer whitespace.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.Range} [range] Range of data to get, all data will be given by default
|
|
|
|
* @returns {Object} A new range if modified, otherwise returns passed range.
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.trimOuterSpaceFromRange = function ( range ) {
|
2012-07-03 20:28:10 +00:00
|
|
|
range.normalize();
|
2012-10-15 18:45:50 +00:00
|
|
|
var start = range.start,
|
2012-07-03 20:28:10 +00:00
|
|
|
end = range.end;
|
|
|
|
while ( this.data[start] === ' ' ) {
|
|
|
|
start++;
|
|
|
|
}
|
|
|
|
while ( this.data[end - 1] === ' ' ) {
|
|
|
|
end--;
|
|
|
|
}
|
|
|
|
return range.to < range.end ? new ve.Range( end, start ) : new ve.Range( start, end );
|
|
|
|
};
|
|
|
|
|
2012-04-27 02:44:44 +00:00
|
|
|
/**
|
2012-05-17 18:23:56 +00:00
|
|
|
* Rebuild one or more nodes following a change in linear model data.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* The data provided to this method may contain either one node or multiple sibling nodes, but it
|
|
|
|
* must be balanced and valid. Data provided to this method also may not contain any content at the
|
2012-06-13 23:16:07 +00:00
|
|
|
* top level. The tree is updated during this operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* Process:
|
|
|
|
* 1. Nodes between {index} and {index} + {numNodes} in {parent} will be removed
|
|
|
|
* 2. Data will be retrieved from this.data using {offset} and {newLength}
|
|
|
|
* 3. A document fragment will be generated from the retrieved data
|
2012-06-13 23:16:07 +00:00
|
|
|
* 4. The document fragment's nodes will be inserted into {parent} at {index}
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* Use cases:
|
|
|
|
* 1. Rebuild old nodes and offset data after a change to the linear model.
|
|
|
|
* 2. Insert new nodes and offset data after a insertion in the linear model.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* @param {ve.dm.Node} parent Parent of the node(s) being rebuilt
|
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} index Index within parent to rebuild or insert nodes
|
2012-09-17 13:30:50 +00:00
|
|
|
* - If {numNodes} == 0: Index to insert nodes at
|
|
|
|
* - If {numNodes} >= 1: Index of first node to rebuild
|
|
|
|
* @param {Number} numNodes Total number of nodes to rebuild
|
|
|
|
* - If {numNodes} == 0: Nothing will be rebuilt, but the node(s) built from data will be
|
|
|
|
* inserted before {index}. To insert nodes at the end, use number of children in 'parent'
|
|
|
|
* - If {numNodes} == 1: Only the node at {index} will be rebuilt
|
|
|
|
* - If {numNodes} > 1: The node at {index} and the next {numNodes-1} nodes will be rebuilt
|
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} offset Linear model offset to rebuild from
|
|
|
|
* @param {Number} newLength Length of data in linear model to rebuild or insert nodes for
|
2012-04-27 02:44:44 +00:00
|
|
|
* @returns {ve.dm.Node[]} Array containing the rebuilt/inserted nodes
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.rebuildNodes = function ( parent, index, numNodes, offset, newLength ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var // Get a slice of the document where it's been changed
|
|
|
|
data = this.data.slice( offset, offset + newLength ),
|
|
|
|
// Build document fragment from data
|
|
|
|
fragment = new ve.dm.Document( data, this ),
|
|
|
|
// Get generated child nodes from the document fragment
|
|
|
|
nodes = fragment.getDocumentNode().getChildren();
|
2012-04-27 02:44:44 +00:00
|
|
|
// Replace nodes in the model tree
|
2012-04-27 22:09:10 +00:00
|
|
|
ve.batchSplice( parent, index, numNodes, nodes );
|
2012-04-27 02:44:44 +00:00
|
|
|
// Return inserted nodes
|
|
|
|
return nodes;
|
|
|
|
};
|
|
|
|
|
2012-05-04 18:07:55 +00:00
|
|
|
/**
|
2012-05-24 00:43:10 +00:00
|
|
|
* Gets an offset a given distance from another using a callback to check if offsets are valid.
|
|
|
|
*
|
|
|
|
* - If {offset} is not already valid, one step will be used to move it to an valid one.
|
|
|
|
* - If {distance} is zero the result will either be {offset} if it's already valid or the
|
|
|
|
* nearest valid offset to the right if possible and to the left otherwise.
|
|
|
|
* - If {offset} is after the last valid offset and {distance} is >= 1, or if {offset} if
|
|
|
|
* before the first valid offset and {distance} <= 1 than the result will be the nearest
|
|
|
|
* valid offset in the opposite direction.
|
2012-05-24 18:43:25 +00:00
|
|
|
* - If the document does not contain a single valid offset the result will be -1
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 18:07: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} offset Offset to start from
|
|
|
|
* @param {Number} distance Number of valid offsets to move
|
2012-05-24 00:43:10 +00:00
|
|
|
* @param {Function} callback Function to call to check if an offset is valid which will be
|
|
|
|
* given two intital arguments of data and offset
|
|
|
|
* @param {Mixed} [...] Additional arguments to pass to the callback
|
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} Relative valid offset or -1 if there are no valid offsets in document
|
2012-05-04 18:07:55 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getRelativeOffset = function ( offset, distance, callback ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var i, direction,
|
|
|
|
args = Array.prototype.slice.call( arguments, 3 ),
|
|
|
|
start = offset,
|
|
|
|
steps = 0,
|
|
|
|
turnedAround = false;
|
2012-05-24 00:43:10 +00:00
|
|
|
// If offset is already a structural offset and distance is zero than no further work is needed,
|
|
|
|
// otherwise distance should be 1 so that we can get out of the invalid starting offset
|
2012-05-11 00:20:57 +00:00
|
|
|
if ( distance === 0 ) {
|
2012-05-24 00:43:10 +00:00
|
|
|
if ( callback.apply( window, [this.data, offset].concat( args ) ) ) {
|
|
|
|
return offset;
|
|
|
|
} else {
|
|
|
|
distance = 1;
|
|
|
|
}
|
2012-05-11 00:20:57 +00:00
|
|
|
}
|
2012-08-02 18:46:13 +00:00
|
|
|
// Initial values
|
|
|
|
direction = (
|
|
|
|
offset <= 0 ? 1 : (
|
|
|
|
offset >= this.data.length ? -1 : (
|
|
|
|
distance > 0 ? 1 : -1
|
2012-05-24 00:43:10 +00:00
|
|
|
)
|
2012-08-02 18:46:13 +00:00
|
|
|
)
|
|
|
|
);
|
2012-05-11 00:20:57 +00:00
|
|
|
distance = Math.abs( distance );
|
2012-08-02 18:46:13 +00:00
|
|
|
i = start + direction;
|
2012-05-24 18:43:25 +00:00
|
|
|
offset = -1;
|
2012-08-02 18:46:13 +00:00
|
|
|
// Iteration
|
2012-05-24 00:43:10 +00:00
|
|
|
while ( i >= 0 && i <= this.data.length ) {
|
|
|
|
if ( callback.apply( window, [this.data, i].concat( args ) ) ) {
|
2012-05-11 00:20:57 +00:00
|
|
|
steps++;
|
|
|
|
offset = i;
|
|
|
|
if ( distance === steps ) {
|
|
|
|
return offset;
|
|
|
|
}
|
2012-05-24 00:43:10 +00:00
|
|
|
} else if (
|
|
|
|
// Don't keep turning around over and over
|
|
|
|
!turnedAround &&
|
|
|
|
// Only turn around if not a single step could be taken
|
|
|
|
steps === 0 &&
|
|
|
|
// Only turn around if we're about to reach the edge
|
|
|
|
( ( direction < 0 && i === 0 ) || ( direction > 0 && i === this.data.length ) )
|
|
|
|
) {
|
|
|
|
// Start over going in the opposite direction
|
|
|
|
direction *= -1;
|
|
|
|
i = start;
|
|
|
|
distance = 1;
|
|
|
|
turnedAround = true;
|
2012-05-04 18:07:55 +00:00
|
|
|
}
|
2012-05-11 00:20:57 +00:00
|
|
|
i += direction;
|
2012-05-04 18:07:55 +00:00
|
|
|
}
|
2012-05-11 00:20:57 +00:00
|
|
|
return offset;
|
2012-05-04 18:07:55 +00:00
|
|
|
};
|
|
|
|
|
2012-05-24 00:43:10 +00:00
|
|
|
/**
|
|
|
|
* Gets a content offset a given distance forwards or backwards from another.
|
|
|
|
*
|
|
|
|
* This method is a wrapper around {getRelativeOffset}, using {ve.dm.Document.isContentOffset} as
|
|
|
|
* the offset validation callback.
|
|
|
|
*
|
|
|
|
* @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} offset Offset to start from
|
|
|
|
* @param {Number} distance Number of content offsets to move
|
|
|
|
* @returns {Number} Relative content offset or -1 if there are no valid offsets in document
|
2012-05-24 00:43:10 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getRelativeContentOffset = function ( offset, distance ) {
|
2012-05-24 00:43:10 +00:00
|
|
|
return this.getRelativeOffset( offset, distance, ve.dm.Document.isContentOffset );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the nearest content offset to a given offset.
|
|
|
|
*
|
|
|
|
* If the offset is already a valid offset, it will be returned unchanged. This method differs from
|
|
|
|
* calling {getRelativeContentOffset} with a zero length differece because the direction can be
|
|
|
|
* controlled without nessecarily moving the offset if it's already valid. Also, if the direction
|
|
|
|
* is 0 or undefined than nearest offsets will be found to the left and right and the one with the
|
|
|
|
* shortest distance will be used.
|
|
|
|
*
|
|
|
|
* This method is a wrapper around {getRelativeOffset}, using {ve.dm.Document.isContentOffset} as
|
|
|
|
* the offset validation callback.
|
|
|
|
*
|
|
|
|
* @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} offset Offset to start from
|
|
|
|
* @param {Number} [direction] Direction to prefer matching offset in, -1 for left and 1 for right
|
|
|
|
* @returns {Number} Nearest content offset or -1 if there are no valid offsets in document
|
2012-05-24 00:43:10 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getNearestContentOffset = function ( offset, direction ) {
|
2012-05-25 03:53:50 +00:00
|
|
|
if ( ve.dm.Document.isContentOffset( this.data, offset ) ) {
|
|
|
|
return offset;
|
|
|
|
}
|
2012-05-24 00:43:10 +00:00
|
|
|
if ( direction === undefined ) {
|
|
|
|
var left = this.getRelativeOffset( offset, -1, ve.dm.Document.isContentOffset ),
|
|
|
|
right = this.getRelativeOffset( offset, 1, ve.dm.Document.isContentOffset );
|
|
|
|
return offset - left < right - offset ? left : right;
|
|
|
|
} else {
|
|
|
|
return this.getRelativeOffset(
|
|
|
|
offset, direction > 0 ? 1 : -1, ve.dm.Document.isContentOffset
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a structural offset a given distance forwards or backwards from another.
|
|
|
|
*
|
|
|
|
* This method is a wrapper around {getRelativeOffset}, using {ve.dm.Document.isStructuralOffset} as
|
|
|
|
* the offset validation callback.
|
|
|
|
*
|
|
|
|
* @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} offset Offset to start from
|
|
|
|
* @param {Number} distance Number of structural offsets to move
|
2012-05-24 00:43:10 +00:00
|
|
|
* @param {Boolean} [unrestricted] Only return true if any kind of element can be inserted at offset
|
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} Relative structural offset
|
2012-05-24 00:43:10 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getRelativeStructuralOffset = function ( offset, distance, unrestricted ) {
|
2012-05-24 00:43:10 +00:00
|
|
|
// Optimization: start and end are always unrestricted structural offsets
|
|
|
|
if ( distance === 0 && ( offset === 0 || offset === this.data.length ) ) {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
return this.getRelativeOffset(
|
|
|
|
offset, distance, ve.dm.Document.isStructuralOffset, unrestricted
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the nearest structural offset to a given offset.
|
|
|
|
*
|
|
|
|
* If the offset is already a valid offset, it will be returned unchanged. This method differs from
|
|
|
|
* calling {getRelativeStructuralOffset} with a zero length differece because the direction can be
|
|
|
|
* controlled without nessecarily moving the offset if it's already valid. Also, if the direction
|
|
|
|
* is 0 or undefined than nearest offsets will be found to the left and right and the one with the
|
|
|
|
* shortest distance will be used.
|
|
|
|
*
|
|
|
|
* This method is a wrapper around {getRelativeOffset}, using {ve.dm.Document.isStructuralOffset} as
|
|
|
|
* the offset validation callback.
|
|
|
|
*
|
|
|
|
* @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} offset Offset to start from
|
|
|
|
* @param {Number} [direction] Direction to prefer matching offset in, -1 for left and 1 for right
|
2012-05-24 00:43:10 +00:00
|
|
|
* @param {Boolean} [unrestricted] Only return true if any kind of element can be inserted at offset
|
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} Nearest structural offset
|
2012-05-24 00:43:10 +00:00
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getNearestStructuralOffset = function ( offset, direction, unrestricted ) {
|
2012-05-25 03:53:50 +00:00
|
|
|
if ( ve.dm.Document.isStructuralOffset( this.data, offset, unrestricted ) ) {
|
|
|
|
return offset;
|
|
|
|
}
|
2012-05-24 00:43:10 +00:00
|
|
|
if ( !direction ) {
|
|
|
|
var left = this.getRelativeOffset(
|
|
|
|
offset, -1, ve.dm.Document.isStructuralOffset, unrestricted
|
|
|
|
),
|
|
|
|
right = this.getRelativeOffset(
|
|
|
|
offset, 1, ve.dm.Document.isStructuralOffset, unrestricted
|
|
|
|
);
|
|
|
|
return offset - left < right - offset ? left : right;
|
|
|
|
} else {
|
|
|
|
return this.getRelativeOffset(
|
|
|
|
offset, direction > 0 ? 1 : -1, ve.dm.Document.isStructuralOffset, unrestricted
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-21 21:22:29 +00:00
|
|
|
/**
|
|
|
|
* Gets the nearest word boundary.
|
|
|
|
*
|
|
|
|
* The offset will first be moved to the nearest content offset if it's not at one already. If a
|
|
|
|
* direction was given, the boundary will be found in that direction, otherwise both directions will
|
|
|
|
* be calculated and the one with the lowest distance from offset will be returned. Elements are
|
|
|
|
* always word boundaries. For more information about what is considered to be a word character,
|
|
|
|
* see {ve.dm.SurfaceFragment.wordPattern}.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Number} offset Offset to start from
|
|
|
|
* @param {Number} [direction] Direction to prefer matching offset in, -1 for left and 1 for right
|
|
|
|
* @returns {Number} Nearest word boundary
|
|
|
|
*/
|
|
|
|
ve.dm.Document.prototype.getNearestWordBoundary = function ( offset, direction ) {
|
|
|
|
var left, right, i, inc,
|
|
|
|
pattern = ve.dm.SurfaceFragment.wordBoundaryPattern,
|
|
|
|
data = this.data;
|
|
|
|
offset = this.getNearestContentOffset( offset );
|
|
|
|
if ( !direction ) {
|
|
|
|
left = this.getNearestWordBoundary( offset, -1 );
|
|
|
|
right = this.getNearestWordBoundary( offset, +1 );
|
|
|
|
return offset - left < right - offset ? left : right;
|
|
|
|
} else {
|
|
|
|
inc = direction > 0 ? 1 : -1;
|
|
|
|
i = offset + ( inc > 0 ? 0 : -1 );
|
|
|
|
do {
|
|
|
|
if ( data[i].type === undefined ) {
|
|
|
|
// Plain text extraction
|
|
|
|
if ( pattern.test( typeof data[i] === 'string' ? data[i] : data[i][0] ) ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ( data[i += inc] );
|
|
|
|
return i + ( inc > 0 ? 0 : 1 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-22 20:14:21 +00:00
|
|
|
// TODO this function needs more work but it seems to work, mostly
|
|
|
|
/**
|
|
|
|
* Fix up data so it can safely be inserted into the linear model at offset.
|
2012-09-17 13:30:50 +00:00
|
|
|
* @param {Array} data Snippet of linear model data to insert
|
|
|
|
* @param {Number} offset Offset in the linear model where the caller wants to insert data
|
2012-05-22 20:14:21 +00:00
|
|
|
* @returns {Array} A (possibly modified) copy of data
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.fixupInsertion = function ( data, offset ) {
|
2012-06-04 15:37:05 +00:00
|
|
|
var
|
|
|
|
// Array where we build the return value
|
|
|
|
newData = [],
|
|
|
|
|
|
|
|
// *** Stacks ***
|
|
|
|
// Array of element openings (object). Openings in data are pushed onto this stack
|
|
|
|
// when they are encountered and popped off when they are closed
|
|
|
|
openingStack = [],
|
2012-07-05 19:55:52 +00:00
|
|
|
// Array of node objects. Closings in data that close nodes that were
|
2012-06-04 15:37:05 +00:00
|
|
|
// not opened in data (i.e. were already in the document) are pushed onto this stack
|
|
|
|
// and popped off when balanced out by an opening in data
|
|
|
|
closingStack = [],
|
|
|
|
// Array of objects describing wrappers that need to be fixed up when a given
|
|
|
|
// element is closed.
|
|
|
|
// 'expectedType': closing type that triggers this fixup. Includes initial '/'
|
|
|
|
// 'openings': array of opening elements that should be closed (in reverse order)
|
|
|
|
// 'reopenElements': array of opening elements to insert (in reverse order)
|
|
|
|
fixupStack = [],
|
|
|
|
|
|
|
|
// *** State persisting across iterations of the outer loop ***
|
|
|
|
// The node (from the document) we're currently in. When in a node that was opened
|
|
|
|
// in data, this is set to its first ancestor that is already in the document
|
|
|
|
parentNode,
|
|
|
|
// The type of the node we're currently in, even if that node was opened within data
|
|
|
|
parentType,
|
2012-06-08 19:17:40 +00:00
|
|
|
// Whether we are currently in a text node
|
|
|
|
inTextNode,
|
2012-06-04 15:37:05 +00:00
|
|
|
|
|
|
|
// *** Temporary variables that do not persist across iterations ***
|
|
|
|
// The type of the node we're currently inserting. When the to-be-inserted node
|
|
|
|
// is wrapped, this is set to the type of the outer wrapper.
|
|
|
|
childType,
|
|
|
|
// Stores the return value of getParentNodeTypes( childType )
|
|
|
|
allowedParents,
|
|
|
|
// Stores the return value of getChildNodeTypes( parentType )
|
|
|
|
allowedChildren,
|
|
|
|
// Whether parentType matches allowedParents
|
|
|
|
parentsOK,
|
|
|
|
// Whether childType matches allowedChildren
|
|
|
|
childrenOK,
|
|
|
|
// Array of opening elements to insert (for wrapping the to-be-inserted element)
|
|
|
|
openings,
|
|
|
|
// Array of closing elements to insert (for splitting nodes)
|
|
|
|
closings,
|
|
|
|
// Array of opening elements matching the elements in closings (in the same order)
|
|
|
|
reopenElements,
|
|
|
|
|
|
|
|
// *** Other variables ***
|
|
|
|
// Used to store values popped from various stacks
|
|
|
|
popped,
|
|
|
|
// Loop variables
|
|
|
|
i, j;
|
2012-05-22 20:14:21 +00:00
|
|
|
|
2012-06-04 12:56:09 +00:00
|
|
|
/**
|
2012-06-04 15:37:05 +00:00
|
|
|
* Append a linear model element to newData and update the state.
|
|
|
|
*
|
|
|
|
* This function updates parentNode, parentType, openingStack and closingStack.
|
|
|
|
*
|
2012-06-04 12:56:09 +00:00
|
|
|
* @param {Object|Array|String} element Linear model element
|
2012-07-19 03:40:49 +00:00
|
|
|
* @param {Number} index Index in data that this element came from (for error reporting only)
|
2012-06-04 12:56:09 +00:00
|
|
|
*/
|
2012-05-22 20:14:21 +00:00
|
|
|
function writeElement( element, index ) {
|
2012-06-04 15:37:05 +00:00
|
|
|
var expectedType;
|
2012-07-05 19:55:52 +00:00
|
|
|
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( element.type !== undefined ) {
|
2012-07-05 19:55:52 +00:00
|
|
|
// Content, do nothing
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( element.type.charAt( 0 ) !== '/' ) {
|
|
|
|
// Opening
|
2012-07-05 19:55:52 +00:00
|
|
|
// Check if this opening balances an earlier closing of a node that was already in
|
|
|
|
// the document. This is only the case if openingStack is empty (otherwise we still
|
|
|
|
// have unclosed nodes from within data) and if this opening matches the top of
|
|
|
|
// closingStack
|
2012-07-19 03:40:49 +00:00
|
|
|
if ( openingStack.length === 0 && closingStack.length > 0 &&
|
2012-07-05 19:55:52 +00:00
|
|
|
closingStack[closingStack.length - 1].getType() === element.type
|
2012-07-19 03:40:49 +00:00
|
|
|
) {
|
|
|
|
// The top of closingStack is now balanced out, so remove it
|
2012-07-05 19:55:52 +00:00
|
|
|
// Also restore parentNode from closingStack. While this is technically not
|
|
|
|
// entirely accurate (the current node is a new node that's a sibling of this
|
|
|
|
// node), it's good enough for the purposes of this algorithm
|
|
|
|
parentNode = closingStack.pop();
|
2012-07-19 03:40:49 +00:00
|
|
|
} else {
|
|
|
|
// This opens something new, put it on openingStack
|
|
|
|
openingStack.push( element );
|
|
|
|
}
|
|
|
|
parentType = element.type;
|
2012-05-22 20:14:21 +00:00
|
|
|
} else {
|
2012-07-19 03:40:49 +00:00
|
|
|
// Closing
|
|
|
|
// Make sure that this closing matches the currently opened node
|
|
|
|
if ( openingStack.length > 0 ) {
|
2012-07-05 19:55:52 +00:00
|
|
|
// The opening was on openingStack, so we're closing a node that was opened
|
|
|
|
// within data. Don't track that on closingStack
|
2012-07-19 03:40:49 +00:00
|
|
|
expectedType = openingStack.pop().type;
|
|
|
|
} else {
|
2012-07-05 19:55:52 +00:00
|
|
|
// openingStack is empty, so we're closing a node that was already in the
|
|
|
|
// document. This means we have to reopen it later, so track this on
|
|
|
|
// closingStack
|
2012-07-19 03:40:49 +00:00
|
|
|
expectedType = parentNode.getType();
|
2012-07-05 19:55:52 +00:00
|
|
|
closingStack.push( parentNode );
|
2012-07-19 03:40:49 +00:00
|
|
|
parentNode = parentNode.getParent();
|
|
|
|
if ( !parentNode ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Inserted data is trying to close the root node ' +
|
2012-08-06 20:38:00 +00:00
|
|
|
'(at index ' + index + ')' );
|
2012-07-19 03:40:49 +00:00
|
|
|
}
|
2012-07-05 19:55:52 +00:00
|
|
|
parentType = expectedType;
|
2012-06-04 15:37:05 +00:00
|
|
|
|
2012-07-05 19:55:52 +00:00
|
|
|
// Validate
|
|
|
|
// FIXME this breaks certain input, should fix it up, not scream and die
|
|
|
|
if ( element.type !== '/' + expectedType ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Type mismatch, expected /' + expectedType +
|
2012-08-06 20:38:00 +00:00
|
|
|
' but got ' + element.type + ' (at index ' + index + ')' );
|
2012-07-05 19:55:52 +00:00
|
|
|
}
|
2012-07-19 03:40:49 +00:00
|
|
|
}
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
newData.push( element );
|
|
|
|
}
|
|
|
|
|
2012-06-04 15:37:05 +00:00
|
|
|
parentNode = this.getNodeFromOffset( offset );
|
2012-05-22 20:14:21 +00:00
|
|
|
parentType = parentNode.getType();
|
2012-06-08 19:17:40 +00:00
|
|
|
inTextNode = false;
|
2012-05-22 20:14:21 +00:00
|
|
|
for ( i = 0; i < data.length; i++ ) {
|
2012-06-08 19:17:40 +00:00
|
|
|
if ( inTextNode && data[i].type !== undefined ) {
|
|
|
|
// We're leaving a text node, process fixupStack if needed
|
|
|
|
// TODO duplicated code
|
2012-07-19 03:40:49 +00:00
|
|
|
if (
|
|
|
|
fixupStack.length > 0 &&
|
|
|
|
fixupStack[fixupStack.length - 1].expectedType === '/text'
|
|
|
|
) {
|
2012-06-08 19:17:40 +00:00
|
|
|
popped = fixupStack.pop();
|
|
|
|
// Go through these in reverse!
|
|
|
|
for ( j = popped.openings.length - 1; j >= 0; j-- ) {
|
|
|
|
writeElement( { 'type': '/' + popped.openings[j].type }, i );
|
|
|
|
}
|
|
|
|
for ( j = popped.reopenElements.length - 1; j >= 0; j-- ) {
|
|
|
|
writeElement( popped.reopenElements[j], i );
|
|
|
|
}
|
|
|
|
}
|
2012-07-19 03:40:49 +00:00
|
|
|
parentType = openingStack.length > 0 ?
|
|
|
|
openingStack[openingStack.length - 1] : parentNode.getType();
|
2012-06-08 19:17:40 +00:00
|
|
|
}
|
Remainder JSHint fixes on modules/ve/*
[jshint]
ce/ve.ce.Surface.js: line 670, col 9, Too many var statements.
ce/ve.ce.Surface.js: line 695, col 6, Missing semicolon.
ce/ve.ce.Surface.js: line 726, col 22, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 726, col 41, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 733, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 734, col 24, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 1013, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 1019, col 17, Too many var statements.
ce/ve.ce.Surface.js: line 1023, col 18, Too many ar statements.
ce/ve.ce.Surface.js: line 1027, col 13, Too many var statements.
dm/annotations/ve.dm.LinkAnnotation.js: line 70, col 52, Insecure '.'.
dm/ve.dm.Converter.js: line 383, col 29, Empty block.
dm/ve.dm.Converter.js: line 423, col 33, Empty block.
Commands:
* jshint .
* ack '(if|else|function|switch|for|while)\('
* Sublime Text 2:
Find(*): (if|else|function|switch|for|while)\(
Replace: $1 (
* ack ' ' -Q # double spaces, except in certain comments
Change-Id: I8e34bf2924bc8688fdf8acef08bbc4f6707e93be
2012-09-02 21:45:01 +00:00
|
|
|
if ( data[i].type === undefined || data[i].type.charAt( 0 ) !== '/' ) {
|
2012-06-04 12:56:09 +00:00
|
|
|
childType = data[i].type || 'text';
|
2012-05-22 20:14:21 +00:00
|
|
|
openings = [];
|
2012-06-04 12:56:09 +00:00
|
|
|
closings = [];
|
2012-06-04 15:37:05 +00:00
|
|
|
reopenElements = [];
|
2012-06-04 12:56:09 +00:00
|
|
|
// Opening or content
|
2012-07-05 19:55:52 +00:00
|
|
|
// Make sure that opening this element here does not violate the parent/children/content
|
|
|
|
// rules. If it does, insert stuff to fix it
|
2012-06-04 12:56:09 +00:00
|
|
|
|
2012-07-05 19:55:52 +00:00
|
|
|
// If this node is content, check that the containing node can contain content. If not,
|
|
|
|
// wrap in a paragraph
|
2012-06-04 12:56:09 +00:00
|
|
|
if ( ve.dm.nodeFactory.isNodeContent( childType ) &&
|
2012-06-04 15:37:05 +00:00
|
|
|
!ve.dm.nodeFactory.canNodeContainContent( parentType )
|
2012-06-04 12:56:09 +00:00
|
|
|
) {
|
|
|
|
childType = 'paragraph';
|
2012-06-08 19:17:40 +00:00
|
|
|
openings.unshift ( { 'type': 'paragraph' } );
|
2012-06-04 12:56:09 +00:00
|
|
|
}
|
|
|
|
|
2012-07-05 19:55:52 +00:00
|
|
|
// Check that this node is allowed to have the containing node as its parent. If not,
|
|
|
|
// wrap it until it's fixed
|
2012-05-22 20:14:21 +00:00
|
|
|
do {
|
2012-05-31 22:20:58 +00:00
|
|
|
allowedParents = ve.dm.nodeFactory.getParentNodeTypes( childType );
|
2012-05-22 20:14:21 +00:00
|
|
|
parentsOK = allowedParents === null ||
|
2012-08-11 08:14:56 +00:00
|
|
|
ve.indexOf( parentType, allowedParents ) !== -1;
|
2012-05-22 20:14:21 +00:00
|
|
|
if ( !parentsOK ) {
|
|
|
|
// We can't have this as the parent
|
|
|
|
if ( allowedParents.length === 0 ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Cannot insert ' + childType + ' because it ' +
|
2012-08-06 20:38:00 +00:00
|
|
|
' cannot have a parent (at index ' + i + ')' );
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
// Open an allowed node around this node
|
|
|
|
childType = allowedParents[0];
|
|
|
|
openings.unshift( { 'type': childType } );
|
|
|
|
}
|
|
|
|
} while ( !parentsOK );
|
2012-06-04 12:56:09 +00:00
|
|
|
|
2012-07-05 19:55:52 +00:00
|
|
|
// Check that the containing node can have this node as its child. If not, close nodes
|
|
|
|
// until it's fixed
|
2012-05-22 20:14:21 +00:00
|
|
|
do {
|
2012-05-31 22:20:58 +00:00
|
|
|
allowedChildren = ve.dm.nodeFactory.getChildNodeTypes( parentType );
|
2012-05-22 20:14:21 +00:00
|
|
|
childrenOK = allowedChildren === null ||
|
2012-08-11 08:14:56 +00:00
|
|
|
ve.indexOf( childType, allowedChildren ) !== -1;
|
2012-07-05 19:55:52 +00:00
|
|
|
// Also check if we're trying to insert structure into a node that has to contain
|
|
|
|
// content
|
2012-06-04 12:56:09 +00:00
|
|
|
childrenOK = childrenOK && !(
|
|
|
|
!ve.dm.nodeFactory.isNodeContent( childType ) &&
|
|
|
|
ve.dm.nodeFactory.canNodeContainContent( parentType )
|
|
|
|
);
|
2012-05-22 20:14:21 +00:00
|
|
|
if ( !childrenOK ) {
|
|
|
|
// We can't insert this into this parent
|
|
|
|
// Close the parent and try one level up
|
|
|
|
closings.push( { 'type': '/' + parentType } );
|
|
|
|
if ( openingStack.length > 0 ) {
|
2012-06-04 14:55:31 +00:00
|
|
|
popped = openingStack.pop();
|
|
|
|
parentType = popped.type;
|
2012-06-04 15:37:05 +00:00
|
|
|
reopenElements.push( ve.copyObject( popped ) );
|
2012-07-05 19:55:52 +00:00
|
|
|
// The opening was on openingStack, so we're closing a node that was opened
|
|
|
|
// within data. Don't track that on closingStack
|
2012-05-22 20:14:21 +00:00
|
|
|
} else {
|
2012-07-05 19:55:52 +00:00
|
|
|
// openingStack is empty, so we're closing a node that was already in the
|
|
|
|
// document. This means we have to reopen it later, so track this on
|
|
|
|
// closingStack
|
|
|
|
closingStack.push( parentNode );
|
2012-06-07 19:31:02 +00:00
|
|
|
reopenElements.push( parentNode.getClonedElement() );
|
2012-05-22 20:14:21 +00:00
|
|
|
parentNode = parentNode.getParent();
|
|
|
|
if ( !parentNode ) {
|
2012-08-08 17:48:53 +00:00
|
|
|
throw new Error( 'Cannot insert ' + childType + ' even ' +
|
2012-05-22 20:14:21 +00:00
|
|
|
' after closing all containing nodes ' +
|
2012-08-06 20:38:00 +00:00
|
|
|
'(at index ' + i + ')' );
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
parentType = parentNode.getType();
|
|
|
|
}
|
|
|
|
}
|
Remainder JSHint fixes on modules/ve/*
[jshint]
ce/ve.ce.Surface.js: line 670, col 9, Too many var statements.
ce/ve.ce.Surface.js: line 695, col 6, Missing semicolon.
ce/ve.ce.Surface.js: line 726, col 22, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 726, col 41, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 733, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 734, col 24, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 1013, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 1019, col 17, Too many var statements.
ce/ve.ce.Surface.js: line 1023, col 18, Too many ar statements.
ce/ve.ce.Surface.js: line 1027, col 13, Too many var statements.
dm/annotations/ve.dm.LinkAnnotation.js: line 70, col 52, Insecure '.'.
dm/ve.dm.Converter.js: line 383, col 29, Empty block.
dm/ve.dm.Converter.js: line 423, col 33, Empty block.
Commands:
* jshint .
* ack '(if|else|function|switch|for|while)\('
* Sublime Text 2:
Find(*): (if|else|function|switch|for|while)\(
Replace: $1 (
* ack ' ' -Q # double spaces, except in certain comments
Change-Id: I8e34bf2924bc8688fdf8acef08bbc4f6707e93be
2012-09-02 21:45:01 +00:00
|
|
|
} while ( !childrenOK );
|
2012-05-22 20:14:21 +00:00
|
|
|
|
|
|
|
for ( j = 0; j < closings.length; j++ ) {
|
2012-07-05 19:55:52 +00:00
|
|
|
// writeElement() would update openingStack/closingStack, but we've already done
|
|
|
|
// that for closings
|
2012-06-04 12:56:09 +00:00
|
|
|
newData.push( closings[j] );
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
for ( j = 0; j < openings.length; j++ ) {
|
|
|
|
writeElement( openings[j], i );
|
|
|
|
}
|
|
|
|
writeElement( data[i], i );
|
2012-06-04 12:56:09 +00:00
|
|
|
if ( data[i].type === undefined ) {
|
|
|
|
// Special treatment for text nodes
|
2012-06-08 19:17:40 +00:00
|
|
|
inTextNode = true;
|
2012-06-04 12:56:09 +00:00
|
|
|
if ( openings.length > 0 ) {
|
|
|
|
// We wrapped the text node, update parentType
|
|
|
|
parentType = childType;
|
2012-07-19 03:40:49 +00:00
|
|
|
fixupStack.push( {
|
|
|
|
'expectedType': '/text',
|
|
|
|
'openings': openings,
|
|
|
|
'reopenElements': reopenElements
|
|
|
|
} );
|
2012-06-04 12:56:09 +00:00
|
|
|
}
|
2012-07-05 19:55:52 +00:00
|
|
|
// If we didn't wrap the text node, then the node we're inserting into can have
|
|
|
|
// content, so we couldn't have closed anything
|
2012-06-04 12:56:09 +00:00
|
|
|
} else {
|
2012-07-19 03:40:49 +00:00
|
|
|
fixupStack.push( {
|
|
|
|
'expectedType': '/' + data[i].type,
|
|
|
|
'openings': openings,
|
|
|
|
'reopenElements': reopenElements
|
|
|
|
} );
|
2012-06-04 12:56:09 +00:00
|
|
|
parentType = data[i].type;
|
|
|
|
}
|
2012-05-22 20:14:21 +00:00
|
|
|
} else {
|
|
|
|
// Closing
|
|
|
|
writeElement( data[i], i );
|
2012-07-19 03:40:49 +00:00
|
|
|
// TODO don't close fixup stuff if the next thing immediately needs to be fixed up as
|
|
|
|
// well; instead, merge the two wrappers
|
|
|
|
if (
|
|
|
|
fixupStack.length > 0 &&
|
|
|
|
fixupStack[fixupStack.length - 1].expectedType === data[i].type
|
|
|
|
) {
|
2012-05-22 20:14:21 +00:00
|
|
|
popped = fixupStack.pop();
|
|
|
|
// Go through these in reverse!
|
|
|
|
for ( j = popped.openings.length - 1; j >= 0; j-- ) {
|
|
|
|
writeElement( { 'type': '/' + popped.openings[j].type }, i );
|
|
|
|
}
|
2012-06-04 15:37:05 +00:00
|
|
|
for ( j = popped.reopenElements.length - 1; j >= 0; j-- ) {
|
|
|
|
writeElement( popped.reopenElements[j], i );
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-19 03:40:49 +00:00
|
|
|
parentType = openingStack.length > 0 ?
|
|
|
|
openingStack[openingStack.length - 1] : parentNode.getType();
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 19:17:40 +00:00
|
|
|
if ( inTextNode ) {
|
|
|
|
// We're leaving a text node, process fixupStack if needed
|
|
|
|
// TODO duplicated code
|
2012-07-19 03:40:49 +00:00
|
|
|
if (
|
|
|
|
fixupStack.length > 0 &&
|
|
|
|
fixupStack[fixupStack.length - 1].expectedType === '/text'
|
|
|
|
) {
|
2012-06-08 19:17:40 +00:00
|
|
|
popped = fixupStack.pop();
|
|
|
|
// Go through these in reverse!
|
|
|
|
for ( j = popped.openings.length - 1; j >= 0; j-- ) {
|
|
|
|
writeElement( { 'type': '/' + popped.openings[j].type }, i );
|
|
|
|
}
|
|
|
|
for ( j = popped.reopenElements.length - 1; j >= 0; j-- ) {
|
|
|
|
writeElement( popped.reopenElements[j], i );
|
|
|
|
}
|
|
|
|
}
|
2012-07-19 03:40:49 +00:00
|
|
|
parentType = openingStack.length > 0 ?
|
|
|
|
openingStack[openingStack.length - 1] : parentNode.getType();
|
2012-06-08 19:17:40 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 20:14:21 +00:00
|
|
|
// Close unclosed openings
|
|
|
|
while ( openingStack.length > 0 ) {
|
|
|
|
popped = openingStack[openingStack.length - 1];
|
|
|
|
// writeElement() will perform the actual pop() that removes
|
|
|
|
// popped from openingStack
|
2012-06-04 14:55:31 +00:00
|
|
|
writeElement( { 'type': '/' + popped.type }, i );
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
// Re-open closed nodes
|
|
|
|
while ( closingStack.length > 0 ) {
|
|
|
|
popped = closingStack[closingStack.length - 1];
|
|
|
|
// writeElement() will perform the actual pop() that removes
|
|
|
|
// popped from closingStack
|
2012-07-05 19:55:52 +00:00
|
|
|
writeElement( { 'type': popped.getType() }, i );
|
2012-05-22 20:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return newData;
|
|
|
|
};
|
|
|
|
|
2012-06-07 20:17:46 +00:00
|
|
|
/**
|
|
|
|
* Get the linear model data for the given range, but fix up unopened closings and unclosed openings
|
|
|
|
* in the data snippet such that the returned snippet is balanced.
|
|
|
|
*
|
|
|
|
* @returns {Array} Balanced snippet of linear model data
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.Document.prototype.getBalancedData = function ( range ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
var first, last, firstNode, lastNode,
|
|
|
|
node = this.getNodeFromOffset( range.start ),
|
2012-06-07 20:17:46 +00:00
|
|
|
selection = this.selectNodes( range, 'siblings' ),
|
|
|
|
addOpenings = [],
|
|
|
|
addClosings = [];
|
|
|
|
if ( selection.length === 0 ) {
|
2012-08-02 18:46:13 +00:00
|
|
|
return [];
|
2012-06-07 20:17:46 +00:00
|
|
|
}
|
|
|
|
if ( selection.length === 1 && selection[0].range.equals( range ) ) {
|
|
|
|
// Nothing to fix up
|
|
|
|
return this.data.slice( range.start, range.end );
|
|
|
|
}
|
|
|
|
|
2012-08-02 18:46:13 +00:00
|
|
|
first = selection[0];
|
|
|
|
last = selection[selection.length - 1];
|
|
|
|
firstNode = first.node;
|
|
|
|
lastNode = last.node;
|
2012-06-07 20:17:46 +00:00
|
|
|
while ( !firstNode.isWrapped() ) {
|
|
|
|
firstNode = firstNode.getParent();
|
|
|
|
}
|
|
|
|
while ( !lastNode.isWrapped() ) {
|
|
|
|
lastNode = lastNode.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( first.range ) {
|
Remainder JSHint fixes on modules/ve/*
[jshint]
ce/ve.ce.Surface.js: line 670, col 9, Too many var statements.
ce/ve.ce.Surface.js: line 695, col 6, Missing semicolon.
ce/ve.ce.Surface.js: line 726, col 22, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 726, col 41, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 733, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 734, col 24, Expected '===' and instead saw '=='.
ce/ve.ce.Surface.js: line 1013, col 13, Too many var statements.
ce/ve.ce.Surface.js: line 1019, col 17, Too many var statements.
ce/ve.ce.Surface.js: line 1023, col 18, Too many ar statements.
ce/ve.ce.Surface.js: line 1027, col 13, Too many var statements.
dm/annotations/ve.dm.LinkAnnotation.js: line 70, col 52, Insecure '.'.
dm/ve.dm.Converter.js: line 383, col 29, Empty block.
dm/ve.dm.Converter.js: line 423, col 33, Empty block.
Commands:
* jshint .
* ack '(if|else|function|switch|for|while)\('
* Sublime Text 2:
Find(*): (if|else|function|switch|for|while)\(
Replace: $1 (
* ack ' ' -Q # double spaces, except in certain comments
Change-Id: I8e34bf2924bc8688fdf8acef08bbc4f6707e93be
2012-09-02 21:45:01 +00:00
|
|
|
while ( true ) {
|
2012-06-07 20:17:46 +00:00
|
|
|
while ( !node.isWrapped() ) {
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
addOpenings.push( node.getClonedElement() );
|
|
|
|
if ( node === firstNode ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node = this.getNodeFromOffset( range.end );
|
|
|
|
if ( last !== first && last.range ) {
|
|
|
|
while ( true ) {
|
|
|
|
while ( !node.isWrapped() ) {
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
addClosings.push( { 'type': '/' + node.getType() } );
|
|
|
|
if ( node === lastNode ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node.getParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addOpenings.reverse()
|
|
|
|
.concat( this.data.slice( range.start, range.end ) )
|
|
|
|
.concat( addClosings );
|
|
|
|
};
|