作者:joelpitte
项目:Min
/**
* 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);
}
作者:laubosslin
项目:la
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');
}
}
作者:nlegof
项目:Blende
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']);
}
}
}
}
作者:ajbde
项目:scrape
/**
* 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);
}
作者:ddrozdi
项目:dmap
/**
* 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);
}
作者:tofla
项目:contao-css-class-replace
/**
* 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;
}
}
作者:GrizliK198
项目:symfony-certification-prepare-projec
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);
}
作者:iBasi
项目:EbayApiBundl
/**
* @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());
}
作者:nstiela
项目:drops-
/**
* 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);
}
作者:zrashwan
项目:news-scrappe
/**
* @param $selector
* @return bool
*/
public static function isCSS($selector)
{
try {
CssSelector::toXPath($selector);
} catch (ParseException $e) {
return false;
}
return true;
}
作者:pf
项目:codeceptio
protected static function toXPath($selector)
{
try {
$xpath = CssSelector::toXPath($selector);
} catch (\Symfony\Component\CssSelector\Exception\ParseException $e) {
$xpath = $selector;
}
return $xpath;
}
作者:boltph
项目:cor
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();
}
作者:mawah
项目:tracke
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');
}
}
作者:spiderling-ph
项目:phpunit-matches-selecto
/**
* 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;
}
作者:jakubzapleta
项目:bank-statement
/**
* @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;
}
作者:heruujok
项目:tsel-net-managemen
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;
}
作者:hitechd
项目:Codeceptio
/**
* @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);
}
作者:northy
项目:css-inline
/**
* 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();
}
作者:paul-
项目:sitemap_verif
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);
}
作者:alie
项目:AtoumBundl
/**
* {@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);
});
}