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

Can't select elements with only whitespace #576

Closed
ks280first opened this issue May 18, 2015 · 4 comments
Closed

Can't select elements with only whitespace #576

ks280first opened this issue May 18, 2015 · 4 comments
Labels
Milestone

Comments

@ks280first
Copy link

Given the html: <html><body><p> </p></body></html>

:matchesOwn(^\s+$) won't select it because element.ownText() performs a trim() on the text before testing it against the regex pattern.

Because of this, :matchesOwn(^$) will select it along with everything else that has no text nodes

Furthermore, MatchesOwn.matches() uses Matcher.find() when it seems that Matcher.matches() would be more appropriate.

I think that adding a method like Element.fullText() which MatchesOwn.matches() could call and changing it to use Matcher.matches() would be an easy solution. Not sure if I am missing any reasons that it was implemented in this way.

@ks280first
Copy link
Author

I did end up finding a workaround selector. It's a bit kludgy but it works

doc.body().select(":not(:has(*)):not(:matchesOwn(.+))");

@techmag
Copy link

techmag commented Feb 17, 2016

I may have tripped over that issue myself today...

I was going to ask if it is possible to add a pseudo selector for :isBlock and :empty as they do have methods but that requires breaking out into a for loop to access them. (I'm in Scala and have overriden a class and forloops are brutal if you can't get the underlying elements into the right "collection").

Having these pseudo selectors would help eliminate the need for extra code when the need for logic related to "empty" elements arises.

If we added those then of course :isNotEmpty and :isNotBlock would just make sense...

@jhy
Copy link
Owner

jhy commented Sep 18, 2021

(I added some thoughts on addressing this in #876)

@jhy jhy added the fixed label Sep 18, 2021
@jhy jhy added this to the 1.15.1 milestone Dec 28, 2021
@jhy
Copy link
Owner

jhy commented Dec 28, 2021

This is fixed now with #1636 - use :matchesWholeOwnText or :matchesWholeText

Extra selectors for :block etc are interesting ideas too.

@jhy jhy closed this as completed Jan 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants