Skip to content

Commit a51de9e

Browse files
committed
Fix issue where el.splice could not clear full array
1 parent 61d8c3e commit a51de9e

2 files changed

Lines changed: 31 additions & 4 deletions

File tree

lib/mixins/property-effects.html

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,13 +1933,33 @@
19331933
// Normalize fancy native splice handling of crazy start values
19341934
if (start < 0) {
19351935
start = array.length - Math.floor(-start);
1936-
} else {
1936+
} else if (start) {
19371937
start = Math.floor(start);
19381938
}
1939-
if (!start) {
1940-
start = 0;
1939+
// array.splice does different things based on the number of arguments
1940+
// you pass in. Therefore, array.splice(0) and array.splice(0, undefined)
1941+
// do different things. In the former, the whole array is cleared. In the
1942+
// latter, no items are removed.
1943+
// This means that we need to detect whether 1. one of the arguments
1944+
// is actually passed in and then 2. determine how many arguments
1945+
// we should pass on to the native array.splice
1946+
//
1947+
let ret;
1948+
// Omit any additional arguments if they were not passed in
1949+
if (start !== undefined && !deleteCount && !items.length) {
1950+
ret = array.splice(start);
1951+
// Either start was undefined and the others were defined, but in this
1952+
// case we can safely pass on all arguments
1953+
//
1954+
// Note: this includes the case where none of the arguments were passed in,
1955+
// e.g. this.splice('array'). However, if both start and deleteCount
1956+
// are undefined, array.splice will not modify the array (as expected)
1957+
} else {
1958+
ret = array.splice(start, deleteCount, ...items);
19411959
}
1942-
let ret = array.splice(start, deleteCount, ...items);
1960+
// At the end, check whether any items were passed in (e.g. insertions)
1961+
// or if the return array contains items (e.g. deletions).
1962+
// Only notify if items were added or deleted.
19431963
if (items.length || ret.length) {
19441964
notifySplice(this, array, info.path, start, items.length, ret);
19451965
}

test/unit/path-effects.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,13 @@
498498
assert.equal(el.$.boundChild.arrayLength, el.data.length);
499499
});
500500

501+
test('splice correctly deletes length', function() {
502+
el.data = [1,2,3];
503+
assert.equal(el.data.length, 3);
504+
el.splice('data', 0);
505+
assert.equal(el.data.length, 0);
506+
});
507+
501508
});
502509

503510
suite('path API', function() {

0 commit comments

Comments
 (0)