Repository navigation
Allow the named selector to skip the partial fallback #882
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |||||
| namespace Behat\Mink\Element; | ||||||
|
|
||||||
| use Behat\Mink\Driver\DriverInterface; | ||||||
| use Behat\Mink\Selector\NamedSelectorMode; | ||||||
| use Behat\Mink\Selector\SelectorsHandler; | ||||||
| use Behat\Mink\Selector\Xpath\Manipulator; | ||||||
|
|
||||||
|
|
@@ -32,12 +33,25 @@ class ElementFinder | |||||
| * @var Manipulator | ||||||
| */ | ||||||
| private $xpathManipulator; | ||||||
| /** | ||||||
| * @var string one of the NamedSelectorMode constants | ||||||
| */ | ||||||
| private $namedMode; | ||||||
|
|
||||||
| public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler, ?Manipulator $xpathManipulator = null) | ||||||
| /** | ||||||
| * @param string $namedMode How the "named" selector is resolved: one of the | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applied, with one small change: the parameter is called Worth knowing that the narrowed type made PHPStan fail on
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would actually rename the parameter. What is named is the selector, not the mode.
A targeted comment is fine for this case, as this is a case where we intentionally pass invalid data.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed, you're right — the mode is a property of the selector, not something named. And thanks for settling the ignore, I'll leave the targeted comment where it is. |
||||||
| * NamedSelectorMode constants. | ||||||
| */ | ||||||
| public function __construct(DriverInterface $driver, SelectorsHandler $selectorsHandler, ?Manipulator $xpathManipulator = null, string $namedMode = NamedSelectorMode::PARTIAL_FALLBACK) | ||||||
| { | ||||||
| if (NamedSelectorMode::PARTIAL_FALLBACK !== $namedMode && NamedSelectorMode::EXACT !== $namedMode) { | ||||||
| throw new \InvalidArgumentException(sprintf('Unknown named selector mode "%s".', $namedMode)); | ||||||
| } | ||||||
|
|
||||||
| $this->driver = $driver; | ||||||
| $this->selectorsHandler = $selectorsHandler; | ||||||
| $this->xpathManipulator = $xpathManipulator ?? new Manipulator(); | ||||||
| $this->namedMode = $namedMode; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -49,7 +63,7 @@ public function findAll(string $selector, $locator, string $parentXpath) | |||||
| { | ||||||
| if ('named' === $selector) { | ||||||
| $items = $this->findAll('named_exact', $locator, $parentXpath); | ||||||
| if (empty($items)) { | ||||||
| if (empty($items) && NamedSelectorMode::EXACT !== $this->namedMode) { | ||||||
| $items = $this->findAll('named_partial', $locator, $parentXpath); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Mink package. | ||
| * (c) Konstantin Kudryashov <ever.zet@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Behat\Mink\Selector; | ||
|
|
||
| /** | ||
| * How the "named" selector resolves a locator. | ||
| * | ||
| * This is a placeholder for an enum, which the minimum PHP version does not allow yet. | ||
| */ | ||
| final class NamedSelectorMode | ||
| { | ||
| /** | ||
| * Try an exact match first, and fall back to a partial one when it finds nothing. | ||
| */ | ||
| public const PARTIAL_FALLBACK = 'partial_fallback'; | ||
|
|
||
| /** | ||
| * Only match exactly, the way the "named_exact" selector does. | ||
| */ | ||
| public const EXACT = 'exact'; | ||
|
|
||
| /** | ||
| * @codeCoverageIgnore | ||
| */ | ||
| private function __construct() | ||
| { | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied.
Session::__construct()was already documented asNamedSelectorMode::*, soElementFinderwas simply the odd one out. I also dropped the "one of the NamedSelectorMode constants" prose from both, since the type now says it better than the sentence did.