Added test for replaceDomWrapper and onSplice in ve.ce.BranchNode

* Also fixed calls to addListenerMethod
* Also routed adding children in the constructor of ve.dm.BranchNode to the splice method
* Renamed types of ve.dm stub nodes to avoid collisions (since we have to register ce nodes by the same names for them to be generated by onSplice)

Change-Id: Ia2e75cf0a62186cc0e214683feb25c619590318a
This commit is contained in:
Trevor Parscal 2012-05-02 13:58:50 -07:00
parent 8bd0d385eb
commit fd28fff50f
10 changed files with 37 additions and 16 deletions

View file

@ -14,7 +14,7 @@ ve.ce.HeadingNode = function( model ) {
this.currentLevel = model.getAttribute( 'level' );
// Events
this.model.addListenerMethod( 'update', this, 'onUpdate' );
this.model.addListenerMethod( this, 'update', 'onUpdate' );
};
/* Static Members */

View file

@ -14,7 +14,7 @@ ve.ce.ImageNode = function( model ) {
this.currentSource = model.getAttribute( 'html/src' );
// Events
this.model.addListenerMethod( 'update', this, 'onUpdate' );
this.model.addListenerMethod( this, 'update', 'onUpdate' );
};
/* Static Members */

View file

@ -14,7 +14,7 @@ ve.ce.ListItemNode = function( model ) {
this.currentStyle = model.getAttribute( 'style' );
// Events
this.model.addListenerMethod( 'update', this, 'onUpdate' );
this.model.addListenerMethod( this, 'update', 'onUpdate' );
};
/* Static Members */

View file

@ -14,7 +14,7 @@ ve.ce.ListNode = function( model ) {
this.currentStyle = model.getAttribute( 'style' );
// Events
this.model.addListenerMethod( 'update', this, 'onUpdate' );
this.model.addListenerMethod( this, 'update', 'onUpdate' );
};
/* Static Members */

View file

@ -15,10 +15,12 @@ ve.ce.BranchNode = function( model, $element ) {
ve.ce.Node.call( this, model, $element );
// Events
this.model.addListenerMethod( 'splice', this, 'onSplice' );
this.model.addListenerMethod( this, 'splice', 'onSplice' );
// Initialization
this.onSplice.apply( this, [0, 0].concat( model.getChildren() ) );
if ( model.getChildren().length ) {
this.onSplice.apply( this, [0, 0].concat( model.getChildren() ) );
}
};
/* Methods */
@ -41,7 +43,7 @@ ve.ce.BranchNode.prototype.onSplice = function( index, howmany ) {
// Convert models to views and attach them to this node
if ( args.length >= 3 ) {
for ( i = 2, length = args.length; i < length; i++ ) {
args[i] = args[i].createView();
args[i] = ve.ce.factory.createNode( args[i].getType(), args[i] );
}
}
var removals = this.children.splice.apply( this.children, args );

View file

@ -16,9 +16,7 @@ ve.dm.BranchNode = function( type, children, attributes ) {
ve.BranchNode.call( this );
if ( ve.isArray( children ) && children.length ) {
for ( var i = 0; i < children.length; i++ ) {
this.push( children[i] );
}
this.splice.apply( this, [0, 0].concat( children ) );
}
};

View file

@ -9,10 +9,29 @@ ve.ce.BranchNodeStub = function( model, $element ) {
ve.extendClass( ve.ce.BranchNodeStub, ve.ce.BranchNode );
ve.ce.factory.register( 'branch-stub', ve.ce.BranchNodeStub );
/* Tests */
/*
test( '', 1, function() {
//
test( 'replaceDomWrapper', 4, function() {
var $oldWrapper = $( '<h1 class="test">hello</h1>' ),
$newWrapper = $( '<h2></h2>' ),
node = new ve.ce.BranchNodeStub( new ve.dm.BranchNodeStub(), $oldWrapper );
equal( node.$, $oldWrapper, 'this.$ references wrapper given to constructor' );
node.replaceDomWrapper( $newWrapper );
equal( node.$, $newWrapper, 'this.$ references new wrapper after replaceDomWrapper is called' );
equal( node.$.attr( 'class' ), 'test', 'old classes are added to new wrapper' );
equal( node.$.text(), 'hello', 'contents are added to new wrapper' );
} );
test( 'onSplice', 3, function() {
var modelA = new ve.dm.BranchNodeStub(),
modelB = new ve.dm.BranchNodeStub(),
modelC = new ve.dm.BranchNodeStub(),
viewA = new ve.ce.BranchNodeStub( modelA );
modelA.splice( 0, 0, modelB, modelC );
equal( viewA.getChildren().length, 2 );
deepEqual( viewA.getChildren()[0].getModel(), modelB );
deepEqual( viewA.getChildren()[1].getModel(), modelC );
} );
*/

View file

@ -9,6 +9,8 @@ ve.ce.LeafNodeStub = function( model ) {
ve.extendClass( ve.ce.LeafNodeStub, ve.ce.LeafNode );
ve.ce.factory.register( 'leaf-stub', ve.ce.BranchNodeStub );
/* Tests */
/*

View file

@ -4,7 +4,7 @@ module( 've.dm.BranchNode' );
ve.dm.BranchNodeStub = function( children ) {
// Inheritance
ve.dm.BranchNode.call( this, 'stub', children );
ve.dm.BranchNode.call( this, 'branch-stub', children );
};
ve.extendClass( ve.dm.BranchNodeStub, ve.dm.BranchNode );

View file

@ -4,7 +4,7 @@ module( 've.dm.LeafNode' );
ve.dm.LeafNodeStub = function() {
// Inheritance
ve.dm.LeafNode.call( this, 'stub' );
ve.dm.LeafNode.call( this, 'leaf-stub' );
};
ve.extendClass( ve.dm.LeafNodeStub, ve.dm.LeafNode );