[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/vendor/symfony/css-selector/ -> CssSelectorConverter.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\CssSelector;
  13  
  14  use Symfony\Component\CssSelector\Parser\Shortcut\ClassParser;
  15  use Symfony\Component\CssSelector\Parser\Shortcut\ElementParser;
  16  use Symfony\Component\CssSelector\Parser\Shortcut\EmptyStringParser;
  17  use Symfony\Component\CssSelector\Parser\Shortcut\HashParser;
  18  use Symfony\Component\CssSelector\XPath\Extension\HtmlExtension;
  19  use Symfony\Component\CssSelector\XPath\Translator;
  20  
  21  /**
  22   * CssSelectorConverter is the main entry point of the component and can convert CSS
  23   * selectors to XPath expressions.
  24   *
  25   * @author Christophe Coevoet <stof@notk.org>
  26   */
  27  class CssSelectorConverter
  28  {
  29      private $translator;
  30  
  31      /**
  32       * @param bool $html Whether HTML support should be enabled. Disable it for XML documents
  33       */
  34      public function __construct(bool $html = true)
  35      {
  36          $this->translator = new Translator();
  37  
  38          if ($html) {
  39              $this->translator->registerExtension(new HtmlExtension($this->translator));
  40          }
  41  
  42          $this->translator
  43              ->registerParserShortcut(new EmptyStringParser())
  44              ->registerParserShortcut(new ElementParser())
  45              ->registerParserShortcut(new ClassParser())
  46              ->registerParserShortcut(new HashParser())
  47          ;
  48      }
  49  
  50      /**
  51       * Translates a CSS expression to its XPath equivalent.
  52       *
  53       * Optionally, a prefix can be added to the resulting XPath
  54       * expression with the $prefix parameter.
  55       *
  56       * @param string $cssExpr The CSS expression
  57       * @param string $prefix  An optional prefix for the XPath expression
  58       *
  59       * @return string
  60       */
  61      public function toXPath($cssExpr, $prefix = 'descendant-or-self::')
  62      {
  63          return $this->translator->cssToXPath($cssExpr, $prefix);
  64      }
  65  }