This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Components/CssSelector/XPathExpr.php

186 lines
3.6 KiB
PHP
Raw Normal View History

2010-03-31 07:42:18 +01:00
<?php
namespace Symfony\Components\CssSelector;
/*
* This file is part of the symfony package.
*
* (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.
*/
/**
* 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.
*
* @package symfony
* @subpackage css_selector
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class XPathExpr
{
protected $prefix;
protected $path;
protected $element;
protected $condition;
protected $starPrefix;
public function __construct($prefix = null, $path = null, $element = '*', $condition = null, $starPrefix = false)
{
$this->prefix = $prefix;
$this->path = $path;
$this->element = $element;
$this->condition = $condition;
$this->starPrefix = $starPrefix;
}
public function getElement()
{
return $this->element;
}
public function getCondition()
{
return $this->condition;
}
public function __toString()
{
$path = '';
if (null !== $this->prefix)
{
$path .= $this->prefix;
}
if (null !== $this->path)
{
$path .= $this->path;
}
$path .= $this->element;
if ($this->condition)
{
$path .= sprintf('[%s]', $this->condition);
}
return $path;
}
public function addCondition($condition)
{
if ($this->condition)
{
$this->condition = sprintf('%s and (%s)', $this->condition, $condition);
}
else
{
$this->condition = $condition;
}
}
public function addPrefix($prefix)
{
if ($this->prefix)
{
$this->prefix = $prefix.$this->prefix;
}
else
{
$this->prefix = $prefix;
}
}
public function addNameTest()
{
if ($this->element == '*')
{
# We weren't doing a test anyway
return;
}
$this->addCondition(sprintf("name() = %s", XPathExpr::xpathLiteral($this->element)));
$this->element = '*';
}
public function addStarPrefix()
{
/*
Adds a /* prefix if there is no prefix. This is when you need
to keep context's constrained to a single parent.
*/
if ($this->path)
{
$this->path .= '*/';
}
else
{
$this->path = '*/';
}
$this->starPrefix = true;
}
public function join($combiner, $other)
{
$prefix = (string) $this;
$prefix .= $combiner;
$path = $other->prefix.$other->path;
# We don't need a star prefix if we are joining to this other
# prefix; so we'll get rid of it
if ($other->starPrefix && $path == '*/')
{
$path = '';
}
$this->prefix = $prefix;
$this->path = $path;
$this->element = $other->element;
$this->condition = $other->condition;
}
static public function xpathLiteral($s)
{
if ($s instanceof Node\ElementNode)
{
# This is probably a symbol that looks like an expression...
$s = $s->formatElement();
}
else
{
$s = (string) $s;
}
if (false === strpos($s, "'"))
{
$s = sprintf("'%s'", $s);
}
elseif (false === strpos($s, '"'))
{
$s = sprintf('"%s"', $s);
}
else
{
$tmp = array();
foreach (preg_split("#('+)#", $s) as $part)
{
if (!$part)
{
continue;
}
$tmp[] = sprintf(false !== strpos($part, "'") ? '"%s"' : "'%s'", $part);
}
$s = sprintf("concat(%s)", implode($tmp, ','));
}
return $s;
}
}