Skip to content

Fix name selector to allow other characters#11537

Merged
christian-bromann merged 1 commit intowebdriverio:mainfrom
aristotelos:fix-issue-11535
Nov 2, 2023
Merged

Fix name selector to allow other characters#11537
christian-bromann merged 1 commit intowebdriverio:mainfrom
aristotelos:fix-issue-11535

Conversation

@aristotelos
Copy link
Contributor

@aristotelos aristotelos commented Oct 26, 2023

Fix the name selector strategy to allow other characters, such as parentheses.
For example, using $('[name="test (Test)"]') will now correctly result in using the name selector strategy instead of using the css selector strategy.

Fixes #11553

Proposed changes

Fix the name selector strategy to allow other characters, such as parentheses.
For example, using $('[name="test (Test)"]') will now correctly result in using the name selector strategy instead of using the css selector strategy.

Fixes #11553

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • I have added proper type definitions for new commands (if appropriate)

Further comments

Reviewers: @webdriverio/project-committers

Copy link
Member

@erwinheitzman erwinheitzman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution, looks good to me with the exception of a small request for change as the testcase is confusing.

Copy link
Member

@erwinheitzman erwinheitzman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just have a question, I assume it is correct because the tests are passing but from looking at the code it feels off somehow and I just want to make sure we are doing the right thing 🙂

}
using = 'name'
value = match[2]
value = match[3] || match[5]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure, I can't really tell how the match could have changed. Perhaps I am overlooking something but could you please explain this so we can be sure this is indeed correct?

The reason I ask is because it is still a single match right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two possibilities: matching with double quotes or matching with single quotes. Both have different capture groups (group 3 and group 5) in the regular expression. It would be possible to make it a single capture with a single regular expression as before, but then the regular expression would also match e.g. [name="test'] which is an invalid expression. The other option is to make 2 different regular expressions, one for double quotes and one for single quotes. I thought this would be easiest: get the capture from the double quotes if it exists (capture 3), otherwise get the capture from the single quotes (capture 5).

Copy link
Member

@erwinheitzman erwinheitzman Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now, I feel like we should optimize the regex a bit.

\[name=(?:"(.[^"]*)"|'(.[^']*)')]

It would be more performant and returns a match on the full regex and 1 group.

What do you think about this?

EDIT: the reason this is more performant is because it uses a non-capturing group to match A or B and only create one group of the result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, pushed an update

Copy link
Member

@christian-bromann christian-bromann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Wdyt @erwinheitzman ?

@christian-bromann christian-bromann added the PR: Bug Fix 🐛 PRs that contain bug fixes label Oct 27, 2023
@erwinheitzman
Copy link
Member

erwinheitzman commented Oct 27, 2023

LGTM 👍

Wdyt @erwinheitzman ?

I would like to optimize the regex first if you don't mind 🙂

EDIT: see #11537 (comment)

@christian-bromann
Copy link
Member

@aristotelos can we address the comment mentioned by @erwinheitzman ?

Fix the `name` selector strategy to allow other characters, such as
parentheses.
For example, using `$('[name="test (Test)"]')` will now
correctly result in using the `name` selector strategy instead of using
the `css selector` strategy.

Fixes issue webdriverio#11553.
@christian-bromann
Copy link
Member

@erwinheitzman wdyt?

@erwinheitzman
Copy link
Member

@erwinheitzman wdyt?

@christian-bromann i think we can merge it and I can optimize it after

@christian-bromann christian-bromann merged commit 5acc51d into webdriverio:main Nov 2, 2023
@christian-bromann
Copy link
Member

Congratulations @aristotelos on your remarkable first contribution to WebdriverIO! This project thrives on the invaluable involvement of our community, and we are truly grateful for your contribution. We eagerly anticipate witnessing more of your exceptional work, so please don't hesitate to inform us if we can assist you in identifying intriguing areas where you can make further contributions. Join our lively Discord channel and reach out to us; we would be delighted to connect with you. Your efforts are deeply appreciated, and we extend our heartfelt gratitude to you. 🙏 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 PRs that contain bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants