Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\CssSelector; Chris@0: Chris@0: use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser; Chris@0: use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser; Chris@0: use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser; Chris@0: use Symfony\Component\CssSelector\Parser\Shortcut\HashParser; Chris@0: use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension; Chris@0: use Symfony\Component\CssSelector\XPath\Translator; Chris@0: Chris@0: /** Chris@0: * CssSelectorConverter is the main entry point of the component and can convert CSS Chris@0: * selectors to XPath expressions. Chris@0: * Chris@0: * @author Christophe Coevoet Chris@0: */ Chris@0: class CssSelectorConverter Chris@0: { Chris@0: private $translator; Chris@0: Chris@0: /** Chris@0: * @param bool $html Whether HTML support should be enabled. Disable it for XML documents Chris@0: */ Chris@0: public function __construct($html = true) Chris@0: { Chris@0: $this->translator = new Translator(); Chris@0: Chris@0: if ($html) { Chris@0: $this->translator->registerExtension(new HtmlExtension($this->translator)); Chris@0: } Chris@0: Chris@0: $this->translator Chris@0: ->registerParserShortcut(new EmptyStringParser()) Chris@0: ->registerParserShortcut(new ElementParser()) Chris@0: ->registerParserShortcut(new ClassParser()) Chris@0: ->registerParserShortcut(new HashParser()) Chris@0: ; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Translates a CSS expression to its XPath equivalent. Chris@0: * Chris@0: * Optionally, a prefix can be added to the resulting XPath Chris@0: * expression with the $prefix parameter. Chris@0: * Chris@0: * @param string $cssExpr The CSS expression Chris@0: * @param string $prefix An optional prefix for the XPath expression Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function toXPath($cssExpr, $prefix = 'descendant-or-self::') Chris@0: { Chris@0: return $this->translator->cssToXPath($cssExpr, $prefix); Chris@0: } Chris@0: }