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

.attr with value of undefined returns undefined #554

Closed
iwarren opened this issue Aug 26, 2014 · 0 comments · Fixed by #1582
Closed

.attr with value of undefined returns undefined #554

iwarren opened this issue Aug 26, 2014 · 0 comments · Fixed by #1582

Comments

@iwarren
Copy link
Contributor

iwarren commented Aug 26, 2014

When calling attr and passing in undefined as the value of the attr you are trying to set cheerio will return undefined. This is different than jQuery:

Assume $ = jQuery

$('<div/>').attr('id', 'foo').attr('id')
=> 'foo'
$('<div/>').attr('id', null).attr('id')
=> undefined
$('<div/>').attr('id', undefined)
=> $('<div/>')
$('<div/>').attr('id', undefined).attr('id')
=> undefined

No problems. Now, assume $ = cheerio

$('<div/>').attr('id', 'foo').attr('id')
=> 'foo'
$('<div/>').attr('id', null).attr('id')
=> undefined
$('<div/>').attr('id', undefined)
=> undefined
$('<div/>').attr('id', undefined).attr('id')
=> TypeError: Cannot call method 'attr' of undefined

I have forked cheerio and created a failing test on master that shows this behavior (iwarren@bc75efb). I did not make the corresponding fix as I wasn't sure if this was an intentional divergence from jquery.

fb55 added a commit that referenced this issue Dec 22, 2020
Fixes #554

This makes cheerio behave like jQuery with these values.

Tests were part of the issue above, contributed by @iwarren.

Co-Authored-By: Ian Warren <[email protected]>
fb55 added a commit that referenced this issue Dec 22, 2020
Fixes #554

This makes cheerio behave like jQuery with these values.

Tests were part of the issue above, contributed by @iwarren.

Co-authored-by: Ian Warren <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant