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 2 issues breaking IE8 compatibility #764

Merged
merged 6 commits into from
Mar 17, 2017

Conversation

wyattoday
Copy link
Contributor

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.

…first time it's used.

Fix "indexof" to use the jquery ".inArray" to keep IE8 compatibility.
@wyattoday
Copy link
Contributor Author

Also, this fixes issue #747

Copy link
Collaborator

@Rycochet Rycochet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly good, just a couple of questions on your code :-)

velocity.js Outdated
var _slice = (function() {
var slice = Array.prototype.slice;
var _fslice = Array.prototype.slice;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to change the function name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll change that.

velocity.js Outdated
slice = function() {
var i = this.length,
clone = [];
Array.prototype.slice = function(begin, end) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't override global Array.slice - other things might be doing it - make sure you find places using Array.slice() and have them use this instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Array.slice() is failing in the first place (because it doesn't exist) I don't see the harm in making it work by defining it globally.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no reason for it to fail, if you look at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/slice#Browser_compatibility then it's pretty much always been supported - so if it's not there then assigning it is actually changing something that is out of Velocity's scope (such as replacing Array entirely) - hence safer to leave it broken and sit quietly in our own little corner and ignoring the broken site ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree. Slice is supported in modern browsers, but that excludes IE8 (a relic that we still support for the next couple of years). And thus users using slice on IE8 will be using a similar shim to gain that ability on IE8. Modern browsers won't have their implementation of slice replaced at all (because the catch() block won't be called).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not our place to shim anything globally - if they want to shim it, then they can decide to do so, not get it as a side-effect of using an animation library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I'll rework that.

velocity.js Outdated
clone = [];
Array.prototype.slice = function(begin, end) {
// IE < 9 gets unhappy with an undefined end argument
end = (typeof end !== 'undefined') ? end : this.length;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(end !== undefined) - fully supported and slightly nicer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I'll make that change.

velocity.js Outdated
end = (typeof end !== 'undefined') ? end : this.length;

// For native Array objects, we use the native slice function
if (Object.prototype.toString.call(this) === '[object Array]'){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just if(this.slice){ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that's better. I'll make that change.

@Rycochet
Copy link
Collaborator

Would be nice to get this in today too - then hopefully have a fully IE8 compatible release! :-)

…he "Array.prototype.slice" function, and instead writing to an "internal" Array.prototype._vel_slice.
velocity.js Outdated

// Quick exit if we've already defined the function.
// Just return it now rather than redefining it.
if (Array.prototype._vel_slice !== undefined)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an IIFE - called a single time and the results then stored in _slice on line 537 - it's not possible to call it again so no need to cache as it simply exists (or not)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, sorry, I wasn't thinking. I'll fix that.

velocity.js Outdated
var _slice = (function() {
var slice = Array.prototype.slice;

try {
// Can't be used with DOM elements in IE < 9
slice.call(document.documentElement);
return Array.prototype.slice;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're caching it above, so can save a few bytes by returning slice directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I'll fix that.

velocity.js Outdated
@@ -1447,7 +1488,7 @@
getUnit: function(str, start) {
var unit = (str.substr(start || 0, 5).match(/^[a-z%]+/) || [])[0] || "";

if (unit && CSS.Lists.units.indexOf(unit) >= 0) {
if (unit && $.inArray(unit, CSS.Lists.units) >= 0) {
Copy link
Collaborator

@Rycochet Rycochet Mar 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah - just had a look, and $.inArray is jQuery only - we don't shim it - if you take these out I'll add a shim when I get home then do the release

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll remove that from my branch.

@Rycochet Rycochet merged commit 6eb4403 into julianshapiro:master Mar 17, 2017
Rycochet pushed a commit that referenced this pull request Aug 3, 2020
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants