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\Parser\Shortcut; Chris@0: Chris@0: use Symfony\Component\CssSelector\Node\ElementNode; Chris@0: use Symfony\Component\CssSelector\Node\SelectorNode; Chris@0: use Symfony\Component\CssSelector\Parser\ParserInterface; Chris@0: Chris@0: /** Chris@0: * CSS selector element parser shortcut. Chris@0: * Chris@0: * This component is a port of the Python cssselect library, Chris@0: * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class ElementParser implements ParserInterface Chris@0: { Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function parse($source) Chris@0: { Chris@0: // Matches an optional namespace, required element or `*` Chris@0: // $source = 'testns|testel'; Chris@0: // $matches = array (size=3) Chris@0: // 0 => string 'testns|testel' (length=13) Chris@0: // 1 => string 'testns' (length=6) Chris@0: // 2 => string 'testel' (length=6) Chris@0: if (preg_match('/^(?:([a-z]++)\|)?([\w-]++|\*)$/i', trim($source), $matches)) { Chris@17: return [new SelectorNode(new ElementNode($matches[1] ?: null, $matches[2]))]; Chris@0: } Chris@0: Chris@17: return []; Chris@0: } Chris@0: }