Index: trunk/parsers/wikidom/tests/hype/es.ModelNode.test.js |
— | — | @@ -3,9 +3,9 @@ |
4 | 4 | test( 'es.ModelNode', 17, function() { |
5 | 5 | // Example data (integers) is used for simplicity of testing |
6 | 6 | var modelNode1 = new es.ModelNode(), |
7 | | - modelNode2 = new es.ModelNode( [1] ), |
8 | | - modelNode3 = new es.ModelNode( [1, 2]), |
9 | | - modelNode4 = new es.ModelNode( [1, 2, 3] ); |
| 7 | + modelNode2 = new es.ModelNode(), |
| 8 | + modelNode3 = new es.ModelNode( [new es.ModelNode()] ), |
| 9 | + modelNode4 = new es.ModelNode( [new es.ModelNode(), new es.ModelNode()] ); |
10 | 10 | |
11 | 11 | // Event triggering is detected using a callback that increments a counter |
12 | 12 | var updates = 0; |
— | — | @@ -25,7 +25,7 @@ |
26 | 26 | modelNode1.push( modelNode2 ); |
27 | 27 | equal( updates, 1, 'es.ModelNode emits update events on push' ); |
28 | 28 | equal( modelNode1[0], modelNode2, 'es.ModelNode appends node on push' ); |
29 | | - |
| 29 | + |
30 | 30 | /** @covers es.ModelNode.attach */ |
31 | 31 | equal( attaches, 1, 'es.ModelNode emits afterAttach events when added to another node' ); |
32 | 32 | |
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js |
— | — | @@ -6,16 +6,13 @@ |
7 | 7 | * |
8 | 8 | * @class |
9 | 9 | * @constructor |
10 | | - * @param {Integer} contentLength Length of content |
| 10 | + * @param {Integer|Array} contents Either Length of content or array of child nodes to append |
11 | 11 | * @property {Integer} contentLength Length of content |
12 | 12 | */ |
13 | | -es.DocumentModelNode = function( contentLength ) { |
| 13 | +es.DocumentModelNode = function( contents ) { |
14 | 14 | // Extension |
15 | | - var node = $.extend( new es.ModelNode(), this ); |
| 15 | + var node = $.extend( new es.ModelNode( $.isArray( contents ) ? contents : [] ), this ); |
16 | 16 | |
17 | | - // Properties |
18 | | - node.contentLength = contentLength || 0; |
19 | | - |
20 | 17 | // Observe add and remove operations to keep lengths up to date |
21 | 18 | node.addListenerMethods( node, { |
22 | 19 | 'beforePush': 'onBeforePush', |
— | — | @@ -25,6 +22,16 @@ |
26 | 23 | 'beforeSplice': 'onBeforeSplice' |
27 | 24 | } ); |
28 | 25 | |
| 26 | + // Properties |
| 27 | + if ( typeof contents === 'number' ) { |
| 28 | + if ( contents < 0 ) { |
| 29 | + throw 'Invalid content length error. Content length can not be less than 0.'; |
| 30 | + } |
| 31 | + node.contentLength = contents; |
| 32 | + } else { |
| 33 | + node.contentLength = 0; |
| 34 | + } |
| 35 | + |
29 | 36 | return node; |
30 | 37 | }; |
31 | 38 | |
Index: trunk/parsers/wikidom/lib/hype/bases/es.ModelNode.js |
— | — | @@ -16,13 +16,20 @@ |
17 | 17 | es.EventEmitter.call( this ); |
18 | 18 | |
19 | 19 | // Extension |
20 | | - var node = $.extend( $.isArray( children ) ? children : [], this ) |
| 20 | + var node = $.extend( [], this ) |
21 | 21 | |
22 | 22 | // Reusable function for passing update events upstream |
23 | 23 | node.emitUpdate = function() { |
24 | 24 | node.emit( 'update' ); |
25 | 25 | }; |
26 | 26 | |
| 27 | + // Children |
| 28 | + if ( $.isArray( children ) ) { |
| 29 | + for ( var i = 0; i < children.length; i++ ) { |
| 30 | + node.push( children[i] ); |
| 31 | + } |
| 32 | + } |
| 33 | + |
27 | 34 | return node; |
28 | 35 | }; |
29 | 36 | |
— | — | @@ -83,7 +90,7 @@ |
84 | 91 | var childModel = this[this.length - 1]; |
85 | 92 | childModel.detach(); |
86 | 93 | childModel.removeListener( 'update', this.emitUpdate ); |
87 | | - Array.prototype.pop.call( this, childModel ); |
| 94 | + Array.prototype.pop.call( this ); |
88 | 95 | this.emit( 'afterPop' ); |
89 | 96 | this.emit( 'update' ); |
90 | 97 | return childModel; |
— | — | @@ -105,7 +112,7 @@ |
106 | 113 | var childModel = this[0]; |
107 | 114 | childModel.detach(); |
108 | 115 | childModel.removeListener( 'update', this.emitUpdate ); |
109 | | - Array.prototype.shift.call( this, childModel ); |
| 116 | + Array.prototype.shift.call( this ); |
110 | 117 | this.emit( 'afterShift' ); |
111 | 118 | this.emit( 'update' ); |
112 | 119 | return childModel; |
— | — | @@ -205,7 +212,7 @@ |
206 | 213 | es.ModelNode.prototype.detach = function() { |
207 | 214 | this.emit( 'beforeDetach' ); |
208 | 215 | var parent = this.parent; |
209 | | - this.parent = null; |
| 216 | + this.parent = undefined; |
210 | 217 | this.emit( 'afterDetach' ); |
211 | 218 | }; |
212 | 219 | |