-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Unexpected result while using .find(">") on a string containing <html> tag #780
Comments
.find(">")
on a string containing <html>
tag
That's due to the shipped version of |
Hey @fb55, I am sorry to bother you again with this closed issue but I have a problem: the update of What I did is that I browserified (just to being able to share a jsFiddle) the last committed version of Cheerio, but with 3 differentes release of v1.0.0 is before the fix, 1.2.0 is the last release and I added the 1.1.0 because I wanted to be sure that it was not my changes which caused a regression. Now, back to the example I provided in jsFiddle: We can see that using v1.0.0 returns 5 as stated before. But the two last versions returns 0 instead of 2. So, as this issue seemed very close to this one, I made the tests. The first case returns 2, which is wrong. The two others versions return 1 which is the correct result. Finally, I installed I looked into Cheerio code and I saw this: exports.find = function(selectorOrHaystack) {
var elems = _.reduce(this, function(memo, elem) {
return memo.concat(_.filter(elem.children, isTag));
}, []);
(...)
return this._make(select(selectorOrHaystack, elems, this.options));
}; What happens is that So, did I misunderstand something or is this a issue which should be fixed? Thank you for your time. |
@Delgan You're right. We need to pass the current elements to |
@fb55 I do not know, I do not have as much knowledge about Cheerio than you, I do not want to break everything. 😅 It seems that var elems = _.reduce(this, function(memo, elem) {
return memo.concat(_.filter(elem.type === 'root' ? elem.children : [elem], isTag))
}, []) It looks that it solves the issue and passes all the tests, but is it the way you want this issue to be fixed? |
This way, the selected elements can be part of the result of One solution would be to pass |
@fb55 My bad, there is such a test case, I missed the file while running the tests, sorry. And indeed, it fails. But your workaround works just fine. |
Hello.
I had not had the opportunity to say it so far, but I wanted first of all to thank you for the wonderful tool that is Cheerio.js. You have done an amazing job, I can not express how much your work is valuable.
Really, thank you.
I recently encountered an unexpected behavior of Cheerio.js which I suspect to be a bug.
I opened a question on StackOverflow but I decided to come here and report this as an issue.
I try to parse HTML:
Using this code:
What happens is that this code returns
5
whereas the second child of the<tr>
tag contains only 2 links.A test case can be found here.
I also tried to remove
$.root()
and to use$.parseHTML()
but the issue is still the same.As you can see here, jQuery works as expected.
Maybe is it related to a limitation of selecting direct children using
querySelector()
?What is strange is that removing
<html>
tag seems to work, it returns2
.Moreover,
tr.find("> :nth-child(2)").find("a")
works just fine too.I really do not see what is wrong with my first code, so I opened this issue hoping you can help me and confirm whether or not it is a bug.
The text was updated successfully, but these errors were encountered: