/** * @dataProvider getGetUriTests */ public function testGetUriOnArea($url, $currentUri, $expected) { $dom = new \DOMDocument(); $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url)); $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri); $this->assertEquals($expected, $link->getUri()); }
/** * @return PagesCollection New Collection with all found links */ public function findLinkedPages() { $pages = new PagesCollection(); $crawler = $this->getCrawler(); foreach ($crawler->filter('a') as $node) { $link = new Link($node, $this->uri); $page = new Page($link->getUri()); $page->setClient($this->client); $pages->add($page); } return $pages; }
public function testGetters() { $dom = new \DOMDocument(); $dom->loadHTML('<html><a href="/foo">foo</a></html>'); $node = $dom->getElementsByTagName('a')->item(0); $link = new Link($node); $this->assertEquals('/foo', $link->getUri(), '->getUri() returns the URI of the link'); $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link'); $this->assertEquals('get', $link->getMethod(), '->getMethod() returns the method of the link'); $link = new Link($node, 'post'); $this->assertEquals('post', $link->getMethod(), '->getMethod() returns the method of the link'); $link = new Link($node, 'get', 'http://localhost', '/bar/'); $this->assertEquals('http://localhost/foo', $link->getUri(), '->getUri() returns the absolute URI of the link'); $this->assertEquals('/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument'); $dom = new \DOMDocument(); $dom->loadHTML('<html><a href="foo">foo</a></html>'); $node = $dom->getElementsByTagName('a')->item(0); $link = new Link($node, 'get', 'http://localhost', '/bar/'); $this->assertEquals('http://localhost/bar/foo', $link->getUri(), '->getUri() returns the absolute URI of the link for relative hrefs'); $this->assertEquals('/bar/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument'); $dom = new \DOMDocument(); $dom->loadHTML('<html><a href="http://login.foo.com/foo">foo</a></html>'); $node = $dom->getElementsByTagName('a')->item(0); $link = new Link($node, 'get', 'http://www.foo.com'); $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object'); $link = new Link($node, 'get'); $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object'); $link = new Link($node, 'get', null, '/bar/'); $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object'); $link = new Link($node, 'get', 'http://www.foo.com', '/bar/'); $this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object'); $dom = new \DOMDocument(); $dom->loadHTML('<html><a href="?get=param">foo</a></html>'); $node = $dom->getElementsByTagName('a')->item(0); $link = new Link($node, 'get', 'http://www.foo.com', '/foo/bar'); $this->assertEquals('http://www.foo.com/foo/bar?get=param', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object'); $link = new Link($node, 'get', 'http://www.foo.com', '/foo/bar'); $this->assertEquals('/foo/bar?get=param', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument'); $dom = new \DOMDocument(); $dom->loadHTML('<html><a href="test.html">foo</a></html>'); $node = $dom->getElementsByTagName('a')->item(0); $link = new Link($node, 'get', null, '/foo/bar', 'http://www.foo.com/'); $this->assertEquals('http://www.foo.com/test.html', $link->getUri()); }
/** * @return null|string */ public function getUri() { switch ($this->type) { case 'application/rss+xml': case 'application/x.atom+xml': case 'application/atom+xml': case 'application/rdf+xml': return parent::getUri(); default: break; } return null; }
/** * Gets the URI of the form. * * The returned URI is not the same as the form "action" attribute. * This method merges the value if the method is GET to mimics * browser behavior. * * @return string The URI * * @api */ public function getUri() { $uri = parent::getUri(); if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH'))) { $query = parse_url($uri, PHP_URL_QUERY); $currentParameters = array(); if ($query) { parse_str($query, $currentParameters); } $queryString = http_build_query(array_merge($currentParameters, $this->getValues()), null, '&'); $pos = strpos($uri, '?'); $base = false === $pos ? $uri : substr($uri, 0, $pos); $uri = rtrim($base . '?' . $queryString, '?'); } return $uri; }
/** * Gets the URI of the form. * * The returned URI is not the same as the form "action" attribute. * This method merges the value if the method is GET to mimics * browser behavior. * * @return string The URI * * @api */ public function getUri() { $uri = parent::getUri(); if (!in_array($this->getMethod(), array('POST', 'PUT', 'DELETE', 'PATCH')) && ($queryString = http_build_query($this->getValues(), null, '&'))) { $sep = false === strpos($uri, '?') ? '?' : '&'; $uri .= $sep . $queryString; } return $uri; }
/** * Adds an HTML content to the list of nodes. * * The libxml errors are disabled when the content is parsed. * * If you want to get parsing errors, be sure to enable * internal errors via libxml_use_internal_errors(true) * and then, get the errors via libxml_get_errors(). Be * sure to clear errors with libxml_clear_errors() afterward. * * @param string $content The HTML content * @param string $charset The charset * * @api */ public function addHtmlContent($content, $charset = 'UTF-8') { $internalErrors = libxml_use_internal_errors(true); $disableEntities = libxml_disable_entity_loader(true); $dom = new \DOMDocument('1.0', $charset); $dom->validateOnParse = true; set_error_handler(function () { throw new \Exception(); }); try { // Convert charset to HTML-entities to work around bugs in DOMDocument::loadHTML() if (function_exists('mb_convert_encoding')) { $content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset); } elseif (function_exists('iconv')) { $content = preg_replace_callback('/[\\x80-\\xFF]+/', function ($m) { $m = unpack('C*', $m[0]); $i = 1; $entities = ''; while (isset($m[$i])) { if (0xf0 <= $m[$i]) { $c = ($m[$i++] - 0xf0 << 18) + ($m[$i++] - 0x80 << 12) + ($m[$i++] - 0x80 << 6) + $m[$i++] - 0x80; } elseif (0xe0 <= $m[$i]) { $c = ($m[$i++] - 0xe0 << 12) + ($m[$i++] - 0x80 << 6) + $m[$i++] - 0x80; } else { $c = ($m[$i++] - 0xc0 << 6) + $m[$i++] - 0x80; } $entities .= '&#' . $c . ';'; } return $entities; }, iconv($charset, 'UTF-8', $content)); } } catch (\Exception $e) { } restore_error_handler(); if ('' !== trim($content)) { @$dom->loadHTML($content); } libxml_use_internal_errors($internalErrors); libxml_disable_entity_loader($disableEntities); $this->addDocument($dom); $base = $this->filterRelativeXPath('descendant-or-self::base')->extract(array('href')); $baseHref = current($base); if (count($base) && !empty($baseHref)) { if ($this->baseHref) { $linkNode = $dom->createElement('a'); $linkNode->setAttribute('href', $baseHref); $link = new Link($linkNode, $this->baseHref); $this->baseHref = $link->getUri(); } else { $this->baseHref = $baseHref; } } }
/** * Adds an HTML content to the list of nodes. * * The libxml errors are disabled when the content is parsed. * * If you want to get parsing errors, be sure to enable * internal errors via libxml_use_internal_errors(true) * and then, get the errors via libxml_get_errors(). Be * sure to clear errors with libxml_clear_errors() afterward. * * @param string $content The HTML content * @param string $charset The charset * * @api */ public function addHtmlContent($content, $charset = 'UTF-8') { $current = libxml_use_internal_errors(true); $disableEntities = libxml_disable_entity_loader(true); $dom = new \DOMDocument('1.0', $charset); $dom->validateOnParse = true; if (function_exists('mb_convert_encoding') && in_array(strtolower($charset), array_map('strtolower', mb_list_encodings()))) { $content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset); } @$dom->loadHTML($content); libxml_use_internal_errors($current); libxml_disable_entity_loader($disableEntities); $this->addDocument($dom); $base = $this->filterXPath('descendant-or-self::base')->extract(array('href')); $baseHref = current($base); if (count($base) && !empty($baseHref)) { if ($this->uri) { $linkNode = $dom->createElement('a'); $linkNode->setAttribute('href', $baseHref); $link = new Link($linkNode, $this->uri); $this->uri = $link->getUri(); } else { $this->uri = $baseHref; } } }
/** * Adds an HTML content to the list of nodes. * * The libxml errors are disabled when the content is parsed. * * If you want to get parsing errors, be sure to enable * internal errors via libxml_use_internal_errors(true) * and then, get the errors via libxml_get_errors(). Be * sure to clear errors with libxml_clear_errors() afterward. * * @param string $content The HTML content * @param string $charset The charset * * @api */ public function addHtmlContent($content, $charset = 'UTF-8') { $internalErrors = libxml_use_internal_errors(true); $disableEntities = libxml_disable_entity_loader(true); $dom = new \DOMDocument('1.0', $charset); $dom->validateOnParse = true; if (function_exists('mb_convert_encoding')) { $hasError = false; set_error_handler(function () use(&$hasError) { $hasError = true; }); $tmpContent = @mb_convert_encoding($content, 'HTML-ENTITIES', $charset); restore_error_handler(); if (!$hasError) { $content = $tmpContent; } } if ('' !== trim($content)) { @$dom->loadHTML($content); } libxml_use_internal_errors($internalErrors); libxml_disable_entity_loader($disableEntities); $this->addDocument($dom); $base = $this->filterRelativeXPath('descendant-or-self::base')->extract(array('href')); $baseHref = current($base); if (count($base) && !empty($baseHref)) { if ($this->uri) { $linkNode = $dom->createElement('a'); $linkNode->setAttribute('href', $baseHref); $link = new Link($linkNode, $this->uri); $this->uri = $link->getUri(); } else { $this->uri = $baseHref; } } }
/** * Clicks on a given link. * * @param Link $link A Link instance */ public function click(Link $link) { return $this->request($link->getMethod(), $link->getUri()); }
/** * Gets the URI of the form. * * The returned URI is not the same as the form "action" attribute. * This method merges the value if the method is GET to mimics * browser behavior. * * @return string The URI * * @api */ public function getUri() { $uri = parent::getUri(); if (!in_array($this->getMethod(), array('post', 'put', 'delete')) && $queryString = http_build_query($this->getValues(), null, '&')) { $sep = false === strpos($uri, '?') ? '?' : '&'; $uri .= $sep.$queryString; } return $uri; }
/** * Link constructor. * * @param DOMElement $node * @param string $currentUri */ public function __construct(DOMElement $node, $currentUri) { parent::__construct($node, $currentUri); }
/** * Adds an HTML content to the list of nodes. * * The libxml errors are disabled when the content is parsed. * * If you want to get parsing errors, be sure to enable * internal errors via libxml_use_internal_errors(true) * and then, get the errors via libxml_get_errors(). Be * sure to clear errors with libxml_clear_errors() afterward. * * @param string $content The HTML content * @param string $charset The charset */ public function addHtmlContent($content, $charset = 'UTF-8') { $internalErrors = libxml_use_internal_errors(true); $disableEntities = libxml_disable_entity_loader(true); $dom = new \DOMDocument('1.0', $charset); $dom->validateOnParse = true; set_error_handler(function () { throw new \Exception(); }); try { // Convert charset to HTML-entities to work around bugs in DOMDocument::loadHTML() $content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset); } catch (\Exception $e) { } restore_error_handler(); if ('' !== trim($content)) { @$dom->loadHTML($content); } libxml_use_internal_errors($internalErrors); libxml_disable_entity_loader($disableEntities); $this->addDocument($dom); $base = $this->filterRelativeXPath('descendant-or-self::base')->extract(array('href')); $baseHref = current($base); if (count($base) && !empty($baseHref)) { if ($this->baseHref) { $linkNode = $dom->createElement('a'); $linkNode->setAttribute('href', $baseHref); $link = new Link($linkNode, $this->baseHref); $this->baseHref = $link->getUri(); } else { $this->baseHref = $baseHref; } } }