r92207 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92206‎ | r92207 | r92208 >
Date:22:25, 14 July 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added EventEmitter and test for Content which now emits change, insert, remove and annotate events.
Modified paths:
  • /trunk/parsers/wikidom/demos/es/index.html (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.EventEmitter.js (added) (history)
  • /trunk/parsers/wikidom/tests/annotations/index.html (modified) (history)
  • /trunk/parsers/wikidom/tests/annotations/test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/annotations/index.html
@@ -10,6 +10,8 @@
1111 <h2 id="qunit-banner"></h2>
1212 <h2 id="qunit-userAgent"></h2>
1313 <ol id="qunit-tests"></ol>
 14+ <script src="../../lib/es/es.js" type="text/javascript"></script>
 15+ <script src="../../lib/es/es.EventEmitter.js" type="text/javascript"></script>
1416 <script src="../../lib/es/es.Content.js" type="text/javascript"></script>
1517 <script src="../../lib/jquery.js" type="text/javascript"></script>
1618 <script src="../../lib/qunit.js" type="text/javascript"></script>
Index: trunk/parsers/wikidom/tests/annotations/test.js
@@ -55,9 +55,47 @@
5656
5757 /* Tests */
5858
59 -test( 'Content.substring', function() {
60 - expect( 5 );
61 -
 59+test( 'Content modification', 17, function() {
 60+ content.on( 'change', function( args ) {
 61+ ok( true, 'Change events get triggered after ' + args.type + ' events' );
 62+ } );
 63+ content.on( 'insert', function( args ) {
 64+ ok( true, 'Insert events get triggered' );
 65+ equal( args.offset, 5, 'Insert events have correct offsets' );
 66+ deepEqual( args.content, ['a', 'b', 'c'], 'Insert events have correct content' );
 67+ deepEqual( content.data.slice( 5, 8 ), ['a', 'b', 'c'], 'Content is inserted correctly' );
 68+ // +1 change event
 69+ } );
 70+ content.on( 'annotate', function( args ) {
 71+ ok( true, 'Annotate events get triggered' );
 72+ equal( args.method, 'add', 'Annotate events have correct method' );
 73+ deepEqual( args.annotation, { 'type': 'italic' }, 'Annotate events have correct annotation' );
 74+ equal( args.start, 5, 'Annotate events have correct start points' );
 75+ equal( args.end, 6, 'Annotate events have correct end points' );
 76+ deepEqual(
 77+ content.data.slice( 4, 8 ),
 78+ [' ', ['a', { 'type': 'italic' }], 'b', 'c'],
 79+ 'Content is annotated correctly'
 80+ );
 81+ // +1 change event
 82+ } );
 83+ content.on( 'remove', function( args ) {
 84+ ok( true, 'Remove events get triggered' );
 85+ equal( args.start, 5, 'Remove events have correct start points' );
 86+ equal( args.end, 8, 'Remove events have correct end points' );
 87+ deepEqual(
 88+ content.data.slice( 4, 8 ),
 89+ [' ', 'i', 's', ' '],
 90+ 'Content is removed correctly'
 91+ );
 92+ // +1 change event
 93+ } );
 94+ content.insert( 5, ['a', 'b', 'c'] );
 95+ content.annotate( 'add', { 'type': 'italic' }, 5, 6 );
 96+ content.remove( 5, 8 );
 97+} );
 98+
 99+test( 'Content.substring', 5, function() {
62100 equal(
63101 content.substring( 3, 39 ),
64102 's is a test paragraph!\nParagraphs ca',
@@ -85,11 +123,11 @@
86124 );
87125 } );
88126
89 -test( 'Content.getLength', function() {
90 - equals( content.getLength(), 65, 'Returns correct length' );
91 -} )
 127+test( 'Content.getLength', 1, function() {
 128+ equal( content.getLength(), 65, 'Returns correct length' );
 129+} );
92130
93 -test( 'Content.slice', function() {
 131+test( 'Content.slice', 2, function() {
94132 deepEqual(
95133 content.slice().data,
96134 [
Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -10,6 +10,7 @@
1111 * @returns {Content}
1212 */
1313 function Content( content ) {
 14+ EventEmitter.call( this );
1415 this.data = content || [];
1516 };
1617
@@ -296,23 +297,28 @@
297298 *
298299 * Inserted content will inherit annotations from neighboring content.
299300 *
300 - * @param start {Integer} Position to insert content at
301 - * @param insert {Array} Content data to insert
 301+ * @param offset {Integer} Position to insert content at
 302+ * @param content {Array} Content data to insert
302303 */
303 -Content.prototype.insert = function( start, insert ) {
 304+Content.prototype.insert = function( offset, content ) {
304305 // TODO: Prefer to not take annotations from a neighbor that's a space character
305 - var neighbor = this.data[Math.max( start - 1, 0 )];
 306+ var neighbor = this.data[Math.max( offset - 1, 0 )];
306307 if ( $.isArray( neighbor ) ) {
307308 var annotations = neighbor.slice( 1 );
308309 var i;
309 - for ( i = 0; i < insert.length; i++ ) {
310 - if ( typeof insert[i] === 'string' ) {
311 - insert[i] = [insert[i]];
 310+ for ( i = 0; i < content.length; i++ ) {
 311+ if ( typeof content[i] === 'string' ) {
 312+ content[i] = [content[i]];
312313 }
313 - insert[i] = insert[i].concat( annotations );
 314+ content[i] = content[i].concat( annotations );
314315 }
315316 }
316 - Array.prototype.splice.apply( this.data, [start, 0].concat( insert ) );
 317+ Array.prototype.splice.apply( this.data, [offset, 0].concat( content ) );
 318+ this.emit( 'insert', {
 319+ 'offset': offset,
 320+ 'content': content
 321+ } );
 322+ this.emit( 'change', { 'type': 'insert' } );
317323 };
318324
319325 /**
@@ -323,6 +329,11 @@
324330 */
325331 Content.prototype.remove = function( start, end ) {
326332 this.data.splice( start, end - start );
 333+ this.emit( 'remove', {
 334+ 'start': start,
 335+ 'end': end
 336+ } );
 337+ this.emit( 'change', { 'type': 'remove' } );
327338 };
328339
329340 /**
@@ -461,6 +472,13 @@
462473 }
463474 }
464475 }
 476+ this.emit( 'annotate', {
 477+ 'method': method,
 478+ 'annotation': annotation,
 479+ 'start': start,
 480+ 'end': end
 481+ } );
 482+ this.emit( 'change', { 'type': 'annotate' } );
465483 };
466484
467485 /**
@@ -513,3 +531,5 @@
514532 }
515533 return out;
516534 };
 535+
 536+extend( Content, EventEmitter );
Index: trunk/parsers/wikidom/lib/es/es.EventEmitter.js
@@ -0,0 +1,77 @@
 2+function EventEmitter() {
 3+ this.events = {};
 4+}
 5+
 6+EventEmitter.prototype.emit = function( type ) {
 7+ if ( type === 'error' && !( 'error' in this.events ) ) {
 8+ throw 'Missing error handler error.';
 9+ }
 10+ if ( !( type in this.events ) ) {
 11+ return false;
 12+ }
 13+ var listeners = this.events[type].slice();
 14+ var args = Array.prototype.slice.call( arguments, 1 );
 15+ for ( var i = 0; i < listeners.length; i++ ) {
 16+ listeners[i].apply( this, args );
 17+ }
 18+ return true;
 19+};
 20+
 21+EventEmitter.prototype.addListener = function( type, listener ) {
 22+ if ( typeof listener !== 'function' ) {
 23+ throw 'Invalid listener error. Function expected.';
 24+ }
 25+ this.emit( 'newListener', type, listener );
 26+ if ( type in this.events ) {
 27+ this.events[type].push( listener );
 28+ } else {
 29+ this.events[type] = [listener];
 30+ }
 31+ return this;
 32+};
 33+
 34+EventEmitter.prototype.on = function( type, listener ) {
 35+ this.addListener( type, listener );
 36+};
 37+
 38+EventEmitter.prototype.once = function( type, listener ) {
 39+ var that = this;
 40+ this.addListener( type, function g() {
 41+ that.removeListener( type, g );
 42+ listener.apply( that, arguments );
 43+ } );
 44+};
 45+
 46+EventEmitter.prototype.removeListener = function( type, listener ) {
 47+ if ( typeof listener !== 'function' ) {
 48+ throw 'Invalid listener error. Function expected.';
 49+ }
 50+ if ( !( type in this.events ) || !this.events[type].length ) {
 51+ return this;
 52+ }
 53+ var handlers = this.events[type];
 54+ if ( handlers.length == 1 && handlers[0] === listener ) {
 55+ delete this.events[type];
 56+ } else {
 57+ var i = handlers.indexOf( listener );
 58+ if ( i < 0 ) {
 59+ return this;
 60+ }
 61+ handlers.splice( i, 1 );
 62+ if ( handlers.length == 0 ) {
 63+ delete this.events[type];
 64+ }
 65+ }
 66+ return this;
 67+};
 68+
 69+EventEmitter.prototype.removeAllListeners = function( type ) {
 70+ if ( type in this.events ) {
 71+ delete this.events[type];
 72+ }
 73+ return this;
 74+};
 75+
 76+EventEmitter.prototype.listeners = function( type ) {
 77+ return type in this.events ? this.events[type] : [];
 78+};
Property changes on: trunk/parsers/wikidom/lib/es/es.EventEmitter.js
___________________________________________________________________
Added: svn:eol-style
179 + native
Added: svn:mime-type
280 + text/plain
Index: trunk/parsers/wikidom/demos/es/index.html
@@ -52,6 +52,7 @@
5353 <!-- EditSurface -->
5454 <script type="text/javascript" src="../../lib/jquery.js"></script>
5555 <script type="text/javascript" src="../../lib/es/es.js"></script>
 56+ <script type="text/javascript" src="../../lib/es/es.EventEmitter.js"></script>
5657 <script type="text/javascript" src="../../lib/es/es.Content.js"></script>
5758 <script type="text/javascript" src="../../lib/es/es.Block.js"></script>
5859 <script type="text/javascript" src="../../lib/es/es.Document.js"></script>

Status & tagging log