scratch

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 86  →  ?path2? @ 87
/vendor/symfony/css-selector/XPath/Extension/AbstractExtension.php
@@ -0,0 +1,65 @@
<?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\XPath\Extension;
 
/**
* XPath expression translator abstract extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
abstract class AbstractExtension implements ExtensionInterface
{
/**
* {@inheritdoc}
*/
public function getNodeTranslators()
{
return array();
}
 
/**
* {@inheritdoc}
*/
public function getCombinationTranslators()
{
return array();
}
 
/**
* {@inheritdoc}
*/
public function getFunctionTranslators()
{
return array();
}
 
/**
* {@inheritdoc}
*/
public function getPseudoClassTranslators()
{
return array();
}
 
/**
* {@inheritdoc}
*/
public function getAttributeMatchingTranslators()
{
return array();
}
}
/vendor/symfony/css-selector/XPath/Extension/AttributeMatchingExtension.php
@@ -0,0 +1,175 @@
<?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\XPath\Extension;
 
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\CssSelector\XPath\XPathExpr;
 
/**
* XPath expression translator attribute extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
class AttributeMatchingExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getAttributeMatchingTranslators()
{
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'),
);
}
 
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateExists(XPathExpr $xpath, $attribute, $value)
{
return $xpath->addCondition($attribute);
}
 
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateEquals(XPathExpr $xpath, $attribute, $value)
{
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)
{
return $xpath->addCondition($value ? sprintf(
'%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
$attribute,
Translator::getXpathLiteral(' '.$value.' ')
) : '0');
}
 
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateDashMatch(XPathExpr $xpath, $attribute, $value)
{
return $xpath->addCondition(sprintf(
'%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
$attribute,
Translator::getXpathLiteral($value),
Translator::getXpathLiteral($value.'-')
));
}
 
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translatePrefixMatch(XPathExpr $xpath, $attribute, $value)
{
return $xpath->addCondition($value ? sprintf(
'%1$s and starts-with(%1$s, %2$s)',
$attribute,
Translator::getXpathLiteral($value)
) : '0');
}
 
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateSuffixMatch(XPathExpr $xpath, $attribute, $value)
{
return $xpath->addCondition($value ? sprintf(
'%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
$attribute,
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)
{
return $xpath->addCondition($value ? sprintf(
'%1$s and contains(%1$s, %2$s)',
$attribute,
Translator::getXpathLiteral($value)
) : '0');
}
 
/**
* @param XPathExpr $xpath
* @param string $attribute
* @param string $value
*
* @return XPathExpr
*/
public function translateDifferent(XPathExpr $xpath, $attribute, $value)
{
return $xpath->addCondition(sprintf(
$value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
$attribute,
Translator::getXpathLiteral($value)
));
}
 
/**
* {@inheritdoc}
*/
public function getName()
{
return 'attribute-matching';
}
}
/vendor/symfony/css-selector/XPath/Extension/CombinationExtension.php
@@ -0,0 +1,95 @@
<?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\XPath\Extension;
 
use Symfony\Component\CssSelector\XPath\XPathExpr;
 
/**
* XPath expression translator combination extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
class CombinationExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getCombinationTranslators()
{
return array(
' ' => array($this, 'translateDescendant'),
'>' => array($this, 'translateChild'),
'+' => array($this, 'translateDirectAdjacent'),
'~' => array($this, 'translateIndirectAdjacent'),
);
}
 
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateDescendant(XPathExpr $xpath, XPathExpr $combinedXpath)
{
return $xpath->join('/descendant-or-self::*/', $combinedXpath);
}
 
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateChild(XPathExpr $xpath, XPathExpr $combinedXpath)
{
return $xpath->join('/', $combinedXpath);
}
 
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateDirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath)
{
return $xpath
->join('/following-sibling::', $combinedXpath)
->addNameTest()
->addCondition('position() = 1');
}
 
/**
* @param XPathExpr $xpath
* @param XPathExpr $combinedXpath
*
* @return XPathExpr
*/
public function translateIndirectAdjacent(XPathExpr $xpath, XPathExpr $combinedXpath)
{
return $xpath->join('/following-sibling::', $combinedXpath);
}
 
/**
* {@inheritdoc}
*/
public function getName()
{
return 'combination';
}
}
/vendor/symfony/css-selector/XPath/Extension/ExtensionInterface.php
@@ -0,0 +1,69 @@
<?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\XPath\Extension;
 
/**
* XPath expression translator extension interface.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
interface ExtensionInterface
{
/**
* Returns node translators.
*
* These callables will receive the node as first argument and the translator as second argument.
*
* @return callable[]
*/
public function getNodeTranslators();
 
/**
* Returns combination translators.
*
* @return callable[]
*/
public function getCombinationTranslators();
 
/**
* Returns function translators.
*
* @return callable[]
*/
public function getFunctionTranslators();
 
/**
* Returns pseudo-class translators.
*
* @return callable[]
*/
public function getPseudoClassTranslators();
 
/**
* Returns attribute operation translators.
*
* @return callable[]
*/
public function getAttributeMatchingTranslators();
 
/**
* Returns extension name.
*
* @return string
*/
public function getName();
}
/vendor/symfony/css-selector/XPath/Extension/FunctionExtension.php
@@ -0,0 +1,211 @@
<?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\XPath\Extension;
 
use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\Parser\Parser;
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\CssSelector\XPath\XPathExpr;
 
/**
* XPath expression translator function extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
class FunctionExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getFunctionTranslators()
{
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'),
);
}
 
/**
* @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)
{
try {
list($a, $b) = Parser::parseSeries($function->getArguments());
} catch (SyntaxErrorException $e) {
throw new ExpressionErrorException(sprintf('Invalid series: %s', implode(', ', $function->getArguments())), 0, $e);
}
 
$xpath->addStarPrefix();
if ($addNameTest) {
$xpath->addNameTest();
}
 
if (0 === $a) {
return $xpath->addCondition('position() = '.($last ? 'last() - '.($b - 1) : $b));
}
 
if ($a < 0) {
if ($b < 1) {
return $xpath->addCondition('false()');
}
 
$sign = '<=';
} else {
$sign = '>=';
}
 
$expr = 'position()';
 
if ($last) {
$expr = 'last() - '.$expr;
--$b;
}
 
if (0 !== $b) {
$expr .= ' - '.$b;
}
 
$conditions = array(sprintf('%s %s 0', $expr, $sign));
 
if (1 !== $a && -1 !== $a) {
$conditions[] = sprintf('(%s) mod %d = 0', $expr, $a);
}
 
return $xpath->addCondition(implode(' and ', $conditions));
 
// todo: handle an+b, odd, even
// an+b means every-a, plus b, e.g., 2n+1 means odd
// 0n+b means b
// n+0 means a=1, i.e., all elements
// an means every a elements, i.e., 2n means even
// -n means -1n
// -1n+6 means elements 6 and previous
}
 
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*/
public function translateNthLastChild(XPathExpr $xpath, FunctionNode $function)
{
return $this->translateNthChild($xpath, $function, true);
}
 
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*/
public function translateNthOfType(XPathExpr $xpath, FunctionNode $function)
{
return $this->translateNthChild($xpath, $function, false, false);
}
 
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateNthLastOfType(XPathExpr $xpath, FunctionNode $function)
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:nth-of-type()" is not implemented.');
}
 
return $this->translateNthChild($xpath, $function, true, false);
}
 
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateContains(XPathExpr $xpath, FunctionNode $function)
{
$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)
);
}
}
 
return $xpath->addCondition(sprintf(
'contains(string(.), %s)',
Translator::getXpathLiteral($arguments[0]->getValue())
));
}
 
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateLang(XPathExpr $xpath, FunctionNode $function)
{
$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)
);
}
}
 
return $xpath->addCondition(sprintf(
'lang(%s)',
Translator::getXpathLiteral($arguments[0]->getValue())
));
}
 
/**
* {@inheritdoc}
*/
public function getName()
{
return 'function';
}
}
/vendor/symfony/css-selector/XPath/Extension/HtmlExtension.php
@@ -0,0 +1,240 @@
<?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\XPath\Extension;
 
use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
use Symfony\Component\CssSelector\Node\FunctionNode;
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\CssSelector\XPath\XPathExpr;
 
/**
* XPath expression translator HTML extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
class HtmlExtension extends AbstractExtension
{
/**
* Constructor.
*
* @param Translator $translator
*/
public function __construct(Translator $translator)
{
$translator
->getExtension('node')
->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true)
->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true);
}
 
/**
* {@inheritdoc}
*/
public function getPseudoClassTranslators()
{
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'),
);
}
 
/**
* {@inheritdoc}
*/
public function getFunctionTranslators()
{
return array(
'lang' => array($this, 'translateLang'),
);
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateChecked(XPathExpr $xpath)
{
return $xpath->addCondition(
'(@checked '
."and (name(.) = 'input' or name(.) = 'command')"
."and (@type = 'checkbox' or @type = 'radio'))"
);
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateLink(XPathExpr $xpath)
{
return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')");
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateDisabled(XPathExpr $xpath)
{
return $xpath->addCondition(
'('
.'@disabled and'
.'('
."(name(.) = 'input' and @type != 'hidden')"
." or name(.) = 'button'"
." or name(.) = 'select'"
." or name(.) = 'textarea'"
." or name(.) = 'command'"
." or name(.) = 'fieldset'"
." or name(.) = 'optgroup'"
." or name(.) = 'option'"
.')'
.') or ('
."(name(.) = 'input' and @type != 'hidden')"
." or name(.) = 'button'"
." or name(.) = 'select'"
." or name(.) = 'textarea'"
.')'
.' and ancestor::fieldset[@disabled]'
);
// 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)
{
return $xpath->addCondition(
'('
.'@href and ('
."name(.) = 'a'"
." or name(.) = 'link'"
." or name(.) = 'area'"
.')'
.') or ('
.'('
."name(.) = 'command'"
." or name(.) = 'fieldset'"
." or name(.) = 'optgroup'"
.')'
.' and not(@disabled)'
.') or ('
.'('
."(name(.) = 'input' and @type != 'hidden')"
." or name(.) = 'button'"
." or name(.) = 'select'"
." or name(.) = 'textarea'"
." or name(.) = 'keygen'"
.')'
.' and not (@disabled or ancestor::fieldset[@disabled])'
.') or ('
."name(.) = 'option' and not("
.'@disabled or ancestor::optgroup[@disabled]'
.')'
.')'
);
}
 
/**
* @param XPathExpr $xpath
* @param FunctionNode $function
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateLang(XPathExpr $xpath, FunctionNode $function)
{
$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)
);
}
}
 
return $xpath->addCondition(sprintf(
'ancestor-or-self::*[@lang][1][starts-with(concat('
."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')"
.', %s)]',
'lang',
Translator::getXpathLiteral(strtolower($arguments[0]->getValue()).'-')
));
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateSelected(XPathExpr $xpath)
{
return $xpath->addCondition("(@selected and name(.) = 'option')");
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateInvalid(XPathExpr $xpath)
{
return $xpath->addCondition('0');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateHover(XPathExpr $xpath)
{
return $xpath->addCondition('0');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateVisited(XPathExpr $xpath)
{
return $xpath->addCondition('0');
}
 
/**
* {@inheritdoc}
*/
public function getName()
{
return 'html';
}
}
/vendor/symfony/css-selector/XPath/Extension/NodeExtension.php
@@ -0,0 +1,273 @@
<?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\XPath\Extension;
 
use Symfony\Component\CssSelector\Node;
use Symfony\Component\CssSelector\XPath\Translator;
use Symfony\Component\CssSelector\XPath\XPathExpr;
 
/**
* XPath expression translator node extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
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;
 
/**
* @var int
*/
private $flags;
 
/**
* Constructor.
*
* @param int $flags
*/
public function __construct($flags = 0)
{
$this->flags = $flags;
}
 
/**
* @param int $flag
* @param bool $on
*
* @return $this
*/
public function setFlag($flag, $on)
{
if ($on && !$this->hasFlag($flag)) {
$this->flags += $flag;
}
 
if (!$on && $this->hasFlag($flag)) {
$this->flags -= $flag;
}
 
return $this;
}
 
/**
* @param int $flag
*
* @return bool
*/
public function hasFlag($flag)
{
return $this->flags & $flag;
}
 
/**
* {@inheritdoc}
*/
public function getNodeTranslators()
{
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'),
);
}
 
/**
* @param Node\SelectorNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateSelector(Node\SelectorNode $node, Translator $translator)
{
return $translator->nodeToXPath($node->getTree());
}
 
/**
* @param Node\CombinedSelectorNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateCombinedSelector(Node\CombinedSelectorNode $node, Translator $translator)
{
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)
{
$xpath = $translator->nodeToXPath($node->getSelector());
$subXpath = $translator->nodeToXPath($node->getSubSelector());
$subXpath->addNameTest();
 
if ($subXpath->getCondition()) {
return $xpath->addCondition(sprintf('not(%s)', $subXpath->getCondition()));
}
 
return $xpath->addCondition('0');
}
 
/**
* @param Node\FunctionNode $node
* @param Translator $translator
*
* @return XPathExpr
*/
public function translateFunction(Node\FunctionNode $node, Translator $translator)
{
$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)
{
$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)
{
$name = $node->getAttribute();
$safe = $this->isSafeName($name);
 
if ($this->hasFlag(self::ATTRIBUTE_NAME_IN_LOWER_CASE)) {
$name = strtolower($name);
}
 
if ($node->getNamespace()) {
$name = sprintf('%s:%s', $node->getNamespace(), $name);
$safe = $safe && $this->isSafeName($node->getNamespace());
}
 
$attribute = $safe ? '@'.$name : sprintf('attribute::*[name() = %s]', Translator::getXpathLiteral($name));
$value = $node->getValue();
$xpath = $translator->nodeToXPath($node->getSelector());
 
if ($this->hasFlag(self::ATTRIBUTE_VALUE_IN_LOWER_CASE)) {
$value = strtolower($value);
}
 
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)
{
$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)
{
$xpath = $translator->nodeToXPath($node->getSelector());
 
return $translator->addAttributeMatching($xpath, '=', '@id', $node->getId());
}
 
/**
* @param Node\ElementNode $node
*
* @return XPathExpr
*/
public function translateElement(Node\ElementNode $node)
{
$element = $node->getElement();
 
if ($this->hasFlag(self::ELEMENT_NAME_IN_LOWER_CASE)) {
$element = strtolower($element);
}
 
if ($element) {
$safe = $this->isSafeName($element);
} else {
$element = '*';
$safe = true;
}
 
if ($node->getNamespace()) {
$element = sprintf('%s:%s', $node->getNamespace(), $element);
$safe = $safe && $this->isSafeName($node->getNamespace());
}
 
$xpath = new XPathExpr('', $element);
 
if (!$safe) {
$xpath->addNameTest();
}
 
return $xpath;
}
 
/**
* {@inheritdoc}
*/
public function getName()
{
return 'node';
}
 
/**
* Tests if given name is safe.
*
* @param string $name
*
* @return bool
*/
private function isSafeName($name)
{
return 0 < preg_match('~^[a-zA-Z_][a-zA-Z0-9_.-]*$~', $name);
}
}
/vendor/symfony/css-selector/XPath/Extension/PseudoClassExtension.php
@@ -0,0 +1,164 @@
<?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\XPath\Extension;
 
use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
use Symfony\Component\CssSelector\XPath\XPathExpr;
 
/**
* XPath expression translator pseudo-class extension.
*
* This component is a port of the Python cssselect library,
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
*
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
*
* @internal
*/
class PseudoClassExtension extends AbstractExtension
{
/**
* {@inheritdoc}
*/
public function getPseudoClassTranslators()
{
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'),
);
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateRoot(XPathExpr $xpath)
{
return $xpath->addCondition('not(parent::*)');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateFirstChild(XPathExpr $xpath)
{
return $xpath
->addStarPrefix()
->addNameTest()
->addCondition('position() = 1');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateLastChild(XPathExpr $xpath)
{
return $xpath
->addStarPrefix()
->addNameTest()
->addCondition('position() = last()');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateFirstOfType(XPathExpr $xpath)
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
}
 
return $xpath
->addStarPrefix()
->addCondition('position() = 1');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateLastOfType(XPathExpr $xpath)
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
}
 
return $xpath
->addStarPrefix()
->addCondition('position() = last()');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateOnlyChild(XPathExpr $xpath)
{
return $xpath
->addStarPrefix()
->addNameTest()
->addCondition('last() = 1');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*
* @throws ExpressionErrorException
*/
public function translateOnlyOfType(XPathExpr $xpath)
{
if ('*' === $xpath->getElement()) {
throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
}
 
return $xpath->addCondition('last() = 1');
}
 
/**
* @param XPathExpr $xpath
*
* @return XPathExpr
*/
public function translateEmpty(XPathExpr $xpath)
{
return $xpath->addCondition('not(*) and not(string-length())');
}
 
/**
* {@inheritdoc}
*/
public function getName()
{
return 'pseudo-class';
}
}