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

$(undefined).prop('href') throws error #875

Closed
rla opened this issue Jun 14, 2016 · 8 comments
Closed

$(undefined).prop('href') throws error #875

rla opened this issue Jun 14, 2016 · 8 comments

Comments

@rla
Copy link

rla commented Jun 14, 2016

$(undefined).prop('href') throws:

TypeError: Cannot read property 'hasOwnProperty' of undefined.

With jQuery the same expression returns undefined.

Tested with Cheerio 0.20.0

@sotojuan
Copy link

sotojuan commented Jun 15, 2016

Seems like the problem is here?

@rla
Copy link
Author

rla commented Jun 17, 2016

@sotojuan, if you are interested in taking over then go ahead.

@elias-winberg
Copy link
Contributor

elias-winberg commented Jun 17, 2016

It should be relatively easy to fix, no? If el isn't a valid tag immediately return. I'll try to create a pull request.

@Delgan
Copy link
Contributor

Delgan commented Jun 17, 2016

Is it related to #554?

@elias-winberg
Copy link
Contributor

Sort of, jQuery creates the element even if the attribute value is undefined, and cheerio doesn't.

@rla
Copy link
Author

rla commented Jun 18, 2016

I add some background info.

The bug might also be triggered when the selector does not match, like in the code:

$('.not-exists').attr('href')

It was not enough to check that $('.not-exists') is truthy before calling .attr() method. $ is Cheerio here. I ported the code from code using querySelector that behaves way more logically than jQuery.

Looking through the current Cheerio tests, it does not seem to consider negative cases when some input is undefiend or null.

@elias-winberg
Copy link
Contributor

.attr seems to be working as expected though (only .prop throws an error if the selector is invalid). I think this is due to the fact that the .getProp function lacks this line.

@fb55 fb55 closed this as completed Jul 4, 2016
@fb55
Copy link
Member

fb55 commented Jul 4, 2016

Fixed in #880

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

No branches or pull requests

5 participants