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

IBX-6230: Added validation of CSS selectors #69

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/lib/Browser/Locator/CSSLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,21 @@

namespace Ibexa\Behat\Browser\Locator;

use Ibexa\Behat\Browser\Locator\Validator\CssLocatorValidator;

class CSSLocator extends BaseLocator
{
public function __construct(string $identifier, string $selector)
{
parent::__construct($identifier, $selector);
$validator = new CssLocatorValidator();
if (str_contains($selector, '%d') || str_contains($selector, '%s')) {
$validator->validate(new self($identifier, str_replace(['%d', '%s'], '1', $selector)));
} else {
$validator->validate($this);
}
}

public function getType(): string
{
return 'css';
Expand Down
112 changes: 112 additions & 0 deletions src/lib/Browser/Locator/Validator/CssLocatorValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Behat\Browser\Locator\Validator;

use Ibexa\Behat\Browser\Locator\CSSLocator;
use Symfony\Component\CssSelector\Exception\ParseException;
use Symfony\Component\CssSelector\Node\CombinedSelectorNode;
use Symfony\Component\CssSelector\Node\ElementNode;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\Node\PseudoNode;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Parser;

class CssLocatorValidator
{
private Parser $parser;

private array $partiallySupportedPseudoClasses = ['first-of-type', 'last-of-type', 'nth-of-type', 'nth-last-of-type'];

public function __construct()
{
$this->parser = new Parser();
}

public function validate(CSSLocator $cssLocator): void
{
if (!$this->isValidSelector($cssLocator->getSelector())) {
throw new ParseException(
sprintf(
"Locator '%s' with ID '%s' cannot be used because of limitations of the CssSelector component. See more: https://symfony.com/doc/current/components/css_selector.html#limitations-of-the-cssselector-component",
$cssLocator->getSelector(),
$cssLocator->getIdentifier()
)
);
}
}

private function isValidSelector(string $selector): bool
{
$tokens = $this->parser->parse($selector);

while (!empty($tokens)) {
$token = array_pop($tokens);

if ($token instanceof SelectorNode) {
array_push($tokens, $token->getTree());
}

if ($token instanceof CombinedSelectorNode) {
array_push($tokens, $token->getSelector(), $token->getSubSelector());
}

if ($token instanceof FunctionNode) {
if (!$this->isValidFunctionNode($token)) {
return false;
}
}

if ($token instanceof PseudoNode) {
if (!$this->isValidPseudoNode($token)) {
return false;
}
}
}

return true;
}

private function isValidFunctionNode(FunctionNode $node): bool
{
if (!in_array($node->getName(), $this->partiallySupportedPseudoClasses)) {
return true;
}

$selector = $node->getSelector();

if (!($selector instanceof ElementNode)) {
$selector = $selector->getSelector();
}

if ($selector->getElement() === null) {
return false;
}

return true;
}

private function isValidPseudoNode(PseudoNode $node): bool
{
if (!in_array($node->getIdentifier(), $this->partiallySupportedPseudoClasses)) {
return true;
}

$selector = $node->getSelector();

if (!($selector instanceof ElementNode)) {
$selector = $selector->getSelector();
}

if ($selector->getElement() === null) {
return false;
}

return true;
}
}
81 changes: 81 additions & 0 deletions tests/Browser/Locator/Validator/CssLocatorValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Behat\Browser\Locator\Validator;

use Ibexa\Behat\Browser\Locator\CSSLocator;
use Ibexa\Behat\Browser\Locator\Validator\CssLocatorValidator;
use PHPUnit\Framework\TestCase;
use Symfony\Component\CssSelector\Exception\ParseException;

class CssLocatorValidatorTest extends TestCase
{
private CssLocatorValidator $validator;

protected function setUp(): void
{
$this->validator = new CssLocatorValidator();
}

/**
* @dataProvider provideValidSelectors
*/
public function testValidationPasses(string $selector): void
{
$this->validator->validate(new CSSLocator('test', $selector));
$this->expectNotToPerformAssertions();
}

/**
* @dataProvider provideInvalidSelectors
*/
public function testValidationFails(string $selector): void
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage(
sprintf(
"Locator '%s' with ID 'test' cannot be used because of limitations of the CssSelector component. See more: https://symfony.com/doc/current/components/css_selector.html#limitations-of-the-cssselector-component",
$selector
)
);
$this->validator->validate(new CSSLocator('test', $selector));
}

public static function provideValidSelectors(): iterable
{
return [
['div'],
['.ibexa-segmentation__items li.ibexa-segmentation__item:last-of-type div.ibexa-segmentation__content-wrapper > button'],
['div:nth-of-type(1)'],
['div.ibexa-content-field:nth-of-type(3)'],
['selector1 section:nth-of-type(2)'],
['tr td:nth-of-type(2)'],
['[data-id="test"] div.ibexa-field-edit:nth-of-type(3)'],
['div.ibexa-details__item:nth-of-type(2) .ibexa-details__item-content'],
['.ibexa-version-compare__field-wrapper div.ibexa-content-field:nth-of-type(1) .ibexa-content-field__name'],
['div.ibexa-dropdown__wrapper > ul.ibexa-dropdown__selection-info > li:nth-child(1)'],
['.ibexa-dropdown__item:nth-child(3)'],
['.nav-item:nth-child(2) .nav-link'],
];
}

public static function provideInvalidSelectors(): iterable
{
return [
['.ibexa:nth-of-type(1)'],
['.ibexa-segmentation__items .ibexa-segmentation__item:last-of-type div.ibexa-segmentation__content-wrapper > button'],
['.ibexa-pb-schedule-active-item:nth-of-type(1)'],
['.ibexa-table__cell:nth-of-type(5),td:nth-of-type(5)'],
['.ibexa-available-field-types__list > li:not(.ibexa-available-field-type--hidden) .ibexa-available-field-type__content:nth-of-type(5)'],
['.c-finder-branch:nth-of-type(3) .c-finder-leaf'],
['.selector .ibexa:nth-of-type(2)'],
['.tab-pane.active .ibexa-fieldgroup:nth-of-type(2)'],
['div.ibexa-ca-company-tab-company-profile__top-wrapper .ibexa-details__items .ibexa-details__item:nth-of-type(3) .ibexa-details__item-content'],
];
}
}
1 change: 0 additions & 1 deletion tests/lib/Core/Behat/ExtendedTableNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
*
* @license For full copyright and license information view LICENSE file distributed with this source code.
*
* @internal
Expand Down
Loading