2012-04-27 02:44:44 +00:00
|
|
|
/**
|
2012-04-30 23:58:41 +00:00
|
|
|
* DataModel document fragment.
|
2012-05-11 17:29:09 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* @class
|
2012-05-10 04:11:09 +00:00
|
|
|
* @extends {ve.Document}
|
2012-04-27 02:44:44 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Array} data Linear model data to start with
|
2012-04-27 21:59:52 +00:00
|
|
|
* @param {ve.dm.Document} [parentDocument] Document to use as root for created nodes
|
2012-04-27 02:44:44 +00:00
|
|
|
*/
|
2012-04-27 21:59:52 +00:00
|
|
|
ve.dm.DocumentFragment = function( data, parentDocument ) {
|
2012-05-10 04:11:09 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.Document.call( this, new ve.dm.DocumentNode() );
|
|
|
|
|
2012-04-27 02:44:44 +00:00
|
|
|
// Properties
|
|
|
|
this.parentDocument = parentDocument;
|
|
|
|
this.data = data || [];
|
2012-04-27 21:59:52 +00:00
|
|
|
this.offsetMap = new Array( this.data.length );
|
2012-04-27 02:44:44 +00:00
|
|
|
|
|
|
|
// Initialization
|
2012-05-01 02:38:42 +00:00
|
|
|
var doc = parentDocument || this;
|
|
|
|
this.documentNode.setDocument( doc );
|
|
|
|
var root = doc.getDocumentNode();
|
|
|
|
this.documentNode.setRoot( root );
|
|
|
|
|
2012-04-27 02:44:44 +00:00
|
|
|
/*
|
|
|
|
* The offsetMap is always one element longer than data because it includes a reference to the
|
|
|
|
* root node at the offset just past the end. To make population work correctly, we have to
|
|
|
|
* start out with that one extra reference.
|
|
|
|
*/
|
2012-04-30 18:37:48 +00:00
|
|
|
this.offsetMap.push( this.documentNode );
|
2012-05-01 02:38:42 +00:00
|
|
|
|
2012-04-27 02:44:44 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* updates being broadcast upstream constantly while building is underway. Also populate the
|
|
|
|
* offset map as we go.
|
|
|
|
*/
|
|
|
|
var node,
|
|
|
|
textLength = 0,
|
|
|
|
inTextNode = false,
|
2012-04-28 00:16:29 +00:00
|
|
|
// Stack of stacks, each containing a
|
2012-04-30 18:37:48 +00:00
|
|
|
stack = [[this.documentNode], []],
|
2012-04-27 02:44:44 +00:00
|
|
|
children,
|
|
|
|
openingIndex,
|
|
|
|
currentStack = stack[1],
|
|
|
|
parentStack = stack[0],
|
2012-04-30 18:37:48 +00:00
|
|
|
currentNode = this.documentNode;
|
2012-04-27 02:44:44 +00:00
|
|
|
for ( var i = 0, length = this.data.length; i < length; i++ ) {
|
2012-04-28 00:16:29 +00:00
|
|
|
/*
|
|
|
|
* Set the node reference for this offset in the offset cache.
|
|
|
|
*
|
|
|
|
* This looks simple, but there are three cases that result in the same thing:
|
|
|
|
*
|
|
|
|
* 1. data[i] is an opening, so offset i is before the opening, so we need to point to the
|
|
|
|
* parent of the opened element. currentNode will be set to the opened element later,
|
|
|
|
* but right now its still set to the parent of the opened element.
|
|
|
|
* 2. data[i] is a closing, so offset i is before the closing, so we need to point to the
|
|
|
|
* closed element. currentNode will be set to the parent of the closed element later,
|
|
|
|
* but right now it's still set to the closed element.
|
|
|
|
* 3. data[i] is content, so offset i is in the middle of an element, so obviously we need
|
|
|
|
* currentNode, which won't be changed by this iteration.
|
|
|
|
*
|
|
|
|
* We want to populate the offsetMap with branches only, but we've just written the actual
|
|
|
|
* node that lives at this offset. So if it's a leaf node, change it to its parent.
|
|
|
|
*/
|
2012-04-30 22:42:36 +00:00
|
|
|
this.offsetMap[i] = ve.dm.factory.canNodeHaveChildren( currentNode.getType() ) ?
|
2012-04-28 00:16:29 +00:00
|
|
|
currentNode : parentStack[parentStack.length - 1];
|
|
|
|
// Infer that if an item in the linear model has a type attribute than it must be an element
|
2012-04-27 02:44:44 +00:00
|
|
|
if ( this.data[i].type === undefined ) {
|
2012-04-28 00:16:29 +00:00
|
|
|
// Text node opening
|
2012-04-27 02:44:44 +00:00
|
|
|
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-04-28 00:16:29 +00:00
|
|
|
// Text node closing
|
2012-04-27 02:44:44 +00:00
|
|
|
if ( inTextNode ) {
|
|
|
|
// Finish the text node by setting the length
|
|
|
|
currentNode.setLength( textLength );
|
2012-04-28 00:16:29 +00:00
|
|
|
// Put the state variables back as they were
|
2012-04-27 02:44:44 +00:00
|
|
|
currentNode = parentStack[parentStack.length - 1];
|
|
|
|
inTextNode = false;
|
|
|
|
textLength = 0;
|
|
|
|
}
|
2012-04-28 00:16:29 +00:00
|
|
|
// Element open/close
|
2012-04-27 02:44:44 +00:00
|
|
|
if ( this.data[i].type.charAt( 0 ) != '/' ) {
|
|
|
|
// Branch or leaf node opening
|
|
|
|
// Create a childless node
|
2012-05-04 20:31:14 +00:00
|
|
|
node = ve.dm.factory.create( this.data[i].type, [], this.data[i].attributes );
|
2012-04-27 02:44:44 +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-04-30 22:42:36 +00:00
|
|
|
if ( ve.dm.factory.canNodeHaveChildren( node.getType() ) ) {
|
2012-04-28 00:16:29 +00:00
|
|
|
// Create a new inner stack for this node
|
|
|
|
parentStack = currentStack;
|
|
|
|
currentStack = [];
|
|
|
|
stack.push( currentStack );
|
|
|
|
}
|
2012-04-27 02:44:44 +00:00
|
|
|
currentNode = node;
|
|
|
|
} else {
|
|
|
|
// Branch or leaf node closing
|
2012-04-30 22:42:36 +00:00
|
|
|
if ( ve.dm.factory.canNodeHaveChildren( currentNode.getType() ) ) {
|
2012-04-28 00:16:29 +00:00
|
|
|
// Pop this node's inner stack from the outer stack. It'll have all of the node's
|
2012-05-11 17:29:09 +00:00
|
|
|
// child nodes fully constructed
|
2012-04-28 00:16:29 +00:00
|
|
|
children = stack.pop();
|
|
|
|
currentStack = parentStack;
|
|
|
|
parentStack = stack[stack.length - 2];
|
2012-05-11 02:28:37 +00:00
|
|
|
if ( !parentStack ) {
|
|
|
|
// This can only happen if we got unbalanced data
|
|
|
|
throw 'Unbalanced input passed to DocumentFragment';
|
|
|
|
}
|
2012-04-28 00:16:29 +00:00
|
|
|
// Attach the children to the node
|
2012-04-27 22:09:10 +00:00
|
|
|
ve.batchSplice( currentNode, 0, 0, children );
|
2012-04-27 21:59:52 +00:00
|
|
|
}
|
2012-04-27 02:44:44 +00:00
|
|
|
currentNode = parentStack[parentStack.length - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 18:37:48 +00:00
|
|
|
// The end state is stack = [ [this.documentNode] [ array, of, its, children ] ]
|
2012-04-27 02:44:44 +00:00
|
|
|
// so attach all nodes in stack[1] to the root node
|
2012-04-30 18:37:48 +00:00
|
|
|
ve.batchSplice( this.documentNode, 0, 0, stack[1] );
|
2012-04-27 02:44:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets slice or copy of the document data.
|
2012-05-11 17:29:09 +00:00
|
|
|
*
|
2012-04-27 02:44:44 +00:00
|
|
|
* @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-04-27 21:59:52 +00:00
|
|
|
ve.dm.DocumentFragment.prototype.getData = function( range, deep ) {
|
2012-04-27 02:44:44 +00:00
|
|
|
var start = 0,
|
|
|
|
end;
|
|
|
|
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
|
|
|
|
var data = end === undefined ? this.data.slice( start ) : this.data.slice( start, end );
|
|
|
|
// Return either the slice or a deep copy of the slice
|
|
|
|
return deep ? ve.copyArray( data ) : data;
|
|
|
|
};
|
|
|
|
|
2012-05-03 20:11:36 +00:00
|
|
|
ve.dm.DocumentFragment.prototype.getOffsetMap = function() {
|
|
|
|
return this.offsetMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.DocumentFragment.prototype.getNodeFromOffset = function( offset ) {
|
|
|
|
return this.offsetMap[offset];
|
|
|
|
};
|
|
|
|
|
2012-05-01 02:38:42 +00:00
|
|
|
/**
|
|
|
|
* Gets the content data of a node.
|
2012-05-11 17:29:09 +00:00
|
|
|
*
|
2012-05-01 02:38:42 +00:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
ve.dm.DocumentFragment.prototype.getDataFromNode = function( node ) {
|
|
|
|
var length = node.getLength(),
|
2012-05-02 21:06:35 +00:00
|
|
|
offset = this.documentNode.getOffsetFromNode( node );
|
2012-05-01 02:38:42 +00:00
|
|
|
if ( offset >= 0 ) {
|
2012-05-07 19:00:07 +00:00
|
|
|
// 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() ) {
|
2012-05-01 02:38:42 +00:00
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
return this.data.slice( offset, offset + length );
|
|
|
|
}
|
|
|
|
return null;
|
2012-04-27 02:44:44 +00:00
|
|
|
};
|
2012-05-09 23:31:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of annotations that a given offset is covered by.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} offset Offset to get annotations for
|
|
|
|
* @returns {Object[]} A copy of all annotation objects offset is covered by
|
|
|
|
*/
|
2012-05-14 17:47:48 +00:00
|
|
|
ve.dm.DocumentFragment.prototype.getAnnotationsFromOffset = function( offset ) {
|
2012-05-09 23:31:56 +00:00
|
|
|
if ( ve.isArray( this.data[offset] ) ) {
|
2012-05-14 18:34:08 +00:00
|
|
|
return ve.getObjectValues( this.data[offset][1] );
|
2012-05-09 23:31:56 +00:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
};
|
2012-05-10 04:11:09 +00:00
|
|
|
|
2012-05-11 23:14:46 +00:00
|
|
|
/**
|
|
|
|
* Gets an array of common annnotations across a range.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Integer} offset Offset to get annotations for
|
|
|
|
* @returns {Object[]} A copy of all annotation objects offset is covered by
|
|
|
|
*/
|
|
|
|
ve.dm.DocumentFragment.prototype.getAnnotationsFromRange = function( range ) {
|
|
|
|
range.normalize();
|
|
|
|
var length = range.getLength(),
|
|
|
|
elementsCount = 0,
|
2012-05-14 17:47:48 +00:00
|
|
|
charAnnotationsObj = {},
|
2012-05-11 23:14:46 +00:00
|
|
|
annotations = [],
|
|
|
|
map = {},
|
2012-05-14 17:47:48 +00:00
|
|
|
aObj = {};
|
2012-05-11 23:14:46 +00:00
|
|
|
|
|
|
|
for (var i=range.start; i<range.end;i++) {
|
|
|
|
if ( ve.dm.Document.isElementData( this.data, i ) ) {
|
|
|
|
elementsCount++;
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-14 17:47:48 +00:00
|
|
|
|
2012-05-11 23:14:46 +00:00
|
|
|
charAnnotationsObj = this.data[i][1];
|
|
|
|
|
|
|
|
for ( aObj in charAnnotationsObj ) {
|
|
|
|
hash = $.toJSON( aObj );
|
|
|
|
if ( hash in map ) {
|
|
|
|
// increase the count for the annotation
|
|
|
|
map[hash][1]++;
|
|
|
|
} else {
|
|
|
|
// save the annotation and start the count at 1
|
|
|
|
map[hash] = [charAnnotationsObj[aObj], 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// build array of common annotations
|
|
|
|
for ( var hash in map ) {
|
|
|
|
if ( map[hash][1] === length - elementsCount ) {
|
|
|
|
annotations.push( map[hash][0] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return annotations;
|
|
|
|
};
|
|
|
|
|
2012-05-10 04:11:09 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.DocumentFragment, ve.Document );
|