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/Component/CssSelector/XPathExpr.php

179 lines
4.2 KiB
PHP
Raw Normal View History

2010-03-31 07:42:18 +01:00
<?php
namespace Symfony\Component\CssSelector;
2010-03-31 07:42:18 +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.
*/
/**
* 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.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2010-03-31 07:42:18 +01:00
*/
class XPathExpr
{
protected $prefix;
protected $path;
protected $element;
protected $condition;
protected $starPrefix;
2010-03-31 07:42:18 +01:00
public function __construct($prefix = null, $path = null, $element = '*', $condition = null, $starPrefix = false)
2010-03-31 07:42:18 +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
}
public function getPrefix()
2010-03-31 07:42:18 +01:00
{
return $this->prefix;
2010-03-31 07:42:18 +01:00
}
public function getPath()
2010-03-31 07:42:18 +01:00
{
return $this->path;
2010-03-31 07:42:18 +01:00
}
public function hasStarPrefix()
2010-03-31 07:42:18 +01:00
{
return $this->starPrefix;
2010-03-31 07:42:18 +01:00
}
public function getElement()
2010-03-31 07:42:18 +01:00
{
return $this->element;
2010-03-31 07:42:18 +01:00
}
public function getCondition()
2010-03-31 07:42:18 +01:00
{
return $this->condition;
2010-03-31 07:42:18 +01:00
}
public function __toString()
2010-03-31 07:42:18 +01:00
{
$path = '';
if (null !== $this->prefix) {
$path .= $this->prefix;
}
2010-03-31 07:42:18 +01:00
if (null !== $this->path) {
$path .= $this->path;
}
2010-03-31 07:42:18 +01:00
$path .= $this->element;
2010-03-31 07:42:18 +01:00
if ($this->condition) {
$path .= sprintf('[%s]', $this->condition);
}
2010-03-31 07:42:18 +01:00
return $path;
2010-03-31 07:42:18 +01:00
}
public function addCondition($condition)
2010-03-31 07:42:18 +01:00
{
if ($this->condition) {
$this->condition = sprintf('%s and (%s)', $this->condition, $condition);
} else {
$this->condition = $condition;
}
2010-03-31 07:42:18 +01:00
}
public function addPrefix($prefix)
2010-03-31 07:42:18 +01:00
{
if ($this->prefix) {
$this->prefix = $prefix.$this->prefix;
} else {
$this->prefix = $prefix;
}
2010-03-31 07:42:18 +01:00
}
public function addNameTest()
2010-03-31 07:42:18 +01:00
{
if ($this->element == '*') {
// 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
}
public function addStarPrefix()
2010-03-31 07:42:18 +01:00
{
/*
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;
2010-03-31 07:42:18 +01:00
}
public function join($combiner, $other)
2010-03-31 07:42:18 +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 */
if ($other->hasStarPrefix() && $path == '*/') {
$path = '';
}
$this->prefix = $prefix;
$this->path = $path;
$this->element = $other->getElement();
$this->condition = $other->GetCondition();
2010-03-31 07:42:18 +01:00
}
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, "'")) {
return sprintf("'%s'", $s);
}
if (false === strpos($s, '"')) {
return sprintf('"%s"', $s);
}
$string = $s;
$parts = array();
while (true) {
if (false !== $pos = strpos($string, "'")) {
$parts[] = sprintf("'%s'", substr($string, 0, $pos));
$parts[] = "\"'\"";
$string = substr($string, $pos + 1);
} else {
$parts[] = "'$string'";
break;
}
}
return sprintf('concat(%s)', implode($parts, ', '));
}
2010-03-31 07:42:18 +01:00
}