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

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
@SteffenDE SteffenDE requested a review from chrismccord January 11, 2025 15:50
@SteffenDE SteffenDE requested a review from josevalim February 1, 2025 19:38
@SteffenDE SteffenDE merged commit ec34461 into phoenixframework:main Feb 7, 2025
8 checks passed
@SteffenDE
Copy link
Collaborator

@josevalim as this potentially breaks existing tests, we'll only include it in 1.1, not backport to 1.0 - right?

@ftes I wonder if we should normalize the text that is passed to element as well? That would probably make things easier for people that currently have tests using element(view, "span", "Text\n with whitespace").

@josevalim
Copy link
Member

Yes, only 1.1.

@ftes
Copy link
Contributor Author

ftes commented Feb 7, 2025

I wonder if we should normalize the text that is passed to element as well? That would probably make things easier for people that currently have tests using element(view, "span", "Text\n with whitespace").

That might be a good idea to ease migration. Could be kept around for 1 minor release and then removed.
Maybe even emit a warning if text != normalize(text)?

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.

3 participants