Added ve.ce.NodeFactory and tests

Also added tests for initialization of factories at ve.ce.factory and ve.dm.factory

Change-Id: Ic6ac74aab86ecdfd4f094d9bb1fa16de930387b7
This commit is contained in:
Trevor Parscal 2012-04-30 16:36:02 -07:00
parent a667425874
commit aaa322642b
6 changed files with 90 additions and 2 deletions

View file

@ -0,0 +1,35 @@
/**
* ContentEditable node factory.
*
* @class
* @extends {ve.NodeFactory}
* @constructor
*/
ve.ce.NodeFactory = function() {
// Inheritance
ve.NodeFactory.call( this );
};
/* Methods */
/**
* Checks if a given node type can be split.
*
* @param {String} type Node type
* @returns {Boolean} The node can have grandchildren
* @throws 'Unknown node type'
*/
ve.ce.NodeFactory.prototype.canNodeBeSplit = function( type ) {
if ( type in this.registry ) {
return this.registry[type].rules.canBeSplit;
}
throw 'Unknown node type: ' + type;
};
/* Inheritance */
ve.extendClass( ve.ce.NodeFactory, ve.NodeFactory );
/* Initialization */
ve.ce.factory = new ve.ce.NodeFactory();

8
modules/ve2/ce/ve.ce.js Normal file
View file

@ -0,0 +1,8 @@
/**
* VisualEditor ContentEditable namespace.
*
* All classes and functions will be attached to this object to keep the global namespace clean.
*/
ve.ce = {
//'factory': Initialized in ve.ce.NodeFactory.js
};

View file

@ -0,0 +1,34 @@
module( 've.ce.NodeFactory' );
/* Stubs */
ve.ce.NodeFactoryNodeStub = function( a, b ) {
this.a = a;
this.b = b;
};
ve.ce.NodeFactoryNodeStub.rules = {
'canBeSplit': false
};
/* Tests */
test( 'canNodeBeSplit', 2, function() {
var factory = new ve.ce.NodeFactory();
raises( function() {
factory.createNode( 'stub', 23, { 'bar': 'baz' } );
},
/^Unknown node type: stub$/,
'throws an exception when getting allowed child nodes of a node of an unregistered type'
);
factory.register( 'stub', ve.ce.NodeFactoryNodeStub );
strictEqual(
factory.canNodeBeSplit( 'stub' ),
false,
'gets split rules for registered nodes'
);
} );
test( 'initialization', 1, function() {
ok( ve.ce.factory instanceof ve.ce.NodeFactory, 'factory is initialized at ve.ce.factory' );
} );

View file

@ -45,3 +45,7 @@ test( 'getParentNodeTypes', 2, function() {
'gets parent type rules for registered nodes'
);
} );
test( 'initialization', 1, function() {
ok( ve.dm.factory instanceof ve.dm.NodeFactory, 'factory is initialized at ve.dm.factory' );
} );

View file

@ -51,6 +51,10 @@
<script src="../../modules/ve2/dm/nodes/ve.dm.TableRowNode.js"></script>
<script src="../../modules/ve2/dm/nodes/ve.dm.TextNode.js"></script>
<!-- VisualEditor ContentEditable -->
<script src="../../modules/ve2/ce/ve.ce.js"></script>
<script src="../../modules/ve2/ce/ve.ce.NodeFactory.js"></script>
<!-- VisualEditor Tests -->
<script src="ve.Node.test.js"></script>
<script src="ve.BranchNode.test.js"></script>
@ -67,5 +71,8 @@
<script src="dm/ve.dm.Document.test.js"></script>
<script src="dm/ve.dm.DocumentFragment.test.js"></script>
<script src="dm/ve.dm.Transaction.test.js"></script>
<!-- VisualEditor ContentEditable Tests -->
<script src="ce/ve.ce.NodeFactory.test.js"></script>
</body>
</html>

View file

@ -49,7 +49,7 @@ test( 'canNodeHaveChildren', 2, function() {
'throws an exception when checking if a node of an unregistered type can have children'
);
factory.register( 'stub', ve.NodeFactoryNodeStub );
equal(
strictEqual(
factory.canNodeHaveChildren( 'stub' ),
false,
'gets child rules for registered nodes'
@ -65,7 +65,7 @@ test( 'canNodeHaveGrandchildren', 2, function() {
'throws an exception when checking if a node of an unregistered type can have grandchildren'
);
factory.register( 'stub', ve.NodeFactoryNodeStub );
equal(
strictEqual(
factory.canNodeHaveGrandchildren( 'stub' ),
false,
'gets grandchild rules for registered nodes'