Merge "Implement SurfaceFragment.unwrapAllNodes and fix wrapAllNodes."

This commit is contained in:
jenkins-bot 2013-02-22 22:36:03 +00:00 committed by Gerrit Code Review
commit 26e623940a
2 changed files with 63 additions and 11 deletions

View file

@ -642,30 +642,53 @@ ve.dm.SurfaceFragment.prototype.wrapAllNodes = function ( wrapper ) {
if ( !this.surface ) { if ( !this.surface ) {
return this; return this;
} }
var tx, newRange;
if ( !ve.isArray( wrapper ) ) { if ( !ve.isArray( wrapper ) ) {
wrapper = [wrapper]; wrapper = [wrapper];
} }
var tx = ve.dm.Transaction.newFromWrap( this.document, this.range, [], wrapper, [], [] );
this.range = tx.translateRange( this.range ); newRange = new ve.Range( this.range.start, this.range.end + ( wrapper.length * 2 ) );
this.surface.change( tx, !this.noAutoSelect && this.range );
tx = ve.dm.Transaction.newFromWrap( this.document, this.range, [], wrapper, [], [] );
this.surface.change( tx, !this.noAutoSelect && newRange );
this.range = newRange;
return this; return this;
}; };
/** /**
* Unwrap nodes in the fragment out of one or more elements. * Unwrap nodes in the fragment out of one or more elements.
* *
* TODO: Figure out what the arguments for this function should be
*
* @method * @method
* @param {string|string[]} type Node types to unwrap, or array of node types to unwrap * @param {number} depth Number of nodes to unwrap
* @returns {ve.dm.SurfaceFragment} This fragment * @returns {ve.dm.SurfaceFragment} This fragment
*/ */
ve.dm.SurfaceFragment.prototype.unwrapAllNodes = function () { ve.dm.SurfaceFragment.prototype.unwrapAllNodes = function ( depth ) {
// Handle null fragment // Handle null fragment
if ( !this.surface ) { if ( !this.surface ) {
return this; return this;
} }
// TODO: Implement var i, tx, newRange, wrapper = [],
innerRange = new ve.Range( this.range.start + depth, this.range.end - depth );
if ( this.range.end - this.range.start < depth * 2 ) {
throw new Error( 'cannot unwrap by greater depth than maximum theoretical depth of selection' );
}
for ( i = 0; i < depth; i++ ) {
wrapper.push( this.surface.getDocument().data[this.range.start + i] );
}
newRange = new ve.Range( this.range.start, this.range.end - ( depth * 2 ) );
tx = ve.dm.Transaction.newFromWrap( this.document, innerRange, wrapper, [], [], [] );
this.surface.change( tx, !this.noAutoSelect && newRange );
this.range = newRange;
return this; return this;
}; };

View file

@ -174,10 +174,12 @@ QUnit.test( 'wrapNodes', 2, function ( assert ) {
); );
} ); } );
QUnit.test( 'wrapAllNodes', 2, function ( assert ) { QUnit.test( 'wrapAllNodes/unwrapAllNodes', 10, function ( assert ) {
var doc = new ve.dm.Document( ve.copyArray( ve.dm.example.data ) ), var doc = new ve.dm.Document( ve.copyArray( ve.dm.example.data ) ),
surface = new ve.dm.Surface( doc ), surface = new ve.dm.Surface( doc ),
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) ); fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 55, 61 ) ),
expectedData;
// Make 2 paragraphs into 1 lists of 1 item with 2 paragraphs // Make 2 paragraphs into 1 lists of 1 item with 2 paragraphs
fragment.wrapAllNodes( fragment.wrapAllNodes(
[{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }] [{ 'type': 'list', 'attributes': { 'style': 'bullet' } }, { 'type': 'listItem' }]
@ -202,6 +204,12 @@ QUnit.test( 'wrapAllNodes', 2, function ( assert ) {
], ],
'wrapping nodes can add multiple levels of wrapping to multiple elements' 'wrapping nodes can add multiple levels of wrapping to multiple elements'
); );
assert.deepEqual( fragment.getRange(), new ve.Range( 55, 65 ), 'new range contains wrapping elements' );
fragment.unwrapAllNodes( 2 );
assert.deepEqual( doc.getData(), ve.dm.example.data, 'unwrapping 2 levels restores document to original state' );
assert.deepEqual( fragment.getRange(), new ve.Range( 55, 61 ), 'range after unwrapping is same as original range' );
// Make a 1 paragraph into 1 list with 1 item // Make a 1 paragraph into 1 list with 1 item
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 9, 12 ) ); fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 9, 12 ) );
fragment.wrapAllNodes( fragment.wrapAllNodes(
@ -224,6 +232,27 @@ QUnit.test( 'wrapAllNodes', 2, function ( assert ) {
], ],
'wrapping nodes can add multiple levels of wrapping to a single element' 'wrapping nodes can add multiple levels of wrapping to a single element'
); );
assert.deepEqual( fragment.getRange(), new ve.Range( 9, 16 ), 'new range contains wrapping elements' );
fragment.unwrapAllNodes( 2 );
assert.deepEqual( doc.getData(), ve.dm.example.data, 'unwrapping 2 levels restores document to original state' );
assert.deepEqual( fragment.getRange(), new ve.Range( 9, 12 ), 'range after unwrapping is same as original range' );
fragment = new ve.dm.SurfaceFragment( surface, new ve.Range( 5, 37 ) );
assert.throws( function() {
fragment.unwrapAllNodes( 20 );
}, /cannot unwrap by greater depth/, 'error thrown trying to unwrap more nodes that it is possible to contain' );
expectedData = ve.copyArray( ve.dm.example.data );
expectedData.splice( 5, 4 );
expectedData.splice( 29, 4 );
fragment.unwrapAllNodes( 4 );
assert.deepEqual(
doc.getData(),
expectedData,
'unwrapping 4 levels (table, tableSection, tableRow and tableCell)'
);
}); });
function runIsolateTest( assert, range, expected, label ) { function runIsolateTest( assert, range, expected, label ) {