Skip to content

Commit

Permalink
Merge pull request #5051 from Polymer/array-splice-only-start
Browse files Browse the repository at this point in the history
Fix issue where el.splice could not clear full array
  • Loading branch information
kevinpschaaf authored Jan 31, 2018
2 parents 0856df4 + 4a4b2ca commit 4c8d255
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/mixins/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -1933,13 +1933,33 @@
// Normalize fancy native splice handling of crazy start values
if (start < 0) {
start = array.length - Math.floor(-start);
} else {
} else if (start) {
start = Math.floor(start);
}
if (!start) {
start = 0;
// array.splice does different things based on the number of arguments
// you pass in. Therefore, array.splice(0) and array.splice(0, undefined)
// do different things. In the former, the whole array is cleared. In the
// latter, no items are removed.
// This means that we need to detect whether 1. one of the arguments
// is actually passed in and then 2. determine how many arguments
// we should pass on to the native array.splice
//
let ret;
// Omit any additional arguments if they were not passed in
if (arguments.length === 2) {
ret = array.splice(start);
// Either start was undefined and the others were defined, but in this
// case we can safely pass on all arguments
//
// Note: this includes the case where none of the arguments were passed in,
// e.g. this.splice('array'). However, if both start and deleteCount
// are undefined, array.splice will not modify the array (as expected)
} else {
ret = array.splice(start, deleteCount, ...items);
}
let ret = array.splice(start, deleteCount, ...items);
// At the end, check whether any items were passed in (e.g. insertions)
// or if the return array contains items (e.g. deletions).
// Only notify if items were added or deleted.
if (items.length || ret.length) {
notifySplice(this, array, info.path, start, items.length, ret);
}
Expand Down
7 changes: 7 additions & 0 deletions test/unit/path-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,13 @@
assert.equal(el.$.boundChild.arrayLength, el.data.length);
});

test('splice correctly deletes full array if only 1 argument is passed in', function() {
el.data = [1,2,3];
assert.equal(el.data.length, 3);
el.splice('data', 0);
assert.equal(el.data.length, 0);
});

});

suite('path API', function() {
Expand Down

0 comments on commit 4c8d255

Please sign in to comment.