2010-03-31 07:42:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-03-31 07:42:18 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\CssSelector;
|
|
|
|
|
2010-03-31 07:42:18 +01:00
|
|
|
/**
|
|
|
|
* XPathExpr represents an XPath expression.
|
|
|
|
*
|
|
|
|
* This component is a port of the Python lxml library,
|
|
|
|
* which is copyright Infrae and distributed under the BSD license.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-03-31 07:42:18 +01:00
|
|
|
*/
|
|
|
|
class XPathExpr
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $prefix;
|
|
|
|
protected $path;
|
|
|
|
protected $element;
|
|
|
|
protected $condition;
|
|
|
|
protected $starPrefix;
|
2010-03-31 07:42:18 +01:00
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $prefix Prefix for the XPath expression.
|
|
|
|
* @param string $path Actual path of the expression.
|
|
|
|
* @param string $element The element in the expression.
|
|
|
|
* @param string $condition A condition for the expression.
|
|
|
|
* @param bool $starPrefix Indicates whether to use a star prefix.
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function __construct($prefix = null, $path = null, $element = '*', $condition = null, $starPrefix = false)
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->prefix = $prefix;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->element = $element;
|
|
|
|
$this->condition = $condition;
|
|
|
|
$this->starPrefix = $starPrefix;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Get the prefix of this XPath expression.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getPrefix()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->prefix;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Get the path of this XPath expression.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getPath()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->path;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Answer whether this XPath expression has a star prefix.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function hasStarPrefix()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->starPrefix;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Get the element of this XPath expression.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getElement()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->element;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Get the condition of this XPath expression.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function getCondition()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->condition;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Get a string representation for this XPath expression.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function __toString()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$path = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $this->prefix) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$path .= $this->prefix;
|
|
|
|
}
|
2010-03-31 07:42:18 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $this->path) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$path .= $this->path;
|
|
|
|
}
|
2010-03-31 07:42:18 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$path .= $this->element;
|
2010-03-31 07:42:18 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->condition) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$path .= sprintf('[%s]', $this->condition);
|
|
|
|
}
|
2010-03-31 07:42:18 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $path;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Add a condition to this XPath expression.
|
|
|
|
* Any pre-existant condition will be ANDed to it.
|
|
|
|
*
|
|
|
|
* @param string $condition The condition to add.
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function addCondition($condition)
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->condition) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->condition = sprintf('%s and (%s)', $this->condition, $condition);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->condition = $condition;
|
|
|
|
}
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Add a prefix to this XPath expression.
|
|
|
|
* It will be prepended to any pre-existant prefixes.
|
|
|
|
*
|
|
|
|
* @param string $prefix The prefix to add.
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function addPrefix($prefix)
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->prefix) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->prefix = $prefix.$this->prefix;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->prefix = $prefix;
|
|
|
|
}
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Add a condition to this XPath expression using the name of the element
|
|
|
|
* as the desired value.
|
|
|
|
* This method resets the element to '*'.
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function addNameTest()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->element == '*') {
|
2010-05-06 12:25:53 +01:00
|
|
|
// We weren't doing a test anyway
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addCondition(sprintf('name() = %s', XPathExpr::xpathLiteral($this->element)));
|
|
|
|
$this->element = '*';
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
2010-04-15 10:53:23 +01:00
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Add a star prefix to this XPath expression.
|
|
|
|
* This method will prepend a '*' to the path and set the star prefix flag
|
|
|
|
* to true.
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function addStarPrefix()
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/*
|
|
|
|
Adds a /* prefix if there is no prefix. This is when you need
|
|
|
|
to keep context's constrained to a single parent.
|
|
|
|
*/
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->path) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->path .= '*/';
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->path = '*/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->starPrefix = true;
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
2010-04-15 10:53:23 +01:00
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Join this XPath expression with $other (another XPath expression) using
|
|
|
|
* $combiner to join them.
|
|
|
|
*
|
|
|
|
* @param string $combiner The combiner string.
|
|
|
|
* @param XPathExpr $other The other XPath expression to combine with
|
|
|
|
* this one.
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
public function join($combiner, $other)
|
2010-03-31 07:42:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$prefix = (string) $this;
|
|
|
|
|
|
|
|
$prefix .= $combiner;
|
|
|
|
$path = $other->getPrefix().$other->getPath();
|
|
|
|
|
|
|
|
/* We don't need a star prefix if we are joining to this other
|
|
|
|
prefix; so we'll get rid of it */
|
2010-12-21 03:06:56 +00:00
|
|
|
if ($other->hasStarPrefix() && '*/' == $path) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$path = '';
|
|
|
|
}
|
|
|
|
$this->prefix = $prefix;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->element = $other->getElement();
|
|
|
|
$this->condition = $other->GetCondition();
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|
|
|
|
|
2011-02-05 22:08:01 +00:00
|
|
|
/**
|
|
|
|
* Get an XPath literal for $s.
|
|
|
|
*
|
|
|
|
* @param mixed $s Can either be a Node\ElementNode or a string.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
static public function xpathLiteral($s)
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($s instanceof Node\ElementNode) {
|
2010-05-06 12:25:53 +01:00
|
|
|
// This is probably a symbol that looks like an expression...
|
|
|
|
$s = $s->formatElement();
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$s = (string) $s;
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false === strpos($s, "'")) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf("'%s'", $s);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false === strpos($s, '"')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return sprintf('"%s"', $s);
|
|
|
|
}
|
|
|
|
|
|
|
|
$string = $s;
|
|
|
|
$parts = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
while (true) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (false !== $pos = strpos($string, "'")) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$parts[] = sprintf("'%s'", substr($string, 0, $pos));
|
|
|
|
$parts[] = "\"'\"";
|
|
|
|
$string = substr($string, $pos + 1);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$parts[] = "'$string'";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('concat(%s)', implode($parts, ', '));
|
|
|
|
}
|
2010-03-31 07:42:18 +01:00
|
|
|
}
|