ClassParser.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\ClassNode;
  12. use Symfony\Component\CssSelector\Node\ElementNode;
  13. use Symfony\Component\CssSelector\Node\SelectorNode;
  14. use Symfony\Component\CssSelector\Parser\ParserInterface;
  15. /**
  16. * CSS selector class parser shortcut.
  17. *
  18. * This component is a port of the Python cssselect library,
  19. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  20. *
  21. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  22. *
  23. * @internal
  24. */
  25. class ClassParser implements ParserInterface
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function parse($source)
  31. {
  32. // Matches an optional namespace, optional element, and required class
  33. // $source = 'test|input.ab6bd_field';
  34. // $matches = array (size=4)
  35. // 0 => string 'test|input.ab6bd_field' (length=22)
  36. // 1 => string 'test' (length=4)
  37. // 2 => string 'input' (length=5)
  38. // 3 => string 'ab6bd_field' (length=11)
  39. if (preg_match('/^(?:([a-z]++)\|)?+([\w-]++|\*)?+\.([\w-]++)$/i', trim($source), $matches)) {
  40. return array(
  41. new SelectorNode(new ClassNode(new ElementNode($matches[1] ?: null, $matches[2] ?: null), $matches[3])),
  42. );
  43. }
  44. return array();
  45. }
  46. }