/** * Translates a CSS expression to its XPath equivalent. * Optionally, a prefix can be added to the resulting XPath * expression with the $prefix parameter. * * @param mixed $cssExpr * The CSS expression. * @param string $prefix * An optional prefix for the XPath expression. * * @return string @api */ public static function toXPath($cssExpr, $prefix = 'descendant-or-self::') { $translator = new Translator(); if (self::$html) { $translator->registerExtension(new HtmlExtension($translator)); } $translator->registerParserShortcut(new EmptyStringParser())->registerParserShortcut(new ElementParser())->registerParserShortcut(new ClassParser())->registerParserShortcut(new HashParser()); return $translator->cssToXPath($cssExpr, $prefix); }
/** @dataProvider getHtmlShakespearTestData */ public function testHtmlShakespear($css, $count) { $translator = new Translator(); $translator->registerExtension(new HtmlExtension($translator)); $document = new \DOMDocument(); $document->strictErrorChecking = false; $document->loadHTMLFile(__DIR__ . '/Fixtures/shakespear.html'); $document = simplexml_import_dom($document); $bodies = $document->xpath('//body'); $elements = $bodies[0]->xpath($translator->cssToXPath($css)); $this->assertEquals($count, count($elements)); }