ElementParser.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Parser\Shortcut;
  11. use Symfony\Component\CssSelector\Node\ElementNode;
  12. use Symfony\Component\CssSelector\Node\SelectorNode;
  13. use Symfony\Component\CssSelector\Parser\ParserInterface;
  14. /**
  15. * CSS selector element parser shortcut.
  16. *
  17. * This component is a port of the Python cssselect library,
  18. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  19. *
  20. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  21. *
  22. * @internal
  23. */
  24. class ElementParser implements ParserInterface
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function parse($source)
  30. {
  31. // Matches an optional namespace, required element or `*`
  32. // $source = 'testns|testel';
  33. // $matches = array (size=3)
  34. // 0 => string 'testns|testel' (length=13)
  35. // 1 => string 'testns' (length=6)
  36. // 2 => string 'testel' (length=6)
  37. if (preg_match('/^(?:([a-z]++)\|)?([\w-]++|\*)$/i', trim($source), $matches)) {
  38. return array(new SelectorNode(new ElementNode($matches[1] ?: null, $matches[2])));
  39. }
  40. return array();
  41. }
  42. }