Index: trunk/parsers/wikidom/tests/hype/es.test.js |
— | — | @@ -0,0 +1,14 @@ |
| 2 | +module( 'es' ); |
| 3 | + |
| 4 | +test( 'es.spliceArray', 1, function() { |
| 5 | + var insert = [], i, arr = ['foo', 'bar'], expected = []; |
| 6 | + expected[0] = 'foo'; |
| 7 | + for ( i = 0; i < 3000; i++ ) { |
| 8 | + insert[i] = i; |
| 9 | + expected[i + 1] = i; |
| 10 | + } |
| 11 | + expected[3001] = 'bar'; |
| 12 | + |
| 13 | + es.spliceArray( arr, 1, insert ); |
| 14 | + deepEqual( arr, expected, 'splicing 3000 elements into the middle of a 2-element array' ); |
| 15 | +} ); |
Property changes on: trunk/parsers/wikidom/tests/hype/es.test.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 16 | + native |
Index: trunk/parsers/wikidom/tests/hype/index.html |
— | — | @@ -26,6 +26,7 @@ |
27 | 27 | <script src="../../lib/hype/models/es.TableCellModel.js"></script> |
28 | 28 | <script src="../../lib/hype/models/es.TableModel.js"></script> |
29 | 29 | <script src="../../lib/hype/models/es.TableRowModel.js"></script> |
| 30 | + <script src="es.test.js"></script> |
30 | 31 | <script src="es.ModelNode.test.js"></script> |
31 | 32 | <script src="es.DocumentModel.test.js"></script> |
32 | 33 | </body> |
Index: trunk/parsers/wikidom/lib/hype/es.js |
— | — | @@ -119,3 +119,26 @@ |
120 | 120 | } |
121 | 121 | return destination; |
122 | 122 | }; |
| 123 | + |
| 124 | +/** |
| 125 | + * Splice one array into another. This is the equivalent of arr.splice( offset, 0, i1, i2, i3, ... ) |
| 126 | + * except that i1, i2, i3, ... are specified as an array rather than separate parameters. |
| 127 | + * |
| 128 | + * @static |
| 129 | + * @method |
| 130 | + * @param arr {Array} Array to splice insertion into. Will be modified |
| 131 | + * @param offset {Number} Offset in arr to splice insertion in at. May be negative; see the 'index' parameter for Array.prototype.splice() |
| 132 | + * @param insertion {Array} Array to insert |
| 133 | + */ |
| 134 | +es.spliceArray = function( arr, offset, insertion ) { |
| 135 | + // We need to splice insertion in in batches, because of parameter list length limits which vary cross-browser. |
| 136 | + // 1024 seems to be a safe batch size on all browsers. |
| 137 | + var index = 0, batchSize = 1024; |
| 138 | + while ( index < insertion.length ) { |
| 139 | + // Call arr.splice( offset, 0, i0, i1, i2, ..., i1023 ); |
| 140 | + arr.splice.apply( |
| 141 | + arr, [index + offset, 0].concat( insertion.slice( index, index + batchSize ) ) |
| 142 | + ); |
| 143 | + index += batchSize; |
| 144 | + } |
| 145 | +}; |
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -79,16 +79,7 @@ |
80 | 80 | } |
81 | 81 | |
82 | 82 | function insert( op ) { |
83 | | - // Splice content into document in 1024 element batches, as to not overflow max allowed |
84 | | - // arguments, which apply is limited by |
85 | | - var index = 0, |
86 | | - batchSize = 1024; |
87 | | - while ( index < op.data.length ) { |
88 | | - this.data.splice.apply( |
89 | | - this.data, [this.cursor, 0].concat( op.data.slice( index, index + batchSize ) ) |
90 | | - ); |
91 | | - index += batchSize; |
92 | | - } |
| 83 | + es.spliceArray( this.data, this.cursor, op.data ); |
93 | 84 | annotate.call( this, this.cursor + op.data.length ); |
94 | 85 | this.cursor += op.data.length; |
95 | 86 | } |