http://www.mediawiki.org/wiki/Special:Code/MediaWiki/100732

Revision: 100732
Author:   catrope
Date:     2011-10-25 19:24:23 +0000 (Tue, 25 Oct 2011)
Log Message:
-----------
Factor out the batched-splice-apply code into es.spliceArray(), fix a logic 
error in the code, and add a test case

Modified Paths:
--------------
    trunk/parsers/wikidom/lib/hype/es.js
    trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
    trunk/parsers/wikidom/tests/hype/index.html

Added Paths:
-----------
    trunk/parsers/wikidom/tests/hype/es.test.js

Modified: trunk/parsers/wikidom/lib/hype/es.js
===================================================================
--- trunk/parsers/wikidom/lib/hype/es.js        2011-10-25 19:14:17 UTC (rev 
100731)
+++ trunk/parsers/wikidom/lib/hype/es.js        2011-10-25 19:24:23 UTC (rev 
100732)
@@ -119,3 +119,26 @@
        }
        return destination;
 };
+
+/**
+ * Splice one array into another. This is the equivalent of arr.splice( 
offset, 0, i1, i2, i3, ... )
+ * except that i1, i2, i3, ... are specified as an array rather than separate 
parameters.
+ * 
+ * @static
+ * @method
+ * @param arr {Array} Array to splice insertion into. Will be modified
+ * @param offset {Number} Offset in arr to splice insertion in at. May be 
negative; see the 'index' parameter for Array.prototype.splice()
+ * @param insertion {Array} Array to insert
+ */
+es.spliceArray = function( arr, offset, insertion ) {
+       // We need to splice insertion in in batches, because of parameter list 
length limits which vary cross-browser.
+       // 1024 seems to be a safe batch size on all browsers.
+       var index = 0, batchSize = 1024;
+       while ( index < insertion.length ) {
+               // Call arr.splice( offset, 0, i0, i1, i2, ..., i1023 );
+               arr.splice.apply(
+                       arr, [index + offset, 0].concat( insertion.slice( 
index, index + batchSize ) )
+               );
+               index += batchSize;
+       }
+};

Modified: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
===================================================================
--- trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js   2011-10-25 
19:14:17 UTC (rev 100731)
+++ trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js   2011-10-25 
19:24:23 UTC (rev 100732)
@@ -79,16 +79,7 @@
        }
        
        function insert( op ) {
-               // Splice content into document in 1024 element batches, as to 
not overflow max allowed
-               // arguments, which apply is limited by
-               var index = 0,
-                       batchSize = 1024;
-               while ( index < op.data.length ) {
-                       this.data.splice.apply(
-                               this.data, [this.cursor, 0].concat( 
op.data.slice( index, index + batchSize ) )
-                       );
-                       index += batchSize;
-               }
+               es.spliceArray( this.data, this.cursor, op.data );
                annotate.call( this, this.cursor + op.data.length );
                this.cursor += op.data.length;
        }

Added: trunk/parsers/wikidom/tests/hype/es.test.js
===================================================================
--- trunk/parsers/wikidom/tests/hype/es.test.js                         (rev 0)
+++ trunk/parsers/wikidom/tests/hype/es.test.js 2011-10-25 19:24:23 UTC (rev 
100732)
@@ -0,0 +1,14 @@
+module( 'es' );
+
+test( 'es.spliceArray', 1, function() {
+       var insert = [], i, arr = ['foo', 'bar'], expected = [];
+       expected[0] = 'foo';
+       for ( i = 0; i < 3000; i++ ) {
+               insert[i] = i;
+               expected[i + 1] = i;
+       }
+       expected[3001] = 'bar';
+       
+       es.spliceArray( arr, 1, insert );
+       deepEqual( arr, expected, 'splicing 3000 elements into the middle of a 
2-element array' );
+} );


Property changes on: trunk/parsers/wikidom/tests/hype/es.test.js
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: trunk/parsers/wikidom/tests/hype/index.html
===================================================================
--- trunk/parsers/wikidom/tests/hype/index.html 2011-10-25 19:14:17 UTC (rev 
100731)
+++ trunk/parsers/wikidom/tests/hype/index.html 2011-10-25 19:24:23 UTC (rev 
100732)
@@ -26,6 +26,7 @@
                <script 
src="../../lib/hype/models/es.TableCellModel.js"></script>
                <script src="../../lib/hype/models/es.TableModel.js"></script>
                <script 
src="../../lib/hype/models/es.TableRowModel.js"></script>
+               <script src="es.test.js"></script>
                <script src="es.ModelNode.test.js"></script>
                <script src="es.DocumentModel.test.js"></script>
        </body>


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to