Example #1
1
 /**
  * Translates CSS into XPath.
  *
  * @param string|array $locator current selector locator
  *
  * @return string
  */
 public function translateToXPath($locator)
 {
     if (!is_string($locator)) {
         throw new \InvalidArgumentException('The CssSelector expects to get a string as locator');
     }
     // Symfony 2.8+ API
     if (class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
         $converter = new CssSelectorConverter();
         return $converter->toXPath($locator);
     }
     // old static API for Symfony 2.7 and older
     return CSS::toXPath($locator);
 }
Example #2
0
 public function testParseExceptions()
 {
     $parser = new CssSelector();
     try {
         $parser->parse('h1:');
         $this->fail('->parse() throws an Exception if the css selector is not valid');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\Symfony\\Component\\CssSelector\\Exception\\ParseException', $e, '->parse() throws an Exception if the css selector is not valid');
         $this->assertEquals("Expected symbol, got '' at h1: -> ", $e->getMessage(), '->parse() throws an Exception if the css selector is not valid');
     }
 }
 public function testBlender()
 {
     $inputDir = __DIR__ . '/../../ressources/input';
     $outputDir = __DIR__ . '/../../ressources/output';
     $this->process->blend($inputDir, $outputDir);
     $exiftoolBinary = __DIR__ . '/../../../vendor/alchemy/exiftool/exiftool';
     $metas = array('NomdelaPhoto' => array('src' => 'IPTC:Headline', 'value' => 'hello'), 'Rubrique' => array('src' => 'IPTC:Category', 'value' => 'salut'), 'MotsCles' => array('src' => 'IPTC:Keywords', 'value' => 'kakoo'), 'DatedeParution' => array('src' => 'IPTC:Source', 'value' => '2012/04/13'), 'DatePrisedeVue' => array('src' => 'IPTC:DateCreated', 'value' => '2012:04:13'), 'Ville' => array('src' => 'IPTC:City', 'value' => 'paris'), 'Pays' => array('src' => 'IPTC:Country-PrimaryLocationName', 'value' => 'france'), 'Copyright' => array('src' => 'IPTC:CopyrightNotice', 'value' => 'yata'));
     $cmd = $exiftoolBinary . ' -X ' . __DIR__ . '/../../ressources/output/1.jpg';
     $output = shell_exec($cmd);
     if ($output) {
         $document = new \DOMDocument();
         $document->loadXML($output);
         $xpath = new \DOMXPath($document);
         $xPathQuery = CssSelector::toXPath('*');
         foreach ($metas as $metaInfo) {
             $found = false;
             foreach ($xpath->query($xPathQuery) as $node) {
                 $nodeName = $node->nodeName;
                 $value = $node->nodeValue;
                 if ($nodeName == $metaInfo['src']) {
                     $this->assertEquals($value, $metaInfo['value']);
                     $found = true;
                     continue;
                 }
             }
             if (!$found) {
                 $this->fail('missing ' . $metaInfo['src']);
             }
         }
     }
 }
Example #4
0
 /**
  * Return DOMNodeList from CSS selector
  *
  * @param $string
  * @return \DOMNodeList
  */
 public function query($string)
 {
     CssSelector::disableHtmlExtension();
     $xpathQuery = CssSelector::toXPath($string);
     $xpath = new \DOMXPath($this->data);
     return $xpath->query($xpathQuery);
 }
Example #5
0
 /**
  * Translates CSS into XPath.
  *
  * @param string|array $locator current selector locator
  *
  * @return string
  */
 public function translateToXPath($locator)
 {
     if (!is_string($locator)) {
         throw new \InvalidArgumentException('The CssSelector expects to get a string as locator');
     }
     return CSS::toXPath($locator);
 }
 /**
  * Create and update XPath expression
  */
 public function updateXPathExpression()
 {
     if ($this->type === 'css') {
         $this->xpath_expression = CssSelector::toXPath($this->selector);
     } else {
         $this->xpath_expression = $this->selector;
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $selector = $input->getArgument('selector');
     /** @var FormatterHelper $formatter */
     $formatter = $this->getHelper('formatter');
     $block = $formatter->formatBlock(CssSelector::toXPath($selector), 'question', true);
     $output->writeln($block);
 }
 /**
  * @dataProvider provideUrls
  * @param $url
  */
 public function testPageIsSuccessful($url)
 {
     $client = static::createClient();
     CssSelector::disableHtmlExtension();
     $crawler = $client->request('GET', $url);
     $this->assertTrue($client->getResponse()->isSuccessful());
     $this->assertGreaterThan(0, $crawler->filter('pre:contains("ck>Success</")')->count());
 }
 /**
  * Ensures that the rendered results are working as expected.
  *
  * @param array $expected
  *   The expected rows of the result.
  */
 protected function assertRows($expected = [])
 {
     $actual = [];
     $rows = $this->cssSelect('div.views-row');
     foreach ($rows as $row) {
         $actual[] = ['title' => (string) $row->xpath(CssSelector::toXPath('.views-field-title span.field-content a'))[0], 'sticky' => (string) $row->xpath(CssSelector::toXPath('.views-field-sticky span.field-content'))[0]];
     }
     $this->assertEqual($actual, $expected);
 }
Example #10
0
 /**
  * @param $selector
  * @return bool
  */
 public static function isCSS($selector)
 {
     try {
         CssSelector::toXPath($selector);
     } catch (ParseException $e) {
         return false;
     }
     return true;
 }
Example #11
0
 protected static function toXPath($selector)
 {
     try {
         $xpath = CssSelector::toXPath($selector);
     } catch (\Symfony\Component\CssSelector\Exception\ParseException $e) {
         $xpath = $selector;
     }
     return $xpath;
 }
Example #12
0
File: dom.php Project: boltphp/core
 public function html()
 {
     $ref = clone $this->_doc;
     $xpath = new DOMXPath($ref);
     foreach ($xpath->query(CssSelector::toXPath('*[data-domref]')) as $node) {
         $node->removeAttribute('data-domref');
     }
     return $ref->saveHTML();
 }
Example #13
0
 public function testParseExceptions()
 {
     try {
         CssSelector::toXPath('h1:');
         $this->fail('->parse() throws an Exception if the css selector is not valid');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\Symfony\\Component\\CssSelector\\Exception\\ParseException', $e, '->parse() throws an Exception if the css selector is not valid');
         $this->assertEquals("Expected identifier, but <eof at 3> found.", $e->getMessage(), '->parse() throws an Exception if the css selector is not valid');
     }
 }
Example #14
0
 /**
  * Metodo que convierte un selector css en un xpath
  * @param  string $css_selector Cadena de texto con el selector css.
  * @return string|NULL          Devuelve una cadena de texto con formato xpath si la converción e sposible o NULL en caso contrario.
  */
 private function toXPath($css_selector)
 {
     $xpath = Null;
     try {
         $xpath = $this->selector->toXPath($css_selector);
     } catch (\Exception $e) {
         $this->logger->logError('E003', array($css_selector));
         $this->logger->logError('E000', array($e->getMessage()));
     }
     return $xpath;
 }
 /**
  * Evaluates the constraint for parameter $other. Returns true if the
  * constraint is met, false otherwise.
  *
  * @param  mixed $other
  * @return bool
  */
 public function matches($other)
 {
     if ($other instanceof DOMElement) {
         $xpathSelector = CssSelector::toXPath($this->cssSelector, '//');
         $xpath = new DOMXPath($other->ownerDocument);
         foreach ($xpath->query($xpathSelector) as $node) {
             if ($node === $other) {
                 return true;
             }
         }
     }
     return false;
 }
 /**
  * @param Crawler $crawler
  *
  * @return \JakubZapletal\Component\BankStatement\Statement\Statement
  */
 protected function parseCrawler(Crawler $crawler)
 {
     $this->statement = $this->getStatementClass();
     CssSelector::disableHtmlExtension();
     $crawler = $crawler->filter('FINSTA > FINSTA03');
     if ($crawler !== null) {
         $this->parseStatementNode($crawler);
         $parser = $this;
         $crawler->filter('FINSTA05')->each(function (Crawler $node) use($parser) {
             $parser->parseAndAddTransaction($node);
         });
     }
     return $this->statement;
 }
 public function toXPath()
 {
     try {
         if (class_exists('Symfony\\Component\\CssSelector\\CssSelectorConverter')) {
             $converter = new CssSelectorConverter();
             $query = $converter->toXPath($this->selector);
         } else {
             $query = CssSelector::toXPath($this->selector);
         }
     } catch (ExceptionInterface $e) {
         $query = null;
     }
     return $query;
 }
Example #18
0
 /**
  * @param $cssOrXPath
  * @return \DOMElement
  */
 public function matchElement($cssOrXPath)
 {
     $xpath = new \DOMXpath($this->xml);
     try {
         $selector = CssSelector::toXPath($cssOrXPath);
         $els = $xpath->query($selector);
         if ($els) {
             return $els->item(0);
         }
     } catch (ParseException $e) {
     }
     $els = $xpath->query($cssOrXPath);
     if ($els) {
         return $els->item(0);
     }
     throw new ElementNotFound($cssOrXPath);
 }
Example #19
0
 /**
  * Prepares everything and inserts inline styles into html
  * @param string $html represents html document
  * @return string
  */
 public function render($html)
 {
     $this->dom = new \DOMDocument();
     $this->dom->loadHTML($html);
     $this->finder = new \DOMXPath($this->dom);
     $this->css = $this->getCSS();
     foreach ($this->css->getAllRuleSets() as $ruleSet) {
         $selector = $ruleSet->getSelector();
         foreach ($this->finder->evaluate(CssSelector::toXPath($selector[0])) as $node) {
             if ($node->getAttribute('style')) {
                 $node->setAttribute('style', $node->getAttribute('style') . implode(' ', $ruleSet->getRules()));
             } else {
                 $node->setAttribute('style', implode(' ', $ruleSet->getRules()));
             }
         }
     }
     return $this->dom->saveHTML();
 }
Example #20
0
 public function __construct(UrlBuilder $u, LoggerInterface $logger)
 {
     $pages = array();
     // Disable `HTML` extension of CssSelector.
     CssSelector::disableHtmlExtension();
     $client = new Client();
     $crawler = $client->request('GET', (string) $u);
     $status = $client->getResponse()->getStatus();
     if ($status > 399) {
         $logger->emergency('Status ' . $status . ' getting ' . (string) $u);
     }
     $sitemap_crawler = $crawler->filter('urlset > url > loc');
     foreach ($sitemap_crawler as $url_loc) {
         $url = $url_loc->nodeValue;
         $pages[$url] = $url;
     }
     parent::__construct($pages);
 }
Example #21
0
 /**
  * {@inheritdoc}
  */
 public function __construct(atoum\adapter $adapter = null, atoum\annotations\extractor $annotationExtractor = null, atoum\asserter\generator $asserterGenerator = null, atoum\test\assertion\manager $assertionManager = null, \closure $reflectionClassFactory = null)
 {
     parent::__construct($adapter, $annotationExtractor, $asserterGenerator, $assertionManager, $reflectionClassFactory);
     $generator = $this->getAsserterGenerator();
     $test = $this;
     $crawler = null;
     $client = null;
     $this->getAssertionManager()->setHandler('request', function (array $options = array(), array $server = array(), array $cookies = array()) use(&$client, $test) {
         $client = $test->createClient($options, $server, $cookies);
         return $test;
     })->setHandler('get', $get = $this->getSendRequestHandler($client, $crawler, 'GET'))->setHandler('GET', $get)->setHandler('head', $head = $this->getSendRequestHandler($client, $crawler, 'HEAD'))->setHandler('HEAD', $head)->setHandler('post', $post = $this->getSendRequestHandler($client, $crawler, 'POST'))->setHandler('POST', $post)->setHandler('put', $put = $this->getSendRequestHandler($client, $crawler, 'PUT'))->setHandler('PUT', $put)->setHandler('patch', $patch = $this->getSendRequestHandler($client, $crawler, 'PATCH'))->setHandler('PATCH', $patch)->setHandler('delete', $delete = $this->getSendRequestHandler($client, $crawler, 'DELETE'))->setHandler('DELETE', $delete)->setHandler('options', $options = $this->getSendRequestHandler($client, $crawler, 'OPTIONS'))->setHandler('OPTIONS', $options)->setHandler('crawler', function ($strict = false) use(&$crawler, $generator, $test) {
         if ($strict) {
             CssSelector::enableHtmlExtension();
         } else {
             CssSelector::disableHtmlExtension();
         }
         return $generator->getAsserterInstance('\\atoum\\AtoumBundle\\Test\\Asserters\\Crawler', array($crawler), $test);
     });
 }
Example #22
0
 /**
  * Build a Table Of Content
  *
  * @static
  * @param string $html html string
  * @return array
  */
 public static function buildTOC($html)
 {
     if (!$html) {
         return array();
     }
     $html = static::addHeadingsId($html, 'h1, h2, h3, h4, h5, h6');
     $document = new \DOMDocument();
     $document->loadHTML('<?xml encoding="UTF-8">' . $html);
     $xpath = new \DOMXPath($document);
     $toc = array();
     $h1 = $h2 = $h3 = $h4 = $h5 = $h5 = $h6 = 0;
     foreach ($xpath->query(CssSelector::toXPath('h1, h2, h3, h4, h5, h6')) as $node) {
         $nodeName = $node->nodeName;
         $title = $node->nodeValue;
         $id = $node->getAttribute('id');
         switch ($nodeName) {
             case 'h1':
                 $toc[++$h1] = array('title' => $title, 'id' => $id, 'children' => array());
                 break;
             case 'h2':
                 $toc = self::fixToc($toc, $nodeName, $h1, $h2, $h3, $h4, $h5, $h5);
                 $toc[$h1]['children'][++$h2] = array('title' => $title, 'id' => $id, 'children' => array());
                 break;
             case 'h3':
                 $toc = self::fixToc($toc, $nodeName, $h1, $h2, $h3, $h4, $h5, $h5);
                 $toc[$h1]['children'][$h2]['children'][++$h3] = array('title' => $title, 'id' => $id, 'children' => array());
                 break;
             case 'h4':
                 $toc = self::fixToc($toc, $nodeName, $h1, $h2, $h3, $h4, $h5, $h5);
                 $toc[$h1]['children'][$h2]['children'][$h3]['children'][++$h4] = array('title' => $title, 'id' => $id, 'children' => array());
                 break;
             case 'h5':
                 $toc = self::fixToc($toc, $nodeName, $h1, $h2, $h3, $h4, $h5, $h5);
                 $toc[$h1]['children'][$h2]['children'][$h3]['children'][$h4]['children'][++$h5] = array('title' => $title, 'id' => $id, 'children' => array());
                 break;
             case 'h6':
                 $toc = self::fixToc($toc, $nodeName, $h1, $h2, $h3, $h4, $h5, $h5);
                 $toc[$h1]['children'][$h2]['children'][$h3]['children'][$h4]['children'][$h5]['children'][++$h6] = array('title' => $title, 'id' => $id, 'children' => array());
                 break;
         }
     }
     return $toc;
 }
 /**
  * @return $this
  */
 public function fetchData()
 {
     if (!empty($this->dataConfig)) {
         foreach ($this->dataConfig as $key => $value) {
             switch (1) {
                 case is_array($value) && strtolower(key($value)) == 'css':
                     // css
                     $value = CssSelector::toXpath(current($value));
                 case is_string($value):
                     // xpath
                     $xpath = $this->getPage()->getXpath();
                     $this->data[$key] = $this->xpathResult($xpath, $value);
                     break;
                 case is_callable($value):
                     // calback funtion
                     $this->data[$key] = call_user_func($value, $this->getPage());
                     break;
             }
         }
     }
     return $this;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var FormatterHelper $formatter */
     $formatter = $this->getHelper('formatter');
     $html = file_get_contents('http://spb.hh.ru/search/vacancy?text=php&clusters=true&enable_snippets=true');
     $crawler = new Crawler($html, 'http://spb.hh.ru/search/vacancy?text=php&clusters=true&enable_snippets=true');
     $output->writeln($formatter->formatBlock('Vacancies', 'question', true));
     $crawler->filterXPath(CssSelector::toXPath('body a.search-result-item__name'))->each(function (Crawler $node) use($output) {
         $output->writeln(sprintf('<comment>%s</comment>', $node->text()));
     });
     $data = $crawler->filter('body a.search-result-item__name')->extract(['_text', 'class']);
     print_r($data[0]);
     $output->writeln($formatter->formatBlock('Link test', 'question', true));
     $link = $crawler->selectLink('PHP developer')->link();
     $output->writeln($link->getUri());
     $crawler = new Crawler(file_get_contents('http://spb.hh.ru/login'), 'http://spb.hh.ru/login');
     $output->writeln($formatter->formatBlock('Form test', 'question', true));
     $form = $crawler->selectButton('Войти')->form(['username' => 'name', 'password' => 'pass']);
     $output->writeln($form->getUri());
     $form['remember']->tick();
     $output->writeln(print_r($form->getPhpValues()));
 }
Example #25
0
 /**
  * Filters the list of nodes with a CSS selector.
  *
  * This method only works if you have installed the CssSelector Symfony Component.
  *
  * @param string $selector A CSS selector
  *
  * @return Crawler A new instance of Crawler with the filtered list of nodes
  *
  * @throws \RuntimeException if the CssSelector Component is not available
  *
  * @api
  */
 public function filter($selector)
 {
     if (!class_exists('Symfony\\Component\\CssSelector\\CssSelector')) {
         // @codeCoverageIgnoreStart
         throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector is not installed (you can use filterXPath instead).');
         // @codeCoverageIgnoreEnd
     }
     return $this->filterXPath(CssSelector::toXPath($selector));
 }
Example #26
0
 public function testFilterWithMultipleNamespaces()
 {
     CssSelector::disableHtmlExtension();
     $crawler = $this->createTestXmlCrawler()->filter('media|group yt|aspectRatio');
     $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
     $this->assertSame('widescreen', $crawler->text());
 }
Example #27
0
 /**
  * @param $selector
  *
  * @return Crawler
  */
 protected function match($selector)
 {
     if (is_array($selector)) {
         return $this->strictMatch($selector);
     }
     try {
         $selector = CssSelector::toXPath($selector);
     } catch (ParseException $e) {
     }
     if (!Locator::isXPath($selector)) {
         return null;
     }
     return @$this->crawler->filterXPath($selector);
 }
Example #28
0
 /**
  * Constructor.
  *
  * @param String $css
  */
 public function __construct($css)
 {
     $this->xpath = CssSelector::toXPath($css);
 }
Example #29
0
 /**
  * Searches elements using a CSS selector in the raw content.
  *
  * The search is relative to the root element (HTML tag normally) of the page.
  *
  * @param string $selector
  *   CSS selector to use in the search.
  *
  * @return \SimpleXMLElement[]
  *   The return value of the XPath search performed after converting the CSS
  *   selector to an XPath selector.
  */
 protected function cssSelect($selector)
 {
     return $this->xpath(CssSelector::toXPath($selector));
 }
 /**
  * @param $content string
  *
  * @return string
  */
 private function removeLastItem($content)
 {
     $document = new \DOMDocument('1.0', \Yii::$app->charset);
     $crawler = new Crawler();
     $crawler->addHTMLContent($content, \Yii::$app->charset);
     $root = $document->appendChild($document->createElement('_root'));
     $crawler->rewind();
     $root->appendChild($document->importNode($crawler->current(), true));
     $domxpath = new \DOMXPath($document);
     $crawlerInverse = $domxpath->query(CssSelector::toXPath($this->widgetItem . ':last-child'));
     foreach ($crawlerInverse as $key => $elementToRemove) {
         $parent = $elementToRemove->parentNode;
         $parent->removeChild($elementToRemove);
     }
     $crawler->clear();
     $crawler->add($document);
     return $crawler->filter('body')->eq(0)->html();
 }