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

204 lines
3.9 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.
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.
*
* @package Symfony
* @subpackage Components_CssSelector
2010-03-31 07:42:18 +01:00
* @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;
}
2010-04-08 11:09:14 +01:00
public function getPrefix()
{
return $this->prefix;
}
public function getPath()
{
return $this->path;
}
public function hasStarPrefix()
{
return $this->starPrefix;
}
2010-03-31 07:42:18 +01:00
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
2010-03-31 07:42:18 +01:00
return;
}
2010-04-24 00:22:16 +01:00
$this->addCondition(sprintf('name() = %s', XPathExpr::xpathLiteral($this->element)));
2010-03-31 07:42:18 +01:00
$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;
2010-04-08 11:09:14 +01:00
$path = $other->getPrefix().$other->getPath();
2010-03-31 07:42:18 +01:00
/* We don't need a star prefix if we are joining to this other
prefix; so we'll get rid of it */
2010-04-08 11:09:14 +01:00
if ($other->hasStarPrefix() && $path == '*/')
2010-03-31 07:42:18 +01:00
{
$path = '';
}
$this->prefix = $prefix;
$this->path = $path;
2010-04-08 11:09:14 +01:00
$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...
2010-03-31 07:42:18 +01:00
$s = $s->formatElement();
}
else
{
$s = (string) $s;
}
if (false === strpos($s, "'"))
{
return sprintf("'%s'", $s);
2010-03-31 07:42:18 +01:00
}
if (false === strpos($s, '"'))
2010-03-31 07:42:18 +01:00
{
return sprintf('"%s"', $s);
2010-03-31 07:42:18 +01:00
}
$string = $s;
$parts = array();
while (true)
2010-03-31 07:42:18 +01:00
{
if (false !== $pos = strpos($string, "'"))
2010-03-31 07:42:18 +01:00
{
$parts[] = sprintf("'%s'", substr($string, 0, $pos));
$parts[] = "\"'\"";
$string = substr($string, $pos + 1);
}
else
{
$parts[] = "'$string'";
break;
2010-03-31 07:42:18 +01:00
}
}
2010-04-24 00:22:16 +01:00
return sprintf('concat(%s)', implode($parts, ', '));
2010-03-31 07:42:18 +01:00
}
}