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

parent() may fail in functions #1637

Merged
merged 2 commits into from
Jan 4, 2021
Merged

parent() may fail in functions #1637

merged 2 commits into from
Jan 4, 2021

Conversation

5saviahv
Copy link
Contributor

@5saviahv 5saviahv commented Jan 2, 2021

parent() don't work well in functions if selector is undefined

function getParent(el, parent) {
    return el.parent(parent).not('body');
}

var parent = getParent($('#rating span'))

In .parent() has condition:

...
  if (arguments.length) {
...

but it will fail if used like in function above, since arguments.length will be 1 even when selector is actually undefined.

@5saviahv 5saviahv added the javascript Pull requests that update Javascript code label Jan 2, 2021
@XhmikosR
Copy link
Contributor

XhmikosR commented Jan 2, 2021

This reminds me, shouldn't everything run in strict mode?

@fb55
Copy link
Member

fb55 commented Jan 4, 2021

Nice find! Happy to merge this, just out of curiosity: Is this actually a fix, or just a short-circuit for an empty input?

@5saviahv
Copy link
Contributor Author

5saviahv commented Jan 4, 2021

What do you mean ? arguments variable is array like, I would call arguments.length a hack.

@5saviahv 5saviahv force-pushed the parent branch 2 times, most recently from b76421d to 173a9c2 Compare January 4, 2021 11:56
@fb55
Copy link
Member

fb55 commented Jan 4, 2021

What I was trying to get at was does this actually produce an error? And yes, it does:

$('<a>').parent(undefined)
// => Uncaught TypeError: Cannot read property 'cheerio' of undefined

Could you add a test case that covers this?

@fb55 fb55 merged commit 43592d6 into cheeriojs:main Jan 4, 2021
@fb55
Copy link
Member

fb55 commented Jan 4, 2021

Awesome, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javascript Pull requests that update Javascript code
Development

Successfully merging this pull request may close these issues.

3 participants