mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-24 22:35:41 +00:00
Merge "Fix error in ve.copyArray() and add tests"
This commit is contained in:
commit
193b70f36d
|
@ -176,3 +176,49 @@ QUnit.test( 'getObjectValues', 6, function ( assert ) {
|
|||
'Throw exception for non-object (null)'
|
||||
);
|
||||
} );
|
||||
|
||||
QUnit.test( 'copyArray', 6, function ( assert ) {
|
||||
var simpleArray = [ 'foo', 3 ],
|
||||
withObj = [ { 'bar': 'baz', 'quux': 3 }, 5 ],
|
||||
nestedArray = [ [ 'a', 'b' ], [ 1, 3, 4 ] ],
|
||||
sparseArray = [ 'a', undefined, undefined, 'b' ],
|
||||
withSparseArray = [ [ 'a', undefined, undefined, 'b' ] ],
|
||||
Cloneable = function ( p ) {
|
||||
this.p = p;
|
||||
};
|
||||
|
||||
Cloneable.prototype.clone = function () {
|
||||
return new Cloneable( this.p + '-clone' );
|
||||
};
|
||||
|
||||
assert.deepEqual(
|
||||
ve.copyArray( simpleArray ),
|
||||
simpleArray,
|
||||
'Simple array'
|
||||
);
|
||||
assert.deepEqual(
|
||||
ve.copyArray( withObj ),
|
||||
withObj,
|
||||
'Array containing object'
|
||||
);
|
||||
assert.deepEqual(
|
||||
ve.copyArray( [ new Cloneable( 'bar' ) ] ),
|
||||
[ new Cloneable( 'bar-clone' ) ],
|
||||
'Use the .clone() method if available'
|
||||
);
|
||||
assert.deepEqual(
|
||||
ve.copyArray( nestedArray ),
|
||||
nestedArray,
|
||||
'Nested array'
|
||||
);
|
||||
assert.deepEqual(
|
||||
ve.copyArray( sparseArray ),
|
||||
sparseArray,
|
||||
'Sparse array'
|
||||
);
|
||||
assert.deepEqual(
|
||||
ve.copyArray( withSparseArray ),
|
||||
withSparseArray,
|
||||
'Nested sparse array'
|
||||
);
|
||||
} );
|
||||
|
|
|
@ -320,13 +320,13 @@ ve.copyArray = function ( source ) {
|
|||
for ( i = 0; i < source.length; i++ ) {
|
||||
sourceValue = source[i];
|
||||
sourceType = typeof sourceValue;
|
||||
if ( sourceType === 'string' || sourceType === 'number' ) {
|
||||
if ( sourceType === 'string' || sourceType === 'number' || sourceType === 'undefined' ) {
|
||||
destination.push( sourceValue );
|
||||
} else if ( ve.isPlainObject( sourceValue ) ) {
|
||||
destination.push( ve.copyObject( sourceValue ) );
|
||||
} else if ( ve.isArray( sourceValue ) ) {
|
||||
destination.push( ve.copyArray( sourceValue ) );
|
||||
} else if ( typeof sourceValue.clone === 'function' ) {
|
||||
} else if ( sourceValue && typeof sourceValue.clone === 'function' ) {
|
||||
destination.push( sourceValue.clone() );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue