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

Improving has-text? (and related) to handle multiple text nodes #243

Closed
rafd opened this issue Jul 22, 2020 · 2 comments
Closed

Improving has-text? (and related) to handle multiple text nodes #243

rafd opened this issue Jul 22, 2020 · 2 comments

Comments

@rafd
Copy link

rafd commented Jul 22, 2020

HTML elements can contain multiple text nodes, which is particularly common when working with React-based libraries.
For example, [:div "foo" "bar"] in Reagent, will create a DOM element that the browser sees as <div>"foo" "bar"</div> (as shown in the inspector). (Or, with plain HTML, one can cause this to happen by doing: <div>foo<!-- --> bar</div>)

The current implementation of has-text? translates to the xpath .//*[contains(text(), \"foo bar\")], which only queries the first text node in a DOM element; in the above examples, "foo bar" would not be found, only "foo" would work.

Based on some research, it seems that using text() is an anti-pattern in xpath, and the recommended approach is to use string(): .//*[contains(string(), \"foo bar\")], which concatenates all the descendant text nodes. This approach works to find "foo bar" in the above examples.

I suggest changing the implementation of has-text? and related functions to use string() instead of text().

This does change behaviour slightly (it's more permissive), so alternatively, it might be good to add a :fn/has-string and has-string?.

Some context / further resources:

@rafd
Copy link
Author

rafd commented Jul 22, 2020

A patch that I'm using for now:

 (defmethod etaoin.xpath/clause :fn/has-string
   [[_ text]]
   (etaoin.xpath/node-contains "string()" text))

@igrishaev
Copy link
Collaborator

@rafd thank you for submitting the issue! It has been fixed and will be available in the upcoming 0.3.9 release. Now the :fn/has-text term uses the string() method as you proposed (see the related PR).

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

No branches or pull requests

2 participants