Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where el.splice could not clear full array #5051

Merged
merged 4 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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