fix ie8 splice bug
This commit is contained in:
parent
0b162a9411
commit
ce366eed62
1 changed files with 88 additions and 10 deletions
|
|
@ -176,17 +176,95 @@ if ((supportsAccessors = owns(prototypeOfObject, "__defineGetter__"))) {
|
|||
// [bugfix, ielt9, old browsers]
|
||||
// IE < 9 bug: [1,2].splice(0).join("") == "" but should be "12"
|
||||
if ([1,2].splice(0).length != 2) {
|
||||
var array_splice = Array.prototype.splice;
|
||||
Array.prototype.splice = function(start, deleteCount) {
|
||||
if (!arguments.length) {
|
||||
return [];
|
||||
} else {
|
||||
return array_splice.apply(this, [
|
||||
start === void 0 ? 0 : start,
|
||||
deleteCount === void 0 ? (this.length - start) : deleteCount
|
||||
].concat(slice.call(arguments, 2)))
|
||||
if(function() { // test IE < 9 to splice bug - see issue #138
|
||||
function makeArray(l) {
|
||||
var a = new Array(l+2);
|
||||
a[0] = a[1] = 0;
|
||||
return a;
|
||||
}
|
||||
};
|
||||
var array = [], lengthBefore;
|
||||
|
||||
array.splice.apply(array, makeArray(20));
|
||||
array.splice.apply(array, makeArray(26));
|
||||
|
||||
lengthBefore = array.length; //46
|
||||
array.splice(5, 0, "XXX"); // add one element
|
||||
|
||||
lengthBefore + 1 == array.length
|
||||
|
||||
if (lengthBefore + 1 == array.length) {
|
||||
return true;// has right splice implementation without bugs
|
||||
}
|
||||
// else {
|
||||
// IE8 bug
|
||||
// }
|
||||
}()) {//IE 6/7
|
||||
var array_splice = Array.prototype.splice;
|
||||
Array.prototype.splice = function(start, deleteCount) {
|
||||
if (!arguments.length) {
|
||||
return [];
|
||||
} else {
|
||||
return array_splice.apply(this, [
|
||||
start === void 0 ? 0 : start,
|
||||
deleteCount === void 0 ? (this.length - start) : deleteCount
|
||||
].concat(slice.call(arguments, 2)))
|
||||
}
|
||||
};
|
||||
} else {//IE8
|
||||
// taken from http://docs.sencha.com/ext-js/4-1/source/Array2.html
|
||||
Array.prototype.splice = function(pos, removeCount){
|
||||
var length = this.length;
|
||||
if (pos > 0) {
|
||||
if (pos > length)
|
||||
pos = length;
|
||||
} else if (pos == void 0) {
|
||||
pos = 0;
|
||||
} else if (pos < 0) {
|
||||
pos = Math.max(length + pos, 0);
|
||||
}
|
||||
|
||||
if (!(pos+removeCount < length))
|
||||
removeCount = length - pos;
|
||||
|
||||
var removed = this.slice(pos, pos+removeCount);
|
||||
var insert = slice.call(arguments, 2);
|
||||
var add = insert.length;
|
||||
|
||||
// we try to use Array.push when we can for efficiency...
|
||||
if (pos === length) {
|
||||
if (add) {
|
||||
this.push.apply(this, insert);
|
||||
}
|
||||
} else {
|
||||
var remove = Math.min(removeCount, length - pos);
|
||||
var tailOldPos = pos + remove;
|
||||
var tailNewPos = tailOldPos + add - remove;
|
||||
var tailCount = length - tailOldPos;
|
||||
var lengthAfterRemove = length - remove;
|
||||
|
||||
if (tailNewPos < tailOldPos) { // case A
|
||||
for (var i = 0; i < tailCount; ++i) {
|
||||
this[tailNewPos+i] = this[tailOldPos+i];
|
||||
}
|
||||
} else if (tailNewPos > tailOldPos) { // case B
|
||||
for (i = tailCount; i--; ) {
|
||||
this[tailNewPos+i] = this[tailOldPos+i];
|
||||
}
|
||||
} // else, add == remove (nothing to do)
|
||||
|
||||
if (add && pos === lengthAfterRemove) {
|
||||
this.length = lengthAfterRemove; // truncate array
|
||||
this.push.apply(this, insert);
|
||||
} else {
|
||||
this.length = lengthAfterRemove + add; // reserves space
|
||||
for (i = 0; i < add; ++i) {
|
||||
this[pos+i] = insert[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ES5 15.4.3.2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue