r101023 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101022‎ | r101023 | r101024 >
Date:18:46, 27 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added some test for es.DocumentNode and made aliases in es for extend, isPlainObject and isArray
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewBranchNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewLeafNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.ModelNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/bases/es.ViewNode.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/es.Transaction.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/es.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.ListItemModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.ListModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.ParagraphModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.TableCellModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.TableModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/models/es.TableRowModel.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.ContentView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.DocumentView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.ListItemView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.ListView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.ParagraphView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.TableCellView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.TableRowView.js (modified) (history)
  • /trunk/parsers/wikidom/lib/hype/views/es.TableView.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.DocumentNode.test.js (added) (history)
  • /trunk/parsers/wikidom/tests/hype/index.html (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/hype/es.DocumentNode.test.js
@@ -0,0 +1,60 @@
 2+module( 'Bases' );
 3+
 4+function DocumentNodeStub( items, name, size ) {
 5+ this.name = name;
 6+ this.size = size;
 7+ return es.extendObject( new es.DocumentNode( items ), this );
 8+}
 9+
 10+DocumentNodeStub.prototype.getElementLength = function() {
 11+ // Mimic document data which has an opening and closing around the content
 12+ return this.size + 2;
 13+};
 14+
 15+test( 'es.DocumentNode', function() {
 16+ strictEqual(
 17+ ( new DocumentNodeStub( [], 'a', 0 ) ).getElementLength(),
 18+ 2,
 19+ 'DocumentNodeStub.getElementLength() returns initialized length plus 2 for elements'
 20+ );
 21+ var a = new DocumentNodeStub( [], 'a', 0 ),
 22+ b = new DocumentNodeStub( [], 'b', 1 ),
 23+ c = new DocumentNodeStub( [], 'c', 2 ),
 24+ d = new DocumentNodeStub( [], 'd', 3 ),
 25+ e = new DocumentNodeStub( [], 'e', 4 ),
 26+ root1 = new DocumentNodeStub( [a, b, c, d, e], 'root1', 20 ),
 27+ i;
 28+
 29+ var getNodeFromOffsetTests = [
 30+ { 'input': -1, 'output': null },
 31+ { 'input': 0, 'output': a },
 32+ { 'input': 1, 'output': a },
 33+ { 'input': 2, 'output': b },
 34+ { 'input': 3, 'output': b },
 35+ { 'input': 4, 'output': b },
 36+ { 'input': 5, 'output': c },
 37+ { 'input': 6, 'output': c },
 38+ { 'input': 7, 'output': c },
 39+ { 'input': 8, 'output': c },
 40+ { 'input': 9, 'output': d },
 41+ { 'input': 10, 'output': d },
 42+ { 'input': 11, 'output': d },
 43+ { 'input': 12, 'output': d },
 44+ { 'input': 13, 'output': d },
 45+ { 'input': 14, 'output': e },
 46+ { 'input': 15, 'output': e },
 47+ { 'input': 16, 'output': e },
 48+ { 'input': 17, 'output': e },
 49+ { 'input': 18, 'output': e },
 50+ { 'input': 19, 'output': e },
 51+ { 'input': 20, 'output': null }
 52+ ];
 53+
 54+ for ( i = 0; i < getNodeFromOffsetTests.length; i++ ) {
 55+ strictEqual(
 56+ root1.getNodeFromOffset( getNodeFromOffsetTests[i].input ),
 57+ getNodeFromOffsetTests[i].output,
 58+ 'getNodeFromOffset finds the right item or returns null when out of range'
 59+ );
 60+ }
 61+} );
Index: trunk/parsers/wikidom/tests/hype/index.html
@@ -29,6 +29,7 @@
3030 <script src="../../lib/hype/models/es.TableRowModel.js"></script>
3131 <script src="es.test.js"></script>
3232 <script src="es.ModelNode.test.js"></script>
 33+ <script src="es.DocumentNode.test.js"></script>
3334 <script src="es.DocumentModel.test.js"></script>
3435 </body>
3536 </html>
Index: trunk/parsers/wikidom/lib/hype/es.js
@@ -31,7 +31,7 @@
3232 * @param {Function} dst Class to extend
3333 * @param {Function} src Base class to use methods from
3434 */
35 -es.extend = function( dst, src ) {
 35+es.extendClass = function( dst, src ) {
3636 var base = new src();
3737 for ( var method in base ) {
3838 if ( typeof base[method] === 'function' && !( method in dst.prototype ) ) {
@@ -40,6 +40,12 @@
4141 }
4242 };
4343
 44+es.extendObject = $.extend;
 45+
 46+es.isPlainObject = $.isPlainObject;
 47+
 48+es.isArray = $.isArray;
 49+
4450 /**
4551 * Recursively compares string and number property between two objects.
4652 *
@@ -64,7 +70,7 @@
6571 bType = typeof bValue;
6672 if ( aType !== bType ||
6773 ( ( aType === 'string' || aType === 'number' ) && aValue !== bValue ) ||
68 - ( $.isPlainObject( aValue ) && !es.compareObjects( aValue, bValue ) ) ) {
 74+ ( es.isPlainObject( aValue ) && !es.compareObjects( aValue, bValue ) ) ) {
6975 return false;
7076 }
7177 }
@@ -87,9 +93,9 @@
8894 sourceType = typeof sourceValue;
8995 if ( sourceType === 'string' || sourceType === 'number' ) {
9096 destination.push( sourceValue );
91 - } else if ( $.isPlainObject( sourceValue ) ) {
 97+ } else if ( es.isPlainObject( sourceValue ) ) {
9298 destination.push( es.copyObject( sourceValue ) );
93 - } else if ( $.isArray( sourceValue ) ) {
 99+ } else if ( es.isArray( sourceValue ) ) {
94100 destination.push( es.copyArray( sourceValue ) );
95101 }
96102 }
@@ -111,9 +117,9 @@
112118 sourceType = typeof sourceValue;
113119 if ( sourceType === 'string' || sourceType === 'number' ) {
114120 destination[key] = sourceValue;
115 - } else if ( $.isPlainObject( sourceValue ) ) {
 121+ } else if ( es.isPlainObject( sourceValue ) ) {
116122 destination[key] = es.copyObject( sourceValue );
117 - } else if ( $.isArray( sourceValue ) ) {
 123+ } else if ( es.isArray( sourceValue ) ) {
118124 destination[key] = es.copyArray( sourceValue );
119125 }
120126 }
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -10,12 +10,12 @@
1111 * @param {Object} attributes Document attributes
1212 */
1313 es.DocumentModel = function( data, attributes ) {
14 - // Inheritance
15 - var node = $.extend( new es.DocumentModelNode( null, length ), this );
 14+ // Extension
 15+ var node = es.extendObject( new es.DocumentModelNode( null, length ), this );
1616
1717 // Properties
18 - node.data = $.isArray( data ) ? data : [];
19 - node.attributes = $.isPlainObject( attributes ) ? attributes : {};
 18+ node.data = es.isArray( data ) ? data : [];
 19+ node.attributes = es.isPlainObject( attributes ) ? attributes : {};
2020
2121 // Auto-generate model tree
2222 var nodes = es.DocumentModel.createNodesFromData( node.data );
@@ -178,7 +178,7 @@
179179 }
180180 for ( j = this.cursor; j < to; j++ ) {
181181 // Auto-convert to array
182 - if ( $.isArray( this.data[j] ) ) {
 182+ if ( es.isArray( this.data[j] ) ) {
183183 this.data[j].push( annotation );
184184 } else {
185185 this.data[j] = [this.data[j], annotation];
@@ -327,7 +327,7 @@
328328 es.DocumentModel.newFromPlainObject = function( obj ) {
329329 if ( obj.type === 'document' ) {
330330 var data = [],
331 - attributes = $.isPlainObject( obj.attributes ) ? es.copyObject( obj.attributes ) : {};
 331+ attributes = es.isPlainObject( obj.attributes ) ? es.copyObject( obj.attributes ) : {};
332332 for ( var i = 0; i < obj.children.length; i++ ) {
333333 data = data.concat( es.DocumentModel.flattenPlainObjectElementNode( obj.children[i] ) );
334334 }
@@ -363,7 +363,7 @@
364364 if ( annotation === undefined || annotation.type === undefined ) {
365365 throw 'Invalid annotation error. Can not find non-annotation data in character.';
366366 }
367 - if ( $.isArray( annotations ) ) {
 367+ if ( es.isArray( annotations ) ) {
368368 // Find the index of a comparable annotation (checking for same value, not reference)
369369 for ( var i = 0; i < annotations.length; i++ ) {
370370 // Skip over character data - used when this is called on a content data item
@@ -407,14 +407,14 @@
408408 * @returns {Array}
409409 */
410410 es.DocumentModel.flattenPlainObjectContentNode = function( obj ) {
411 - if ( !$.isPlainObject( obj ) ) {
 411+ if ( !es.isPlainObject( obj ) ) {
412412 // Use empty content
413413 return [];
414414 } else {
415415 // Convert string to array of characters
416416 var data = obj.text.split('');
417417 // Render annotations
418 - if ( $.isArray( obj.annotations ) ) {
 418+ if ( es.isArray( obj.annotations ) ) {
419419 for ( var i = 0, length = obj.annotations.length; i < length; i++ ) {
420420 var src = obj.annotations[i];
421421 // Build simplified annotation object
@@ -463,15 +463,15 @@
464464 var i,
465465 data = [],
466466 element = { 'type': obj.type };
467 - if ( $.isPlainObject( obj.attributes ) ) {
 467+ if ( es.isPlainObject( obj.attributes ) ) {
468468 element.attributes = es.copyObject( obj.attributes );
469469 }
470470 // Open element
471471 data.push( element );
472 - if ( $.isPlainObject( obj.content ) ) {
 472+ if ( es.isPlainObject( obj.content ) ) {
473473 // Add content
474474 data = data.concat( es.DocumentModel.flattenPlainObjectContentNode( obj.content ) );
475 - } else if ( $.isArray( obj.children ) ) {
 475+ } else if ( es.isArray( obj.children ) ) {
476476 // Add children - only do this if there is no content property
477477 for ( i = 0; i < obj.children.length; i++ ) {
478478 // TODO: Figure out if all this concatenating is inefficient. I think it is
@@ -502,7 +502,7 @@
503503 // Content can't exist at the edges
504504 if ( offset > 0 && offset < data.length ) {
505505 // Shortcut: if there's already content there, we will trust it's supposed to be there
506 - if ( typeof data[offset] === 'string' || $.isArray( data[offset] ) ) {
 506+ if ( typeof data[offset] === 'string' || es.isArray( data[offset] ) ) {
507507 return true;
508508 }
509509 // Empty elements will have an opening and a closing next to each other
@@ -1042,4 +1042,4 @@
10431043
10441044 /* Inheritance */
10451045
1046 -es.extend( es.DocumentModel, es.DocumentModelNode );
 1046+es.extendClass( es.DocumentModel, es.DocumentModelNode );
Index: trunk/parsers/wikidom/lib/hype/models/es.TableRowModel.js
@@ -6,7 +6,7 @@
77 */
88 es.TableRowModel = function( element, length ) {
99 // Extension
10 - return $.extend( new es.DocumentModelNode( element, length ), this );
 10+ return es.extendObject( new es.DocumentModelNode( element, length ), this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/models/es.ParagraphModel.js
@@ -6,7 +6,7 @@
77 */
88 es.ParagraphModel = function( element, length ) {
99 // Extension
10 - return $.extend( new es.DocumentModelNode( element, length ), this );
 10+ return es.extendObject( new es.DocumentModelNode( element, length ), this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/models/es.TableCellModel.js
@@ -6,7 +6,7 @@
77 */
88 es.TableCellModel = function( element, length ) {
99 // Extension
10 - return $.extend( new es.DocumentModelNode( element, length ), this );
 10+ return es.extendObject( new es.DocumentModelNode( element, length ), this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/models/es.TableModel.js
@@ -6,7 +6,7 @@
77 */
88 es.TableModel = function( element, length ) {
99 // Extension
10 - return $.extend( new es.DocumentModelNode( element, length ), this );
 10+ return es.extendObject( new es.DocumentModelNode( element, length ), this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/models/es.ListItemModel.js
@@ -6,7 +6,7 @@
77 */
88 es.ListItemModel = function( element, length ) {
99 // Extension
10 - return $.extend( new es.DocumentModelNode( element, length ), this );
 10+ return es.extendObject( new es.DocumentModelNode( element, length ), this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/models/es.ListModel.js
@@ -6,7 +6,7 @@
77 */
88 es.ListModel = function( element, length ) {
99 // Extension
10 - return $.extend( new es.DocumentModelNode( element, length ), this );
 10+ return es.extendObject( new es.DocumentModelNode( element, length ), this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/es.Transaction.js
@@ -6,7 +6,7 @@
77 * @constructor
88 */
99 es.Transaction = function() {
10 - return $.extend( [], this );
 10+ return es.extendObject( [], this );
1111 };
1212
1313 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/bases/es.ViewNode.js
@@ -27,7 +27,7 @@
2828 }
2929
3030 // Extension
31 - var node = $.extend( [], this );
 31+ var node = es.extendObject( [], this );
3232
3333 // Properties
3434 node.model = model;
@@ -185,4 +185,4 @@
186186
187187 /* Inheritance */
188188
189 -es.extend( es.ViewNode, es.EventEmitter );
 189+es.extendClass( es.ViewNode, es.EventEmitter );
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentNode.js
@@ -2,11 +2,7 @@
33 * Creates an es.DocumentNode object.
44 */
55 es.DocumentNode = function( nodes ) {
6 - if ( nodes === undefined ) {
7 - nodes = [];
8 - }
9 - $.extend( nodes, this );
10 - return nodes;
 6+ return es.extendObject( nodes === undefined ? [] : nodes, this );
117 };
128
139 /**
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentModelNode.js
@@ -12,7 +12,7 @@
1313 */
1414 es.DocumentModelNode = function( element, contents ) {
1515 // Extension
16 - var node = $.extend( new es.DocumentNode( new es.ModelNode() ), this );
 16+ var node = es.extendObject( new es.DocumentNode( new es.ModelNode() ), this );
1717
1818 // Observe add and remove operations to keep lengths up to date
1919 node.addListenerMethods( node, {
@@ -31,7 +31,7 @@
3232 throw 'Invalid content length error. Content length can not be less than 0.';
3333 }
3434 node.contentLength = contents;
35 - } else if ( $.isArray( contents ) ) {
 35+ } else if ( es.isArray( contents ) ) {
3636 for ( var i = 0; i < contents.length; i++ ) {
3737 node.push( contents[i] );
3838 }
Index: trunk/parsers/wikidom/lib/hype/bases/es.ModelNode.js
@@ -16,7 +16,7 @@
1717 es.EventEmitter.call( this );
1818
1919 // Extension
20 - var node = $.extend( [], this );
 20+ var node = es.extendObject( [], this );
2121
2222 // Reusable function for passing update events upstream
2323 node.emitUpdate = function() {
@@ -24,7 +24,7 @@
2525 };
2626
2727 // Children
28 - if ( $.isArray( children ) ) {
 28+ if ( es.isArray( children ) ) {
2929 for ( var i = 0; i < children.length; i++ ) {
3030 node.push( children[i] );
3131 }
@@ -245,4 +245,4 @@
246246
247247 /* Inheritance */
248248
249 -es.extend( es.ModelNode, es.EventEmitter );
 249+es.extendClass( es.ModelNode, es.EventEmitter );
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewLeafNode.js
@@ -9,7 +9,7 @@
1010 */
1111 es.DocumentViewLeafNode = function( model, $element ) {
1212 // Extension
13 - var node = $.extend( new es.DocumentNode( new es.ViewNode( model, $element ) ), this );
 13+ var node = es.extendObject( new es.DocumentNode( new es.ViewNode( model, $element ) ), this );
1414
1515 // Content
1616 node.contentView = new es.ContentView( node.$, model );
Index: trunk/parsers/wikidom/lib/hype/bases/es.DocumentViewBranchNode.js
@@ -9,7 +9,7 @@
1010 */
1111 es.DocumentViewBranchNode = function( model, $element ) {
1212 // Extension
13 - return $.extend( new es.DocumentNode( new es.ViewNode( model, $element ) ), this );
 13+ return es.extendObject( new es.DocumentNode( new es.ViewNode( model, $element ) ), this );
1414 };
1515
1616 /* Methods */
Index: trunk/parsers/wikidom/lib/hype/views/es.ParagraphView.js
@@ -7,5 +7,5 @@
88 */
99 es.ParagraphView = function( model ) {
1010 // Extension
11 - return $.extend( new es.DocumentViewLeafNode( model ), this );
 11+ return es.extendObject( new es.DocumentViewLeafNode( model ), this );
1212 };
Index: trunk/parsers/wikidom/lib/hype/views/es.TableCellView.js
@@ -7,5 +7,5 @@
88 */
99 es.TableCellView = function( model ) {
1010 // Extension
11 - return $.extend( new es.DocumentViewBranchNode( model ), this );
 11+ return es.extendObject( new es.DocumentViewBranchNode( model ), this );
1212 };
Index: trunk/parsers/wikidom/lib/hype/views/es.TableView.js
@@ -7,5 +7,5 @@
88 */
99 es.TableView = function( model ) {
1010 // Extension
11 - return $.extend( new es.DocumentViewBranchNode( model ), this );
 11+ return es.extendObject( new es.DocumentViewBranchNode( model ), this );
1212 };
Index: trunk/parsers/wikidom/lib/hype/views/es.ContentView.js
@@ -840,4 +840,4 @@
841841
842842 /* Inheritance */
843843
844 -es.extend( es.ContentView, es.EventEmitter );
 844+es.extendClass( es.ContentView, es.EventEmitter );
Index: trunk/parsers/wikidom/lib/hype/views/es.ListItemView.js
@@ -7,5 +7,5 @@
88 */
99 es.ListItemView = function( model ) {
1010 // Extension
11 - return $.extend( new es.DocumentViewLeafNode( model ), this );
 11+ return es.extendObject( new es.DocumentViewLeafNode( model ), this );
1212 };
Index: trunk/parsers/wikidom/lib/hype/views/es.ListView.js
@@ -7,5 +7,5 @@
88 */
99 es.ListView = function( model ) {
1010 // Extension
11 - return $.extend( new es.DocumentViewBranchNode( model ), this );
 11+ return es.extendObject( new es.DocumentViewBranchNode( model ), this );
1212 };
Index: trunk/parsers/wikidom/lib/hype/views/es.DocumentView.js
@@ -1,5 +1,5 @@
22 es.DocumentView = function( documentModel, surfaceView ) {
3 - var node = $.extend( new es.DocumentViewBranchNode( documentModel ), this );
 3+ var node = es.extendObject( new es.DocumentViewBranchNode( documentModel ), this );
44 node.$.addClass( 'editSurface-document' );
55 node.surfaceView = surfaceView;
66 return node;
Index: trunk/parsers/wikidom/lib/hype/views/es.TableRowView.js
@@ -7,5 +7,5 @@
88 */
99 es.TableRowView = function( model ) {
1010 // Extension
11 - return $.extend( new es.DocumentViewBranchNode( model ), this );
 11+ return es.extendObject( new es.DocumentViewBranchNode( model ), this );
1212 };

Status & tagging log