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

Element text selector: Normalize whitespace #3621

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ftes
Copy link
Contributor

@ftes ftes commented Jan 9, 2025

Goal: Make it easier to find an element by text (element/3, has_element?/3) if there is nested markup.

Example input:

<span>
  Text
  <span>with whitespace</span>
</span>

Status quo:

  • element(view, "span", "Text\n with whitespace")
  • element(view, "span", ~r/Text\n.*with whitespace/)

With this PR:

  • element(view, "span", "Text with whitespace")

Solution: Normalize whitespace (convert different whitespace characters to regular space, collapse consecutive whitespace).

Breaking change: This PR changes the default behaviour. Maybe this should be opt-in instead?

Pointers

To feature test libraries, which of course can't be compared 1:1.

Wallaby

Matches any element by its inner text.
~s{.//*[contains(normalize-space(text()), "#{selector}")]}
xpath.ex
https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/normalize-space

Playwright

Matching by text always normalizes whitespace, even with exact match. For example, it turns multiple spaces into one, turns line breaks into spaces and ignores leading and trailing whitespace.
https://playwright.dev/docs/locators#get-by-text

@ftes ftes force-pushed the feature/element-text-normalize-whitespace branch 2 times, most recently from e068a30 to fc7272a Compare January 9, 2025 11:06
@ftes ftes marked this pull request as draft January 9, 2025 11:08
@ftes ftes force-pushed the feature/element-text-normalize-whitespace branch from fc7272a to 7d295f9 Compare January 9, 2025 11:15
@ftes ftes marked this pull request as ready for review January 9, 2025 11:15
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.

1 participant