Add getDocumentSlice

A document slice is a document built from a data slice of an existing
document. It's completely separate from the original document and has
its own store and internalList. The new document's data also contains
the entirety of the original document's internal list. It's possible
to create a document slice of data located inside the internal list,
in which case the resulting document will contain that data twice (one
mutable copy at the top level, and one immutable copy in the internal
list).

ve.dm.Document.js:
* Optionally take an internalList in the constructor. This allows us to
  create a document with a clone of an existing internalList rather than
  an empty one.
* Add edgeMetadata flag to getFullData()

ve.dm.IndexValueStore.js, ve.dm.InternalList.js:
* Make these classes cloneable

Change-Id: I93e06f764ace16aee9df941b07f8c2bff1a28e2b
This commit is contained in:
Catrope 2013-05-22 16:48:41 +02:00
parent 8157bcc474
commit 6562b32aa7
4 changed files with 134 additions and 8 deletions

View file

@ -18,8 +18,9 @@
* @constructor
* @param {HTMLDocument|Array|ve.dm.LinearData} documentOrData HTML document, raw linear model data or LinearData to start with
* @param {ve.dm.Document} [parentDocument] Document to use as root for created nodes
* @param {ve.dm.InternalList} [internalList] Internal list to clone; passed when creating a document slice
*/
ve.dm.Document = function VeDmDocument( documentOrData, parentDocument ) {
ve.dm.Document = function VeDmDocument( documentOrData, parentDocument, internalList ) {
// Parent constructor
ve.Document.call( this, new ve.dm.DocumentNode() );
@ -41,7 +42,7 @@ ve.dm.Document = function VeDmDocument( documentOrData, parentDocument ) {
currentNode = this.documentNode;
this.documentNode.setDocument( doc );
this.documentNode.setRoot( root );
this.internalList = new ve.dm.InternalList( this );
this.internalList = internalList ? internalList.clone( this ) : new ve.dm.InternalList( this );
// Properties
this.parentDocument = parentDocument;
@ -292,6 +293,36 @@ ve.dm.Document.prototype.getInternalList = function () {
return this.internalList;
};
/**
* Get a document from a slice of this document. The new document's store and internal list will be
* clones of the ones in this document.
*
* @param {ve.Range|ve.dm.Node} rangeOrNode Range of data to clone, or node whose contents should be cloned
* @returns {ve.dm.Document} New document
* @throws {Error} rangeOrNode must be a ve.Range or a ve.dm.Node
*/
ve.dm.Document.prototype.getDocumentSlice = function ( rangeOrNode ) {
var data, range,
store = this.store.clone(),
listRange = this.internalList.getListNode().getOuterRange();
if ( rangeOrNode instanceof ve.dm.Node ) {
range = rangeOrNode.getRange();
} else if ( rangeOrNode instanceof ve.Range ) {
range = rangeOrNode;
} else {
throw new Error( 'rangeOrNode must be a ve.Range or a ve.dm.Node' );
}
data = ve.copyArray( this.getFullData( range, true ) );
if ( range.start > listRange.start || range.end < listRange.end ) {
// The range does not include the entire internal list, so add it
data = data.concat( this.getFullData( listRange ) );
}
return new this.constructor(
new ve.dm.ElementLinearData( store, data ),
undefined, this.internalList
);
};
/**
* Get the metadata replace operation required to keep data & metadata in sync after a splice
*
@ -349,14 +380,25 @@ ve.dm.Document.prototype.spliceMetadata = function ( offset, index, remove, inse
/**
* Get the full document data including metadata.
*
* Metadata will be into the document data to produce the "full data" result.
* Metadata will be into the document data to produce the "full data" result. If a range is passed,
* metadata at the edges of the range won't be included unless edgeMetadata is set to true. If
* no range is passed, the entire document's data is returned and metadata at the edges is
* included.
*
* @param {ve.Range} [range] Range to get full data for. If omitted, all data will be returned
* @param {boolean} [edgeMetadata=false] Include metadata at the edges of the range
* @returns {Array} Data with metadata interleaved
*/
ve.dm.Document.prototype.getFullData = function () {
var result = [], i, j, jLen, iLen = this.data.getLength();
for ( i = 0; i <= iLen; i++ ) {
if ( this.metadata.getData( i ) ) {
ve.dm.Document.prototype.getFullData = function ( range, edgeMetadata ) {
var j, jLen,
i = range ? range.start : 0,
iLen = range ? range.end : this.data.getLength(),
result = [];
if ( edgeMetadata === undefined ) {
edgeMetadata = !range;
}
while ( i <= iLen ) {
if ( this.metadata.getData( i ) && ( edgeMetadata || ( i !== range.start && i !== range.end ) ) ) {
for ( j = 0, jLen = this.metadata.getData( i ).length; j < jLen; j++ ) {
result.push( this.metadata.getData( i )[j] );
result.push( { 'type': '/' + this.metadata.getData( i )[j].type } );
@ -365,6 +407,7 @@ ve.dm.Document.prototype.getFullData = function () {
if ( i < iLen ) {
result.push( this.data.getData( i ) );
}
i++;
}
return result;
};

View file

@ -106,3 +106,21 @@ ve.dm.IndexValueStore.prototype.values = function ( indexes ) {
}
return values;
};
/**
* Clone a store.
*
* The returned clone is shallow: the valueStore array and the hashStore array are cloned, but
* the values inside them are copied by reference. These values are supposed to be immutable,
* though.
*
* @returns {ve.dm.IndexValueStore} New store with the same contents as this one
*/
ve.dm.IndexValueStore.prototype.clone = function () {
var key, clone = new this.constructor();
clone.valueStore = this.valueStore.slice();
for ( key in this.hashStore ) {
clone.hashStore[key] = this.hashStore[key];
}
return clone;
};

View file

@ -128,4 +128,17 @@ ve.dm.InternalList.prototype.getDataFromDom = function ( converter ) {
list.push( { 'type': '/internalList' } );
}
return list;
};
};
/**
* Clone this internal list.
*
* @param {ve.dm.Document} [doc] The new list's document. Defaults to this list's document.
* @returns {ve.dm.InternalList} Clone of this internal
*/
ve.dm.InternalList.prototype.clone = function ( doc ) {
var clone = new this.constructor( doc || this.doc );
clone.store = this.store.clone();
clone.itemsHtml = this.itemsHtml.slice();
return clone;
};

View file

@ -67,6 +67,58 @@ QUnit.test( 'getFullData', 1, function ( assert ) {
assert.deepEqual( doc.getFullData(), ve.dm.example.withMeta );
} );
QUnit.test( 'getDocumentSlice', function ( assert ) {
var i, doc2, doc = ve.dm.example.createExampleDocument( 'internalData' ),
cases = [
{
'msg': 'with range',
'doc': 'internalData',
'arg': new ve.Range( 7, 12 ),
'expectedData': doc.data.slice( 7, 12 ).concat( doc.data.slice( 5, 21 ) )
},
{
'msg': 'with node',
'doc': 'internalData',
'arg': doc.getInternalList().getItemNode( 1 ),
'expectedData': doc.data.slice( 14, 19 ).concat( doc.data.slice( 5, 21 ) )
},
{
'msg': 'paragraph at the start',
'doc': 'internalData',
'arg': new ve.Range( 0, 5 ),
'expectedData': doc.data.slice( 0, 21 )
},
{
'msg': 'paragraph at the end',
'doc': 'internalData',
'arg': new ve.Range( 21, 27 ),
'expectedData': doc.data.slice( 21, 27 ).concat( doc.data.slice( 5, 21 ) )
}
];
QUnit.expect( 8*cases.length );
for ( i = 0; i < cases.length; i++ ) {
doc = ve.dm.example.createExampleDocument( cases[i].doc );
doc2 = doc.getDocumentSlice( cases[i].arg );
assert.deepEqual( doc2.data.data, cases[i].expectedData,
cases[i].msg + ': sliced data' );
assert.notStrictEqual( doc2.data[0], cases[i].expectedData[0],
cases[i].msg + ': data is cloned, not the same' );
assert.deepEqual( doc2.store, doc.store,
cases[i].msg + ': store is copied' );
assert.notStrictEqual( doc2.store, doc.store,
cases[i].msg + ': store is a clone, not the same' );
assert.deepEqual( doc2.internalList.itemsHtml, doc.internalList.itemsHtml,
cases[i].msg + ': internal list items are copied' );
assert.notStrictEqual( doc2.internalList.itemsHtml, doc.internalList.itemsHtml,
cases[i].msg + ': internal list items array is cloned, not the same' );
assert.deepEqual( doc2.internalList.store, doc.internalList.store,
cases[i].msg + ': internal list store is copied' );
assert.notStrictEqual( doc2.internalList.store, doc.internalList.store,
cases[i].msg + ': internal list store is a clone, not the same' );
}
} );
QUnit.test( 'getMetadataReplace', 3, function ( assert ) {
var replace, expectedReplace,
doc = ve.dm.example.createExampleDocument( 'withMeta' );