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

feat(text selector): match text in child nodes #5293

Merged
merged 1 commit into from Feb 5, 2021
Merged

feat(text selector): match text in child nodes #5293

merged 1 commit into from Feb 5, 2021

Conversation

dgozman
Copy link
Contributor

@dgozman dgozman commented Feb 3, 2021

This changes text= and :text() selectors to match the element when:

  • it's combined text content matches the text;
  • combined text content of any immediate child does not match the text.

This allows the following markup to match "Some bold and italics text":
<div>Some <b>bold</b> and <i>italics</i> text</div>.

For the reference, "combined text content" is almost equal to element.textContent,
but with some changes like using value of <input type=button> or ignoring <head>.

This also includes some caching optimizations, meaningful in complex matches
that involve multiple calls to the text engine.

Performance changes (measured on large page with ~25000 elements):

  • :has-text() - 14% faster.
  • text= - 50% faster.
  • :text() - 0-35% slower.
  • :text-matches() - 28% slower.

src/server/injected/injectedScript.ts Outdated Show resolved Hide resolved
src/server/injected/injectedScript.ts Outdated Show resolved Hide resolved
This changes `text=` and `:text()` selectors to match the element when:
- it's combined text content matches the text;
- combined text content of any immediate child does not match the text.

This allows the following markup to match "Some bold and italics text":
`<div>Some <b>bold</b> and <i>italics</i> text</div>`.

For the reference, "combined text content" is almost equal to `element.textContent`,
but with some changes like using value of `<input type=button>` or ignoring `<head>`.

This also includes some caching optimizations, meaningful in complex matches
that involve multiple calls to the text engine.

Performance changes (measured on large page with ~25000 elements):
- `:has-text()` - 14% faster.
- `text=` - 50% faster.
- `:text()` - 0-35% slower.
- `:text-matches()` - 28% slower.
@dgozman dgozman merged commit 0cbb2c1 into microsoft:master Feb 5, 2021
@TrySound
Copy link

TrySound commented Feb 6, 2021

Is there a use case for :has-text if :text provides almost equal functionality?

@dgozman
Copy link
Contributor Author

dgozman commented Feb 6, 2021

Is there a use case for :has-text if :text provides almost equal functionality?

Consider the following example:

<my-input>
  <span>Your address</span>
  <span class=wrapper><input></span>
</my-input>

One way to target the input is page.fill('my-input:has-text("Your address") input', 'my address').

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

Successfully merging this pull request may close these issues.

None yet

3 participants