r95240 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95239‎ | r95240 | r95241 >
Date:20:27, 22 August 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Finished making transactions on blocks, adding some methods to conent and blocks to support this
Modified paths:
  • /trunk/parsers/wikidom/lib/es/es.Block.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.BlockTransaction.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Container.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Content.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Mutation.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js (modified) (history)
  • /trunk/parsers/wikidom/lib/es/es.Selection.js (modified) (history)
  • /trunk/parsers/wikidom/tests/transactions/test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/tests/transactions/test.js
@@ -87,12 +87,13 @@
8888 tx.add( 'remove', removal );
8989 tx.add( 'retain', 18 );
9090
91 - var committed = tx.commit( paragraph );
92 - equal( committed.getText(), after.getText(), 'Committing' );
93 - deepEqual( committed.getContent().getData(), after.getContent().getData(), 'Committing' );
94 - var rolledback = tx.rollback( committed );
95 - equal( rolledback.getText(), before.getText(), 'Rolling back' );
96 - deepEqual( rolledback.getContent().getData(), before.getContent().getData(), 'Rolling back' );
 91+ tx.commit( paragraph );
 92+ equal( paragraph.getText(), after.getText(), 'Committing' );
 93+ deepEqual( paragraph.getContent().getData(), after.getContent().getData(), 'Committing' );
 94+
 95+ tx.rollback( paragraph );
 96+ equal( paragraph.getText(), before.getText(), 'Rolling back' );
 97+ deepEqual( paragraph.getContent().getData(), before.getContent().getData(), 'Rolling back' );
9798 } );
9899
99100 test( 'Annotating', 4, function() {
@@ -134,11 +135,11 @@
135136 tx.add( 'end', annotation );
136137 tx.add( 'retain', 18 );
137138
138 - var committed = tx.commit( paragraph );
 139+ tx.commit( paragraph );
 140+ equal( paragraph.getText(), after.getText(), 'Committing' );
 141+ deepEqual( paragraph.getContent().getData(), after.getContent().getData(), 'Committing' );
139142
140 - equal( committed.getText(), after.getText(), 'Committing' );
141 - deepEqual( committed.getContent().getData(), after.getContent().getData(), 'Committing' );
142 - var rolledback = tx.rollback( committed );
143 - equal( rolledback.getText(), before.getText(), 'Rolling back' );
144 - deepEqual( rolledback.getContent().getData(), before.getContent().getData(), 'Rolling back' );
 143+ tx.rollback( paragraph );
 144+ equal( paragraph.getText(), before.getText(), 'Rolling back' );
 145+ deepEqual( paragraph.getContent().getData(), before.getContent().getData(), 'Rolling back' );
145146 } );
Index: trunk/parsers/wikidom/lib/es/es.Mutation.js
@@ -2,14 +2,18 @@
33 this.transactions = transactions || [];
44 };
55
6 -es.Mutation.prototype.add = function( id, transaction ) {
7 - this.transactions.push( { 'block': id, 'transaction': transaction } );
 6+es.Mutation.prototype.add = function( index, transaction ) {
 7+ this.transactions.push( { 'block': index, 'transaction': transaction } );
88 };
99
10 -es.Mutation.prototype.commit = function() {
11 -
 10+es.Mutation.prototype.commit = function( document ) {
 11+ for ( var i = 0; i < this.transactions.length; i++ ) {
 12+ this.transactions[i].transaction.commit( document.get( this.transactions[i].block ) );
 13+ }
1214 };
1315
14 -es.Mutation.prototype.rollback = function() {
15 -
 16+es.Mutation.prototype.rollback = function( document ) {
 17+ for ( var i = 0; i < this.transactions.length; i++ ) {
 18+ this.transactions[i].transaction.rollback( document.get( this.transactions[i].block ) );
 19+ }
1620 };
Index: trunk/parsers/wikidom/lib/es/es.BlockTransaction.js
@@ -50,7 +50,7 @@
5151 if ( add.length || rem.length ) {
5252 annotate( con, add, rem );
5353 }
54 - dst.insert( dst.getLength(), con.getData() );
 54+ dst.insertContent( dst.getLength(), con.getData() );
5555 return val;
5656 }
5757 function insert( val, cur, src, dst, add, rem ) {
@@ -58,7 +58,7 @@
5959 if ( add.length || rem.length ) {
6060 annotate( con, add, rem );
6161 }
62 - dst.insert( dst.getLength(), con.getData() );
 62+ dst.insertContent( dst.getLength(), con.getData() );
6363 return 0;
6464 }
6565 function start( val, cur, src, dst, add, rem ) {
@@ -158,28 +158,28 @@
159159 this.cursor += model.advance( val );
160160 };
161161
162 -es.BlockTransaction.prototype.commit = function( src ) {
 162+es.BlockTransaction.prototype.commit = function( dst ) {
163163 var cur = 0,
164 - dst = new es.Content(),
 164+ src = dst.getContent(),
165165 add = [],
166166 rem = [],
167167 adv;
 168+ dst.clearContent();
168169 for ( var i = 0; i < this.operations.length; i++ ) {
169170 var op = this.operations[i];
170171 cur += op.model.commit( op.val, cur, src, dst, add, rem );
171172 }
172 - return dst;
173173 };
174174
175 -es.BlockTransaction.prototype.rollback = function( src ) {
 175+es.BlockTransaction.prototype.rollback = function( dst ) {
176176 var cur = 0,
177 - dst = new es.Content(),
 177+ src = dst.getContent(),
178178 add = [],
179179 rem = [],
180180 adv;
 181+ dst.clearContent();
181182 for ( var i = 0; i < this.operations.length; i++ ) {
182183 var op = this.operations[i];
183184 cur += op.model.rollback( op.val, cur, src, dst, add, rem );
184185 }
185 - return dst;
186186 };
Index: trunk/parsers/wikidom/lib/es/es.Content.js
@@ -397,6 +397,19 @@
398398 };
399399
400400 /**
 401+ * Removes all content data.
 402+ *
 403+ * @method
 404+ * @emits "clear"
 405+ * @emits "change" with type:"clear" data property
 406+ */
 407+es.Content.prototype.clear = function() {
 408+ this.data = [];
 409+ this.emit( 'clear' );
 410+ this.emit( 'change', { 'type': 'clear' } );
 411+};
 412+
 413+/**
401414 * Gets a list of indexes within the content data which use a given annotation.
402415 *
403416 * Strict coverage may be used to compare not only annotation types, but also their data. Since new
Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js
@@ -74,6 +74,15 @@
7575 };
7676
7777 /**
 78+ * Deletes all content in a block.
 79+ *
 80+ * @method
 81+ */
 82+es.ParagraphBlock.prototype.clearContent = function() {
 83+ this.content.clear();
 84+};
 85+
 86+/**
7887 * Applies an annotation to a given range.
7988 *
8089 * If a range arguments are not provided, all content will be annotated.
Index: trunk/parsers/wikidom/lib/es/es.Container.js
@@ -34,6 +34,16 @@
3535 /* Methods */
3636
3737 /**
 38+ * Gets an item at a specific index.
 39+ *
 40+ * @method
 41+ * @returns {Object} Child object at index
 42+ */
 43+es.Container.prototype.get = function( index ) {
 44+ return this._list[index] || null;
 45+};
 46+
 47+/**
3848 * Gets the first item in the container.
3949 *
4050 * @method
Index: trunk/parsers/wikidom/lib/es/es.Selection.js
@@ -46,7 +46,7 @@
4747 } else {
4848 mutation = new es.Mutation();
4949 }
50 - mutation.add( this.start.block.getId(), this.start.block.prepareInsert( content ) );
 50+ mutation.add( this.start.block.getIndex(), this.start.block.prepareInsert( content ) );
5151 return mutation;
5252 }
5353 throw 'Mutation preparation error. Can not insert content at undefined location.';
@@ -58,7 +58,7 @@
5959 if ( this.start.block === this.end.block ) {
6060 // Single block deletion
6161 mutation.add(
62 - this.start.block.getId(),
 62+ this.start.block.getIndex(),
6363 this.start.block.prepareRemove( new es.Range( this.start.offset, this.end.offset ) )
6464 );
6565 } else {
@@ -66,17 +66,17 @@
6767 var block = this.start.block.next();
6868 // First
6969 mutation.add(
70 - block.getId(),
 70+ block.getIndex(),
7171 block.prepareRemove( new es.Range( this.start.offset, block.getLength() ) )
7272 );
7373 while ( ( block = block.next() ) !== this.end.block ) {
7474 // Middle
7575 mutation.add(
76 - block.getId(), block.prepareRemove( new es.Range( 0, block.getLength() ) )
 76+ block.getIndex(), block.prepareRemove( new es.Range( 0, block.getLength() ) )
7777 );
7878 }
7979 // Last
80 - mutation.add( block.getId(), block.prepareRemove( new es.Range( 0, this.end.offset ) ) );
 80+ mutation.add( block.getIndex(), block.prepareRemove( new es.Range( 0, this.end.offset ) ) );
8181 }
8282 return mutation;
8383 };
@@ -87,7 +87,7 @@
8888 if ( this.start.block === this.end.block ) {
8989 // Single block annotation
9090 mutation.add(
91 - this.start.block.getId(),
 91+ this.start.block.getIndex(),
9292 this.start.block.prepareRemove( new es.Range( this.start.offset, this.end.offset ) )
9393 );
9494 } else {
@@ -95,7 +95,7 @@
9696 var block = this.start.block.next();
9797 // First
9898 mutation.add(
99 - block.getId(),
 99+ block.getIndex(),
100100 block.prepareAnnotate(
101101 annotation, new es.Range( this.start.offset, block.getLength() )
102102 )
@@ -103,13 +103,14 @@
104104 while ( ( block = block.next() ) !== this.end.block ) {
105105 // Middle
106106 mutation.add(
107 - block.getId(),
 107+ block.getIndex(),
108108 block.prepareAnnotate( annotation, new es.Range( 0, block.getLength() ) )
109109 );
110110 }
111111 // Last
112112 mutation.add(
113 - block.getId(), block.prepareAnnotate( annotation, new es.Range( 0, this.end.offset ) )
 113+ block.getIndex(),
 114+ block.prepareAnnotate( annotation, new es.Range( 0, this.end.offset ) )
114115 );
115116 }
116117 return mutation;
Index: trunk/parsers/wikidom/lib/es/es.Block.js
@@ -125,6 +125,15 @@
126126 };
127127
128128 /**
 129+ * Deletes all content in a block.
 130+ *
 131+ * @method
 132+ */
 133+es.Block.prototype.clearContent = function() {
 134+ throw 'Block.deleteContent not implemented in this subclass.';
 135+};
 136+
 137+/**
129138 * Applies an annotation to a given range.
130139 *
131140 * If a range arguments are not provided, all content will be annotated.

Status & tagging log