laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

@@ -1,3 +0,0 @@
vendor/
composer.lock
phpunit.xml

View File

@@ -1,6 +1,11 @@
CHANGELOG
=========
4.4.0
-----
* Added support for `*:only-of-type`
2.8.0
-----

View File

@@ -31,7 +31,7 @@ class CssSelectorConverter
/**
* @param bool $html Whether HTML support should be enabled. Disable it for XML documents
*/
public function __construct($html = true)
public function __construct(bool $html = true)
{
$this->translator = new Translator();

View File

@@ -19,6 +19,6 @@ namespace Symfony\Component\CssSelector\Exception;
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
interface ExceptionInterface
interface ExceptionInterface extends \Throwable
{
}

View File

@@ -25,7 +25,6 @@ class SyntaxErrorException extends ParseException
{
/**
* @param string $expectedValue
* @param Token $foundToken
*
* @return self
*/

View File

@@ -1,4 +1,4 @@
Copyright (c) 2004-2017 Fabien Potencier
Copyright (c) 2004-2022 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -28,13 +28,10 @@ abstract class AbstractNode implements NodeInterface
*/
private $nodeName;
/**
* @return string
*/
public function getNodeName()
public function getNodeName(): string
{
if (null === $this->nodeName) {
$this->nodeName = preg_replace('~.*\\\\([^\\\\]+)Node$~', '$1', get_called_class());
$this->nodeName = preg_replace('~.*\\\\([^\\\\]+)Node$~', '$1', static::class);
}
return $this->nodeName;

View File

@@ -23,39 +23,13 @@ namespace Symfony\Component\CssSelector\Node;
*/
class AttributeNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var string
*/
private $namespace;
/**
* @var string
*/
private $attribute;
/**
* @var string
*/
private $operator;
/**
* @var string
*/
private $value;
/**
* @param NodeInterface $selector
* @param string $namespace
* @param string $attribute
* @param string $operator
* @param string $value
*/
public function __construct(NodeInterface $selector, $namespace, $attribute, $operator, $value)
public function __construct(NodeInterface $selector, ?string $namespace, string $attribute, string $operator, ?string $value)
{
$this->selector = $selector;
$this->namespace = $namespace;
@@ -64,42 +38,27 @@ class AttributeNode extends AbstractNode
$this->value = $value;
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return string
*/
public function getNamespace()
public function getNamespace(): ?string
{
return $this->namespace;
}
/**
* @return string
*/
public function getAttribute()
public function getAttribute(): string
{
return $this->attribute;
}
/**
* @return string
*/
public function getOperator()
public function getOperator(): string
{
return $this->operator;
}
/**
* @return string
*/
public function getValue()
public function getValue(): ?string
{
return $this->value;
}
@@ -107,15 +66,12 @@ class AttributeNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
$attribute = $this->namespace ? $this->namespace.'|'.$this->attribute : $this->attribute;

View File

@@ -23,38 +23,21 @@ namespace Symfony\Component\CssSelector\Node;
*/
class ClassNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var string
*/
private $name;
/**
* @param NodeInterface $selector
* @param string $name
*/
public function __construct(NodeInterface $selector, $name)
public function __construct(NodeInterface $selector, string $name)
{
$this->selector = $selector;
$this->name = $name;
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -62,15 +45,12 @@ class ClassNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
return sprintf('%s[%s.%s]', $this->getNodeName(), $this->selector, $this->name);
}

View File

@@ -23,53 +23,28 @@ namespace Symfony\Component\CssSelector\Node;
*/
class CombinedSelectorNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var string
*/
private $combinator;
/**
* @var NodeInterface
*/
private $subSelector;
/**
* @param NodeInterface $selector
* @param string $combinator
* @param NodeInterface $subSelector
*/
public function __construct(NodeInterface $selector, $combinator, NodeInterface $subSelector)
public function __construct(NodeInterface $selector, string $combinator, NodeInterface $subSelector)
{
$this->selector = $selector;
$this->combinator = $combinator;
$this->subSelector = $subSelector;
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return string
*/
public function getCombinator()
public function getCombinator(): string
{
return $this->combinator;
}
/**
* @return NodeInterface
*/
public function getSubSelector()
public function getSubSelector(): NodeInterface
{
return $this->subSelector;
}
@@ -77,15 +52,12 @@ class CombinedSelectorNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus($this->subSelector->getSpecificity());
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
$combinator = ' ' === $this->combinator ? '<followed>' : $this->combinator;

View File

@@ -23,38 +23,21 @@ namespace Symfony\Component\CssSelector\Node;
*/
class ElementNode extends AbstractNode
{
/**
* @var string|null
*/
private $namespace;
/**
* @var string|null
*/
private $element;
/**
* @param string|null $namespace
* @param string|null $element
*/
public function __construct($namespace = null, $element = null)
public function __construct(string $namespace = null, string $element = null)
{
$this->namespace = $namespace;
$this->element = $element;
}
/**
* @return null|string
*/
public function getNamespace()
public function getNamespace(): ?string
{
return $this->namespace;
}
/**
* @return null|string
*/
public function getElement()
public function getElement(): ?string
{
return $this->element;
}
@@ -62,15 +45,12 @@ class ElementNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return new Specificity(0, 0, $this->element ? 1 : 0);
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
$element = $this->element ?: '*';

View File

@@ -25,45 +25,26 @@ use Symfony\Component\CssSelector\Parser\Token;
*/
class FunctionNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var string
*/
private $name;
/**
* @var Token[]
*/
private $arguments;
/**
* @param NodeInterface $selector
* @param string $name
* @param Token[] $arguments
* @param Token[] $arguments
*/
public function __construct(NodeInterface $selector, $name, array $arguments = array())
public function __construct(NodeInterface $selector, string $name, array $arguments = [])
{
$this->selector = $selector;
$this->name = strtolower($name);
$this->arguments = $arguments;
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -71,7 +52,7 @@ class FunctionNode extends AbstractNode
/**
* @return Token[]
*/
public function getArguments()
public function getArguments(): array
{
return $this->arguments;
}
@@ -79,15 +60,12 @@ class FunctionNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
$arguments = implode(', ', array_map(function (Token $token) {
return "'".$token->getValue()."'";

View File

@@ -23,38 +23,21 @@ namespace Symfony\Component\CssSelector\Node;
*/
class HashNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var string
*/
private $id;
/**
* @param NodeInterface $selector
* @param string $id
*/
public function __construct(NodeInterface $selector, $id)
public function __construct(NodeInterface $selector, string $id)
{
$this->selector = $selector;
$this->id = $id;
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return string
*/
public function getId()
public function getId(): string
{
return $this->id;
}
@@ -62,15 +45,12 @@ class HashNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus(new Specificity(1, 0, 0));
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
return sprintf('%s[%s#%s]', $this->getNodeName(), $this->selector, $this->id);
}

View File

@@ -23,38 +23,21 @@ namespace Symfony\Component\CssSelector\Node;
*/
class NegationNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var NodeInterface
*/
private $subSelector;
/**
* @param NodeInterface $selector
* @param NodeInterface $subSelector
*/
public function __construct(NodeInterface $selector, NodeInterface $subSelector)
{
$this->selector = $selector;
$this->subSelector = $subSelector;
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return NodeInterface
*/
public function getSubSelector()
public function getSubSelector(): NodeInterface
{
return $this->subSelector;
}
@@ -62,15 +45,12 @@ class NegationNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus($this->subSelector->getSpecificity());
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
return sprintf('%s[%s:not(%s)]', $this->getNodeName(), $this->selector, $this->subSelector);
}

View File

@@ -23,24 +23,9 @@ namespace Symfony\Component\CssSelector\Node;
*/
interface NodeInterface
{
/**
* Returns node's name.
*
* @return string
*/
public function getNodeName();
public function getNodeName(): string;
/**
* Returns node's specificity.
*
* @return Specificity
*/
public function getSpecificity();
public function getSpecificity(): Specificity;
/**
* Returns node's string representation.
*
* @return string
*/
public function __toString();
public function __toString(): string;
}

View File

@@ -23,38 +23,21 @@ namespace Symfony\Component\CssSelector\Node;
*/
class PseudoNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $selector;
/**
* @var string
*/
private $identifier;
/**
* @param NodeInterface $selector
* @param string $identifier
*/
public function __construct(NodeInterface $selector, $identifier)
public function __construct(NodeInterface $selector, string $identifier)
{
$this->selector = $selector;
$this->identifier = strtolower($identifier);
}
/**
* @return NodeInterface
*/
public function getSelector()
public function getSelector(): NodeInterface
{
return $this->selector;
}
/**
* @return string
*/
public function getIdentifier()
public function getIdentifier(): string
{
return $this->identifier;
}
@@ -62,15 +45,12 @@ class PseudoNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->selector->getSpecificity()->plus(new Specificity(0, 1, 0));
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
return sprintf('%s[%s:%s]', $this->getNodeName(), $this->selector, $this->identifier);
}

View File

@@ -23,38 +23,21 @@ namespace Symfony\Component\CssSelector\Node;
*/
class SelectorNode extends AbstractNode
{
/**
* @var NodeInterface
*/
private $tree;
/**
* @var null|string
*/
private $pseudoElement;
/**
* @param NodeInterface $tree
* @param null|string $pseudoElement
*/
public function __construct(NodeInterface $tree, $pseudoElement = null)
public function __construct(NodeInterface $tree, string $pseudoElement = null)
{
$this->tree = $tree;
$this->pseudoElement = $pseudoElement ? strtolower($pseudoElement) : null;
}
/**
* @return NodeInterface
*/
public function getTree()
public function getTree(): NodeInterface
{
return $this->tree;
}
/**
* @return null|string
*/
public function getPseudoElement()
public function getPseudoElement(): ?string
{
return $this->pseudoElement;
}
@@ -62,15 +45,12 @@ class SelectorNode extends AbstractNode
/**
* {@inheritdoc}
*/
public function getSpecificity()
public function getSpecificity(): Specificity
{
return $this->tree->getSpecificity()->plus(new Specificity(0, 0, $this->pseudoElement ? 1 : 0));
}
/**
* {@inheritdoc}
*/
public function __toString()
public function __toString(): string
{
return sprintf('%s[%s%s]', $this->getNodeName(), $this->tree, $this->pseudoElement ? '::'.$this->pseudoElement : '');
}

View File

@@ -25,55 +25,27 @@ namespace Symfony\Component\CssSelector\Node;
*/
class Specificity
{
const A_FACTOR = 100;
const B_FACTOR = 10;
const C_FACTOR = 1;
public const A_FACTOR = 100;
public const B_FACTOR = 10;
public const C_FACTOR = 1;
/**
* @var int
*/
private $a;
/**
* @var int
*/
private $b;
/**
* @var int
*/
private $c;
/**
* Constructor.
*
* @param int $a
* @param int $b
* @param int $c
*/
public function __construct($a, $b, $c)
public function __construct(int $a, int $b, int $c)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
/**
* @param Specificity $specificity
*
* @return self
*/
public function plus(Specificity $specificity)
public function plus(self $specificity): self
{
return new self($this->a + $specificity->a, $this->b + $specificity->b, $this->c + $specificity->c);
}
/**
* Returns global specificity value.
*
* @return int
*/
public function getValue()
public function getValue(): int
{
return $this->a * self::A_FACTOR + $this->b * self::B_FACTOR + $this->c * self::C_FACTOR;
}
@@ -81,12 +53,8 @@ class Specificity
/**
* Returns -1 if the object specificity is lower than the argument,
* 0 if they are equal, and 1 if the argument is lower.
*
* @param Specificity $specificity
*
* @return int
*/
public function compareTo(Specificity $specificity)
public function compareTo(self $specificity): int
{
if ($this->a !== $specificity->a) {
return $this->a > $specificity->a ? 1 : -1;

View File

@@ -29,7 +29,7 @@ class CommentHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
if ('/*' !== $reader->getSubstring(2)) {
return false;

View File

@@ -26,11 +26,5 @@ use Symfony\Component\CssSelector\Parser\TokenStream;
*/
interface HandlerInterface
{
/**
* @param Reader $reader
* @param TokenStream $stream
*
* @return bool
*/
public function handle(Reader $reader, TokenStream $stream);
public function handle(Reader $reader, TokenStream $stream): bool;
}

View File

@@ -13,9 +13,9 @@ namespace Symfony\Component\CssSelector\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -29,20 +29,9 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class HashHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @var TokenizerEscaping
*/
private $escaping;
/**
* @param TokenizerPatterns $patterns
* @param TokenizerEscaping $escaping
*/
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
@@ -52,7 +41,7 @@ class HashHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern($this->patterns->getHashPattern());
@@ -62,7 +51,7 @@ class HashHandler implements HandlerInterface
$value = $this->escaping->escapeUnicode($match[1]);
$stream->push(new Token(Token::TYPE_HASH, $value, $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -13,9 +13,9 @@ namespace Symfony\Component\CssSelector\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -29,20 +29,9 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class IdentifierHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @var TokenizerEscaping
*/
private $escaping;
/**
* @param TokenizerPatterns $patterns
* @param TokenizerEscaping $escaping
*/
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
@@ -52,7 +41,7 @@ class IdentifierHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern($this->patterns->getIdentifierPattern());
@@ -62,7 +51,7 @@ class IdentifierHandler implements HandlerInterface
$value = $this->escaping->escapeUnicode($match[0]);
$stream->push(new Token(Token::TYPE_IDENTIFIER, $value, $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -13,8 +13,8 @@ namespace Symfony\Component\CssSelector\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -28,14 +28,8 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class NumberHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @param TokenizerPatterns $patterns
*/
public function __construct(TokenizerPatterns $patterns)
{
$this->patterns = $patterns;
@@ -44,7 +38,7 @@ class NumberHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern($this->patterns->getNumberPattern());
@@ -53,7 +47,7 @@ class NumberHandler implements HandlerInterface
}
$stream->push(new Token(Token::TYPE_NUMBER, $match[0], $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -15,9 +15,9 @@ use Symfony\Component\CssSelector\Exception\InternalErrorException;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* CSS selector comment handler.
@@ -31,20 +31,9 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
*/
class StringHandler implements HandlerInterface
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @var TokenizerEscaping
*/
private $escaping;
/**
* @param TokenizerPatterns $patterns
* @param TokenizerEscaping $escaping
*/
public function __construct(TokenizerPatterns $patterns, TokenizerEscaping $escaping)
{
$this->patterns = $patterns;
@@ -54,11 +43,11 @@ class StringHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$quote = $reader->getSubstring(1);
if (!in_array($quote, array("'", '"'))) {
if (!\in_array($quote, ["'", '"'])) {
return false;
}
@@ -66,22 +55,22 @@ class StringHandler implements HandlerInterface
$match = $reader->findPattern($this->patterns->getQuotedStringPattern($quote));
if (!$match) {
throw new InternalErrorException(sprintf('Should have found at least an empty match at %s.', $reader->getPosition()));
throw new InternalErrorException(sprintf('Should have found at least an empty match at %d.', $reader->getPosition()));
}
// check unclosed strings
if (strlen($match[0]) === $reader->getRemainingLength()) {
if (\strlen($match[0]) === $reader->getRemainingLength()) {
throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
}
// check quotes pairs validity
if ($quote !== $reader->getSubstring(1, strlen($match[0]))) {
if ($quote !== $reader->getSubstring(1, \strlen($match[0]))) {
throw SyntaxErrorException::unclosedString($reader->getPosition() - 1);
}
$string = $this->escaping->escapeUnicodeAndNewLine($match[0]);
$stream->push(new Token(Token::TYPE_STRING, $string, $reader->getPosition()));
$reader->moveForward(strlen($match[0]) + 1);
$reader->moveForward(\strlen($match[0]) + 1);
return true;
}

View File

@@ -30,7 +30,7 @@ class WhitespaceHandler implements HandlerInterface
/**
* {@inheritdoc}
*/
public function handle(Reader $reader, TokenStream $stream)
public function handle(Reader $reader, TokenStream $stream): bool
{
$match = $reader->findPattern('~^[ \t\r\n\f]+~');
@@ -39,7 +39,7 @@ class WhitespaceHandler implements HandlerInterface
}
$stream->push(new Token(Token::TYPE_WHITESPACE, $match[0], $reader->getPosition()));
$reader->moveForward(strlen($match[0]));
$reader->moveForward(\strlen($match[0]));
return true;
}

View File

@@ -27,25 +27,17 @@ use Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer;
*/
class Parser implements ParserInterface
{
/**
* @var Tokenizer
*/
private $tokenizer;
/**
* Constructor.
*
* @param null|Tokenizer $tokenizer
*/
public function __construct(Tokenizer $tokenizer = null)
{
$this->tokenizer = $tokenizer ?: new Tokenizer();
$this->tokenizer = $tokenizer ?? new Tokenizer();
}
/**
* {@inheritdoc}
*/
public function parse($source)
public function parse(string $source): array
{
$reader = new Reader($source);
$stream = $this->tokenizer->tokenize($reader);
@@ -58,11 +50,9 @@ class Parser implements ParserInterface
*
* @param Token[] $tokens
*
* @return array
*
* @throws SyntaxErrorException
*/
public static function parseSeries(array $tokens)
public static function parseSeries(array $tokens): array
{
foreach ($tokens as $token) {
if ($token->isString()) {
@@ -84,40 +74,33 @@ class Parser implements ParserInterface
switch (true) {
case 'odd' === $joined:
return array(2, 1);
return [2, 1];
case 'even' === $joined:
return array(2, 0);
return [2, 0];
case 'n' === $joined:
return array(1, 0);
case false === strpos($joined, 'n'):
return array(0, $int($joined));
return [1, 0];
case !str_contains($joined, 'n'):
return [0, $int($joined)];
}
$split = explode('n', $joined);
$first = isset($split[0]) ? $split[0] : null;
$first = $split[0] ?? null;
return array(
return [
$first ? ('-' === $first || '+' === $first ? $int($first.'1') : $int($first)) : 1,
isset($split[1]) && $split[1] ? $int($split[1]) : 0,
);
];
}
/**
* Parses selector nodes.
*
* @param TokenStream $stream
*
* @return array
*/
private function parseSelectorList(TokenStream $stream)
private function parseSelectorList(TokenStream $stream): array
{
$stream->skipWhitespace();
$selectors = array();
$selectors = [];
while (true) {
$selectors[] = $this->parserSelectorNode($stream);
if ($stream->getPeek()->isDelimiter(array(','))) {
if ($stream->getPeek()->isDelimiter([','])) {
$stream->getNext();
$stream->skipWhitespace();
} else {
@@ -128,24 +111,15 @@ class Parser implements ParserInterface
return $selectors;
}
/**
* Parses next selector or combined node.
*
* @param TokenStream $stream
*
* @return Node\SelectorNode
*
* @throws SyntaxErrorException
*/
private function parserSelectorNode(TokenStream $stream)
private function parserSelectorNode(TokenStream $stream): Node\SelectorNode
{
list($result, $pseudoElement) = $this->parseSimpleSelector($stream);
[$result, $pseudoElement] = $this->parseSimpleSelector($stream);
while (true) {
$stream->skipWhitespace();
$peek = $stream->getPeek();
if ($peek->isFileEnd() || $peek->isDelimiter(array(','))) {
if ($peek->isFileEnd() || $peek->isDelimiter([','])) {
break;
}
@@ -153,14 +127,14 @@ class Parser implements ParserInterface
throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
}
if ($peek->isDelimiter(array('+', '>', '~'))) {
if ($peek->isDelimiter(['+', '>', '~'])) {
$combinator = $stream->getNext()->getValue();
$stream->skipWhitespace();
} else {
$combinator = ' ';
}
list($nextSelector, $pseudoElement) = $this->parseSimpleSelector($stream);
[$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream);
$result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
}
@@ -170,18 +144,13 @@ class Parser implements ParserInterface
/**
* Parses next simple node (hash, class, pseudo, negation).
*
* @param TokenStream $stream
* @param bool $insideNegation
*
* @return array
*
* @throws SyntaxErrorException
*/
private function parseSimpleSelector(TokenStream $stream, $insideNegation = false)
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
{
$stream->skipWhitespace();
$selectorStart = count($stream->getUsed());
$selectorStart = \count($stream->getUsed());
$result = $this->parseElementNode($stream);
$pseudoElement = null;
@@ -189,8 +158,8 @@ class Parser implements ParserInterface
$peek = $stream->getPeek();
if ($peek->isWhitespace()
|| $peek->isFileEnd()
|| $peek->isDelimiter(array(',', '+', '>', '~'))
|| ($insideNegation && $peek->isDelimiter(array(')')))
|| $peek->isDelimiter([',', '+', '>', '~'])
|| ($insideNegation && $peek->isDelimiter([')']))
) {
break;
}
@@ -201,16 +170,16 @@ class Parser implements ParserInterface
if ($peek->isHash()) {
$result = new Node\HashNode($result, $stream->getNext()->getValue());
} elseif ($peek->isDelimiter(array('.'))) {
} elseif ($peek->isDelimiter(['.'])) {
$stream->getNext();
$result = new Node\ClassNode($result, $stream->getNextIdentifier());
} elseif ($peek->isDelimiter(array('['))) {
} elseif ($peek->isDelimiter(['['])) {
$stream->getNext();
$result = $this->parseAttributeNode($result, $stream);
} elseif ($peek->isDelimiter(array(':'))) {
} elseif ($peek->isDelimiter([':'])) {
$stream->getNext();
if ($stream->getPeek()->isDelimiter(array(':'))) {
if ($stream->getPeek()->isDelimiter([':'])) {
$stream->getNext();
$pseudoElement = $stream->getNextIdentifier();
@@ -218,7 +187,7 @@ class Parser implements ParserInterface
}
$identifier = $stream->getNextIdentifier();
if (in_array(strtolower($identifier), array('first-line', 'first-letter', 'before', 'after'))) {
if (\in_array(strtolower($identifier), ['first-line', 'first-letter', 'before', 'after'])) {
// Special case: CSS 2.1 pseudo-elements can have a single ':'.
// Any new pseudo-element must have two.
$pseudoElement = $identifier;
@@ -226,7 +195,7 @@ class Parser implements ParserInterface
continue;
}
if (!$stream->getPeek()->isDelimiter(array('('))) {
if (!$stream->getPeek()->isDelimiter(['('])) {
$result = new Node\PseudoNode($result, $identifier);
continue;
@@ -240,20 +209,20 @@ class Parser implements ParserInterface
throw SyntaxErrorException::nestedNot();
}
list($argument, $argumentPseudoElement) = $this->parseSimpleSelector($stream, true);
[$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, true);
$next = $stream->getNext();
if (null !== $argumentPseudoElement) {
throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
}
if (!$next->isDelimiter(array(')'))) {
if (!$next->isDelimiter([')'])) {
throw SyntaxErrorException::unexpectedToken('")"', $next);
}
$result = new Node\NegationNode($result, $argument);
} else {
$arguments = array();
$arguments = [];
$next = null;
while (true) {
@@ -263,10 +232,10 @@ class Parser implements ParserInterface
if ($next->isIdentifier()
|| $next->isString()
|| $next->isNumber()
|| $next->isDelimiter(array('+', '-'))
|| $next->isDelimiter(['+', '-'])
) {
$arguments[] = $next;
} elseif ($next->isDelimiter(array(')'))) {
} elseif ($next->isDelimiter([')'])) {
break;
} else {
throw SyntaxErrorException::unexpectedToken('an argument', $next);
@@ -284,25 +253,18 @@ class Parser implements ParserInterface
}
}
if (count($stream->getUsed()) === $selectorStart) {
if (\count($stream->getUsed()) === $selectorStart) {
throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
}
return array($result, $pseudoElement);
return [$result, $pseudoElement];
}
/**
* Parses next element node.
*
* @param TokenStream $stream
*
* @return Node\ElementNode
*/
private function parseElementNode(TokenStream $stream)
private function parseElementNode(TokenStream $stream): Node\ElementNode
{
$peek = $stream->getPeek();
if ($peek->isIdentifier() || $peek->isDelimiter(array('*'))) {
if ($peek->isIdentifier() || $peek->isDelimiter(['*'])) {
if ($peek->isIdentifier()) {
$namespace = $stream->getNext()->getValue();
} else {
@@ -310,7 +272,7 @@ class Parser implements ParserInterface
$namespace = null;
}
if ($stream->getPeek()->isDelimiter(array('|'))) {
if ($stream->getPeek()->isDelimiter(['|'])) {
$stream->getNext();
$element = $stream->getNextIdentifierOrStar();
} else {
@@ -324,29 +286,19 @@ class Parser implements ParserInterface
return new Node\ElementNode($namespace, $element);
}
/**
* Parses next attribute node.
*
* @param Node\NodeInterface $selector
* @param TokenStream $stream
*
* @return Node\AttributeNode
*
* @throws SyntaxErrorException
*/
private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream)
private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream): Node\AttributeNode
{
$stream->skipWhitespace();
$attribute = $stream->getNextIdentifierOrStar();
if (null === $attribute && !$stream->getPeek()->isDelimiter(array('|'))) {
if (null === $attribute && !$stream->getPeek()->isDelimiter(['|'])) {
throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
}
if ($stream->getPeek()->isDelimiter(array('|'))) {
if ($stream->getPeek()->isDelimiter(['|'])) {
$stream->getNext();
if ($stream->getPeek()->isDelimiter(array('='))) {
if ($stream->getPeek()->isDelimiter(['='])) {
$namespace = null;
$stream->getNext();
$operator = '|=';
@@ -363,12 +315,12 @@ class Parser implements ParserInterface
$stream->skipWhitespace();
$next = $stream->getNext();
if ($next->isDelimiter(array(']'))) {
if ($next->isDelimiter([']'])) {
return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
} elseif ($next->isDelimiter(array('='))) {
} elseif ($next->isDelimiter(['='])) {
$operator = '=';
} elseif ($next->isDelimiter(array('^', '$', '*', '~', '|', '!'))
&& $stream->getPeek()->isDelimiter(array('='))
} elseif ($next->isDelimiter(['^', '$', '*', '~', '|', '!'])
&& $stream->getPeek()->isDelimiter(['='])
) {
$operator = $next->getValue().'=';
$stream->getNext();
@@ -392,7 +344,7 @@ class Parser implements ParserInterface
$stream->skipWhitespace();
$next = $stream->getNext();
if (!$next->isDelimiter(array(']'))) {
if (!$next->isDelimiter([']'])) {
throw SyntaxErrorException::unexpectedToken('"]"', $next);
}

View File

@@ -28,9 +28,7 @@ interface ParserInterface
/**
* Parses given selector source into an array of tokens.
*
* @param string $source
*
* @return SelectorNode[]
*/
public function parse($source);
public function parse(string $source): array;
}

View File

@@ -23,71 +23,37 @@ namespace Symfony\Component\CssSelector\Parser;
*/
class Reader
{
/**
* @var string
*/
private $source;
/**
* @var int
*/
private $length;
/**
* @var int
*/
private $position = 0;
/**
* @param string $source
*/
public function __construct($source)
public function __construct(string $source)
{
$this->source = $source;
$this->length = strlen($source);
$this->length = \strlen($source);
}
/**
* @return bool
*/
public function isEOF()
public function isEOF(): bool
{
return $this->position >= $this->length;
}
/**
* @return int
*/
public function getPosition()
public function getPosition(): int
{
return $this->position;
}
/**
* @return int
*/
public function getRemainingLength()
public function getRemainingLength(): int
{
return $this->length - $this->position;
}
/**
* @param int $length
* @param int $offset
*
* @return string
*/
public function getSubstring($length, $offset = 0)
public function getSubstring(int $length, int $offset = 0): string
{
return substr($this->source, $this->position + $offset, $length);
}
/**
* @param string $string
*
* @return int
*/
public function getOffset($string)
public function getOffset(string $string)
{
$position = strpos($this->source, $string, $this->position);
@@ -95,11 +61,9 @@ class Reader
}
/**
* @param string $pattern
*
* @return bool
* @return array|false
*/
public function findPattern($pattern)
public function findPattern(string $pattern)
{
$source = substr($this->source, $this->position);
@@ -110,10 +74,7 @@ class Reader
return false;
}
/**
* @param int $length
*/
public function moveForward($length)
public function moveForward(int $length)
{
$this->position += $length;
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -23,79 +23,46 @@ namespace Symfony\Component\CssSelector\Parser;
*/
class Token
{
const TYPE_FILE_END = 'eof';
const TYPE_DELIMITER = 'delimiter';
const TYPE_WHITESPACE = 'whitespace';
const TYPE_IDENTIFIER = 'identifier';
const TYPE_HASH = 'hash';
const TYPE_NUMBER = 'number';
const TYPE_STRING = 'string';
public const TYPE_FILE_END = 'eof';
public const TYPE_DELIMITER = 'delimiter';
public const TYPE_WHITESPACE = 'whitespace';
public const TYPE_IDENTIFIER = 'identifier';
public const TYPE_HASH = 'hash';
public const TYPE_NUMBER = 'number';
public const TYPE_STRING = 'string';
/**
* @var int
*/
private $type;
/**
* @var string
*/
private $value;
/**
* @var int
*/
private $position;
/**
* @param int $type
* @param string $value
* @param int $position
*/
public function __construct($type, $value, $position)
public function __construct(?string $type, ?string $value, ?int $position)
{
$this->type = $type;
$this->value = $value;
$this->position = $position;
}
/**
* @return int
*/
public function getType()
public function getType(): ?int
{
return $this->type;
}
/**
* @return string
*/
public function getValue()
public function getValue(): ?string
{
return $this->value;
}
/**
* @return int
*/
public function getPosition()
public function getPosition(): ?int
{
return $this->position;
}
/**
* @return bool
*/
public function isFileEnd()
public function isFileEnd(): bool
{
return self::TYPE_FILE_END === $this->type;
}
/**
* @param array $values
*
* @return bool
*/
public function isDelimiter(array $values = array())
public function isDelimiter(array $values = []): bool
{
if (self::TYPE_DELIMITER !== $this->type) {
return false;
@@ -105,53 +72,35 @@ class Token
return true;
}
return in_array($this->value, $values);
return \in_array($this->value, $values);
}
/**
* @return bool
*/
public function isWhitespace()
public function isWhitespace(): bool
{
return self::TYPE_WHITESPACE === $this->type;
}
/**
* @return bool
*/
public function isIdentifier()
public function isIdentifier(): bool
{
return self::TYPE_IDENTIFIER === $this->type;
}
/**
* @return bool
*/
public function isHash()
public function isHash(): bool
{
return self::TYPE_HASH === $this->type;
}
/**
* @return bool
*/
public function isNumber()
public function isNumber(): bool
{
return self::TYPE_NUMBER === $this->type;
}
/**
* @return bool
*/
public function isString()
public function isString(): bool
{
return self::TYPE_STRING === $this->type;
}
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
if ($this->value) {
return sprintf('<%s "%s" at %s>', $this->type, $this->value, $this->position);

View File

@@ -29,17 +29,12 @@ class TokenStream
/**
* @var Token[]
*/
private $tokens = array();
/**
* @var bool
*/
private $frozen = false;
private $tokens = [];
/**
* @var Token[]
*/
private $used = array();
private $used = [];
/**
* @var int
@@ -49,7 +44,7 @@ class TokenStream
/**
* @var Token|null
*/
private $peeked = null;
private $peeked;
/**
* @var bool
@@ -59,11 +54,9 @@ class TokenStream
/**
* Pushes a token.
*
* @param Token $token
*
* @return $this
*/
public function push(Token $token)
public function push(Token $token): self
{
$this->tokens[] = $token;
@@ -75,21 +68,17 @@ class TokenStream
*
* @return $this
*/
public function freeze()
public function freeze(): self
{
$this->frozen = true;
return $this;
}
/**
* Returns next token.
*
* @return Token
*
* @throws InternalErrorException If there is no more token
*/
public function getNext()
public function getNext(): Token
{
if ($this->peeking) {
$this->peeking = false;
@@ -107,10 +96,8 @@ class TokenStream
/**
* Returns peeked token.
*
* @return Token
*/
public function getPeek()
public function getPeek(): Token
{
if (!$this->peeking) {
$this->peeked = $this->getNext();
@@ -125,7 +112,7 @@ class TokenStream
*
* @return Token[]
*/
public function getUsed()
public function getUsed(): array
{
return $this->used;
}
@@ -137,7 +124,7 @@ class TokenStream
*
* @throws SyntaxErrorException If next token is not an identifier
*/
public function getNextIdentifier()
public function getNextIdentifier(): string
{
$next = $this->getNext();
@@ -151,11 +138,11 @@ class TokenStream
/**
* Returns nex identifier or star delimiter token.
*
* @return null|string The identifier token value or null if star found
* @return string|null The identifier token value or null if star found
*
* @throws SyntaxErrorException If next token is not an identifier or a star delimiter
*/
public function getNextIdentifierOrStar()
public function getNextIdentifierOrStar(): ?string
{
$next = $this->getNext();
@@ -163,8 +150,8 @@ class TokenStream
return $next->getValue();
}
if ($next->isDelimiter(array('*'))) {
return;
if ($next->isDelimiter(['*'])) {
return null;
}
throw SyntaxErrorException::unexpectedToken('identifier or "*"', $next);

View File

@@ -33,32 +33,25 @@ class Tokenizer
*/
private $handlers;
/**
* Constructor.
*/
public function __construct()
{
$patterns = new TokenizerPatterns();
$escaping = new TokenizerEscaping($patterns);
$this->handlers = array(
$this->handlers = [
new Handler\WhitespaceHandler(),
new Handler\IdentifierHandler($patterns, $escaping),
new Handler\HashHandler($patterns, $escaping),
new Handler\StringHandler($patterns, $escaping),
new Handler\NumberHandler($patterns),
new Handler\CommentHandler(),
);
];
}
/**
* Tokenize selector source code.
*
* @param Reader $reader
*
* @return TokenStream
*/
public function tokenize(Reader $reader)
public function tokenize(Reader $reader): TokenStream
{
$stream = new TokenStream();

View File

@@ -23,62 +23,43 @@ namespace Symfony\Component\CssSelector\Parser\Tokenizer;
*/
class TokenizerEscaping
{
/**
* @var TokenizerPatterns
*/
private $patterns;
/**
* @param TokenizerPatterns $patterns
*/
public function __construct(TokenizerPatterns $patterns)
{
$this->patterns = $patterns;
}
/**
* @param string $value
*
* @return string
*/
public function escapeUnicode($value)
public function escapeUnicode(string $value): string
{
$value = $this->replaceUnicodeSequences($value);
return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value);
}
/**
* @param string $value
*
* @return string
*/
public function escapeUnicodeAndNewLine($value)
public function escapeUnicodeAndNewLine(string $value): string
{
$value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value);
return $this->escapeUnicode($value);
}
/**
* @param string $value
*
* @return string
*/
private function replaceUnicodeSequences($value)
private function replaceUnicodeSequences(string $value): string
{
return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) {
$c = hexdec($match[1]);
if (0x80 > $c %= 0x200000) {
return chr($c);
return \chr($c);
}
if (0x800 > $c) {
return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
return \chr(0xC0 | $c >> 6).\chr(0x80 | $c & 0x3F);
}
if (0x10000 > $c) {
return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
return \chr(0xE0 | $c >> 12).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F);
}
return '';
}, $value);
}
}

View File

@@ -23,69 +23,19 @@ namespace Symfony\Component\CssSelector\Parser\Tokenizer;
*/
class TokenizerPatterns
{
/**
* @var string
*/
private $unicodeEscapePattern;
/**
* @var string
*/
private $simpleEscapePattern;
/**
* @var string
*/
private $newLineEscapePattern;
/**
* @var string
*/
private $escapePattern;
/**
* @var string
*/
private $stringEscapePattern;
/**
* @var string
*/
private $nonAsciiPattern;
/**
* @var string
*/
private $nmCharPattern;
/**
* @var string
*/
private $nmStartPattern;
/**
* @var string
*/
private $identifierPattern;
/**
* @var string
*/
private $hashPattern;
/**
* @var string
*/
private $numberPattern;
/**
* @var string
*/
private $quotedStringPattern;
/**
* Constructor.
*/
public function __construct()
{
$this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?';
@@ -96,66 +46,43 @@ class TokenizerPatterns
$this->nonAsciiPattern = '[^\x00-\x7F]';
$this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
$this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
$this->identifierPattern = '(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
$this->identifierPattern = '-?(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
$this->hashPattern = '#((?:'.$this->nmCharPattern.')+)';
$this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)';
$this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*';
}
/**
* @return string
*/
public function getNewLineEscapePattern()
public function getNewLineEscapePattern(): string
{
return '~^'.$this->newLineEscapePattern.'~';
}
/**
* @return string
*/
public function getSimpleEscapePattern()
public function getSimpleEscapePattern(): string
{
return '~^'.$this->simpleEscapePattern.'~';
}
/**
* @return string
*/
public function getUnicodeEscapePattern()
public function getUnicodeEscapePattern(): string
{
return '~^'.$this->unicodeEscapePattern.'~i';
}
/**
* @return string
*/
public function getIdentifierPattern()
public function getIdentifierPattern(): string
{
return '~^'.$this->identifierPattern.'~i';
}
/**
* @return string
*/
public function getHashPattern()
public function getHashPattern(): string
{
return '~^'.$this->hashPattern.'~i';
}
/**
* @return string
*/
public function getNumberPattern()
public function getNumberPattern(): string
{
return '~^'.$this->numberPattern.'~';
}
/**
* @param string $quote
*
* @return string
*/
public function getQuotedStringPattern($quote)
public function getQuotedStringPattern(string $quote): string
{
return '~^'.sprintf($this->quotedStringPattern, $quote).'~i';
}

View File

@@ -6,11 +6,11 @@ The CssSelector component converts CSS selectors to XPath expressions.
Resources
---------
* [Documentation](https://symfony.com/doc/current/components/css_selector.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
* [Documentation](https://symfony.com/doc/current/components/css_selector.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Credits
-------

View File

@@ -1,75 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests;
use Symfony\Component\CssSelector\CssSelectorConverter;
class CssSelectorConverterTest extends \PHPUnit_Framework_TestCase
{
public function testCssToXPath()
{
$converter = new CssSelectorConverter();
$this->assertEquals('descendant-or-self::*', $converter->toXPath(''));
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('h1'));
$this->assertEquals("descendant-or-self::h1[@id = 'foo']", $converter->toXPath('h1#foo'));
$this->assertEquals("descendant-or-self::h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]", $converter->toXPath('h1.foo'));
$this->assertEquals('descendant-or-self::foo:h1', $converter->toXPath('foo|h1'));
$this->assertEquals('descendant-or-self::h1', $converter->toXPath('H1'));
}
public function testCssToXPathXml()
{
$converter = new CssSelectorConverter(false);
$this->assertEquals('descendant-or-self::H1', $converter->toXPath('H1'));
}
/**
* @expectedException \Symfony\Component\CssSelector\Exception\ParseException
* @expectedExceptionMessage Expected identifier, but <eof at 3> found.
*/
public function testParseExceptions()
{
$converter = new CssSelectorConverter();
$converter->toXPath('h1:');
}
/** @dataProvider getCssToXPathWithoutPrefixTestData */
public function testCssToXPathWithoutPrefix($css, $xpath)
{
$converter = new CssSelectorConverter();
$this->assertEquals($xpath, $converter->toXPath($css, ''), '->parse() parses an input string and returns a node');
}
public function getCssToXPathWithoutPrefixTestData()
{
return array(
array('h1', 'h1'),
array('foo|h1', 'foo:h1'),
array('h1, h2, h3', 'h1 | h2 | h3'),
array('h1:nth-child(3n+1)', "*/*[name() = 'h1' and (position() - 1 >= 0 and (position() - 1) mod 3 = 0)]"),
array('h1 > p', 'h1/p'),
array('h1#foo', "h1[@id = 'foo']"),
array('h1.foo', "h1[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
array('h1[class*="foo bar"]', "h1[@class and contains(@class, 'foo bar')]"),
array('h1[foo|class*="foo bar"]', "h1[@foo:class and contains(@foo:class, 'foo bar')]"),
array('h1[class]', 'h1[@class]'),
array('h1 .foo', "h1/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
array('h1 #foo', "h1/descendant-or-self::*/*[@id = 'foo']"),
array('h1 [class*=foo]', "h1/descendant-or-self::*/*[@class and contains(@class, 'foo')]"),
array('div>.foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
array('div > .foo', "div/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]"),
);
}
}

View File

@@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\NodeInterface;
abstract class AbstractNodeTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getToStringConversionTestData */
public function testToStringConversion(NodeInterface $node, $representation)
{
$this->assertEquals($representation, (string) $node);
}
/** @dataProvider getSpecificityValueTestData */
public function testSpecificityValue(NodeInterface $node, $value)
{
$this->assertEquals($value, $node->getSpecificity()->getValue());
}
abstract public function getToStringConversionTestData();
abstract public function getSpecificityValueTestData();
}

View File

@@ -1,37 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\AttributeNode;
use Symfony\Component\CssSelector\Node\ElementNode;
class AttributeNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new AttributeNode(new ElementNode(), null, 'attribute', 'exists', null), 'Attribute[Element[*][attribute]]'),
array(new AttributeNode(new ElementNode(), null, 'attribute', '$=', 'value'), "Attribute[Element[*][attribute $= 'value']]"),
array(new AttributeNode(new ElementNode(), 'namespace', 'attribute', '$=', 'value'), "Attribute[Element[*][namespace|attribute $= 'value']]"),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new AttributeNode(new ElementNode(), null, 'attribute', 'exists', null), 10),
array(new AttributeNode(new ElementNode(null, 'element'), null, 'attribute', 'exists', null), 11),
array(new AttributeNode(new ElementNode(), null, 'attribute', '$=', 'value'), 10),
array(new AttributeNode(new ElementNode(), 'namespace', 'attribute', '$=', 'value'), 10),
);
}
}

View File

@@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\ClassNode;
use Symfony\Component\CssSelector\Node\ElementNode;
class ClassNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new ClassNode(new ElementNode(), 'class'), 'Class[Element[*].class]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new ClassNode(new ElementNode(), 'class'), 10),
array(new ClassNode(new ElementNode(null, 'element'), 'class'), 11),
);
}
}

View File

@@ -1,35 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\CombinedSelectorNode;
use Symfony\Component\CssSelector\Node\ElementNode;
class CombinedSelectorNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new CombinedSelectorNode(new ElementNode(), '>', new ElementNode()), 'CombinedSelector[Element[*] > Element[*]]'),
array(new CombinedSelectorNode(new ElementNode(), ' ', new ElementNode()), 'CombinedSelector[Element[*] <followed> Element[*]]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new CombinedSelectorNode(new ElementNode(), '>', new ElementNode()), 0),
array(new CombinedSelectorNode(new ElementNode(null, 'element'), '>', new ElementNode()), 1),
array(new CombinedSelectorNode(new ElementNode(null, 'element'), '>', new ElementNode(null, 'element')), 2),
);
}
}

View File

@@ -1,35 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\ElementNode;
class ElementNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new ElementNode(), 'Element[*]'),
array(new ElementNode(null, 'element'), 'Element[element]'),
array(new ElementNode('namespace', 'element'), 'Element[namespace|element]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new ElementNode(), 0),
array(new ElementNode(null, 'element'), 1),
array(new ElementNode('namespace', 'element'), 1),
);
}
}

View File

@@ -1,47 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\ElementNode;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\Parser\Token;
class FunctionNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new FunctionNode(new ElementNode(), 'function'), 'Function[Element[*]:function()]'),
array(new FunctionNode(new ElementNode(), 'function', array(
new Token(Token::TYPE_IDENTIFIER, 'value', 0),
)), "Function[Element[*]:function(['value'])]"),
array(new FunctionNode(new ElementNode(), 'function', array(
new Token(Token::TYPE_STRING, 'value1', 0),
new Token(Token::TYPE_NUMBER, 'value2', 0),
)), "Function[Element[*]:function(['value1', 'value2'])]"),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new FunctionNode(new ElementNode(), 'function'), 10),
array(new FunctionNode(new ElementNode(), 'function', array(
new Token(Token::TYPE_IDENTIFIER, 'value', 0),
)), 10),
array(new FunctionNode(new ElementNode(), 'function', array(
new Token(Token::TYPE_STRING, 'value1', 0),
new Token(Token::TYPE_NUMBER, 'value2', 0),
)), 10),
);
}
}

View File

@@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\HashNode;
use Symfony\Component\CssSelector\Node\ElementNode;
class HashNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new HashNode(new ElementNode(), 'id'), 'Hash[Element[*]#id]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new HashNode(new ElementNode(), 'id'), 100),
array(new HashNode(new ElementNode(null, 'id'), 'class'), 101),
);
}
}

View File

@@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\ClassNode;
use Symfony\Component\CssSelector\Node\NegationNode;
use Symfony\Component\CssSelector\Node\ElementNode;
class NegationNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new NegationNode(new ElementNode(), new ClassNode(new ElementNode(), 'class')), 'Negation[Element[*]:not(Class[Element[*].class])]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new NegationNode(new ElementNode(), new ClassNode(new ElementNode(), 'class')), 10),
);
}
}

View File

@@ -1,32 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\ElementNode;
use Symfony\Component\CssSelector\Node\PseudoNode;
class PseudoNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new PseudoNode(new ElementNode(), 'pseudo'), 'Pseudo[Element[*]:pseudo]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new PseudoNode(new ElementNode(), 'pseudo'), 10),
);
}
}

View File

@@ -1,34 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\ElementNode;
use Symfony\Component\CssSelector\Node\SelectorNode;
class SelectorNodeTest extends AbstractNodeTest
{
public function getToStringConversionTestData()
{
return array(
array(new SelectorNode(new ElementNode()), 'Selector[Element[*]]'),
array(new SelectorNode(new ElementNode(), 'pseudo'), 'Selector[Element[*]::pseudo]'),
);
}
public function getSpecificityValueTestData()
{
return array(
array(new SelectorNode(new ElementNode()), 0),
array(new SelectorNode(new ElementNode(), 'pseudo'), 1),
);
}
}

View File

@@ -1,62 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Node;
use Symfony\Component\CssSelector\Node\Specificity;
class SpecificityTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getValueTestData */
public function testValue(Specificity $specificity, $value)
{
$this->assertEquals($value, $specificity->getValue());
}
/** @dataProvider getValueTestData */
public function testPlusValue(Specificity $specificity, $value)
{
$this->assertEquals($value + 123, $specificity->plus(new Specificity(1, 2, 3))->getValue());
}
public function getValueTestData()
{
return array(
array(new Specificity(0, 0, 0), 0),
array(new Specificity(0, 0, 2), 2),
array(new Specificity(0, 3, 0), 30),
array(new Specificity(4, 0, 0), 400),
array(new Specificity(4, 3, 2), 432),
);
}
/** @dataProvider getCompareTestData */
public function testCompareTo(Specificity $a, Specificity $b, $result)
{
$this->assertEquals($result, $a->compareTo($b));
}
public function getCompareTestData()
{
return array(
array(new Specificity(0, 0, 0), new Specificity(0, 0, 0), 0),
array(new Specificity(0, 0, 1), new Specificity(0, 0, 1), 0),
array(new Specificity(0, 0, 2), new Specificity(0, 0, 1), 1),
array(new Specificity(0, 0, 2), new Specificity(0, 0, 3), -1),
array(new Specificity(0, 4, 0), new Specificity(0, 4, 0), 0),
array(new Specificity(0, 6, 0), new Specificity(0, 5, 11), 1),
array(new Specificity(0, 7, 0), new Specificity(0, 8, 0), -1),
array(new Specificity(9, 0, 0), new Specificity(9, 0, 0), 0),
array(new Specificity(11, 0, 0), new Specificity(10, 11, 0), 1),
array(new Specificity(12, 11, 0), new Specificity(13, 0, 0), -1),
);
}
}

View File

@@ -1,69 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
/**
* @author Jean-François Simon <contact@jfsimon.fr>
*/
abstract class AbstractHandlerTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getHandleValueTestData */
public function testHandleValue($value, Token $expectedToken, $remainingContent)
{
$reader = new Reader($value);
$stream = new TokenStream();
$this->assertTrue($this->generateHandler()->handle($reader, $stream));
$this->assertEquals($expectedToken, $stream->getNext());
$this->assertRemainingContent($reader, $remainingContent);
}
/** @dataProvider getDontHandleValueTestData */
public function testDontHandleValue($value)
{
$reader = new Reader($value);
$stream = new TokenStream();
$this->assertFalse($this->generateHandler()->handle($reader, $stream));
$this->assertStreamEmpty($stream);
$this->assertRemainingContent($reader, $value);
}
abstract public function getHandleValueTestData();
abstract public function getDontHandleValueTestData();
abstract protected function generateHandler();
protected function assertStreamEmpty(TokenStream $stream)
{
$property = new \ReflectionProperty($stream, 'tokens');
$property->setAccessible(true);
$this->assertEquals(array(), $property->getValue($stream));
}
protected function assertRemainingContent(Reader $reader, $remainingContent)
{
if ('' === $remainingContent) {
$this->assertEquals(0, $reader->getRemainingLength());
$this->assertTrue($reader->isEOF());
} else {
$this->assertEquals(strlen($remainingContent), $reader->getRemainingLength());
$this->assertEquals(0, $reader->getOffset($remainingContent));
}
}
}

View File

@@ -1,55 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\CommentHandler;
use Symfony\Component\CssSelector\Parser\Reader;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
class CommentHandlerTest extends AbstractHandlerTest
{
/** @dataProvider getHandleValueTestData */
public function testHandleValue($value, Token $unusedArgument, $remainingContent)
{
$reader = new Reader($value);
$stream = new TokenStream();
$this->assertTrue($this->generateHandler()->handle($reader, $stream));
// comments are ignored (not pushed as token in stream)
$this->assertStreamEmpty($stream);
$this->assertRemainingContent($reader, $remainingContent);
}
public function getHandleValueTestData()
{
return array(
// 2nd argument only exists for inherited method compatibility
array('/* comment */', new Token(null, null, null), ''),
array('/* comment */foo', new Token(null, null, null), 'foo'),
);
}
public function getDontHandleValueTestData()
{
return array(
array('>'),
array('+'),
array(' '),
);
}
protected function generateHandler()
{
return new CommentHandler();
}
}

View File

@@ -1,49 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\HashHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
class HashHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return array(
array('#id', new Token(Token::TYPE_HASH, 'id', 0), ''),
array('#123', new Token(Token::TYPE_HASH, '123', 0), ''),
array('#id.class', new Token(Token::TYPE_HASH, 'id', 0), '.class'),
array('#id element', new Token(Token::TYPE_HASH, 'id', 0), ' element'),
);
}
public function getDontHandleValueTestData()
{
return array(
array('id'),
array('123'),
array('<'),
array('<'),
array('#'),
);
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new HashHandler($patterns, new TokenizerEscaping($patterns));
}
}

View File

@@ -1,49 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\IdentifierHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
class IdentifierHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return array(
array('foo', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), ''),
array('foo|bar', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), '|bar'),
array('foo.class', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), '.class'),
array('foo[attr]', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), '[attr]'),
array('foo bar', new Token(Token::TYPE_IDENTIFIER, 'foo', 0), ' bar'),
);
}
public function getDontHandleValueTestData()
{
return array(
array('>'),
array('+'),
array(' '),
array('*|foo'),
array('/* comment */'),
);
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new IdentifierHandler($patterns, new TokenizerEscaping($patterns));
}
}

View File

@@ -1,50 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\NumberHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
class NumberHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return array(
array('12', new Token(Token::TYPE_NUMBER, '12', 0), ''),
array('12.34', new Token(Token::TYPE_NUMBER, '12.34', 0), ''),
array('+12.34', new Token(Token::TYPE_NUMBER, '+12.34', 0), ''),
array('-12.34', new Token(Token::TYPE_NUMBER, '-12.34', 0), ''),
array('12 arg', new Token(Token::TYPE_NUMBER, '12', 0), ' arg'),
array('12]', new Token(Token::TYPE_NUMBER, '12', 0), ']'),
);
}
public function getDontHandleValueTestData()
{
return array(
array('hello'),
array('>'),
array('+'),
array(' '),
array('/* comment */'),
);
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new NumberHandler($patterns);
}
}

View File

@@ -1,50 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\StringHandler;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerPatterns;
use Symfony\Component\CssSelector\Parser\Tokenizer\TokenizerEscaping;
class StringHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return array(
array('"hello"', new Token(Token::TYPE_STRING, 'hello', 1), ''),
array('"1"', new Token(Token::TYPE_STRING, '1', 1), ''),
array('" "', new Token(Token::TYPE_STRING, ' ', 1), ''),
array('""', new Token(Token::TYPE_STRING, '', 1), ''),
array("'hello'", new Token(Token::TYPE_STRING, 'hello', 1), ''),
array("'foo'bar", new Token(Token::TYPE_STRING, 'foo', 1), 'bar'),
);
}
public function getDontHandleValueTestData()
{
return array(
array('hello'),
array('>'),
array('1'),
array(' '),
);
}
protected function generateHandler()
{
$patterns = new TokenizerPatterns();
return new StringHandler($patterns, new TokenizerEscaping($patterns));
}
}

View File

@@ -1,44 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Handler;
use Symfony\Component\CssSelector\Parser\Handler\WhitespaceHandler;
use Symfony\Component\CssSelector\Parser\Token;
class WhitespaceHandlerTest extends AbstractHandlerTest
{
public function getHandleValueTestData()
{
return array(
array(' ', new Token(Token::TYPE_WHITESPACE, ' ', 0), ''),
array("\n", new Token(Token::TYPE_WHITESPACE, "\n", 0), ''),
array("\t", new Token(Token::TYPE_WHITESPACE, "\t", 0), ''),
array(' foo', new Token(Token::TYPE_WHITESPACE, ' ', 0), 'foo'),
array(' .foo', new Token(Token::TYPE_WHITESPACE, ' ', 0), '.foo'),
);
}
public function getDontHandleValueTestData()
{
return array(
array('>'),
array('1'),
array('a'),
);
}
protected function generateHandler()
{
return new WhitespaceHandler();
}
}

View File

@@ -1,248 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Parser;
use Symfony\Component\CssSelector\Parser\Token;
class ParserTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getParserTestData */
public function testParser($source, $representation)
{
$parser = new Parser();
$this->assertEquals($representation, array_map(function (SelectorNode $node) {
return (string) $node->getTree();
}, $parser->parse($source)));
}
/** @dataProvider getParserExceptionTestData */
public function testParserException($source, $message)
{
$parser = new Parser();
try {
$parser->parse($source);
$this->fail('Parser should throw a SyntaxErrorException.');
} catch (SyntaxErrorException $e) {
$this->assertEquals($message, $e->getMessage());
}
}
/** @dataProvider getPseudoElementsTestData */
public function testPseudoElements($source, $element, $pseudo)
{
$parser = new Parser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($element, (string) $selector->getTree());
$this->assertEquals($pseudo, (string) $selector->getPseudoElement());
}
/** @dataProvider getSpecificityTestData */
public function testSpecificity($source, $value)
{
$parser = new Parser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($value, $selector->getSpecificity()->getValue());
}
/** @dataProvider getParseSeriesTestData */
public function testParseSeries($series, $a, $b)
{
$parser = new Parser();
$selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
$this->assertCount(1, $selectors);
/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->assertEquals(array($a, $b), Parser::parseSeries($function->getArguments()));
}
/** @dataProvider getParseSeriesExceptionTestData */
public function testParseSeriesException($series)
{
$parser = new Parser();
$selectors = $parser->parse(sprintf(':nth-child(%s)', $series));
$this->assertCount(1, $selectors);
/** @var FunctionNode $function */
$function = $selectors[0]->getTree();
$this->setExpectedException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
Parser::parseSeries($function->getArguments());
}
public function getParserTestData()
{
return array(
array('*', array('Element[*]')),
array('*|*', array('Element[*]')),
array('*|foo', array('Element[foo]')),
array('foo|*', array('Element[foo|*]')),
array('foo|bar', array('Element[foo|bar]')),
array('#foo#bar', array('Hash[Hash[Element[*]#foo]#bar]')),
array('div>.foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
array('div> .foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
array('div >.foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
array('div > .foo', array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
array("div \n> \t \t .foo", array('CombinedSelector[Element[div] > Class[Element[*].foo]]')),
array('td.foo,.bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
array('td.foo, .bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
array("td.foo\t\r\n\f ,\t\r\n\f .bar", array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
array('td.foo,.bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
array('td.foo, .bar', array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
array("td.foo\t\r\n\f ,\t\r\n\f .bar", array('Class[Element[td].foo]', 'Class[Element[*].bar]')),
array('div, td.foo, div.bar span', array('Element[div]', 'Class[Element[td].foo]', 'CombinedSelector[Class[Element[div].bar] <followed> Element[span]]')),
array('div > p', array('CombinedSelector[Element[div] > Element[p]]')),
array('td:first', array('Pseudo[Element[td]:first]')),
array('td :first', array('CombinedSelector[Element[td] <followed> Pseudo[Element[*]:first]]')),
array('a[name]', array('Attribute[Element[a][name]]')),
array("a[ name\t]", array('Attribute[Element[a][name]]')),
array('a [name]', array('CombinedSelector[Element[a] <followed> Attribute[Element[*][name]]]')),
array('a[rel="include"]', array("Attribute[Element[a][rel = 'include']]")),
array('a[rel = include]', array("Attribute[Element[a][rel = 'include']]")),
array("a[hreflang |= 'en']", array("Attribute[Element[a][hreflang |= 'en']]")),
array('a[hreflang|=en]', array("Attribute[Element[a][hreflang |= 'en']]")),
array('div:nth-child(10)', array("Function[Element[div]:nth-child(['10'])]")),
array(':nth-child(2n+2)', array("Function[Element[*]:nth-child(['2', 'n', '+2'])]")),
array('div:nth-of-type(10)', array("Function[Element[div]:nth-of-type(['10'])]")),
array('div div:nth-of-type(10) .aclass', array("CombinedSelector[CombinedSelector[Element[div] <followed> Function[Element[div]:nth-of-type(['10'])]] <followed> Class[Element[*].aclass]]")),
array('label:only', array('Pseudo[Element[label]:only]')),
array('a:lang(fr)', array("Function[Element[a]:lang(['fr'])]")),
array('div:contains("foo")', array("Function[Element[div]:contains(['foo'])]")),
array('div#foobar', array('Hash[Element[div]#foobar]')),
array('div:not(div.foo)', array('Negation[Element[div]:not(Class[Element[div].foo])]')),
array('td ~ th', array('CombinedSelector[Element[td] ~ Element[th]]')),
array('.foo[data-bar][data-baz=0]', array("Attribute[Attribute[Class[Element[*].foo][data-bar]][data-baz = '0']]")),
);
}
public function getParserExceptionTestData()
{
return array(
array('attributes(href)/html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
array('attributes(href)', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '(', 10))->getMessage()),
array('html/body/a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '/', 4))->getMessage()),
array(' ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 1))->getMessage()),
array('div, ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 5))->getMessage()),
array(' , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 1))->getMessage()),
array('p, , div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, ',', 3))->getMessage()),
array('div > ', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_FILE_END, '', 6))->getMessage()),
array(' > div', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '>', 2))->getMessage()),
array('foo|#bar', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_HASH, 'bar', 4))->getMessage()),
array('#.foo', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '#', 0))->getMessage()),
array('.#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
array(':#foo', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_HASH, 'foo', 1))->getMessage()),
array('[*]', SyntaxErrorException::unexpectedToken('"|"', new Token(Token::TYPE_DELIMITER, ']', 2))->getMessage()),
array('[foo|]', SyntaxErrorException::unexpectedToken('identifier', new Token(Token::TYPE_DELIMITER, ']', 5))->getMessage()),
array('[#]', SyntaxErrorException::unexpectedToken('identifier or "*"', new Token(Token::TYPE_DELIMITER, '#', 1))->getMessage()),
array('[foo=#]', SyntaxErrorException::unexpectedToken('string or identifier', new Token(Token::TYPE_DELIMITER, '#', 5))->getMessage()),
array(':nth-child()', SyntaxErrorException::unexpectedToken('at least one argument', new Token(Token::TYPE_DELIMITER, ')', 11))->getMessage()),
array('[href]a', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_IDENTIFIER, 'a', 6))->getMessage()),
array('[rel:stylesheet]', SyntaxErrorException::unexpectedToken('operator', new Token(Token::TYPE_DELIMITER, ':', 4))->getMessage()),
array('[rel=stylesheet', SyntaxErrorException::unexpectedToken('"]"', new Token(Token::TYPE_FILE_END, '', 15))->getMessage()),
array(':lang(fr', SyntaxErrorException::unexpectedToken('an argument', new Token(Token::TYPE_FILE_END, '', 8))->getMessage()),
array(':contains("foo', SyntaxErrorException::unclosedString(10)->getMessage()),
array('foo!', SyntaxErrorException::unexpectedToken('selector', new Token(Token::TYPE_DELIMITER, '!', 3))->getMessage()),
);
}
public function getPseudoElementsTestData()
{
return array(
array('foo', 'Element[foo]', ''),
array('*', 'Element[*]', ''),
array(':empty', 'Pseudo[Element[*]:empty]', ''),
array(':BEfore', 'Element[*]', 'before'),
array(':aftER', 'Element[*]', 'after'),
array(':First-Line', 'Element[*]', 'first-line'),
array(':First-Letter', 'Element[*]', 'first-letter'),
array('::befoRE', 'Element[*]', 'before'),
array('::AFter', 'Element[*]', 'after'),
array('::firsT-linE', 'Element[*]', 'first-line'),
array('::firsT-letteR', 'Element[*]', 'first-letter'),
array('::Selection', 'Element[*]', 'selection'),
array('foo:after', 'Element[foo]', 'after'),
array('foo::selection', 'Element[foo]', 'selection'),
array('lorem#ipsum ~ a#b.c[href]:empty::selection', 'CombinedSelector[Hash[Element[lorem]#ipsum] ~ Pseudo[Attribute[Class[Hash[Element[a]#b].c][href]]:empty]]', 'selection'),
);
}
public function getSpecificityTestData()
{
return array(
array('*', 0),
array(' foo', 1),
array(':empty ', 10),
array(':before', 1),
array('*:before', 1),
array(':nth-child(2)', 10),
array('.bar', 10),
array('[baz]', 10),
array('[baz="4"]', 10),
array('[baz^="4"]', 10),
array('#lipsum', 100),
array(':not(*)', 0),
array(':not(foo)', 1),
array(':not(.foo)', 10),
array(':not([foo])', 10),
array(':not(:empty)', 10),
array(':not(#foo)', 100),
array('foo:empty', 11),
array('foo:before', 2),
array('foo::before', 2),
array('foo:empty::before', 12),
array('#lorem + foo#ipsum:first-child > bar:first-line', 213),
);
}
public function getParseSeriesTestData()
{
return array(
array('1n+3', 1, 3),
array('1n +3', 1, 3),
array('1n + 3', 1, 3),
array('1n+ 3', 1, 3),
array('1n-3', 1, -3),
array('1n -3', 1, -3),
array('1n - 3', 1, -3),
array('1n- 3', 1, -3),
array('n-5', 1, -5),
array('odd', 2, 1),
array('even', 2, 0),
array('3n', 3, 0),
array('n', 1, 0),
array('+n', 1, 0),
array('-n', -1, 0),
array('5', 0, 5),
);
}
public function getParseSeriesExceptionTestData()
{
return array(
array('foo'),
array('n+'),
);
}
}

View File

@@ -1,101 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser;
use Symfony\Component\CssSelector\Parser\Reader;
class ReaderTest extends \PHPUnit_Framework_TestCase
{
public function testIsEOF()
{
$reader = new Reader('');
$this->assertTrue($reader->isEOF());
$reader = new Reader('hello');
$this->assertFalse($reader->isEOF());
$this->assignPosition($reader, 2);
$this->assertFalse($reader->isEOF());
$this->assignPosition($reader, 5);
$this->assertTrue($reader->isEOF());
}
public function testGetRemainingLength()
{
$reader = new Reader('hello');
$this->assertEquals(5, $reader->getRemainingLength());
$this->assignPosition($reader, 2);
$this->assertEquals(3, $reader->getRemainingLength());
$this->assignPosition($reader, 5);
$this->assertEquals(0, $reader->getRemainingLength());
}
public function testGetSubstring()
{
$reader = new Reader('hello');
$this->assertEquals('he', $reader->getSubstring(2));
$this->assertEquals('el', $reader->getSubstring(2, 1));
$this->assignPosition($reader, 2);
$this->assertEquals('ll', $reader->getSubstring(2));
$this->assertEquals('lo', $reader->getSubstring(2, 1));
}
public function testGetOffset()
{
$reader = new Reader('hello');
$this->assertEquals(2, $reader->getOffset('ll'));
$this->assertFalse($reader->getOffset('w'));
$this->assignPosition($reader, 2);
$this->assertEquals(0, $reader->getOffset('ll'));
$this->assertFalse($reader->getOffset('he'));
}
public function testFindPattern()
{
$reader = new Reader('hello');
$this->assertFalse($reader->findPattern('/world/'));
$this->assertEquals(array('hello', 'h'), $reader->findPattern('/^([a-z]).*/'));
$this->assignPosition($reader, 2);
$this->assertFalse($reader->findPattern('/^h.*/'));
$this->assertEquals(array('llo'), $reader->findPattern('/^llo$/'));
}
public function testMoveForward()
{
$reader = new Reader('hello');
$this->assertEquals(0, $reader->getPosition());
$reader->moveForward(2);
$this->assertEquals(2, $reader->getPosition());
}
public function testToEnd()
{
$reader = new Reader('hello');
$reader->moveToEnd();
$this->assertTrue($reader->isEOF());
}
private function assignPosition(Reader $reader, $value)
{
$position = new \ReflectionProperty($reader, 'position');
$position->setAccessible(true);
$position->setValue($reader, $value);
}
}

View File

@@ -1,44 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class ClassParserTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getParseTestData */
public function testParse($source, $representation)
{
$parser = new ClassParser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($representation, (string) $selector->getTree());
}
public function getParseTestData()
{
return array(
array('.testclass', 'Class[Element[*].testclass]'),
array('testel.testclass', 'Class[Element[testel].testclass]'),
array('testns|.testclass', 'Class[Element[testns|*].testclass]'),
array('testns|*.testclass', 'Class[Element[testns|*].testclass]'),
array('testns|testel.testclass', 'Class[Element[testns|testel].testclass]'),
);
}
}

View File

@@ -1,43 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class ElementParserTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getParseTestData */
public function testParse($source, $representation)
{
$parser = new ElementParser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($representation, (string) $selector->getTree());
}
public function getParseTestData()
{
return array(
array('*', 'Element[*]'),
array('testel', 'Element[testel]'),
array('testns|*', 'Element[testns|*]'),
array('testns|testel', 'Element[testns|testel]'),
);
}
}

View File

@@ -1,35 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class EmptyStringParserTest extends \PHPUnit_Framework_TestCase
{
public function testParse()
{
$parser = new EmptyStringParser();
$selectors = $parser->parse('');
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals('Element[*]', (string) $selector->getTree());
$selectors = $parser->parse('this will produce an empty array');
$this->assertCount(0, $selectors);
}
}

View File

@@ -1,44 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser\Shortcut;
use Symfony\Component\CssSelector\Node\SelectorNode;
use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*/
class HashParserTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getParseTestData */
public function testParse($source, $representation)
{
$parser = new HashParser();
$selectors = $parser->parse($source);
$this->assertCount(1, $selectors);
/** @var SelectorNode $selector */
$selector = $selectors[0];
$this->assertEquals($representation, (string) $selector->getTree());
}
public function getParseTestData()
{
return array(
array('#testid', 'Hash[Element[*]#testid]'),
array('testel#testid', 'Hash[Element[testel]#testid]'),
array('testns|#testid', 'Hash[Element[testns|*]#testid]'),
array('testns|*#testid', 'Hash[Element[testns|*]#testid]'),
array('testns|testel#testid', 'Hash[Element[testns|testel]#testid]'),
);
}
}

View File

@@ -1,95 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\Parser;
use Symfony\Component\CssSelector\Parser\Token;
use Symfony\Component\CssSelector\Parser\TokenStream;
class TokenStreamTest extends \PHPUnit_Framework_TestCase
{
public function testGetNext()
{
$stream = new TokenStream();
$stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));
$this->assertSame($t1, $stream->getNext());
$this->assertSame($t2, $stream->getNext());
$this->assertSame($t3, $stream->getNext());
}
public function testGetPeek()
{
$stream = new TokenStream();
$stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$stream->push($t2 = new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'title', 3));
$this->assertSame($t1, $stream->getPeek());
$this->assertSame($t1, $stream->getNext());
$this->assertSame($t2, $stream->getPeek());
$this->assertSame($t2, $stream->getPeek());
$this->assertSame($t2, $stream->getNext());
}
public function testGetNextIdentifier()
{
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$this->assertEquals('h1', $stream->getNextIdentifier());
}
public function testFailToGetNextIdentifier()
{
$this->setExpectedException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->getNextIdentifier();
}
public function testGetNextIdentifierOrStar()
{
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$this->assertEquals('h1', $stream->getNextIdentifierOrStar());
$stream->push(new Token(Token::TYPE_DELIMITER, '*', 0));
$this->assertNull($stream->getNextIdentifierOrStar());
}
public function testFailToGetNextIdentifierOrStar()
{
$this->setExpectedException('Symfony\Component\CssSelector\Exception\SyntaxErrorException');
$stream = new TokenStream();
$stream->push(new Token(Token::TYPE_DELIMITER, '.', 2));
$stream->getNextIdentifierOrStar();
}
public function testSkipWhitespace()
{
$stream = new TokenStream();
$stream->push($t1 = new Token(Token::TYPE_IDENTIFIER, 'h1', 0));
$stream->push($t2 = new Token(Token::TYPE_WHITESPACE, ' ', 2));
$stream->push($t3 = new Token(Token::TYPE_IDENTIFIER, 'h1', 3));
$stream->skipWhitespace();
$this->assertSame($t1, $stream->getNext());
$stream->skipWhitespace();
$this->assertSame($t3, $stream->getNext());
}
}

View File

@@ -1,48 +0,0 @@
<html id="html"><head>
<link id="link-href" href="foo" />
<link id="link-nohref" />
</head><body>
<div id="outer-div">
<a id="name-anchor" name="foo"></a>
<a id="tag-anchor" rel="tag" href="http://localhost/foo">link</a>
<a id="nofollow-anchor" rel="nofollow" href="https://example.org">
link</a>
<ol id="first-ol" class="a b c">
<li id="first-li">content</li>
<li id="second-li" lang="En-us">
<div id="li-div">
</div>
</li>
<li id="third-li" class="ab c"></li>
<li id="fourth-li" class="ab
c"></li>
<li id="fifth-li"></li>
<li id="sixth-li"></li>
<li id="seventh-li"> </li>
</ol>
<p id="paragraph">
<b id="p-b">hi</b> <em id="p-em">there</em>
<b id="p-b2">guy</b>
<input type="checkbox" id="checkbox-unchecked" />
<input type="checkbox" id="checkbox-disabled" disabled="" />
<input type="text" id="text-checked" checked="checked" />
<input type="hidden" />
<input type="hidden" disabled="disabled" />
<input type="checkbox" id="checkbox-checked" checked="checked" />
<input type="checkbox" id="checkbox-disabled-checked"
disabled="disabled" checked="checked" />
<fieldset id="fieldset" disabled="disabled">
<input type="checkbox" id="checkbox-fieldset-disabled" />
<input type="hidden" />
</fieldset>
</p>
<ol id="second-ol">
</ol>
<map name="dummymap">
<area shape="circle" coords="200,250,25" href="foo.html" id="area-href" />
<area shape="default" id="area-nohref" />
</map>
</div>
<div id="foobar-div" foobar="ab bc
cde"><span id="foobar-span"></span></div>
</body></html>

View File

@@ -1,11 +0,0 @@
<test>
<a id="first" xml:lang="en">a</a>
<b id="second" xml:lang="en-US">b</b>
<c id="third" xml:lang="en-Nz">c</c>
<d id="fourth" xml:lang="En-us">d</d>
<e id="fifth" xml:lang="fr">e</e>
<f id="sixth" xml:lang="ru">f</f>
<g id="seventh" xml:lang="de">
<h id="eighth" xml:lang="zh"/>
</g>
</test>

View File

@@ -1,308 +0,0 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" debug="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div id="test">
<div class="dialog">
<h2>As You Like It</h2>
<div id="playwright">
by William Shakespeare
</div>
<div class="dialog scene thirdClass" id="scene1">
<h3>ACT I, SCENE III. A room in the palace.</h3>
<div class="dialog">
<div class="direction">Enter CELIA and ROSALIND</div>
</div>
<div id="speech1" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.1">Why, cousin! why, Rosalind! Cupid have mercy! not a word?</div>
</div>
<div id="speech2" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.2">Not one to throw at a dog.</div>
</div>
<div id="speech3" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.3">No, thy words are too precious to be cast away upon</div>
<div id="scene1.3.4">curs; throw some of them at me; come, lame me with reasons.</div>
</div>
<div id="speech4" class="character">ROSALIND</div>
<div id="speech5" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.8">But is all this for your father?</div>
</div>
<div class="dialog">
<div id="scene1.3.5">Then there were two cousins laid up; when the one</div>
<div id="scene1.3.6">should be lamed with reasons and the other mad</div>
<div id="scene1.3.7">without any.</div>
</div>
<div id="speech6" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.9">No, some of it is for my child's father. O, how</div>
<div id="scene1.3.10">full of briers is this working-day world!</div>
</div>
<div id="speech7" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.11">They are but burs, cousin, thrown upon thee in</div>
<div id="scene1.3.12">holiday foolery: if we walk not in the trodden</div>
<div id="scene1.3.13">paths our very petticoats will catch them.</div>
</div>
<div id="speech8" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.14">I could shake them off my coat: these burs are in my heart.</div>
</div>
<div id="speech9" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.15">Hem them away.</div>
</div>
<div id="speech10" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.16">I would try, if I could cry 'hem' and have him.</div>
</div>
<div id="speech11" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.17">Come, come, wrestle with thy affections.</div>
</div>
<div id="speech12" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.18">O, they take the part of a better wrestler than myself!</div>
</div>
<div id="speech13" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.19">O, a good wish upon you! you will try in time, in</div>
<div id="scene1.3.20">despite of a fall. But, turning these jests out of</div>
<div id="scene1.3.21">service, let us talk in good earnest: is it</div>
<div id="scene1.3.22">possible, on such a sudden, you should fall into so</div>
<div id="scene1.3.23">strong a liking with old Sir Rowland's youngest son?</div>
</div>
<div id="speech14" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.24">The duke my father loved his father dearly.</div>
</div>
<div id="speech15" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.25">Doth it therefore ensue that you should love his son</div>
<div id="scene1.3.26">dearly? By this kind of chase, I should hate him,</div>
<div id="scene1.3.27">for my father hated his father dearly; yet I hate</div>
<div id="scene1.3.28">not Orlando.</div>
</div>
<div id="speech16" class="character">ROSALIND</div>
<div title="wtf" class="dialog">
<div id="scene1.3.29">No, faith, hate him not, for my sake.</div>
</div>
<div id="speech17" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.30">Why should I not? doth he not deserve well?</div>
</div>
<div id="speech18" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.31">Let me love him for that, and do you love him</div>
<div id="scene1.3.32">because I do. Look, here comes the duke.</div>
</div>
<div id="speech19" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.33">With his eyes full of anger.</div>
<div class="direction">Enter DUKE FREDERICK, with Lords</div>
</div>
<div id="speech20" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.34">Mistress, dispatch you with your safest haste</div>
<div id="scene1.3.35">And get you from our court.</div>
</div>
<div id="speech21" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.36">Me, uncle?</div>
</div>
<div id="speech22" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.37">You, cousin</div>
<div id="scene1.3.38">Within these ten days if that thou be'st found</div>
<div id="scene1.3.39">So near our public court as twenty miles,</div>
<div id="scene1.3.40">Thou diest for it.</div>
</div>
<div id="speech23" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.41"> I do beseech your grace,</div>
<div id="scene1.3.42">Let me the knowledge of my fault bear with me:</div>
<div id="scene1.3.43">If with myself I hold intelligence</div>
<div id="scene1.3.44">Or have acquaintance with mine own desires,</div>
<div id="scene1.3.45">If that I do not dream or be not frantic,--</div>
<div id="scene1.3.46">As I do trust I am not--then, dear uncle,</div>
<div id="scene1.3.47">Never so much as in a thought unborn</div>
<div id="scene1.3.48">Did I offend your highness.</div>
</div>
<div id="speech24" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.49">Thus do all traitors:</div>
<div id="scene1.3.50">If their purgation did consist in words,</div>
<div id="scene1.3.51">They are as innocent as grace itself:</div>
<div id="scene1.3.52">Let it suffice thee that I trust thee not.</div>
</div>
<div id="speech25" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.53">Yet your mistrust cannot make me a traitor:</div>
<div id="scene1.3.54">Tell me whereon the likelihood depends.</div>
</div>
<div id="speech26" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.55">Thou art thy father's daughter; there's enough.</div>
</div>
<div id="speech27" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.56">So was I when your highness took his dukedom;</div>
<div id="scene1.3.57">So was I when your highness banish'd him:</div>
<div id="scene1.3.58">Treason is not inherited, my lord;</div>
<div id="scene1.3.59">Or, if we did derive it from our friends,</div>
<div id="scene1.3.60">What's that to me? my father was no traitor:</div>
<div id="scene1.3.61">Then, good my liege, mistake me not so much</div>
<div id="scene1.3.62">To think my poverty is treacherous.</div>
</div>
<div id="speech28" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.63">Dear sovereign, hear me speak.</div>
</div>
<div id="speech29" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.64">Ay, Celia; we stay'd her for your sake,</div>
<div id="scene1.3.65">Else had she with her father ranged along.</div>
</div>
<div id="speech30" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.66">I did not then entreat to have her stay;</div>
<div id="scene1.3.67">It was your pleasure and your own remorse:</div>
<div id="scene1.3.68">I was too young that time to value her;</div>
<div id="scene1.3.69">But now I know her: if she be a traitor,</div>
<div id="scene1.3.70">Why so am I; we still have slept together,</div>
<div id="scene1.3.71">Rose at an instant, learn'd, play'd, eat together,</div>
<div id="scene1.3.72">And wheresoever we went, like Juno's swans,</div>
<div id="scene1.3.73">Still we went coupled and inseparable.</div>
</div>
<div id="speech31" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.74">She is too subtle for thee; and her smoothness,</div>
<div id="scene1.3.75">Her very silence and her patience</div>
<div id="scene1.3.76">Speak to the people, and they pity her.</div>
<div id="scene1.3.77">Thou art a fool: she robs thee of thy name;</div>
<div id="scene1.3.78">And thou wilt show more bright and seem more virtuous</div>
<div id="scene1.3.79">When she is gone. Then open not thy lips:</div>
<div id="scene1.3.80">Firm and irrevocable is my doom</div>
<div id="scene1.3.81">Which I have pass'd upon her; she is banish'd.</div>
</div>
<div id="speech32" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.82">Pronounce that sentence then on me, my liege:</div>
<div id="scene1.3.83">I cannot live out of her company.</div>
</div>
<div id="speech33" class="character">DUKE FREDERICK</div>
<div class="dialog">
<div id="scene1.3.84">You are a fool. You, niece, provide yourself:</div>
<div id="scene1.3.85">If you outstay the time, upon mine honour,</div>
<div id="scene1.3.86">And in the greatness of my word, you die.</div>
<div class="direction">Exeunt DUKE FREDERICK and Lords</div>
</div>
<div id="speech34" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.87">O my poor Rosalind, whither wilt thou go?</div>
<div id="scene1.3.88">Wilt thou change fathers? I will give thee mine.</div>
<div id="scene1.3.89">I charge thee, be not thou more grieved than I am.</div>
</div>
<div id="speech35" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.90">I have more cause.</div>
</div>
<div id="speech36" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.91"> Thou hast not, cousin;</div>
<div id="scene1.3.92">Prithee be cheerful: know'st thou not, the duke</div>
<div id="scene1.3.93">Hath banish'd me, his daughter?</div>
</div>
<div id="speech37" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.94">That he hath not.</div>
</div>
<div id="speech38" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.95">No, hath not? Rosalind lacks then the love</div>
<div id="scene1.3.96">Which teacheth thee that thou and I am one:</div>
<div id="scene1.3.97">Shall we be sunder'd? shall we part, sweet girl?</div>
<div id="scene1.3.98">No: let my father seek another heir.</div>
<div id="scene1.3.99">Therefore devise with me how we may fly,</div>
<div id="scene1.3.100">Whither to go and what to bear with us;</div>
<div id="scene1.3.101">And do not seek to take your change upon you,</div>
<div id="scene1.3.102">To bear your griefs yourself and leave me out;</div>
<div id="scene1.3.103">For, by this heaven, now at our sorrows pale,</div>
<div id="scene1.3.104">Say what thou canst, I'll go along with thee.</div>
</div>
<div id="speech39" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.105">Why, whither shall we go?</div>
</div>
<div id="speech40" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.106">To seek my uncle in the forest of Arden.</div>
</div>
<div id="speech41" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.107">Alas, what danger will it be to us,</div>
<div id="scene1.3.108">Maids as we are, to travel forth so far!</div>
<div id="scene1.3.109">Beauty provoketh thieves sooner than gold.</div>
</div>
<div id="speech42" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.110">I'll put myself in poor and mean attire</div>
<div id="scene1.3.111">And with a kind of umber smirch my face;</div>
<div id="scene1.3.112">The like do you: so shall we pass along</div>
<div id="scene1.3.113">And never stir assailants.</div>
</div>
<div id="speech43" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.114">Were it not better,</div>
<div id="scene1.3.115">Because that I am more than common tall,</div>
<div id="scene1.3.116">That I did suit me all points like a man?</div>
<div id="scene1.3.117">A gallant curtle-axe upon my thigh,</div>
<div id="scene1.3.118">A boar-spear in my hand; and--in my heart</div>
<div id="scene1.3.119">Lie there what hidden woman's fear there will--</div>
<div id="scene1.3.120">We'll have a swashing and a martial outside,</div>
<div id="scene1.3.121">As many other mannish cowards have</div>
<div id="scene1.3.122">That do outface it with their semblances.</div>
</div>
<div id="speech44" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.123">What shall I call thee when thou art a man?</div>
</div>
<div id="speech45" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.124">I'll have no worse a name than Jove's own page;</div>
<div id="scene1.3.125">And therefore look you call me Ganymede.</div>
<div id="scene1.3.126">But what will you be call'd?</div>
</div>
<div id="speech46" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.127">Something that hath a reference to my state</div>
<div id="scene1.3.128">No longer Celia, but Aliena.</div>
</div>
<div id="speech47" class="character">ROSALIND</div>
<div class="dialog">
<div id="scene1.3.129">But, cousin, what if we assay'd to steal</div>
<div id="scene1.3.130">The clownish fool out of your father's court?</div>
<div id="scene1.3.131">Would he not be a comfort to our travel?</div>
</div>
<div id="speech48" class="character">CELIA</div>
<div class="dialog">
<div id="scene1.3.132">He'll go along o'er the wide world with me;</div>
<div id="scene1.3.133">Leave me alone to woo him. Let's away,</div>
<div id="scene1.3.134">And get our jewels and our wealth together,</div>
<div id="scene1.3.135">Devise the fittest time and safest way</div>
<div id="scene1.3.136">To hide us from pursuit that will be made</div>
<div id="scene1.3.137">After my flight. Now go we in content</div>
<div id="scene1.3.138">To liberty and not to banishment.</div>
<div class="direction">Exeunt</div>
</div>
</div>
</div>
</div>
</body>
</html>

View File

@@ -1,324 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\CssSelector\Tests\XPath;
use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
use Symfony\Component\CssSelector\XPath\Translator;
class TranslatorTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getXpathLiteralTestData */
public function testXpathLiteral($value, $literal)
{
$this->assertEquals($literal, Translator::getXpathLiteral($value));
}
/** @dataProvider getCssToXPathTestData */
public function testCssToXPath($css, $xpath)
{
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$this->assertEquals($xpath, $translator->cssToXPath($css, ''));
}
/** @dataProvider getXmlLangTestData */
public function testXmlLang($css, array $elementsId)
{
$translator = new Translator();
$document = new \SimpleXMLElement(file_get_contents(__DIR__.'/Fixtures/lang.xml'));
$elements = $document->xpath($translator->cssToXPath($css));
$this->assertEquals(count($elementsId), count($elements));
foreach ($elements as $element) {
$this->assertTrue(in_array($element->attributes()->id, $elementsId));
}
}
/** @dataProvider getHtmlIdsTestData */
public function testHtmlIds($css, array $elementsId)
{
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$document = new \DOMDocument();
$document->strictErrorChecking = false;
$internalErrors = libxml_use_internal_errors(true);
$document->loadHTMLFile(__DIR__.'/Fixtures/ids.html');
$document = simplexml_import_dom($document);
$elements = $document->xpath($translator->cssToXPath($css));
$this->assertCount(count($elementsId), $elementsId);
foreach ($elements as $element) {
if (null !== $element->attributes()->id) {
$this->assertTrue(in_array($element->attributes()->id, $elementsId));
}
}
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
}
/** @dataProvider getHtmlShakespearTestData */
public function testHtmlShakespear($css, $count)
{
$translator = new Translator();
$translator->registerExtension(new HtmlExtension($translator));
$document = new \DOMDocument();
$document->strictErrorChecking = false;
$document->loadHTMLFile(__DIR__.'/Fixtures/shakespear.html');
$document = simplexml_import_dom($document);
$bodies = $document->xpath('//body');
$elements = $bodies[0]->xpath($translator->cssToXPath($css));
$this->assertCount($count, $elements);
}
public function getXpathLiteralTestData()
{
return array(
array('foo', "'foo'"),
array("foo's bar", '"foo\'s bar"'),
array("foo's \"middle\" bar", 'concat(\'foo\', "\'", \'s "middle" bar\')'),
array("foo's 'middle' \"bar\"", 'concat(\'foo\', "\'", \'s \', "\'", \'middle\', "\'", \' "bar"\')'),
);
}
public function getCssToXPathTestData()
{
return array(
array('*', '*'),
array('e', 'e'),
array('*|e', 'e'),
array('e|f', 'e:f'),
array('e[foo]', 'e[@foo]'),
array('e[foo|bar]', 'e[@foo:bar]'),
array('e[foo="bar"]', "e[@foo = 'bar']"),
array('e[foo~="bar"]', "e[@foo and contains(concat(' ', normalize-space(@foo), ' '), ' bar ')]"),
array('e[foo^="bar"]', "e[@foo and starts-with(@foo, 'bar')]"),
array('e[foo$="bar"]', "e[@foo and substring(@foo, string-length(@foo)-2) = 'bar']"),
array('e[foo*="bar"]', "e[@foo and contains(@foo, 'bar')]"),
array('e[hreflang|="en"]', "e[@hreflang and (@hreflang = 'en' or starts-with(@hreflang, 'en-'))]"),
array('e:nth-child(1)', "*/*[name() = 'e' and (position() = 1)]"),
array('e:nth-last-child(1)', "*/*[name() = 'e' and (position() = last() - 0)]"),
array('e:nth-last-child(2n+2)', "*/*[name() = 'e' and (last() - position() - 1 >= 0 and (last() - position() - 1) mod 2 = 0)]"),
array('e:nth-of-type(1)', '*/e[position() = 1]'),
array('e:nth-last-of-type(1)', '*/e[position() = last() - 0]'),
array('div e:nth-last-of-type(1) .aclass', "div/descendant-or-self::*/e[position() = last() - 0]/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' aclass ')]"),
array('e:first-child', "*/*[name() = 'e' and (position() = 1)]"),
array('e:last-child', "*/*[name() = 'e' and (position() = last())]"),
array('e:first-of-type', '*/e[position() = 1]'),
array('e:last-of-type', '*/e[position() = last()]'),
array('e:only-child', "*/*[name() = 'e' and (last() = 1)]"),
array('e:only-of-type', 'e[last() = 1]'),
array('e:empty', 'e[not(*) and not(string-length())]'),
array('e:EmPTY', 'e[not(*) and not(string-length())]'),
array('e:root', 'e[not(parent::*)]'),
array('e:hover', 'e[0]'),
array('e:contains("foo")', "e[contains(string(.), 'foo')]"),
array('e:ConTains(foo)', "e[contains(string(.), 'foo')]"),
array('e.warning', "e[@class and contains(concat(' ', normalize-space(@class), ' '), ' warning ')]"),
array('e#myid', "e[@id = 'myid']"),
array('e:not(:nth-child(odd))', 'e[not(position() - 1 >= 0 and (position() - 1) mod 2 = 0)]'),
array('e:nOT(*)', 'e[0]'),
array('e f', 'e/descendant-or-self::*/f'),
array('e > f', 'e/f'),
array('e + f', "e/following-sibling::*[name() = 'f' and (position() = 1)]"),
array('e ~ f', 'e/following-sibling::f'),
array('div#container p', "div[@id = 'container']/descendant-or-self::*/p"),
);
}
public function getXmlLangTestData()
{
return array(
array(':lang("EN")', array('first', 'second', 'third', 'fourth')),
array(':lang("en-us")', array('second', 'fourth')),
array(':lang(en-nz)', array('third')),
array(':lang(fr)', array('fifth')),
array(':lang(ru)', array('sixth')),
array(":lang('ZH')", array('eighth')),
array(':lang(de) :lang(zh)', array('eighth')),
array(':lang(en), :lang(zh)', array('first', 'second', 'third', 'fourth', 'eighth')),
array(':lang(es)', array()),
);
}
public function getHtmlIdsTestData()
{
return array(
array('div', array('outer-div', 'li-div', 'foobar-div')),
array('DIV', array('outer-div', 'li-div', 'foobar-div')), // case-insensitive in HTML
array('div div', array('li-div')),
array('div, div div', array('outer-div', 'li-div', 'foobar-div')),
array('a[name]', array('name-anchor')),
array('a[NAme]', array('name-anchor')), // case-insensitive in HTML:
array('a[rel]', array('tag-anchor', 'nofollow-anchor')),
array('a[rel="tag"]', array('tag-anchor')),
array('a[href*="localhost"]', array('tag-anchor')),
array('a[href*=""]', array()),
array('a[href^="http"]', array('tag-anchor', 'nofollow-anchor')),
array('a[href^="http:"]', array('tag-anchor')),
array('a[href^=""]', array()),
array('a[href$="org"]', array('nofollow-anchor')),
array('a[href$=""]', array()),
array('div[foobar~="bc"]', array('foobar-div')),
array('div[foobar~="cde"]', array('foobar-div')),
array('[foobar~="ab bc"]', array('foobar-div')),
array('[foobar~=""]', array()),
array('[foobar~=" \t"]', array()),
array('div[foobar~="cd"]', array()),
array('*[lang|="En"]', array('second-li')),
array('[lang|="En-us"]', array('second-li')),
// Attribute values are case sensitive
array('*[lang|="en"]', array()),
array('[lang|="en-US"]', array()),
array('*[lang|="e"]', array()),
// ... :lang() is not.
array(':lang("EN")', array('second-li', 'li-div')),
array('*:lang(en-US)', array('second-li', 'li-div')),
array(':lang("e")', array()),
array('li:nth-child(3)', array('third-li')),
array('li:nth-child(10)', array()),
array('li:nth-child(2n)', array('second-li', 'fourth-li', 'sixth-li')),
array('li:nth-child(even)', array('second-li', 'fourth-li', 'sixth-li')),
array('li:nth-child(2n+0)', array('second-li', 'fourth-li', 'sixth-li')),
array('li:nth-child(+2n+1)', array('first-li', 'third-li', 'fifth-li', 'seventh-li')),
array('li:nth-child(odd)', array('first-li', 'third-li', 'fifth-li', 'seventh-li')),
array('li:nth-child(2n+4)', array('fourth-li', 'sixth-li')),
array('li:nth-child(3n+1)', array('first-li', 'fourth-li', 'seventh-li')),
array('li:nth-child(n)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-child(n-1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-child(n+1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-child(n+3)', array('third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-child(-n)', array()),
array('li:nth-child(-n-1)', array()),
array('li:nth-child(-n+1)', array('first-li')),
array('li:nth-child(-n+3)', array('first-li', 'second-li', 'third-li')),
array('li:nth-last-child(0)', array()),
array('li:nth-last-child(2n)', array('second-li', 'fourth-li', 'sixth-li')),
array('li:nth-last-child(even)', array('second-li', 'fourth-li', 'sixth-li')),
array('li:nth-last-child(2n+2)', array('second-li', 'fourth-li', 'sixth-li')),
array('li:nth-last-child(n)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-last-child(n-1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-last-child(n-3)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-last-child(n+1)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li', 'sixth-li', 'seventh-li')),
array('li:nth-last-child(n+3)', array('first-li', 'second-li', 'third-li', 'fourth-li', 'fifth-li')),
array('li:nth-last-child(-n)', array()),
array('li:nth-last-child(-n-1)', array()),
array('li:nth-last-child(-n+1)', array('seventh-li')),
array('li:nth-last-child(-n+3)', array('fifth-li', 'sixth-li', 'seventh-li')),
array('ol:first-of-type', array('first-ol')),
array('ol:nth-child(1)', array('first-ol')),
array('ol:nth-of-type(2)', array('second-ol')),
array('ol:nth-last-of-type(1)', array('second-ol')),
array('span:only-child', array('foobar-span')),
array('li div:only-child', array('li-div')),
array('div *:only-child', array('li-div', 'foobar-span')),
array('p:only-of-type', array('paragraph')),
array('a:empty', array('name-anchor')),
array('a:EMpty', array('name-anchor')),
array('li:empty', array('third-li', 'fourth-li', 'fifth-li', 'sixth-li')),
array(':root', array('html')),
array('html:root', array('html')),
array('li:root', array()),
array('* :root', array()),
array('*:contains("link")', array('html', 'outer-div', 'tag-anchor', 'nofollow-anchor')),
array(':CONtains("link")', array('html', 'outer-div', 'tag-anchor', 'nofollow-anchor')),
array('*:contains("LInk")', array()), // case sensitive
array('*:contains("e")', array('html', 'nil', 'outer-div', 'first-ol', 'first-li', 'paragraph', 'p-em')),
array('*:contains("E")', array()), // case-sensitive
array('.a', array('first-ol')),
array('.b', array('first-ol')),
array('*.a', array('first-ol')),
array('ol.a', array('first-ol')),
array('.c', array('first-ol', 'third-li', 'fourth-li')),
array('*.c', array('first-ol', 'third-li', 'fourth-li')),
array('ol *.c', array('third-li', 'fourth-li')),
array('ol li.c', array('third-li', 'fourth-li')),
array('li ~ li.c', array('third-li', 'fourth-li')),
array('ol > li.c', array('third-li', 'fourth-li')),
array('#first-li', array('first-li')),
array('li#first-li', array('first-li')),
array('*#first-li', array('first-li')),
array('li div', array('li-div')),
array('li > div', array('li-div')),
array('div div', array('li-div')),
array('div > div', array()),
array('div>.c', array('first-ol')),
array('div > .c', array('first-ol')),
array('div + div', array('foobar-div')),
array('a ~ a', array('tag-anchor', 'nofollow-anchor')),
array('a[rel="tag"] ~ a', array('nofollow-anchor')),
array('ol#first-ol li:last-child', array('seventh-li')),
array('ol#first-ol *:last-child', array('li-div', 'seventh-li')),
array('#outer-div:first-child', array('outer-div')),
array('#outer-div :first-child', array('name-anchor', 'first-li', 'li-div', 'p-b', 'checkbox-fieldset-disabled', 'area-href')),
array('a[href]', array('tag-anchor', 'nofollow-anchor')),
array(':not(*)', array()),
array('a:not([href])', array('name-anchor')),
array('ol :Not(li[class])', array('first-li', 'second-li', 'li-div', 'fifth-li', 'sixth-li', 'seventh-li')),
// HTML-specific
array(':link', array('link-href', 'tag-anchor', 'nofollow-anchor', 'area-href')),
array(':visited', array()),
array(':enabled', array('link-href', 'tag-anchor', 'nofollow-anchor', 'checkbox-unchecked', 'text-checked', 'checkbox-checked', 'area-href')),
array(':disabled', array('checkbox-disabled', 'checkbox-disabled-checked', 'fieldset', 'checkbox-fieldset-disabled')),
array(':checked', array('checkbox-checked', 'checkbox-disabled-checked')),
);
}
public function getHtmlShakespearTestData()
{
return array(
array('*', 246),
array('div:contains(CELIA)', 26),
array('div:only-child', 22), // ?
array('div:nth-child(even)', 106),
array('div:nth-child(2n)', 106),
array('div:nth-child(odd)', 137),
array('div:nth-child(2n+1)', 137),
array('div:nth-child(n)', 243),
array('div:last-child', 53),
array('div:first-child', 51),
array('div > div', 242),
array('div + div', 190),
array('div ~ div', 190),
array('body', 1),
array('body div', 243),
array('div', 243),
array('div div', 242),
array('div div div', 241),
array('div, div, div', 243),
array('div, a, span', 243),
array('.dialog', 51),
array('div.dialog', 51),
array('div .dialog', 51),
array('div.character, div.dialog', 99),
array('div.direction.dialog', 0),
array('div.dialog.direction', 0),
array('div.dialog.scene', 1),
array('div.scene.scene', 1),
array('div.scene .scene', 0),
array('div.direction .dialog ', 0),
array('div .dialog .direction', 4),
array('div.dialog .dialog .direction', 4),
array('#speech5', 1),
array('div#speech5', 1),
array('div #speech5', 1),
array('div.scene div.dialog', 49),
array('div#scene1 div.dialog div', 142),
array('#scene1 #speech1', 1),
array('div[class]', 103),
array('div[class=dialog]', 50),
array('div[class^=dia]', 51),
array('div[class$=log]', 50),
array('div[class*=sce]', 1),
array('div[class|=dialog]', 50), // ? Seems right
array('div[class!=madeup]', 243), // ? Seems right
array('div[class~=dialog]', 51), // ? Seems right
);
}
}

View File

@@ -26,40 +26,40 @@ abstract class AbstractExtension implements ExtensionInterface
/**
* {@inheritdoc}
*/
public function getNodeTranslators()
public function getNodeTranslators(): array
{
return array();
return [];
}
/**
* {@inheritdoc}
*/
public function getCombinationTranslators()
public function getCombinationTranslators(): array
{
return array();
return [];
}
/**
* {@inheritdoc}
*/
public function getFunctionTranslators()
public function getFunctionTranslators(): array
{
return array();
return [];
}
/**
* {@inheritdoc}
*/
public function getPseudoClassTranslators()
public function getPseudoClassTranslators(): array
{
return array();
return [];
}
/**
* {@inheritdoc}
*/
public function getAttributeMatchingTranslators()
public function getAttributeMatchingTranslators(): array
{
return array();
return [];
}
}

View File

@@ -29,52 +29,31 @@ class AttributeMatchingExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getAttributeMatchingTranslators()
public function getAttributeMatchingTranslators(): array
{
return array(
'exists' => array($this, 'translateExists'),
'=' => array($this, 'translateEquals'),
'~=' => array($this, 'translateIncludes'),
'|=' => array($this, 'translateDashMatch'),
'^=' => array($this, 'translatePrefixMatch'),
'$=' => array($this, 'translateSuffixMatch'),
'*=' => array($this, 'translateSubstringMatch'),
'!=' => array($this, 'translateDifferent'),
);
return [
'exists' => [$this, 'translateExists'],
'=' => [$this, 'translateEquals'],
'~=' => [$this, 'translateIncludes'],
'|=' => [$this, 'translateDashMatch'],
'^=' => [$this, 'translatePrefixMatch'],
'$=' => [$this, 'translateSuffixMatch'],
'*=' => [$this, 'translateSubstringMatch'],
'!=' => [$this, 'translateDifferent'],
];
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateExists(XPathExpr $xpath, $attribute, $value)
public function translateExists(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($attribute);
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateEquals(XPathExpr $xpath, $attribute, $value)
public function translateEquals(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateIncludes(XPathExpr $xpath, $attribute, $value)
public function translateIncludes(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
'%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
@@ -83,14 +62,7 @@ class AttributeMatchingExtension extends AbstractExtension
) : '0');
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateDashMatch(XPathExpr $xpath, $attribute, $value)
public function translateDashMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition(sprintf(
'%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
@@ -100,14 +72,7 @@ class AttributeMatchingExtension extends AbstractExtension
));
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translatePrefixMatch(XPathExpr $xpath, $attribute, $value)
public function translatePrefixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
'%1$s and starts-with(%1$s, %2$s)',
@@ -116,31 +81,17 @@ class AttributeMatchingExtension extends AbstractExtension
) : '0');
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateSuffixMatch(XPathExpr $xpath, $attribute, $value)
public function translateSuffixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
'%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
$attribute,
strlen($value) - 1,
\strlen($value) - 1,
Translator::getXpathLiteral($value)
) : '0');
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateSubstringMatch(XPathExpr $xpath, $attribute, $value)
public function translateSubstringMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition($value ? sprintf(
'%1$s and contains(%1$s, %2$s)',
@@ -149,14 +100,7 @@ class AttributeMatchingExtension extends AbstractExtension
) : '0');
}
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateDifferent(XPathExpr $xpath, $attribute, $value)
public function translateDifferent(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
{
return $xpath->addCondition(sprintf(
$value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
@@ -168,7 +112,7 @@ class AttributeMatchingExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'attribute-matching';
}

View File

@@ -28,45 +28,27 @@ class CombinationExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getCombinationTranslators()
public function getCombinationTranslators(): array
{
return array(
' ' => array($this, 'translateDescendant'),
'>' => array($this, 'translateChild'),
'+' => array($this, 'translateDirectAdjacent'),
'~' => array($this, 'translateIndirectAdjacent'),
);
return [
' ' => [$this, 'translateDescendant'],
'>' => [$this, 'translateChild'],
'+' => [$this, 'translateDirectAdjacent'],
'~' => [$this, 'translateIndirectAdjacent'],
];
}
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateDescendant(XPathExpr $xpath, XPathExpr $combinedXpath)
public function translateDescendant(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath->join('/descendant-or-self::*/', $combinedXpath);
}
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateChild(XPathExpr $xpath, XPathExpr $combinedXpath)
public function translateChild(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath->join('/', $combinedXpath);
}
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath)
public function translateDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath
->join('/following-sibling::', $combinedXpath)
@@ -74,13 +56,7 @@ class CombinationExtension extends AbstractExtension
->addCondition('position() = 1');
}
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath)
public function translateIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath): XPathExpr
{
return $xpath->join('/following-sibling::', $combinedXpath);
}
@@ -88,7 +64,7 @@ class CombinationExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'combination';
}

View File

@@ -30,40 +30,38 @@ interface ExtensionInterface
*
* @return callable[]
*/
public function getNodeTranslators();
public function getNodeTranslators(): array;
/**
* Returns combination translators.
*
* @return callable[]
*/
public function getCombinationTranslators();
public function getCombinationTranslators(): array;
/**
* Returns function translators.
*
* @return callable[]
*/
public function getFunctionTranslators();
public function getFunctionTranslators(): array;
/**
* Returns pseudo-class translators.
*
* @return callable[]
*/
public function getPseudoClassTranslators();
public function getPseudoClassTranslators(): array;
/**
* Returns attribute operation translators.
*
* @return callable[]
*/
public function getAttributeMatchingTranslators();
public function getAttributeMatchingTranslators(): array;
/**
* Returns extension name.
*
* @return string
*/
public function getName();
public function getName(): string;
}

View File

@@ -33,34 +33,27 @@ class FunctionExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getFunctionTranslators()
public function getFunctionTranslators(): array
{
return array(
'nth-child' => array($this, 'translateNthChild'),
'nth-last-child' => array($this, 'translateNthLastChild'),
'nth-of-type' => array($this, 'translateNthOfType'),
'nth-last-of-type' => array($this, 'translateNthLastOfType'),
'contains' => array($this, 'translateContains'),
'lang' => array($this, 'translateLang'),
);
return [
'nth-child' => [$this, 'translateNthChild'],
'nth-last-child' => [$this, 'translateNthLastChild'],
'nth-of-type' => [$this, 'translateNthOfType'],
'nth-last-of-type' => [$this, 'translateNthLastOfType'],
'contains' => [$this, 'translateContains'],
'lang' => [$this, 'translateLang'],
];
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
* @param bool $last
* @param bool $addNameTest
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateNthChild(XPathExpr $xpath, FunctionNode $function, $last = false, $addNameTest = true)
public function translateNthChild(XPathExpr $xpath, FunctionNode $function, bool $last = false, bool $addNameTest = true): XPathExpr
{
try {
list($a, $b) = Parser::parseSeries($function->getArguments());
[$a, $b] = Parser::parseSeries($function->getArguments());
} catch (SyntaxErrorException $e) {
throw new ExpressionErrorException(sprintf('Invalid series: %s', implode(', ', $function->getArguments())), 0, $e);
throw new ExpressionErrorException(sprintf('Invalid series: "%s".', implode('", "', $function->getArguments())), 0, $e);
}
$xpath->addStarPrefix();
@@ -93,7 +86,7 @@ class FunctionExtension extends AbstractExtension
$expr .= ' - '.$b;
}
$conditions = array(sprintf('%s %s 0', $expr, $sign));
$conditions = [sprintf('%s %s 0', $expr, $sign)];
if (1 !== $a && -1 !== $a) {
$conditions[] = sprintf('(%s) mod %d = 0', $expr, $a);
@@ -110,37 +103,20 @@ class FunctionExtension extends AbstractExtension
// -1n+6 means elements 6 and previous
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*/
public function translateNthLastChild(XPathExpr $xpath, FunctionNode $function)
public function translateNthLastChild(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
return $this->translateNthChild($xpath, $function, true);
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*/
public function translateNthOfType(XPathExpr $xpath, FunctionNode $function)
public function translateNthOfType(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
return $this->translateNthChild($xpath, $function, false, false);
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateNthLastOfType(XPathExpr $xpath, FunctionNode $function)
public function translateNthLastOfType(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:nth-of-type()" is not implemented.');
@@ -150,22 +126,14 @@ class FunctionExtension extends AbstractExtension
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateContains(XPathExpr $xpath, FunctionNode $function)
public function translateContains(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
$arguments = $function->getArguments();
foreach ($arguments as $token) {
if (!($token->isString() || $token->isIdentifier())) {
throw new ExpressionErrorException(
'Expected a single string or identifier for :contains(), got '
.implode(', ', $arguments)
);
throw new ExpressionErrorException('Expected a single string or identifier for :contains(), got '.implode(', ', $arguments));
}
}
@@ -176,22 +144,14 @@ class FunctionExtension extends AbstractExtension
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateLang(XPathExpr $xpath, FunctionNode $function)
public function translateLang(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
$arguments = $function->getArguments();
foreach ($arguments as $token) {
if (!($token->isString() || $token->isIdentifier())) {
throw new ExpressionErrorException(
'Expected a single string or identifier for :lang(), got '
.implode(', ', $arguments)
);
throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got '.implode(', ', $arguments));
}
}
@@ -204,7 +164,7 @@ class FunctionExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'function';
}

View File

@@ -28,11 +28,6 @@ use Symfony\Component\CssSelector\XPath\XPathExpr;
*/
class HtmlExtension extends AbstractExtension
{
/**
* Constructor.
*
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$translator
@@ -44,36 +39,31 @@ class HtmlExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getPseudoClassTranslators()
public function getPseudoClassTranslators(): array
{
return array(
'checked' => array($this, 'translateChecked'),
'link' => array($this, 'translateLink'),
'disabled' => array($this, 'translateDisabled'),
'enabled' => array($this, 'translateEnabled'),
'selected' => array($this, 'translateSelected'),
'invalid' => array($this, 'translateInvalid'),
'hover' => array($this, 'translateHover'),
'visited' => array($this, 'translateVisited'),
);
return [
'checked' => [$this, 'translateChecked'],
'link' => [$this, 'translateLink'],
'disabled' => [$this, 'translateDisabled'],
'enabled' => [$this, 'translateEnabled'],
'selected' => [$this, 'translateSelected'],
'invalid' => [$this, 'translateInvalid'],
'hover' => [$this, 'translateHover'],
'visited' => [$this, 'translateVisited'],
];
}
/**
* {@inheritdoc}
*/
public function getFunctionTranslators()
public function getFunctionTranslators(): array
{
return array(
'lang' => array($this, 'translateLang'),
);
return [
'lang' => [$this, 'translateLang'],
];
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateChecked(XPathExpr $xpath)
public function translateChecked(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition(
'(@checked '
@@ -82,22 +72,12 @@ class HtmlExtension extends AbstractExtension
);
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateLink(XPathExpr $xpath)
public function translateLink(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')");
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateDisabled(XPathExpr $xpath)
public function translateDisabled(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition(
'('
@@ -123,12 +103,7 @@ class HtmlExtension extends AbstractExtension
// todo: in the second half, add "and is not a descendant of that fieldset element's first legend element child, if any."
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateEnabled(XPathExpr $xpath)
public function translateEnabled(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition(
'('
@@ -162,22 +137,14 @@ class HtmlExtension extends AbstractExtension
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateLang(XPathExpr $xpath, FunctionNode $function)
public function translateLang(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
$arguments = $function->getArguments();
foreach ($arguments as $token) {
if (!($token->isString() || $token->isIdentifier())) {
throw new ExpressionErrorException(
'Expected a single string or identifier for :lang(), got '
.implode(', ', $arguments)
);
throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got '.implode(', ', $arguments));
}
}
@@ -190,42 +157,22 @@ class HtmlExtension extends AbstractExtension
));
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateSelected(XPathExpr $xpath)
public function translateSelected(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition("(@selected and name(.) = 'option')");
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateInvalid(XPathExpr $xpath)
public function translateInvalid(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition('0');
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateHover(XPathExpr $xpath)
public function translateHover(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition('0');
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateVisited(XPathExpr $xpath)
public function translateVisited(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition('0');
}
@@ -233,7 +180,7 @@ class HtmlExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'html';
}

View File

@@ -27,32 +27,21 @@ use Symfony\Component\CssSelector\XPath\XPathExpr;
*/
class NodeExtension extends AbstractExtension
{
const ELEMENT_NAME_IN_LOWER_CASE = 1;
const ATTRIBUTE_NAME_IN_LOWER_CASE = 2;
const ATTRIBUTE_VALUE_IN_LOWER_CASE = 4;
public const ELEMENT_NAME_IN_LOWER_CASE = 1;
public const ATTRIBUTE_NAME_IN_LOWER_CASE = 2;
public const ATTRIBUTE_VALUE_IN_LOWER_CASE = 4;
/**
* @var int
*/
private $flags;
/**
* Constructor.
*
* @param int $flags
*/
public function __construct($flags = 0)
public function __construct(int $flags = 0)
{
$this->flags = $flags;
}
/**
* @param int $flag
* @param bool $on
*
* @return $this
*/
public function setFlag($flag, $on)
public function setFlag(int $flag, bool $on): self
{
if ($on && !$this->hasFlag($flag)) {
$this->flags += $flag;
@@ -65,63 +54,40 @@ class NodeExtension extends AbstractExtension
return $this;
}
/**
* @param int $flag
*
* @return bool
*/
public function hasFlag($flag)
public function hasFlag(int $flag): bool
{
return $this->flags & $flag;
return (bool) ($this->flags & $flag);
}
/**
* {@inheritdoc}
*/
public function getNodeTranslators()
public function getNodeTranslators(): array
{
return array(
'Selector' => array($this, 'translateSelector'),
'CombinedSelector' => array($this, 'translateCombinedSelector'),
'Negation' => array($this, 'translateNegation'),
'Function' => array($this, 'translateFunction'),
'Pseudo' => array($this, 'translatePseudo'),
'Attribute' => array($this, 'translateAttribute'),
'Class' => array($this, 'translateClass'),
'Hash' => array($this, 'translateHash'),
'Element' => array($this, 'translateElement'),
);
return [
'Selector' => [$this, 'translateSelector'],
'CombinedSelector' => [$this, 'translateCombinedSelector'],
'Negation' => [$this, 'translateNegation'],
'Function' => [$this, 'translateFunction'],
'Pseudo' => [$this, 'translatePseudo'],
'Attribute' => [$this, 'translateAttribute'],
'Class' => [$this, 'translateClass'],
'Hash' => [$this, 'translateHash'],
'Element' => [$this, 'translateElement'],
];
}
/**
* @param Node\SelectorNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateSelector(Node\SelectorNode $node, Translator $translator)
public function translateSelector(Node\SelectorNode $node, Translator $translator): XPathExpr
{
return $translator->nodeToXPath($node->getTree());
}
/**
* @param Node\CombinedSelectorNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateCombinedSelector(Node\CombinedSelectorNode $node, Translator $translator)
public function translateCombinedSelector(Node\CombinedSelectorNode $node, Translator $translator): XPathExpr
{
return $translator->addCombination($node->getCombinator(), $node->getSelector(), $node->getSubSelector());
}
/**
* @param Node\NegationNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateNegation(Node\NegationNode $node, Translator $translator)
public function translateNegation(Node\NegationNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
$subXpath = $translator->nodeToXPath($node->getSubSelector());
@@ -134,39 +100,21 @@ class NodeExtension extends AbstractExtension
return $xpath->addCondition('0');
}
/**
* @param Node\FunctionNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateFunction(Node\FunctionNode $node, Translator $translator)
public function translateFunction(Node\FunctionNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
return $translator->addFunction($xpath, $node);
}
/**
* @param Node\PseudoNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translatePseudo(Node\PseudoNode $node, Translator $translator)
public function translatePseudo(Node\PseudoNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
return $translator->addPseudoClass($xpath, $node->getIdentifier());
}
/**
* @param Node\AttributeNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateAttribute(Node\AttributeNode $node, Translator $translator)
public function translateAttribute(Node\AttributeNode $node, Translator $translator): XPathExpr
{
$name = $node->getAttribute();
$safe = $this->isSafeName($name);
@@ -191,42 +139,25 @@ class NodeExtension extends AbstractExtension
return $translator->addAttributeMatching($xpath, $node->getOperator(), $attribute, $value);
}
/**
* @param Node\ClassNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateClass(Node\ClassNode $node, Translator $translator)
public function translateClass(Node\ClassNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
return $translator->addAttributeMatching($xpath, '~=', '@class', $node->getName());
}
/**
* @param Node\HashNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateHash(Node\HashNode $node, Translator $translator)
public function translateHash(Node\HashNode $node, Translator $translator): XPathExpr
{
$xpath = $translator->nodeToXPath($node->getSelector());
return $translator->addAttributeMatching($xpath, '=', '@id', $node->getId());
}
/**
* @param Node\ElementNode $node
*
* @return XPathExpr
*/
public function translateElement(Node\ElementNode $node)
public function translateElement(Node\ElementNode $node): XPathExpr
{
$element = $node->getElement();
if ($this->hasFlag(self::ELEMENT_NAME_IN_LOWER_CASE)) {
if ($element && $this->hasFlag(self::ELEMENT_NAME_IN_LOWER_CASE)) {
$element = strtolower($element);
}
@@ -254,19 +185,12 @@ class NodeExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'node';
}
/**
* Tests if given name is safe.
*
* @param string $name
*
* @return bool
*/
private function isSafeName($name)
private function isSafeName(string $name): bool
{
return 0 < preg_match('~^[a-zA-Z_][a-zA-Z0-9_.-]*$~', $name);
}

View File

@@ -29,36 +29,26 @@ class PseudoClassExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getPseudoClassTranslators()
public function getPseudoClassTranslators(): array
{
return array(
'root' => array($this, 'translateRoot'),
'first-child' => array($this, 'translateFirstChild'),
'last-child' => array($this, 'translateLastChild'),
'first-of-type' => array($this, 'translateFirstOfType'),
'last-of-type' => array($this, 'translateLastOfType'),
'only-child' => array($this, 'translateOnlyChild'),
'only-of-type' => array($this, 'translateOnlyOfType'),
'empty' => array($this, 'translateEmpty'),
);
return [
'root' => [$this, 'translateRoot'],
'first-child' => [$this, 'translateFirstChild'],
'last-child' => [$this, 'translateLastChild'],
'first-of-type' => [$this, 'translateFirstOfType'],
'last-of-type' => [$this, 'translateLastOfType'],
'only-child' => [$this, 'translateOnlyChild'],
'only-of-type' => [$this, 'translateOnlyOfType'],
'empty' => [$this, 'translateEmpty'],
];
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateRoot(XPathExpr $xpath)
public function translateRoot(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition('not(parent::*)');
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateFirstChild(XPathExpr $xpath)
public function translateFirstChild(XPathExpr $xpath): XPathExpr
{
return $xpath
->addStarPrefix()
@@ -66,12 +56,7 @@ class PseudoClassExtension extends AbstractExtension
->addCondition('position() = 1');
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateLastChild(XPathExpr $xpath)
public function translateLastChild(XPathExpr $xpath): XPathExpr
{
return $xpath
->addStarPrefix()
@@ -80,13 +65,9 @@ class PseudoClassExtension extends AbstractExtension
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateFirstOfType(XPathExpr $xpath)
public function translateFirstOfType(XPathExpr $xpath): XPathExpr
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
@@ -98,13 +79,9 @@ class PseudoClassExtension extends AbstractExtension
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateLastOfType(XPathExpr $xpath)
public function translateLastOfType(XPathExpr $xpath): XPathExpr
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
@@ -115,12 +92,7 @@ class PseudoClassExtension extends AbstractExtension
->addCondition('position() = last()');
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateOnlyChild(XPathExpr $xpath)
public function translateOnlyChild(XPathExpr $xpath): XPathExpr
{
return $xpath
->addStarPrefix()
@@ -128,28 +100,14 @@ class PseudoClassExtension extends AbstractExtension
->addCondition('last() = 1');
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateOnlyOfType(XPathExpr $xpath)
public function translateOnlyOfType(XPathExpr $xpath): XPathExpr
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
}
$element = $xpath->getElement();
return $xpath->addCondition('last() = 1');
return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
}
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateEmpty(XPathExpr $xpath)
public function translateEmpty(XPathExpr $xpath): XPathExpr
{
return $xpath->addCondition('not(*) and not(string-length())');
}
@@ -157,7 +115,7 @@ class PseudoClassExtension extends AbstractExtension
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'pseudo-class';
}

View File

@@ -30,49 +30,27 @@ use Symfony\Component\CssSelector\Parser\ParserInterface;
*/
class Translator implements TranslatorInterface
{
/**
* @var ParserInterface
*/
private $mainParser;
/**
* @var ParserInterface[]
*/
private $shortcutParsers = array();
private $shortcutParsers = [];
/**
* @var Extension\ExtensionInterface
* @var Extension\ExtensionInterface[]
*/
private $extensions = array();
private $extensions = [];
/**
* @var array
*/
private $nodeTranslators = array();
/**
* @var array
*/
private $combinationTranslators = array();
/**
* @var array
*/
private $functionTranslators = array();
/**
* @var array
*/
private $pseudoClassTranslators = array();
/**
* @var array
*/
private $attributeMatchingTranslators = array();
private $nodeTranslators = [];
private $combinationTranslators = [];
private $functionTranslators = [];
private $pseudoClassTranslators = [];
private $attributeMatchingTranslators = [];
public function __construct(ParserInterface $parser = null)
{
$this->mainParser = $parser ?: new Parser();
$this->mainParser = $parser ?? new Parser();
$this
->registerExtension(new Extension\NodeExtension())
@@ -83,23 +61,18 @@ class Translator implements TranslatorInterface
;
}
/**
* @param string $element
*
* @return string
*/
public static function getXpathLiteral($element)
public static function getXpathLiteral(string $element): string
{
if (false === strpos($element, "'")) {
if (!str_contains($element, "'")) {
return "'".$element."'";
}
if (false === strpos($element, '"')) {
if (!str_contains($element, '"')) {
return '"'.$element.'"';
}
$string = $element;
$parts = array();
$parts = [];
while (true) {
if (false !== $pos = strpos($string, "'")) {
$parts[] = sprintf("'%s'", substr($string, 0, $pos));
@@ -111,13 +84,13 @@ class Translator implements TranslatorInterface
}
}
return sprintf('concat(%s)', implode($parts, ', '));
return sprintf('concat(%s)', implode(', ', $parts));
}
/**
* {@inheritdoc}
*/
public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string
{
$selectors = $this->parseSelectors($cssExpr);
@@ -136,19 +109,15 @@ class Translator implements TranslatorInterface
/**
* {@inheritdoc}
*/
public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::')
public function selectorToXPath(SelectorNode $selector, string $prefix = 'descendant-or-self::'): string
{
return ($prefix ?: '').$this->nodeToXPath($selector);
}
/**
* Registers an extension.
*
* @param Extension\ExtensionInterface $extension
*
* @return $this
*/
public function registerExtension(Extension\ExtensionInterface $extension)
public function registerExtension(Extension\ExtensionInterface $extension): self
{
$this->extensions[$extension->getName()] = $extension;
@@ -162,13 +131,9 @@ class Translator implements TranslatorInterface
}
/**
* @param string $name
*
* @return Extension\ExtensionInterface
*
* @throws ExpressionErrorException
*/
public function getExtension($name)
public function getExtension(string $name): Extension\ExtensionInterface
{
if (!isset($this->extensions[$name])) {
throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
@@ -178,13 +143,9 @@ class Translator implements TranslatorInterface
}
/**
* Registers a shortcut parser.
*
* @param ParserInterface $shortcut
*
* @return $this
*/
public function registerParserShortcut(ParserInterface $shortcut)
public function registerParserShortcut(ParserInterface $shortcut): self
{
$this->shortcutParsers[] = $shortcut;
@@ -192,98 +153,69 @@ class Translator implements TranslatorInterface
}
/**
* @param NodeInterface $node
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function nodeToXPath(NodeInterface $node)
public function nodeToXPath(NodeInterface $node): XPathExpr
{
if (!isset($this->nodeTranslators[$node->getNodeName()])) {
throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
}
return call_user_func($this->nodeTranslators[$node->getNodeName()], $node, $this);
return $this->nodeTranslators[$node->getNodeName()]($node, $this);
}
/**
* @param string $combiner
* @param NodeInterface $xpath
* @param NodeInterface $combinedXpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function addCombination($combiner, NodeInterface $xpath, NodeInterface $combinedXpath)
public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr
{
if (!isset($this->combinationTranslators[$combiner])) {
throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
}
return call_user_func($this->combinationTranslators[$combiner], $this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
}
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function addFunction(XPathExpr $xpath, FunctionNode $function)
public function addFunction(XPathExpr $xpath, FunctionNode $function): XPathExpr
{
if (!isset($this->functionTranslators[$function->getName()])) {
throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
}
return call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
return $this->functionTranslators[$function->getName()]($xpath, $function);
}
/**
* @param XPathExpr $xpath
* @param string $pseudoClass
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function addPseudoClass(XPathExpr $xpath, $pseudoClass)
public function addPseudoClass(XPathExpr $xpath, string $pseudoClass): XPathExpr
{
if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
}
return call_user_func($this->pseudoClassTranslators[$pseudoClass], $xpath);
return $this->pseudoClassTranslators[$pseudoClass]($xpath);
}
/**
* @param XPathExpr $xpath
* @param string $operator
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function addAttributeMatching(XPathExpr $xpath, $operator, $attribute, $value)
public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, $value): XPathExpr
{
if (!isset($this->attributeMatchingTranslators[$operator])) {
throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
}
return call_user_func($this->attributeMatchingTranslators[$operator], $xpath, $attribute, $value);
return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value);
}
/**
* @param string $css
*
* @return SelectorNode[]
*/
private function parseSelectors($css)
private function parseSelectors(string $css): array
{
foreach ($this->shortcutParsers as $shortcut) {
$tokens = $shortcut->parse($css);

View File

@@ -27,21 +27,11 @@ interface TranslatorInterface
{
/**
* Translates a CSS selector to an XPath expression.
*
* @param string $cssExpr
* @param string $prefix
*
* @return string
*/
public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::');
public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string;
/**
* Translates a parsed selector node to an XPath expression.
*
* @param SelectorNode $selector
* @param string $prefix
*
* @return string
*/
public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::');
public function selectorToXPath(SelectorNode $selector, string $prefix = 'descendant-or-self::'): string;
}

View File

@@ -23,28 +23,11 @@ namespace Symfony\Component\CssSelector\XPath;
*/
class XPathExpr
{
/**
* @var string
*/
private $path;
/**
* @var string
*/
private $element;
/**
* @var string
*/
private $condition;
/**
* @param string $path
* @param string $element
* @param string $condition
* @param bool $starPrefix
*/
public function __construct($path = '', $element = '*', $condition = '', $starPrefix = false)
public function __construct(string $path = '', string $element = '*', string $condition = '', bool $starPrefix = false)
{
$this->path = $path;
$this->element = $element;
@@ -55,38 +38,24 @@ class XPathExpr
}
}
/**
* @return string
*/
public function getElement()
public function getElement(): string
{
return $this->element;
}
/**
* @param $condition
*
* @return $this
*/
public function addCondition($condition)
public function addCondition(string $condition): self
{
$this->condition = $this->condition ? sprintf('%s and (%s)', $this->condition, $condition) : $condition;
$this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition;
return $this;
}
/**
* @return string
*/
public function getCondition()
public function getCondition(): string
{
return $this->condition;
}
/**
* @return $this
*/
public function addNameTest()
public function addNameTest(): self
{
if ('*' !== $this->element) {
$this->addCondition('name() = '.Translator::getXpathLiteral($this->element));
@@ -96,10 +65,7 @@ class XPathExpr
return $this;
}
/**
* @return $this
*/
public function addStarPrefix()
public function addStarPrefix(): self
{
$this->path .= '*/';
@@ -109,12 +75,9 @@ class XPathExpr
/**
* Joins another XPathExpr with a combiner.
*
* @param string $combiner
* @param XPathExpr $expr
*
* @return $this
*/
public function join($combiner, XPathExpr $expr)
public function join(string $combiner, self $expr): self
{
$path = $this->__toString().$combiner;
@@ -129,10 +92,7 @@ class XPathExpr
return $this;
}
/**
* @return string
*/
public function __toString()
public function __toString(): string
{
$path = $this->path.$this->element;
$condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']';

View File

@@ -1,7 +1,7 @@
{
"name": "symfony/css-selector",
"type": "library",
"description": "Symfony CssSelector Component",
"description": "Converts CSS selectors to XPath expressions",
"keywords": [],
"homepage": "https://symfony.com",
"license": "MIT",
@@ -20,7 +20,8 @@
}
],
"require": {
"php": ">=5.5.9"
"php": ">=7.1.3",
"symfony/polyfill-php80": "^1.16"
},
"autoload": {
"psr-4": { "Symfony\\Component\\CssSelector\\": "" },
@@ -28,10 +29,5 @@
"/Tests/"
]
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "3.1-dev"
}
}
"minimum-stability": "dev"
}

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
</php>
<testsuites>
<testsuite name="Symfony CssSelector Component Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@@ -1,3 +0,0 @@
vendor/
composer.lock
phpunit.xml

View File

@@ -24,7 +24,7 @@ abstract class AbstractUriElement
protected $node;
/**
* @var string The method to use for the element
* @var string|null The method to use for the element
*/
protected $method;
@@ -35,20 +35,22 @@ abstract class AbstractUriElement
/**
* @param \DOMElement $node A \DOMElement instance
* @param string $currentUri The URI of the page where the link is embedded (or the base href)
* @param string $method The method to use for the link (get by default)
* @param string|null $currentUri The URI of the page where the link is embedded (or the base href)
* @param string|null $method The method to use for the link (GET by default)
*
* @throws \InvalidArgumentException if the node is not a link
*/
public function __construct(\DOMElement $node, $currentUri, $method = 'GET')
public function __construct(\DOMElement $node, string $currentUri = null, ?string $method = 'GET')
{
if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
}
$this->setNode($node);
$this->method = $method ? strtoupper($method) : null;
$this->currentUri = $currentUri;
$elementUriIsRelative = null === parse_url(trim($this->getRawUri()), \PHP_URL_SCHEME);
$baseUriIsAbsolute = null !== $this->currentUri && \in_array(strtolower(substr($this->currentUri, 0, 4)), ['http', 'file']);
if ($elementUriIsRelative && !$baseUriIsAbsolute) {
throw new \InvalidArgumentException(sprintf('The URL of the element is relative, so you must define its base URI passing an absolute URL to the constructor of the "%s" class ("%s" was passed).', __CLASS__, $this->currentUri));
}
}
/**
@@ -68,7 +70,7 @@ abstract class AbstractUriElement
*/
public function getMethod()
{
return $this->method;
return $this->method ?? 'GET';
}
/**
@@ -81,7 +83,7 @@ abstract class AbstractUriElement
$uri = trim($this->getRawUri());
// absolute URL?
if (null !== parse_url($uri, PHP_URL_SCHEME)) {
if (null !== parse_url($uri, \PHP_URL_SCHEME)) {
return $uri;
}
@@ -102,7 +104,7 @@ abstract class AbstractUriElement
}
// absolute URL with relative schema
if (0 === strpos($uri, '//')) {
if (str_starts_with($uri, '//')) {
return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri;
}
@@ -114,7 +116,7 @@ abstract class AbstractUriElement
}
// relative path
$path = parse_url(substr($this->currentUri, strlen($baseUri)), PHP_URL_PATH);
$path = parse_url(substr($this->currentUri, \strlen($baseUri)), \PHP_URL_PATH);
$path = $this->canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path;
@@ -140,11 +142,11 @@ abstract class AbstractUriElement
return $path;
}
if ('.' === substr($path, -1)) {
if (str_ends_with($path, '.')) {
$path .= '/';
}
$output = array();
$output = [];
foreach (explode('/', $path) as $segment) {
if ('..' === $segment) {
@@ -168,24 +170,16 @@ abstract class AbstractUriElement
/**
* Removes the query string and the anchor from the given uri.
*
* @param string $uri The uri to clean
*
* @return string
*/
private function cleanupUri($uri)
private function cleanupUri(string $uri): string
{
return $this->cleanupQuery($this->cleanupAnchor($uri));
}
/**
* Remove the query string from the uri.
*
* @param string $uri
*
* @return string
*/
private function cleanupQuery($uri)
private function cleanupQuery(string $uri): string
{
if (false !== $pos = strpos($uri, '?')) {
return substr($uri, 0, $pos);
@@ -196,12 +190,8 @@ abstract class AbstractUriElement
/**
* Remove the anchor from the uri.
*
* @param string $uri
*
* @return string
*/
private function cleanupAnchor($uri)
private function cleanupAnchor(string $uri): string
{
if (false !== $pos = strpos($uri, '#')) {
return substr($uri, 0, $pos);

View File

@@ -1,22 +1,49 @@
CHANGELOG
=========
4.4.0
-----
* Added `Form::getName()` method.
* Added `Crawler::matches()` method.
* Added `Crawler::closest()` method.
* Added `Crawler::outerHtml()` method.
* Added an argument to the `Crawler::text()` method to opt-in normalizing whitespaces.
4.3.0
-----
* Added PHPUnit constraints: `CrawlerSelectorAttributeValueSame`, `CrawlerSelectorExists`, `CrawlerSelectorTextContains`
and `CrawlerSelectorTextSame`
* Added return of element name (`_name`) in `extract()` method.
* Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty.
* When available, the [html5-php library](https://github.com/Masterminds/html5-php) is used to
parse HTML added to a Crawler for better support of HTML5 tags.
4.2.0
-----
* The `$currentUri` constructor argument of the `AbstractUriElement`, `Link` and
`Image` classes is now optional.
* The `Crawler::children()` method will have a new `$selector` argument in version 5.0,
not defining it is deprecated.
3.1.0
-----
* All the URI parsing logic have been abstracted in the `AbstractUriElement` class.
The `Link` class is now a child of `AbstractUriElement`.
* Added an `Image` class to crawl images and parse their `src` attribute,
and `selectImage`, `image`, `images` methods in the `Crawler` (the image version of the equivalent `link` methods).
* All the URI parsing logic have been abstracted in the `AbstractUriElement` class.
The `Link` class is now a child of `AbstractUriElement`.
* Added an `Image` class to crawl images and parse their `src` attribute,
and `selectImage`, `image`, `images` methods in the `Crawler` (the image version of the equivalent `link` methods).
2.5.0
-----
* [BC BREAK] The default value for checkbox and radio inputs without a value attribute have changed
from '1' to 'on' to match the HTML specification.
* [BC BREAK] The typehints on the `Link`, `Form` and `FormField` classes have been changed from
`\DOMNode` to `DOMElement`. Using any other type of `DOMNode` was triggering fatal errors in previous
versions. Code extending these classes will need to update the typehints when overwriting these methods.
* [BC BREAK] The default value for checkbox and radio inputs without a value attribute have changed
from '1' to 'on' to match the HTML specification.
* [BC BREAK] The typehints on the `Link`, `Form` and `FormField` classes have been changed from
`\DOMNode` to `DOMElement`. Using any other type of `DOMNode` was triggering fatal errors in previous
versions. Code extending these classes will need to update the typehints when overwriting these methods.
2.4.0
-----

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,7 @@ namespace Symfony\Component\DomCrawler\Field;
/**
* ChoiceFormField represents a choice form field.
*
* It is constructed from a HTML select tag, or a HTML checkbox, or radio inputs.
* It is constructed from an HTML select tag, or an HTML checkbox, or radio inputs.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
@@ -45,7 +45,7 @@ class ChoiceFormField extends FormField
public function hasValue()
{
// don't send a value for unchecked checkboxes
if (in_array($this->type, array('checkbox', 'radio')) && null === $this->value) {
if (\in_array($this->type, ['checkbox', 'radio']) && null === $this->value) {
return false;
}
@@ -75,7 +75,7 @@ class ChoiceFormField extends FormField
/**
* Sets the value of the field.
*
* @param string $value The value of the field
* @param string|array $value The value of the field
*/
public function select($value)
{
@@ -97,14 +97,14 @@ class ChoiceFormField extends FormField
}
/**
* Ticks a checkbox.
* Unticks a checkbox.
*
* @throws \LogicException When the type provided is not correct
*/
public function untick()
{
if ('checkbox' !== $this->type) {
throw new \LogicException(sprintf('You cannot tick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
throw new \LogicException(sprintf('You cannot untick "%s" as it is not a checkbox (%s).', $this->name, $this->type));
}
$this->setValue(false);
@@ -113,7 +113,7 @@ class ChoiceFormField extends FormField
/**
* Sets the value of the field.
*
* @param string $value The value of the field
* @param string|array|bool $value The value of the field
*
* @throws \InvalidArgumentException When value type provided is not correct
*/
@@ -126,25 +126,25 @@ class ChoiceFormField extends FormField
// check
$this->value = $this->options[0]['value'];
} else {
if (is_array($value)) {
if (\is_array($value)) {
if (!$this->multiple) {
throw new \InvalidArgumentException(sprintf('The value for "%s" cannot be an array.', $this->name));
}
foreach ($value as $v) {
if (!$this->containsOption($v, $this->options)) {
throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $v, implode(', ', $this->availableOptionValues())));
throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: "%s").', $this->name, $v, implode('", "', $this->availableOptionValues())));
}
}
} elseif (!$this->containsOption($value, $this->options)) {
throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: %s).', $this->name, $value, implode(', ', $this->availableOptionValues())));
throw new \InvalidArgumentException(sprintf('Input "%s" cannot take "%s" as a value (possible values: "%s").', $this->name, $value, implode('", "', $this->availableOptionValues())));
}
if ($this->multiple) {
$value = (array) $value;
}
if (is_array($value)) {
if (\is_array($value)) {
$this->value = $value;
} else {
parent::setValue($value);
@@ -155,8 +155,6 @@ class ChoiceFormField extends FormField
/**
* Adds a choice to the current ones.
*
* @param \DOMElement $node
*
* @throws \LogicException When choice provided is not multiple nor radio
*
* @internal
@@ -207,11 +205,11 @@ class ChoiceFormField extends FormField
}
if ('input' === $this->node->nodeName && 'checkbox' !== strtolower($this->node->getAttribute('type')) && 'radio' !== strtolower($this->node->getAttribute('type'))) {
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $this->node->getAttribute('type')));
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is "%s").', $this->node->getAttribute('type')));
}
$this->value = null;
$this->options = array();
$this->options = [];
$this->multiple = false;
if ('input' == $this->node->nodeName) {
@@ -226,7 +224,7 @@ class ChoiceFormField extends FormField
$this->type = 'select';
if ($this->node->hasAttribute('multiple')) {
$this->multiple = true;
$this->value = array();
$this->value = [];
$this->name = str_replace('[]', '', $this->name);
}
@@ -254,14 +252,10 @@ class ChoiceFormField extends FormField
/**
* Returns option value with associated disabled flag.
*
* @param \DOMElement $node
*
* @return array
*/
private function buildOptionValue(\DOMElement $node)
private function buildOptionValue(\DOMElement $node): array
{
$option = array();
$option = [];
$defaultDefaultValue = 'select' === $this->node->nodeName ? '' : 'on';
$defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : $defaultDefaultValue;
@@ -301,7 +295,7 @@ class ChoiceFormField extends FormField
*/
public function availableOptionValues()
{
$values = array();
$values = [];
foreach ($this->options as $option) {
$values[] = $option['value'];

View File

@@ -27,12 +27,12 @@ class FileFormField extends FormField
*/
public function setErrorCode($error)
{
$codes = array(UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_NO_FILE, UPLOAD_ERR_NO_TMP_DIR, UPLOAD_ERR_CANT_WRITE, UPLOAD_ERR_EXTENSION);
if (!in_array($error, $codes)) {
throw new \InvalidArgumentException(sprintf('The error code %s is not valid.', $error));
$codes = [\UPLOAD_ERR_INI_SIZE, \UPLOAD_ERR_FORM_SIZE, \UPLOAD_ERR_PARTIAL, \UPLOAD_ERR_NO_FILE, \UPLOAD_ERR_NO_TMP_DIR, \UPLOAD_ERR_CANT_WRITE, \UPLOAD_ERR_EXTENSION];
if (!\in_array($error, $codes)) {
throw new \InvalidArgumentException(sprintf('The error code "%s" is not valid.', $error));
}
$this->value = array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0);
$this->value = ['name' => '', 'type' => '', 'tmp_name' => '', 'error' => $error, 'size' => 0];
}
/**
@@ -48,19 +48,19 @@ class FileFormField extends FormField
/**
* Sets the value of the field.
*
* @param string $value The value of the field
* @param string|null $value The value of the field
*/
public function setValue($value)
{
if (null !== $value && is_readable($value)) {
$error = UPLOAD_ERR_OK;
$error = \UPLOAD_ERR_OK;
$size = filesize($value);
$info = pathinfo($value);
$name = $info['basename'];
// copy to a tmp location
$tmp = sys_get_temp_dir().'/'.sha1(uniqid(mt_rand(), true));
if (array_key_exists('extension', $info)) {
$tmp = sys_get_temp_dir().'/'.strtr(substr(base64_encode(hash('sha256', uniqid(mt_rand(), true), true)), 0, 7), '/', '_');
if (\array_key_exists('extension', $info)) {
$tmp .= '.'.$info['extension'];
}
if (is_file($tmp)) {
@@ -69,13 +69,13 @@ class FileFormField extends FormField
copy($value, $tmp);
$value = $tmp;
} else {
$error = UPLOAD_ERR_NO_FILE;
$error = \UPLOAD_ERR_NO_FILE;
$size = 0;
$name = '';
$value = '';
}
$this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
$this->value = ['name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size];
}
/**
@@ -100,7 +100,7 @@ class FileFormField extends FormField
}
if ('file' !== strtolower($this->node->getAttribute('type'))) {
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $this->node->getAttribute('type')));
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is "%s").', $this->node->getAttribute('type')));
}
$this->setValue(null);

View File

@@ -44,8 +44,6 @@ abstract class FormField
protected $disabled;
/**
* Constructor.
*
* @param \DOMElement $node The node associated with this field
*/
public function __construct(\DOMElement $node)
@@ -57,6 +55,27 @@ abstract class FormField
$this->initialize();
}
/**
* Returns the label tag associated to the field or null if none.
*
* @return \DOMElement|null
*/
public function getLabel()
{
$xpath = new \DOMXPath($this->node->ownerDocument);
if ($this->node->hasAttribute('id')) {
$labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id')));
if ($labels->length > 0) {
return $labels->item(0);
}
}
$labels = $xpath->query('ancestor::label[1]', $this->node);
return $labels->length > 0 ? $labels->item(0) : null;
}
/**
* Returns the name of the field.
*
@@ -80,7 +99,7 @@ abstract class FormField
/**
* Sets the value of the field.
*
* @param string $value The value of the field
* @param string|array|bool|null $value The value of the field
*/
public function setValue($value)
{

View File

@@ -32,11 +32,12 @@ class InputFormField extends FormField
throw new \LogicException(sprintf('An InputFormField can only be created from an input or button tag (%s given).', $this->node->nodeName));
}
if ('checkbox' === strtolower($this->node->getAttribute('type'))) {
$type = strtolower($this->node->getAttribute('type'));
if ('checkbox' === $type) {
throw new \LogicException('Checkboxes should be instances of ChoiceFormField.');
}
if ('file' === strtolower($this->node->getAttribute('type'))) {
if ('file' === $type) {
throw new \LogicException('File inputs should be instances of FileFormField.');
}

View File

@@ -37,16 +37,14 @@ class Form extends Link implements \ArrayAccess
private $baseHref;
/**
* Constructor.
*
* @param \DOMElement $node A \DOMElement instance
* @param string $currentUri The URI of the page where the form is embedded
* @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
* @param string $baseHref The URI of the <base> used for relative links, but not for empty action
* @param string|null $currentUri The URI of the page where the form is embedded
* @param string|null $method The method to use for the link (if null, it defaults to the method defined by the form)
* @param string|null $baseHref The URI of the <base> used for relative links, but not for empty action
*
* @throws \LogicException if the node is not a button inside a form tag
*/
public function __construct(\DOMElement $node, $currentUri, $method = null, $baseHref = null)
public function __construct(\DOMElement $node, string $currentUri = null, string $method = null, string $baseHref = null)
{
parent::__construct($node, $currentUri, $method);
$this->baseHref = $baseHref;
@@ -89,7 +87,7 @@ class Form extends Link implements \ArrayAccess
*/
public function getValues()
{
$values = array();
$values = [];
foreach ($this->fields->all() as $name => $field) {
if ($field->isDisabled()) {
continue;
@@ -110,11 +108,11 @@ class Form extends Link implements \ArrayAccess
*/
public function getFiles()
{
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
return array();
if (!\in_array($this->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
return [];
}
$files = array();
$files = [];
foreach ($this->fields->all() as $name => $field) {
if ($field->isDisabled()) {
@@ -139,13 +137,13 @@ class Form extends Link implements \ArrayAccess
*/
public function getPhpValues()
{
$values = array();
$values = [];
foreach ($this->getValues() as $name => $value) {
$qs = http_build_query(array($name => $value), '', '&');
$qs = http_build_query([$name => $value], '', '&');
if (!empty($qs)) {
parse_str($qs, $expandedValue);
$varName = substr($name, 0, strlen(key($expandedValue)));
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
$varName = substr($name, 0, \strlen(key($expandedValue)));
$values = array_replace_recursive($values, [$varName => current($expandedValue)]);
}
}
@@ -166,13 +164,25 @@ class Form extends Link implements \ArrayAccess
*/
public function getPhpFiles()
{
$values = array();
$values = [];
foreach ($this->getFiles() as $name => $value) {
$qs = http_build_query(array($name => $value), '', '&');
$qs = http_build_query([$name => $value], '', '&');
if (!empty($qs)) {
parse_str($qs, $expandedValue);
$varName = substr($name, 0, strlen(key($expandedValue)));
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
$varName = substr($name, 0, \strlen(key($expandedValue)));
array_walk_recursive(
$expandedValue,
function (&$value, $key) {
if (ctype_digit($value) && ('size' === $key || 'error' === $key)) {
$value = (int) $value;
}
}
);
reset($expandedValue);
$values = array_replace_recursive($values, [$varName => current($expandedValue)]);
}
}
@@ -192,14 +202,14 @@ class Form extends Link implements \ArrayAccess
{
$uri = parent::getUri();
if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) {
$query = parse_url($uri, PHP_URL_QUERY);
$currentParameters = array();
if (!\in_array($this->getMethod(), ['POST', 'PUT', 'DELETE', 'PATCH'])) {
$query = parse_url($uri, \PHP_URL_QUERY);
$currentParameters = [];
if ($query) {
parse_str($query, $currentParameters);
}
$queryString = http_build_query(array_merge($currentParameters, $this->getValues()), null, '&');
$queryString = http_build_query(array_merge($currentParameters, $this->getValues()), '', '&');
$pos = strpos($uri, '?');
$base = false === $pos ? $uri : substr($uri, 0, $pos);
@@ -211,6 +221,11 @@ class Form extends Link implements \ArrayAccess
protected function getRawUri()
{
// If the form was created from a button rather than the form node, check for HTML5 action overrides
if ($this->button !== $this->node && $this->button->getAttribute('formaction')) {
return $this->button->getAttribute('formaction');
}
return $this->node->getAttribute('action');
}
@@ -227,9 +242,24 @@ class Form extends Link implements \ArrayAccess
return $this->method;
}
// If the form was created from a button rather than the form node, check for HTML5 method override
if ($this->button !== $this->node && $this->button->getAttribute('formmethod')) {
return strtoupper($this->button->getAttribute('formmethod'));
}
return $this->node->getAttribute('method') ? strtoupper($this->node->getAttribute('method')) : 'GET';
}
/**
* Gets the form name.
*
* If no name is defined on the form, an empty string is returned.
*/
public function getName(): string
{
return $this->node->getAttribute('name');
}
/**
* Returns true if the named field exists.
*
@@ -257,7 +287,7 @@ class Form extends Link implements \ArrayAccess
*
* @param string $name The field name
*
* @return FormField The field instance
* @return FormField|FormField[]|FormField[][] The value of the field
*
* @throws \InvalidArgumentException When field is not present in this form
*/
@@ -268,8 +298,6 @@ class Form extends Link implements \ArrayAccess
/**
* Sets a named field.
*
* @param FormField $field The field
*/
public function set(FormField $field)
{
@@ -293,6 +321,7 @@ class Form extends Link implements \ArrayAccess
*
* @return bool true if the field exists, false otherwise
*/
#[\ReturnTypeWillChange]
public function offsetExists($name)
{
return $this->has($name);
@@ -303,10 +332,11 @@ class Form extends Link implements \ArrayAccess
*
* @param string $name The field name
*
* @return FormField The associated Field instance
* @return FormField|FormField[]|FormField[][] The value of the field
*
* @throws \InvalidArgumentException if the field does not exist
*/
#[\ReturnTypeWillChange]
public function offsetGet($name)
{
return $this->fields->get($name);
@@ -318,8 +348,11 @@ class Form extends Link implements \ArrayAccess
* @param string $name The field name
* @param string|array $value The value of the field
*
* @return void
*
* @throws \InvalidArgumentException if the field does not exist
*/
#[\ReturnTypeWillChange]
public function offsetSet($name, $value)
{
$this->fields->set($name, $value);
@@ -329,7 +362,10 @@ class Form extends Link implements \ArrayAccess
* Removes a field from the form.
*
* @param string $name The field name
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($name)
{
$this->fields->remove($name);
@@ -356,14 +392,12 @@ class Form extends Link implements \ArrayAccess
*
* Expects a 'submit' button \DOMElement and finds the corresponding form element, or the form element itself.
*
* @param \DOMElement $node A \DOMElement instance
*
* @throws \LogicException If given node is not a button or input or does not have a form ancestor
*/
protected function setNode(\DOMElement $node)
{
$this->button = $node;
if ('button' === $node->nodeName || ('input' === $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
if ('button' === $node->nodeName || ('input' === $node->nodeName && \in_array(strtolower($node->getAttribute('type')), ['submit', 'button', 'image']))) {
if ($node->hasAttribute('form')) {
// if the node has the HTML5-compliant 'form' attribute, use it
$formId = $node->getAttribute('form');
@@ -427,14 +461,14 @@ class Form extends Link implements \ArrayAccess
// corresponding elements are either descendants or have a matching HTML5 form attribute
$formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
$fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s] | //form[@id=%s]//input[not(@form)] | //form[@id=%s]//button[not(@form)] | //form[@id=%s]//textarea[not(@form)] | //form[@id=%s]//select[not(@form)]', $formId, $formId, $formId, $formId, $formId, $formId, $formId, $formId));
$fieldNodes = $xpath->query(sprintf('( descendant::input[@form=%s] | descendant::button[@form=%1$s] | descendant::textarea[@form=%1$s] | descendant::select[@form=%1$s] | //form[@id=%1$s]//input[not(@form)] | //form[@id=%1$s]//button[not(@form)] | //form[@id=%1$s]//textarea[not(@form)] | //form[@id=%1$s]//select[not(@form)] )[not(ancestor::template)]', $formId));
foreach ($fieldNodes as $node) {
$this->addField($node);
}
} else {
// do the xpath query with $this->node as the context node, to only find descendant elements
// however, descendant elements with form attribute are not part of this form
$fieldNodes = $xpath->query('descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)]', $this->node);
$fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[not(ancestor::template)]', $this->node);
foreach ($fieldNodes as $node) {
$this->addField($node);
}
@@ -464,7 +498,7 @@ class Form extends Link implements \ArrayAccess
}
} elseif ('input' == $nodeName && 'file' == strtolower($node->getAttribute('type'))) {
$this->set(new Field\FileFormField($node));
} elseif ('input' == $nodeName && !in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image'))) {
} elseif ('input' == $nodeName && !\in_array(strtolower($node->getAttribute('type')), ['submit', 'button', 'image'])) {
$this->set(new Field\InputFormField($node));
} elseif ('textarea' == $nodeName) {
$this->set(new Field\TextareaFormField($node));

View File

@@ -20,14 +20,12 @@ use Symfony\Component\DomCrawler\Field\FormField;
*/
class FormFieldRegistry
{
private $fields = array();
private $fields = [];
private $base;
private $base = '';
/**
* Adds a field to the registry.
*
* @param FormField $field The field
*/
public function add(FormField $field)
{
@@ -35,8 +33,8 @@ class FormFieldRegistry
$target = &$this->fields;
while ($segments) {
if (!is_array($target)) {
$target = array();
if (!\is_array($target)) {
$target = [];
}
$path = array_shift($segments);
if ('' === $path) {
@@ -49,17 +47,15 @@ class FormFieldRegistry
}
/**
* Removes a field and its children from the registry.
*
* @param string $name The fully qualified name of the base field
* Removes a field based on the fully qualifed name and its children from the registry.
*/
public function remove($name)
public function remove(string $name)
{
$segments = $this->getSegments($name);
$target = &$this->fields;
while (count($segments) > 1) {
while (\count($segments) > 1) {
$path = array_shift($segments);
if (!array_key_exists($path, $target)) {
if (!\is_array($target) || !\array_key_exists($path, $target)) {
return;
}
$target = &$target[$path];
@@ -68,22 +64,20 @@ class FormFieldRegistry
}
/**
* Returns the value of the field and its children.
* Returns the value of the field based on the fully qualifed name and its children.
*
* @param string $name The fully qualified name of the field
*
* @return mixed The value of the field
* @return FormField|FormField[]|FormField[][] The value of the field
*
* @throws \InvalidArgumentException if the field does not exist
*/
public function &get($name)
public function &get(string $name)
{
$segments = $this->getSegments($name);
$target = &$this->fields;
while ($segments) {
$path = array_shift($segments);
if (!array_key_exists($path, $target)) {
throw new \InvalidArgumentException(sprintf('Unreachable field "%s"', $path));
if (!\is_array($target) || !\array_key_exists($path, $target)) {
throw new \InvalidArgumentException(sprintf('Unreachable field "%s".', $path));
}
$target = &$target[$path];
}
@@ -92,13 +86,11 @@ class FormFieldRegistry
}
/**
* Tests whether the form has the given field.
*
* @param string $name The fully qualified name of the field
* Tests whether the form has the given field based on the fully qualified name.
*
* @return bool Whether the form has the given field
*/
public function has($name)
public function has(string $name): bool
{
try {
$this->get($name);
@@ -110,21 +102,22 @@ class FormFieldRegistry
}
/**
* Set the value of a field and its children.
* Set the value of a field based on the fully qualified name and its children.
*
* @param string $name The fully qualified name of the field
* @param mixed $value The value
* @param mixed $value The value
*
* @throws \InvalidArgumentException if the field does not exist
*/
public function set($name, $value)
public function set(string $name, $value)
{
$target = &$this->get($name);
if ((!is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) {
if ((!\is_array($value) && $target instanceof Field\FormField) || $target instanceof Field\ChoiceFormField) {
$target->setValue($value);
} elseif (is_array($value)) {
$fields = self::create($name, $value);
foreach ($fields->all() as $k => $v) {
} elseif (\is_array($value)) {
$registry = new static();
$registry->base = $name;
$registry->fields = $value;
foreach ($registry->all() as $k => $v) {
$this->set($k, $v);
}
} else {
@@ -135,47 +128,21 @@ class FormFieldRegistry
/**
* Returns the list of field with their value.
*
* @return FormField[] The list of fields as array((string) Fully qualified name => (mixed) value)
* @return FormField[] The list of fields as [string] Fully qualified name => (mixed) value)
*/
public function all()
public function all(): array
{
return $this->walk($this->fields, $this->base);
}
/**
* Creates an instance of the class.
*
* This function is made private because it allows overriding the $base and
* the $values properties without any type checking.
*
* @param string $base The fully qualified name of the base field
* @param array $values The values of the fields
*
* @return static
*/
private static function create($base, array $values)
{
$registry = new static();
$registry->base = $base;
$registry->fields = $values;
return $registry;
}
/**
* Transforms a PHP array in a list of fully qualified name / value.
*
* @param array $array The PHP array
* @param string $base The name of the base field
* @param array $output The initial values
*
* @return array The list of fields as array((string) Fully qualified name => (mixed) value)
*/
private function walk(array $array, $base = '', array &$output = array())
private function walk(array $array, ?string $base = '', array &$output = []): array
{
foreach ($array as $k => $v) {
$path = empty($base) ? $k : sprintf('%s[%s]', $base, $k);
if (is_array($v)) {
if (\is_array($v)) {
$this->walk($v, $path, $output);
} else {
$output[$path] = $v;
@@ -188,18 +155,14 @@ class FormFieldRegistry
/**
* Splits a field name into segments as a web browser would do.
*
* <code>
* getSegments('base[foo][3][]') = array('base', 'foo, '3', '');
* </code>
*
* @param string $name The name of the field
* getSegments('base[foo][3][]') = ['base', 'foo, '3', ''];
*
* @return string[] The list of segments
*/
private function getSegments($name)
private function getSegments(string $name): array
{
if (preg_match('/^(?P<base>[^[]+)(?P<extra>(\[.*)|$)/', $name, $m)) {
$segments = array($m['base']);
$segments = [$m['base']];
while (!empty($m['extra'])) {
$extra = $m['extra'];
if (preg_match('/^\[(?P<segment>.*?)\](?P<extra>.*)$/', $extra, $m)) {
@@ -212,6 +175,6 @@ class FormFieldRegistry
return $segments;
}
return array($name);
return [$name];
}
}

View File

@@ -16,7 +16,7 @@ namespace Symfony\Component\DomCrawler;
*/
class Image extends AbstractUriElement
{
public function __construct(\DOMElement $node, $currentUri)
public function __construct(\DOMElement $node, string $currentUri = null)
{
parent::__construct($node, $currentUri, 'GET');
}

View File

@@ -1,4 +1,4 @@
Copyright (c) 2004-2017 Fabien Potencier
Copyright (c) 2004-2022 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -6,8 +6,8 @@ The DomCrawler component eases DOM navigation for HTML and XML documents.
Resources
---------
* [Documentation](https://symfony.com/doc/current/components/dom_crawler.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
* [Documentation](https://symfony.com/doc/current/components/dom_crawler.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DomCrawler\Crawler;
final class CrawlerSelectorAttributeValueSame extends Constraint
{
private $selector;
private $attribute;
private $expectedText;
public function __construct(string $selector, string $attribute, string $expectedText)
{
$this->selector = $selector;
$this->attribute = $attribute;
$this->expectedText = $expectedText;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('has a node matching selector "%s" with attribute "%s" of value "%s"', $this->selector, $this->attribute, $this->expectedText);
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function matches($crawler): bool
{
$crawler = $crawler->filter($this->selector);
if (!\count($crawler)) {
return false;
}
return $this->expectedText === trim($crawler->attr($this->attribute) ?? '');
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function failureDescription($crawler): string
{
return 'the Crawler '.$this->toString();
}
}

View File

@@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DomCrawler\Crawler;
final class CrawlerSelectorExists extends Constraint
{
private $selector;
public function __construct(string $selector)
{
$this->selector = $selector;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('matches selector "%s"', $this->selector);
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function matches($crawler): bool
{
return 0 < \count($crawler->filter($this->selector));
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function failureDescription($crawler): string
{
return 'the Crawler '.$this->toString();
}
}

View File

@@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DomCrawler\Crawler;
final class CrawlerSelectorTextContains extends Constraint
{
private $selector;
private $expectedText;
private $hasNode = false;
private $nodeText;
public function __construct(string $selector, string $expectedText)
{
$this->selector = $selector;
$this->expectedText = $expectedText;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
if ($this->hasNode) {
return sprintf('the text "%s" of the node matching selector "%s" contains "%s"', $this->nodeText, $this->selector, $this->expectedText);
}
return sprintf('the Crawler has a node matching selector "%s"', $this->selector);
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function matches($crawler): bool
{
$crawler = $crawler->filter($this->selector);
if (!\count($crawler)) {
$this->hasNode = false;
return false;
}
$this->hasNode = true;
$this->nodeText = $crawler->text(null, true);
return false !== mb_strpos($this->nodeText, $this->expectedText);
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function failureDescription($crawler): string
{
return $this->toString();
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler\Test\Constraint;
use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\DomCrawler\Crawler;
final class CrawlerSelectorTextSame extends Constraint
{
private $selector;
private $expectedText;
public function __construct(string $selector, string $expectedText)
{
$this->selector = $selector;
$this->expectedText = $expectedText;
}
/**
* {@inheritdoc}
*/
public function toString(): string
{
return sprintf('has a node matching selector "%s" with content "%s"', $this->selector, $this->expectedText);
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function matches($crawler): bool
{
$crawler = $crawler->filter($this->selector);
if (!\count($crawler)) {
return false;
}
return $this->expectedText === trim($crawler->text(null, true));
}
/**
* @param Crawler $crawler
*
* {@inheritdoc}
*/
protected function failureDescription($crawler): string
{
return 'the Crawler '.$this->toString();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,404 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler\Tests\Field;
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
class ChoiceFormFieldTest extends FormFieldTestCase
{
public function testInitialize()
{
$node = $this->createNode('textarea', '');
try {
$field = new ChoiceFormField($node);
$this->fail('->initialize() throws a \LogicException if the node is not an input or a select');
} catch (\LogicException $e) {
$this->assertTrue(true, '->initialize() throws a \LogicException if the node is not an input or a select');
}
$node = $this->createNode('input', '', array('type' => 'text'));
try {
$field = new ChoiceFormField($node);
$this->fail('->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
} catch (\LogicException $e) {
$this->assertTrue(true, '->initialize() throws a \LogicException if the node is an input with a type different from checkbox or radio');
}
}
public function testGetType()
{
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$this->assertEquals('radio', $field->getType(), '->getType() returns radio for radio buttons');
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$this->assertEquals('checkbox', $field->getType(), '->getType() returns radio for a checkbox');
$node = $this->createNode('select', '');
$field = new ChoiceFormField($node);
$this->assertEquals('select', $field->getType(), '->getType() returns radio for a select');
}
public function testIsMultiple()
{
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$this->assertFalse($field->isMultiple(), '->isMultiple() returns false for checkboxes');
$node = $this->createNode('select', '');
$field = new ChoiceFormField($node);
$this->assertFalse($field->isMultiple(), '->isMultiple() returns false for selects without the multiple attribute');
$node = $this->createNode('select', '', array('multiple' => 'multiple'));
$field = new ChoiceFormField($node);
$this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with the multiple attribute');
$node = $this->createNode('select', '', array('multiple' => ''));
$field = new ChoiceFormField($node);
$this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with an empty multiple attribute');
}
public function testSelects()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => false));
$field = new ChoiceFormField($node);
$this->assertTrue($field->hasValue(), '->hasValue() returns true for selects');
$this->assertEquals('foo', $field->getValue(), '->getValue() returns the first option if none are selected');
$this->assertFalse($field->isMultiple(), '->isMultiple() returns false when no multiple attribute is defined');
$node = $this->createSelectNode(array('foo' => false, 'bar' => true));
$field = new ChoiceFormField($node);
$this->assertEquals('bar', $field->getValue(), '->getValue() returns the selected option');
$field->setValue('foo');
$this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected option');
try {
$field->setValue('foobar');
$this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the selected options');
}
try {
$field->setValue(array('foobar'));
$this->fail('->setValue() throws an \InvalidArgumentException if the value is an array');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is an array');
}
}
public function testSelectWithEmptyBooleanAttribute()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => true), array(), '');
$field = new ChoiceFormField($node);
$this->assertEquals('bar', $field->getValue());
}
public function testSelectIsDisabled()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => true), array('disabled' => 'disabled'));
$field = new ChoiceFormField($node);
$this->assertTrue($field->isDisabled(), '->isDisabled() returns true for selects with a disabled attribute');
}
public function testMultipleSelects()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
$field = new ChoiceFormField($node);
$this->assertEquals(array(), $field->getValue(), '->setValue() returns an empty array if multiple is true and no option is selected');
$field->setValue('foo');
$this->assertEquals(array('foo'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
$field->setValue('bar');
$this->assertEquals(array('bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
$field->setValue(array('foo', 'bar'));
$this->assertEquals(array('foo', 'bar'), $field->getValue(), '->setValue() returns an array of options if multiple is true');
$node = $this->createSelectNode(array('foo' => true, 'bar' => true), array('multiple' => 'multiple'));
$field = new ChoiceFormField($node);
$this->assertEquals(array('foo', 'bar'), $field->getValue(), '->getValue() returns the selected options');
try {
$field->setValue(array('foobar'));
$this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the options');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the options');
}
}
public function testRadioButtons()
{
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
$field->addChoice($node);
$this->assertFalse($field->hasValue(), '->hasValue() returns false when no radio button is selected');
$this->assertNull($field->getValue(), '->getValue() returns null if no radio button is selected');
$this->assertFalse($field->isMultiple(), '->isMultiple() returns false for radio buttons');
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => 'checked'));
$field->addChoice($node);
$this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
$this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
$field->setValue('foo');
$this->assertEquals('foo', $field->getValue(), '->setValue() changes the selected radio button');
try {
$field->setValue('foobar');
$this->fail('->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one of the radio button values');
}
}
public function testRadioButtonsWithEmptyBooleanAttribute()
{
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => ''));
$field->addChoice($node);
$this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
$this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
}
public function testRadioButtonIsDisabled()
{
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo', 'disabled' => 'disabled'));
$field = new ChoiceFormField($node);
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
$field->addChoice($node);
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'baz', 'disabled' => ''));
$field->addChoice($node);
$field->select('foo');
$this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
$this->assertTrue($field->isDisabled());
$field->select('bar');
$this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
$this->assertFalse($field->isDisabled());
$field->select('baz');
$this->assertEquals('baz', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
$this->assertTrue($field->isDisabled());
}
public function testCheckboxes()
{
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
$field = new ChoiceFormField($node);
$this->assertFalse($field->hasValue(), '->hasValue() returns false when the checkbox is not checked');
$this->assertNull($field->getValue(), '->getValue() returns null if the checkbox is not checked');
$this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
try {
$field->addChoice(new \DOMElement('input'));
$this->fail('->addChoice() throws a \LogicException for checkboxes');
} catch (\LogicException $e) {
$this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes');
}
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
$field = new ChoiceFormField($node);
$this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
$this->assertEquals('on', $field->getValue(), '->getValue() returns 1 if the checkbox is checked and has no value attribute');
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute if the checkbox is checked');
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked', 'value' => 'foo'));
$field = new ChoiceFormField($node);
$field->setValue(false);
$this->assertNull($field->getValue(), '->setValue() unchecks the checkbox is value is false');
$field->setValue(true);
$this->assertEquals('foo', $field->getValue(), '->setValue() checks the checkbox is value is true');
try {
$field->setValue('bar');
$this->fail('->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is not one from the value attribute');
}
}
public function testCheckboxWithEmptyBooleanAttribute()
{
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo', 'checked' => ''));
$field = new ChoiceFormField($node);
$this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
$this->assertEquals('foo', $field->getValue());
}
public function testTick()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => false));
$field = new ChoiceFormField($node);
try {
$field->tick();
$this->fail('->tick() throws a \LogicException for select boxes');
} catch (\LogicException $e) {
$this->assertTrue(true, '->tick() throws a \LogicException for select boxes');
}
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name'));
$field = new ChoiceFormField($node);
$field->tick();
$this->assertEquals('on', $field->getValue(), '->tick() ticks checkboxes');
}
public function testUntick()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => false));
$field = new ChoiceFormField($node);
try {
$field->untick();
$this->fail('->untick() throws a \LogicException for select boxes');
} catch (\LogicException $e) {
$this->assertTrue(true, '->untick() throws a \LogicException for select boxes');
}
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
$field = new ChoiceFormField($node);
$field->untick();
$this->assertNull($field->getValue(), '->untick() unticks checkboxes');
}
public function testSelect()
{
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'checked' => 'checked'));
$field = new ChoiceFormField($node);
$field->select(true);
$this->assertEquals('on', $field->getValue(), '->select() changes the value of the field');
$field->select(false);
$this->assertNull($field->getValue(), '->select() changes the value of the field');
$node = $this->createSelectNode(array('foo' => false, 'bar' => false));
$field = new ChoiceFormField($node);
$field->select('foo');
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
}
public function testOptionWithNoValue()
{
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false));
$field = new ChoiceFormField($node);
$this->assertEquals('foo', $field->getValue());
$node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => true));
$field = new ChoiceFormField($node);
$this->assertEquals('bar', $field->getValue());
$field->select('foo');
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
}
public function testDisableValidation()
{
$node = $this->createSelectNode(array('foo' => false, 'bar' => false));
$field = new ChoiceFormField($node);
$field->disableValidation();
$field->setValue('foobar');
$this->assertEquals('foobar', $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
$node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
$field = new ChoiceFormField($node);
$field->disableValidation();
$field->setValue(array('foobar'));
$this->assertEquals(array('foobar'), $field->getValue(), '->disableValidation() allows to set a value which is not in the selected options.');
}
public function testSelectWithEmptyValue()
{
$node = $this->createSelectNodeWithEmptyOption(array('' => true, 'Female' => false, 'Male' => false));
$field = new ChoiceFormField($node);
$this->assertSame('', $field->getValue());
}
protected function createSelectNode($options, $attributes = array(), $selectedAttrText = 'selected')
{
$document = new \DOMDocument();
$node = $document->createElement('select');
foreach ($attributes as $name => $value) {
$node->setAttribute($name, $value);
}
$node->setAttribute('name', 'name');
foreach ($options as $value => $selected) {
$option = $document->createElement('option', $value);
$option->setAttribute('value', $value);
if ($selected) {
$option->setAttribute('selected', $selectedAttrText);
}
$node->appendChild($option);
}
return $node;
}
protected function createSelectNodeWithEmptyOption($options, $attributes = array())
{
$document = new \DOMDocument();
$node = $document->createElement('select');
foreach ($attributes as $name => $value) {
$node->setAttribute($name, $value);
}
$node->setAttribute('name', 'name');
foreach ($options as $value => $selected) {
$option = $document->createElement('option', $value);
if ($selected) {
$option->setAttribute('selected', 'selected');
}
$node->appendChild($option);
}
return $node;
}
}

Some files were not shown because too many files have changed in this diff Show More