2010-04-15 13:41:42 +01:00
< ? php
/*
2010-04-24 00:22:16 +01:00
* This file is part of the Symfony package .
2010-04-15 13:41:42 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-15 13:41:42 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\DomCrawler ;
2011-06-15 06:52:56 +01:00
use Symfony\Component\CssSelector\CssSelector ;
2011-01-15 13:29:43 +00:00
2010-04-15 13:41:42 +01:00
/**
* Crawler eases navigation of a list of \DOMNode objects .
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2011-03-24 09:00:10 +00:00
*
* @ api
2010-04-15 13:41:42 +01:00
*/
class Crawler extends \SplObjectStorage
{
2012-04-08 08:55:44 +01:00
/**
* @ var string The current URI or the base href value
*/
2013-02-28 10:23:15 +00:00
protected $uri ;
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Constructor .
*
* @ param mixed $node A Node to use as the base for the crawling
2011-04-23 15:25:21 +01:00
* @ param string $uri The current URI or the base href value
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
2011-04-23 15:25:21 +01:00
public function __construct ( $node = null , $uri = null )
2010-05-06 12:25:53 +01:00
{
$this -> uri = $uri ;
2011-01-13 01:05:26 +00:00
2010-05-06 12:25:53 +01:00
$this -> add ( $node );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Removes all the nodes .
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function clear ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$this -> removeAll ( $this );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds a node to the current list of nodes .
*
* This method uses the appropriate specialized add * () method based
* on the type of the argument .
*
2013-05-03 23:53:35 +01:00
* @ param \DOMNodeList | \DOMNode | array | string | null $node A node
*
* @ throws \InvalidArgumentException When node is not the expected type .
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function add ( $node )
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
if ( $node instanceof \DOMNodeList ) {
2010-05-06 12:25:53 +01:00
$this -> addNodeList ( $node );
2013-05-03 23:53:35 +01:00
} elseif ( $node instanceof \DOMNode ) {
$this -> addNode ( $node );
2010-05-07 15:09:11 +01:00
} elseif ( is_array ( $node )) {
2010-05-06 12:25:53 +01:00
$this -> addNodes ( $node );
2010-06-19 10:42:07 +01:00
} elseif ( is_string ( $node )) {
$this -> addContent ( $node );
2013-05-06 07:26:30 +01:00
} elseif ( null !== $node ) {
throw new \InvalidArgumentException ( sprintf ( 'Expecting a DOMNodeList or DOMNode instance, an array, a string, or null, but got "%s".' , is_object ( $node ) ? get_class ( $node ) : gettype ( $node )));
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
}
2011-03-24 09:00:10 +00:00
/**
* Adds HTML / XML content .
*
2013-06-13 12:40:37 +01:00
* If the charset is not set via the content type , it is assumed
* to be ISO - 8859 - 1 , which is the default charset defined by the
* HTTP 1.1 specification .
*
2012-04-08 08:55:44 +01:00
* @ param string $content A string to parse as HTML / XML
* @ param null | string $type The content type of the string
2011-03-24 09:00:10 +00:00
*/
2010-05-06 12:25:53 +01:00
public function addContent ( $content , $type = null )
2010-04-19 07:33:45 +01:00
{
2010-05-07 15:09:11 +01:00
if ( empty ( $type )) {
2010-05-06 12:25:53 +01:00
$type = 'text/html' ;
}
// DOM only for HTML/XML content
2013-09-19 09:03:41 +01:00
if ( ! preg_match ( '/(x|ht)ml/i' , $type , $xmlMatches )) {
2010-05-06 12:25:53 +01:00
return null ;
}
2013-09-19 09:03:41 +01:00
$charset = null ;
2013-10-25 16:19:22 +01:00
if ( false !== $pos = stripos ( $type , 'charset=' )) {
2010-05-06 12:25:53 +01:00
$charset = substr ( $type , $pos + 8 );
2011-08-10 10:41:07 +01:00
if ( false !== $pos = strpos ( $charset , ';' )) {
$charset = substr ( $charset , 0 , $pos );
}
2010-05-06 12:25:53 +01:00
}
2013-09-19 09:03:41 +01:00
if ( null === $charset &&
preg_match ( '/\<meta[^\>]+charset *= *["\']?([a-zA-Z\-0-9]+)/i' , $content , $matches )) {
$charset = $matches [ 1 ];
}
if ( null === $charset ) {
$charset = 'ISO-8859-1' ;
}
if ( 'x' === $xmlMatches [ 1 ]) {
2010-05-06 12:25:53 +01:00
$this -> addXmlContent ( $content , $charset );
2010-05-07 15:09:11 +01:00
} else {
2010-05-06 12:25:53 +01:00
$this -> addHtmlContent ( $content , $charset );
}
2010-04-19 07:33:45 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds an HTML content to the list of nodes .
*
2011-09-28 08:53:50 +01:00
* 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 .
*
2010-05-06 12:25:53 +01:00
* @ param string $content The HTML content
* @ param string $charset The charset
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function addHtmlContent ( $content , $charset = 'UTF-8' )
2010-04-19 07:33:45 +01:00
{
2012-08-28 08:54:04 +01:00
$current = libxml_use_internal_errors ( true );
2012-08-27 18:17:44 +01:00
$disableEntities = libxml_disable_entity_loader ( true );
2010-05-06 12:25:53 +01:00
$dom = new \DOMDocument ( '1.0' , $charset );
$dom -> validateOnParse = true ;
2012-09-25 10:03:23 +01:00
if ( function_exists ( 'mb_convert_encoding' ) && in_array ( strtolower ( $charset ), array_map ( 'strtolower' , mb_list_encodings ()))) {
2012-05-07 13:20:03 +01:00
$content = mb_convert_encoding ( $content , 'HTML-ENTITIES' , $charset );
}
2010-05-06 12:25:53 +01:00
@ $dom -> loadHTML ( $content );
2012-08-27 18:17:44 +01:00
2011-09-28 08:53:50 +01:00
libxml_use_internal_errors ( $current );
2012-08-27 18:17:44 +01:00
libxml_disable_entity_loader ( $disableEntities );
2011-09-28 08:53:50 +01:00
2010-05-06 12:25:53 +01:00
$this -> addDocument ( $dom );
2011-01-13 01:05:26 +00:00
2012-03-28 17:08:10 +01:00
$base = $this -> filterXPath ( 'descendant-or-self::base' ) -> extract ( array ( 'href' ));
2011-01-13 01:05:26 +00:00
2013-02-25 13:25:47 +00:00
$baseHref = current ( $base );
if ( count ( $base ) && ! empty ( $baseHref )) {
2013-04-20 13:52:51 +01:00
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 ;
}
2011-01-13 01:05:26 +00:00
}
2010-04-19 07:33:45 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds an XML content to the list of nodes .
*
2011-09-28 08:53:50 +01:00
* 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 .
*
2010-05-06 12:25:53 +01:00
* @ param string $content The XML content
* @ param string $charset The charset
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function addXmlContent ( $content , $charset = 'UTF-8' )
2010-04-19 07:33:45 +01:00
{
2012-08-28 08:54:04 +01:00
$current = libxml_use_internal_errors ( true );
2012-08-27 18:17:44 +01:00
$disableEntities = libxml_disable_entity_loader ( true );
2010-05-06 12:25:53 +01:00
$dom = new \DOMDocument ( '1.0' , $charset );
$dom -> validateOnParse = true ;
// remove the default namespace to make XPath expressions simpler
2012-08-27 18:17:44 +01:00
@ $dom -> loadXML ( str_replace ( 'xmlns' , 'ns' , $content ), LIBXML_NONET );
2011-09-28 08:53:50 +01:00
libxml_use_internal_errors ( $current );
2012-08-27 18:17:44 +01:00
libxml_disable_entity_loader ( $disableEntities );
2011-09-28 08:53:50 +01:00
2010-05-06 12:25:53 +01:00
$this -> addDocument ( $dom );
2010-04-19 07:33:45 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds a \DOMDocument to the list of nodes .
*
* @ param \DOMDocument $dom A \DOMDocument instance
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function addDocument ( \DOMDocument $dom )
2010-04-19 07:33:45 +01:00
{
2010-05-07 15:09:11 +01:00
if ( $dom -> documentElement ) {
2010-05-06 12:25:53 +01:00
$this -> addNode ( $dom -> documentElement );
}
2010-04-19 07:33:45 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds a \DOMNodeList to the list of nodes .
*
* @ param \DOMNodeList $nodes A \DOMNodeList instance
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function addNodeList ( \DOMNodeList $nodes )
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
foreach ( $nodes as $node ) {
2010-05-06 12:25:53 +01:00
$this -> addNode ( $node );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds an array of \DOMNode instances to the list of nodes .
*
2012-11-01 15:08:59 +00:00
* @ param \DOMNode [] $nodes An array of \DOMNode instances
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function addNodes ( array $nodes )
{
2010-05-07 15:09:11 +01:00
foreach ( $nodes as $node ) {
2010-05-06 12:25:53 +01:00
$this -> add ( $node );
}
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Adds a \DOMNode instance to the list of nodes .
*
* @ param \DOMNode $node A \DOMNode instance
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function addNode ( \DOMNode $node )
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
if ( $node instanceof \DOMDocument ) {
2010-05-06 12:25:53 +01:00
$this -> attach ( $node -> documentElement );
2010-05-07 15:09:11 +01:00
} else {
2010-05-06 12:25:53 +01:00
$this -> attach ( $node );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns a node given its position in the node list .
*
* @ param integer $position The position
*
2012-04-08 08:55:44 +01:00
* @ return Crawler A new instance of the Crawler with the selected node , or an empty Crawler if it does not exist .
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function eq ( $position )
{
2010-05-07 15:09:11 +01:00
foreach ( $this as $i => $node ) {
2010-05-08 14:32:30 +01:00
if ( $i == $position ) {
2010-05-06 12:25:53 +01:00
return new static ( $node , $this -> uri );
}
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
return new static ( null , $this -> uri );
}
/**
* Calls an anonymous function on each node of the list .
*
2013-04-22 05:49:51 +01:00
* The anonymous function receives the position and the node wrapped
* in a Crawler instance as arguments .
2010-05-06 12:25:53 +01:00
*
* Example :
*
2013-04-22 05:49:51 +01:00
* $crawler -> filter ( 'h1' ) -> each ( function ( $node , $i ) {
* return $node -> text ();
2010-05-06 12:25:53 +01:00
* });
*
* @ param \Closure $closure An anonymous function
*
* @ return array An array of values returned by the anonymous function
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function each ( \Closure $closure )
{
$data = array ();
2010-05-07 15:09:11 +01:00
foreach ( $this as $i => $node ) {
2013-04-22 05:49:51 +01:00
$data [] = $closure ( new static ( $node , $this -> uri ), $i );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
return $data ;
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Reduces the list of nodes by calling an anonymous function .
*
* To remove a node from the list , the anonymous function must return false .
*
* @ param \Closure $closure An anonymous function
*
2010-07-01 19:17:03 +01:00
* @ return Crawler A Crawler instance with the selected nodes .
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function reduce ( \Closure $closure )
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$nodes = array ();
2010-05-07 15:09:11 +01:00
foreach ( $this as $i => $node ) {
2013-04-22 05:49:51 +01:00
if ( false !== $closure ( new static ( $node , $this -> uri ), $i )) {
2010-05-06 12:25:53 +01:00
$nodes [] = $node ;
}
}
return new static ( $nodes , $this -> uri );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns the first node of the current selection
*
* @ return Crawler A Crawler instance with the first selected node
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function first ()
{
return $this -> eq ( 0 );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Returns the last node of the current selection
*
* @ return Crawler A Crawler instance with the last selected node
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function last ()
{
2010-06-25 16:58:29 +01:00
return $this -> eq ( count ( $this ) - 1 );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Returns the siblings nodes of the current selection
*
* @ return Crawler A Crawler instance with the sibling nodes
*
* @ throws \InvalidArgumentException When current node is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function siblings ()
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
return new static ( $this -> sibling ( $this -> getNode ( 0 ) -> parentNode -> firstChild ), $this -> uri );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns the next siblings nodes of the current selection
*
* @ return Crawler A Crawler instance with the next sibling nodes
*
* @ throws \InvalidArgumentException When current node is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function nextAll ()
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
return new static ( $this -> sibling ( $this -> getNode ( 0 )), $this -> uri );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Returns the previous sibling nodes of the current selection
*
* @ return Crawler A Crawler instance with the previous sibling nodes
2011-03-24 09:00:10 +00:00
*
2012-12-16 12:02:54 +00:00
* @ throws \InvalidArgumentException
*
2011-03-24 09:00:10 +00:00
* @ api
2010-05-06 12:25:53 +01:00
*/
public function previousAll ()
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
return new static ( $this -> sibling ( $this -> getNode ( 0 ), 'previousSibling' ), $this -> uri );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns the parents nodes of the current selection
*
* @ return Crawler A Crawler instance with the parents nodes of the current selection
*
* @ throws \InvalidArgumentException When current node is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function parents ()
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
$node = $this -> getNode ( 0 );
$nodes = array ();
2010-05-07 15:09:11 +01:00
while ( $node = $node -> parentNode ) {
2010-05-08 14:32:30 +01:00
if ( 1 === $node -> nodeType && '_root' !== $node -> nodeName ) {
2010-05-06 12:25:53 +01:00
$nodes [] = $node ;
}
}
return new static ( $nodes , $this -> uri );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Returns the children nodes of the current selection
*
* @ return Crawler A Crawler instance with the children nodes
*
* @ throws \InvalidArgumentException When current node is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function children ()
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
2013-05-16 01:06:15 +01:00
$node = $this -> getNode ( 0 ) -> firstChild ;
return new static ( $node ? $this -> sibling ( $node ) : array (), $this -> uri );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns the attribute value of the first node of the list .
*
* @ param string $attribute The attribute name
*
* @ return string The attribute value
*
* @ throws \InvalidArgumentException When current node is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function attr ( $attribute )
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
return $this -> getNode ( 0 ) -> getAttribute ( $attribute );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Returns the node value of the first node of the list .
*
* @ return string The node value
*
* @ throws \InvalidArgumentException When current node is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function text ()
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
return $this -> getNode ( 0 ) -> nodeValue ;
}
2013-04-21 01:11:42 +01:00
/**
2013-04-22 06:03:23 +01:00
* Returns the first node of the list as HTML .
2013-04-21 01:11:42 +01:00
*
* @ return string The node html
*
* @ throws \InvalidArgumentException When current node is empty
*/
public function html ()
{
if ( ! count ( $this )) {
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
$html = '' ;
foreach ( $this -> getNode ( 0 ) -> childNodes as $child ) {
2013-05-11 21:34:40 +01:00
if ( version_compare ( PHP_VERSION , '5.3.6' , '>=' )) {
// node parameter was added to the saveHTML() method in PHP 5.3.6
// @see http://php.net/manual/en/domdocument.savehtml.php
$html .= $child -> ownerDocument -> saveHTML ( $child );
} else {
$document = new \DOMDocument ( '1.0' , 'UTF-8' );
$document -> appendChild ( $document -> importNode ( $child , true ));
$html .= rtrim ( $document -> saveHTML ());
}
2013-04-21 01:11:42 +01:00
}
2013-04-22 06:03:23 +01:00
return $html ;
2013-04-21 01:11:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Extracts information from the list of nodes .
*
* You can extract attributes or / and the node value ( _text ) .
*
* Example :
*
* $crawler -> filter ( 'h1 a' ) -> extract ( array ( '_text' , 'href' ));
*
* @ param array $attributes An array of attributes
*
2010-07-01 19:17:03 +01:00
* @ return array An array of extracted values
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function extract ( $attributes )
{
2011-04-26 10:39:34 +01:00
$attributes = ( array ) $attributes ;
2013-11-29 00:05:12 +00:00
$count = count ( $attributes );
2010-05-06 12:25:53 +01:00
$data = array ();
2010-05-07 15:09:11 +01:00
foreach ( $this as $node ) {
2010-05-06 12:25:53 +01:00
$elements = array ();
2010-05-07 15:09:11 +01:00
foreach ( $attributes as $attribute ) {
2010-05-08 14:32:30 +01:00
if ( '_text' === $attribute ) {
2010-05-06 12:25:53 +01:00
$elements [] = $node -> nodeValue ;
2010-05-07 15:09:11 +01:00
} else {
2010-05-06 12:25:53 +01:00
$elements [] = $node -> getAttribute ( $attribute );
}
}
2013-11-29 00:05:12 +00:00
$data [] = $count > 1 ? $elements : $elements [ 0 ];
2010-05-06 12:25:53 +01:00
}
return $data ;
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Filters the list of nodes with an XPath expression .
*
* @ param string $xpath An XPath expression
*
* @ return Crawler A new instance of Crawler with the filtered list of nodes
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function filterXPath ( $xpath )
{
$document = new \DOMDocument ( '1.0' , 'UTF-8' );
$root = $document -> appendChild ( $document -> createElement ( '_root' ));
2010-05-07 15:09:11 +01:00
foreach ( $this as $node ) {
2010-05-06 12:25:53 +01:00
$root -> appendChild ( $document -> importNode ( $node , true ));
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$domxpath = new \DOMXPath ( $document );
2011-04-23 15:25:21 +01:00
return new static ( $domxpath -> query ( $xpath ), $this -> uri );
2010-05-06 12:25:53 +01:00
}
/**
* 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
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function filter ( $selector )
2010-04-15 13:41:42 +01:00
{
2011-06-15 06:52:56 +01:00
if ( ! class_exists ( 'Symfony\\Component\\CssSelector\\CssSelector' )) {
2010-05-06 12:25:53 +01:00
// @codeCoverageIgnoreStart
throw new \RuntimeException ( 'Unable to filter with a CSS selector as the Symfony CssSelector is not installed (you can use filterXPath instead).' );
// @codeCoverageIgnoreEnd
}
2011-06-15 06:52:56 +01:00
return $this -> filterXPath ( CssSelector :: toXPath ( $selector ));
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Selects links by name or alt value for clickable images .
*
2012-04-23 18:19:47 +01:00
* @ param string $value The link text
2010-05-06 12:25:53 +01:00
*
* @ return Crawler A new instance of Crawler with the filtered list of nodes
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function selectLink ( $value )
{
2013-04-22 05:49:51 +01:00
$xpath = sprintf ( '//a[contains(concat(\' \', normalize-space(string(.)), \' \'), %s)] ' , static :: xpathLiteral ( ' ' . $value . ' ' )) .
2010-05-06 12:25:53 +01:00
sprintf ( '| //a/img[contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)]/ancestor::a' , static :: xpathLiteral ( ' ' . $value . ' ' ));
2010-05-14 12:50:08 +01:00
return $this -> filterXPath ( $xpath );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Selects a button by name or alt value for images .
*
2012-04-23 18:19:47 +01:00
* @ param string $value The button text
2010-05-06 12:25:53 +01:00
*
* @ return Crawler A new instance of Crawler with the filtered list of nodes
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function selectButton ( $value )
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$xpath = sprintf ( '//input[((@type="submit" or @type="button") and contains(concat(\' \', normalize-space(string(@value)), \' \'), %s)) ' , static :: xpathLiteral ( ' ' . $value . ' ' )) .
sprintf ( 'or (@type="image" and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)) or @id="%s" or @name="%s"] ' , static :: xpathLiteral ( ' ' . $value . ' ' ), $value , $value ) .
sprintf ( '| //button[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) or @id="%s" or @name="%s"]' , static :: xpathLiteral ( ' ' . $value . ' ' ), $value , $value );
return $this -> filterXPath ( $xpath );
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns a Link object for the first node in the list .
*
2012-04-23 18:19:47 +01:00
* @ param string $method The method for the link ( get by default )
2010-05-06 12:25:53 +01:00
*
2012-11-01 15:08:59 +00:00
* @ return Link A Link instance
2010-05-06 12:25:53 +01:00
*
* @ throws \InvalidArgumentException If the current node list is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function link ( $method = 'get' )
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
$node = $this -> getNode ( 0 );
2011-04-23 15:25:21 +01:00
return new Link ( $node , $this -> uri , $method );
2010-05-06 12:25:53 +01:00
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
/**
* Returns an array of Link objects for the nodes in the list .
*
2012-11-01 15:08:59 +00:00
* @ return Link [] An array of Link instances
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function links ()
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$links = array ();
2010-05-07 15:09:11 +01:00
foreach ( $this as $node ) {
2011-04-23 15:25:21 +01:00
$links [] = new Link ( $node , $this -> uri , 'get' );
2010-05-06 12:25:53 +01:00
}
return $links ;
2010-04-15 13:41:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns a Form object for the first node in the list .
*
2012-04-23 18:19:47 +01:00
* @ param array $values An array of values for the form fields
* @ param string $method The method for the form
2010-05-06 12:25:53 +01:00
*
2012-11-01 15:08:59 +00:00
* @ return Form A Form instance
2010-05-06 12:25:53 +01:00
*
* @ throws \InvalidArgumentException If the current node list is empty
2011-03-24 09:00:10 +00:00
*
* @ api
2010-05-06 12:25:53 +01:00
*/
public function form ( array $values = null , $method = null )
{
2010-05-07 15:09:11 +01:00
if ( ! count ( $this )) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( 'The current node list is empty.' );
}
2011-04-23 15:25:21 +01:00
$form = new Form ( $this -> getNode ( 0 ), $this -> uri , $method );
2010-05-06 12:25:53 +01:00
2010-05-07 15:09:11 +01:00
if ( null !== $values ) {
2010-05-06 12:25:53 +01:00
$form -> setValues ( $values );
}
2010-04-15 13:41:42 +01:00
2010-05-06 12:25:53 +01:00
return $form ;
}
2012-04-23 18:19:47 +01:00
/**
* Converts string for XPath expressions .
*
* Escaped characters are : quotes ( " ) and apostrophe (').
*
* Examples :
* < code >
* echo Crawler :: xpathLiteral ( 'foo " bar' );
* //prints 'foo " bar'
*
* echo Crawler :: xpathLiteral ( " foo ' bar " );
* //prints "foo ' bar"
*
* echo Crawler :: xpathLiteral ( 'a\'b"c' );
* //prints concat('a', "'", 'b"c')
* </ code >
*
* @ param string $s String to be escaped
*
* @ return string Converted string
*/
2012-07-09 13:50:58 +01:00
public static function xpathLiteral ( $s )
2011-03-12 12:33:07 +00:00
{
if ( false === strpos ( $s , " ' " )) {
return sprintf ( " '%s' " , $s );
}
if ( false === strpos ( $s , '"' )) {
return sprintf ( '"%s"' , $s );
}
$string = $s ;
$parts = array ();
while ( true ) {
if ( false !== $pos = strpos ( $string , " ' " )) {
$parts [] = sprintf ( " '%s' " , substr ( $string , 0 , $pos ));
$parts [] = " \" ' \" " ;
$string = substr ( $string , $pos + 1 );
} else {
$parts [] = " ' $string ' " ;
break ;
}
}
return sprintf ( " concat(%s) " , implode ( $parts , ', ' ));
}
2013-09-13 12:38:25 +01:00
/**
* @ param integer $position
*
* @ return \DOMElement | null
*/
2013-02-28 10:23:15 +00:00
protected function getNode ( $position )
2010-04-15 13:41:42 +01:00
{
2010-05-07 15:09:11 +01:00
foreach ( $this as $i => $node ) {
2010-05-08 14:32:30 +01:00
if ( $i == $position ) {
2010-05-06 12:25:53 +01:00
return $node ;
}
// @codeCoverageIgnoreStart
}
return null ;
// @codeCoverageIgnoreEnd
2010-04-15 13:41:42 +01:00
}
2013-09-13 12:38:25 +01:00
/**
* @ param \DOMElement $node
* @ param string $siblingDir
*
* @ return array
*/
2013-02-28 10:23:15 +00:00
protected function sibling ( $node , $siblingDir = 'nextSibling' )
2010-04-15 13:41:42 +01:00
{
2010-05-06 12:25:53 +01:00
$nodes = array ();
2010-05-07 15:09:11 +01:00
do {
2010-05-08 14:32:30 +01:00
if ( $node !== $this -> getNode ( 0 ) && $node -> nodeType === 1 ) {
2010-05-06 12:25:53 +01:00
$nodes [] = $node ;
}
2011-02-27 19:56:29 +00:00
} while ( $node = $node -> $siblingDir );
2010-05-06 12:25:53 +01:00
return $nodes ;
2010-04-15 13:41:42 +01:00
}
}