SelectorNode example

namespace Symfony\Component\CssSelector\Tests\Node;

use Symfony\Component\CssSelector\Node\ElementNode;
use Symfony\Component\CssSelector\Node\SelectorNode;

class SelectorNodeTest extends AbstractNodeTestCase
{
    public static function getToStringConversionTestData()
    {
        return [
            [new SelectorNode(new ElementNode()), 'Selector[Element[*]]'],
            [new SelectorNode(new ElementNode(), 'pseudo'), 'Selector[Element[*]::pseudo]'],
        ];
    }

    public static function getSpecificityValueTestData()
    {
        return [
            [new SelectorNode(new ElementNode()), 0],
            [new SelectorNode(new ElementNode(), 'pseudo'), 1],
        ];
    }
}
if ($peek->isDelimiter(['+', '>', '~'])) {
                $combinator = $stream->getNext()->getValue();
                $stream->skipWhitespace();
            } else {
                $combinator = ' ';
            }

            [$nextSelector$pseudoElement] = $this->parseSimpleSelector($stream);
            $result = new Node\CombinedSelectorNode($result$combinator$nextSelector);
        }

        return new Node\SelectorNode($result$pseudoElement);
    }

    /** * Parses next simple node (hash, class, pseudo, negation). * * @throws SyntaxErrorException */
    private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
    {
        $stream->skipWhitespace();

        
class ElementParser implements ParserInterface
{
    public function parse(string $source): array
    {
        // Matches an optional namespace, required element or `*`         // $source = 'testns|testel';         // $matches = array (size=3)         // 0 => string 'testns|testel' (length=13)         // 1 => string 'testns' (length=6)         // 2 => string 'testel' (length=6)         if (preg_match('/^(?:([a-z]++)\|)?([\w-]++|\*)$/i', trim($source)$matches)) {
            return [new SelectorNode(new ElementNode($matches[1] ?: null, $matches[2]))];
        }

        return [];
    }
}
public function parse(string $source): array
    {
        // Matches an optional namespace, optional element, and required class         // $source = 'test|input.ab6bd_field';         // $matches = array (size=4)         // 0 => string 'test|input.ab6bd_field' (length=22)         // 1 => string 'test' (length=4)         // 2 => string 'input' (length=5)         // 3 => string 'ab6bd_field' (length=11)         if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+\.([\w-]++)$/i', trim($source)$matches)) {
            return [
                new SelectorNode(new ClassNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null)$matches[3])),
            ];
        }

        return [];
    }
}

class EmptyStringParser implements ParserInterface
{
    public function parse(string $source): array
    {
        // Matches an empty string         if ('' == $source) {
            return [new SelectorNode(new ElementNode(null, '*'))];
        }

        return [];
    }
}
public function parse(string $source): array
    {
        // Matches an optional namespace, optional element, and required id         // $source = 'test|input#ab6bd_field';         // $matches = array (size=4)         // 0 => string 'test|input#ab6bd_field' (length=22)         // 1 => string 'test' (length=4)         // 2 => string 'input' (length=5)         // 3 => string 'ab6bd_field' (length=11)         if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+#([\w-]++)$/i', trim($source)$matches)) {
            return [
                new SelectorNode(new HashNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null)$matches[3])),
            ];
        }

        return [];
    }
}
Home | Imprint | This part of the site doesn't use cookies.