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

Checking instanceof Arrays cross frames (needed for the vjs.Player.prototype.src method) #1218

Closed
wants to merge 7 commits into from

Conversation

shmulik
Copy link
Contributor

@shmulik shmulik commented May 18, 2014

vjs.Player.prototype.src uses the instanceof Array method.
This will not work for arrays created on another frame,
Instead of instanceof we should use Array.isArray, but this is not supported on IE8.
So vjs.obj.isArray shim was created for the isArray method.

instanceof Array, will not work on arrays created cross frames.
Array,isArray is the method to check if an object isArray across frames.
IE8 does not suppoort the Array.isArray method, so we have to create a shim to support all browsers.
instanceof Array, will not work on arrays created cross frames.
Array,isArray is the method to check if an object isArray across frames.
IE8 does not suppoort the Array.isArray method, so we have to create a shim to support all browsers.
* @private
*/
vjs.obj.isArray = Array.isArray || function(arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
Copy link
Member

Choose a reason for hiding this comment

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

You need to use single quotes, which is why the build failed on travis.

@gkatsev
Copy link
Member

gkatsev commented May 18, 2014

Other than that, looks good.

Are there any other cases of foo instanceof Array in the code base? Maybe hunt those down as well?

@mmcc mmcc added bug labels May 19, 2014
@shmulik
Copy link
Contributor Author

shmulik commented May 19, 2014

Thanks @gkatsev.
So I guess I need to make another pull request....
I did not find another instanceof Array in the code base

@gkatsev
Copy link
Member

gkatsev commented May 19, 2014

So I guess I need to make another pull request....

Nope. Just do another commit to the same branch and this PR will pick it up and re-run the build.

I did not find another instanceof Array in the code base

Cool. That's fine.

vjs.Player.prototype.src uses the instanceof Array method.
This will not work for arrays created on another frame,
Instead of instanceof we should use Array.isArray, but this is not supported on IE8.
So vjs.obj.isArray shim was created for the isArray method.
@heff
Copy link
Member

heff commented May 19, 2014

Great stuff! Thanks @shmulik.

We do need to add tests to finish this off, but that shouldn't be too difficult for this. The tests would be added to the test/unit/lib.js file.

You can follow how some of the other tests are written to get started. I think we would need at least two assertions here.

  • Check that an array is appropriately identified as an array
  • Check that an object is not identified as an array

Would you be up for doing that?

@gkatsev
Copy link
Member

gkatsev commented May 19, 2014

Can you also do the same replacement for the instanceof Array here.

While we're at it, we also probably want to do a similar thing to objects:

} else if (source instanceof Object) {

@heff
Copy link
Member

heff commented May 19, 2014

Good find.

we also probably want to do a similar thing to objects

should we just be using typeof source === 'object' there instead?

@gkatsev
Copy link
Member

gkatsev commented May 19, 2014

no, because if source is null, it will fail.
We also want to do the toString.call trick for objects as well.

@heff
Copy link
Member

heff commented May 19, 2014

We also want to do the toString.call trick for objects as well

Except that won't work if the source object is a source element.

Object.prototype.toString.call(document.createElement('source'));
--> "[object HTMLSourceElement]"

Either way, if @shmulik doesn't want to take on the object piece for this specific issue we can create a new issue for.

@gkatsev
Copy link
Member

gkatsev commented May 19, 2014

Yep, that can be done in a different commit. The component piece, might as well be done here as well :D

@heff
Copy link
Member

heff commented May 19, 2014

Yeah, the instance of Array in Component should be updated here

@shmulik
Copy link
Contributor Author

shmulik commented May 20, 2014

Sorry for my late response, I am on a different time zone.....
I will take care for the instanceof Array in component.js.(missed it somehow)
And will take a look at using a similar trick for objects

Using the instanceof Array method will not work for arrays created on another frame,
Instead of instanceof we should use Array.isArray, but this is not supported on IE8.
So vjs.obj.isArray shim was created for the isArray method.
@shmulik
Copy link
Contributor Author

shmulik commented May 20, 2014

I will also do the tests for the isArray, but only for arrays created on the same frame. Is this good enough?

Adding test for the vjs.obj.isArray method
Added test for the vjs.obj.isArray method.
Added test for the vgs.obj.isArray
@shmulik
Copy link
Contributor Author

shmulik commented May 20, 2014

@gkatsev , @heff

What do you say about the following code for the Object trick:

vjs.obj.isObject = function(obj) {
return Object.prototype.toString.call(obj) === '[object Object];
};

vjs.obj.isSourceObject = function(src) {
return vjs.obj.isObject(src) || Object.prototype.toString.call(src) === '[object HTMLSourceElement]'
}

@heff
Copy link
Member

heff commented May 20, 2014

Yeah, same frame is fine.


Steve Heffernan (mobile)

On May 19, 2014, at 9:04 PM, shmulik notifications@github.com wrote:

I will also do the tests for the isArray, but only for arrays created on the same frame. Is this good enough?


Reply to this email directly or view it on GitHub.

@heff
Copy link
Member

heff commented May 20, 2014

We need to think a little more on where and how we'd use the object one because I don't think it's the right fit for that specific instance.


Steve Heffernan (mobile)

On May 19, 2014, at 10:19 PM, shmulik notifications@github.com wrote:

@gkatsev , @heff

What do you say about the following code for the Object trick:

vjs.obj.isObject = function(obj) {
return Object.prototype.toString.call(obj) === '[object Object];
};

vjs.obj.isSourceObject = function(src) {
return vjs.obj.isObject(src) || Object.prototype.toString.call(src) === '[object HTMLSourceElement]'
}


Reply to this email directly or view it on GitHub.

@heff heff closed this in 6d78c95 Jun 12, 2014
@heff
Copy link
Member

heff commented Jun 12, 2014

Merged in. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants