Skip to content

Commit

Permalink
Fix 2 issues breaking IE8 compatibility (#764)
Browse files Browse the repository at this point in the history
* Fix the shim _slice to work in IE8. Also caches the "slice" shim the first time it's used.

Fix "indexof" to use the jquery ".inArray" to keep IE8 compatibility.

* Incorporate changes suggested by @Rycochet including not overriding the "Array.prototype.slice" function, and instead writing to an "internal" Array.prototype._vel_slice.

* Remove pointless check for previously defined function (it's called once, fails once, never called again).

* Scratch that. Just store the actual custom function in _slice.

* Return the cached slice.

* Remove use of $.inArray so that @Rycochet can write a shim for it for non-jQuery users.
  • Loading branch information
wyattoday authored and Rycochet committed Aug 2, 2020
1 parent 392533b commit c3b4836
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions packages/velocity/velocity.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,29 +525,70 @@
return result;
}

/**
* Shim for "fixing" IE's lack of support (IE < 9) for applying slice
* on host objects like NamedNodeMap, NodeList, and HTMLCollection
* (technically, since host objects have been implementation-dependent,
* at least before ES2015, IE hasn't needed to work this way).
* Also works on strings, fixes IE < 9 to allow an explicit undefined
* for the 2nd argument (as in Firefox), and prevents errors when
* called on other DOM objects.
*/
var _slice = (function() {
var slice = Array.prototype.slice;

try {
// Can't be used with DOM elements in IE < 9
slice.call(document.documentElement);
return slice;
} catch (e) { // Fails in IE < 9

// This will work for genuine arrays, array-like objects,
// NamedNodeMap (attributes, entities, notations),
// NodeList (e.g., getElementsByTagName), HTMLCollection (e.g., childNodes),
// and will not fail on other DOM objects (as do DOM elements in IE < 9)
slice = function() {
var i = this.length,
clone = [];
return function(begin, end) {
// IE < 9 gets unhappy with an undefined end argument
end = (end !== undefined) ? end : this.length;

// For native Array objects, we use the native slice function
if (this.slice){
return slice.call(this, begin, end);
}

while (--i > 0) {
clone[i] = this[i];
// For array like object we handle it ourselves.
var i, cloned = [], size, len = this.length;

// Handle negative value for "begin"
var start = begin || 0;
start = (start >= 0) ? start : Math.max(0, len + start);

// Handle negative value for "end"
var upTo = (typeof end == 'number') ? Math.min(end, len) : len;
if (end < 0) {
upTo = len + end;
}
return clone;

// Actual expected size of the slice
size = upTo - start;

if (size > 0) {
cloned = new Array(size);
if (this.charAt) {
for (i = 0; i < size; i++) {
cloned[i] = this.charAt(start + i);
}
} else {
for (i = 0; i < size; i++) {
cloned[i] = this[start + i];
}
}
}

return cloned;
};
}
return slice;
})(); // TODO: IE8, Cache of Array.prototype.slice that works on IE8
})();

function sanitizeElements(elements) {
/* Unwrap jQuery/Zepto objects. */
Expand Down

0 comments on commit c3b4836

Please sign in to comment.