laravel-6 support
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user