-
Notifications
You must be signed in to change notification settings - Fork 280
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
Moved the escaping of the xpath locator to the NamedSelector #641
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
namespace Behat\Mink\Selector; | ||
|
||
use Behat\Mink\Selector\Xpath\Escaper; | ||
|
||
/** | ||
* Named selectors engine. Uses registered XPath selectors to create new expressions. | ||
* | ||
|
@@ -157,12 +159,15 @@ class NamedSelector implements SelectorInterface | |
.//*[%idOrNameMatch%] | ||
XPATH | ||
); | ||
private $xpathEscaper; | ||
|
||
/** | ||
* Creates selector instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->xpathEscaper = new Escaper(); | ||
|
||
foreach ($this->replacements as $from => $to) { | ||
$this->replacements[$from] = strtr($to, $this->replacements); | ||
} | ||
|
@@ -217,7 +222,7 @@ public function translateToXPath($locator) | |
$xpath = $this->selectors[$selector]; | ||
|
||
if (null !== $locator) { | ||
$xpath = strtr($xpath, array('%locator%' => $locator)); | ||
$xpath = strtr($xpath, array('%locator%' => $this->escapeLocator($locator))); | ||
} | ||
|
||
return $xpath; | ||
|
@@ -235,4 +240,24 @@ protected function registerReplacement($from, $to) | |
{ | ||
$this->replacements[$from] = $to; | ||
} | ||
|
||
private function escapeLocator($locator) | ||
{ | ||
// If the locator looks like an escaped one, don't escape it again for BC reasons. | ||
if ( | ||
preg_match('/^\'[^\']*+\'$/', $locator) | ||
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. It's inverted version of 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. It detects strings looking like the output of the Xpath escaping, yes |
||
|| (false !== strpos($locator, '\'') && preg_match('/^"[^"]*+"$/', $locator)) | ||
|| ((8 < $length = strlen($locator)) && 'concat(' === substr($locator, 0, 7) && ')' === $locator[$length - 1]) | ||
) { | ||
trigger_error( | ||
'Passing an excaped locator to the named selector is deprecated as of 1.7 and will be removed in 2.0.' | ||
.' Pass the raw value instead.', | ||
E_USER_DEPRECATED | ||
); | ||
|
||
return $locator; | ||
} | ||
|
||
return $this->xpathEscaper->escapeLiteral($locator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This will surely break something within QA-Tools, once merged.
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.
I guess I need to do
Escaper::escapeLiteral
when I need to escape xpath instead of doingWas this
Escaper
class part of Mink 1.6 release? Looks like it was, because commit 5bff8b5, which added this class is shown below 1.6.0 tag.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.
Also what could bullet-proof way of detecting if used Mink does automatic locator escaping by NamedSelector or I need to manually escape it upfront? I'm asking because of https://github.com/qa-tools/qa-tools/blob/develop/library/QATools/QATools/HtmlElements/Element/Form.php#L86 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.
Once Mink 1.7 is released, the easiest way would probably be to bump the dependency to Mink 1.7 (which autoescapes)
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.
and yes, I should not remove this method for BC. It will be removed only in 2.0
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.
@aik099 you could use
method_exists(NamedSelector::class, 'escapeLocator')
to detect that this PR is included in the codebase if you want to keep the Mink 1.6 compatibility.And when building your own Xpath, you should indeed use the Escaper class (which is available in 1.6.0)