Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 37 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
}
},
"require": {
"php": ">=5.3.3"
"php": ">=5.3.3",
"zendframework/zend-stdlib": "self.version"
},
"extra": {
"branch-alias": {
"dev-master": "2.4-dev",
"dev-develop": "2.5-dev"
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
}
},
"autoload-dev": {
Expand Down
6 changes: 1 addition & 5 deletions src/Css2Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/

namespace Zend\Dom;

/**
* Transform CSS selectors to XPath
*
* @package Zend_Dom
* @subpackage Query
*/
class Css2Xpath
{
Expand Down
17 changes: 17 additions & 0 deletions src/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Dom\Exception;

/**
* Zend_Dom Exceptions
*/
class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
{
}
6 changes: 1 addition & 5 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/

namespace Zend\Dom\Exception;

/**
* Zend_Dom Exceptions
*
* @category Zend
* @package Zend_Dom
*/
interface ExceptionInterface
{
Expand Down
6 changes: 1 addition & 5 deletions src/Exception/RuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/

namespace Zend\Dom\Exception;

/**
* Zend_Dom Exceptions
*
* @category Zend
* @package Zend_Dom
*/
class RuntimeException extends \RuntimeException implements ExceptionInterface
{
Expand Down
66 changes: 55 additions & 11 deletions src/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/

namespace Zend\Dom;

use ArrayAccess;
use Countable;
use DOMDocument;
use DOMNodeList;
Expand All @@ -18,11 +18,8 @@

/**
* Nodelist for DOM XPath query
*
* @package Zend_Dom
* @subpackage Query
*/
class NodeList implements Iterator, Countable
class NodeList implements Iterator, Countable, ArrayAccess
{
/**
* CSS Selector query
Expand Down Expand Up @@ -55,11 +52,10 @@ class NodeList implements Iterator, Countable
/**
* Constructor
*
* @param string $cssQuery
* @param string|array $xpathQuery
* @param DOMDocument $document
* @param DOMNodeList $nodeList
* @return void
* @param string $cssQuery
* @param string|array $xpathQuery
* @param DOMDocument $document
* @param DOMNodeList $nodeList
*/
public function __construct($cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList)
{
Expand Down Expand Up @@ -166,4 +162,52 @@ public function count()
{
return $this->nodeList->length;
}

/**
* ArrayAccess: offset exists
*
* @param int $key
* @return bool
*/
public function offsetExists($key)
{
if (in_array($key, range(0, $this->nodeList->length - 1)) && $this->nodeList->length > 0) {
return true;
}
return false;
}

/**
* ArrayAccess: get offset
*
* @param int $key
* @return mixed
*/
public function offsetGet($key)
{
return $this->nodeList->item($key);
}

/**
* ArrayAccess: set offset
*
* @param mixed $key
* @param mixed $value
* @throws Exception\BadMethodCallException when attemptingn to write to a read-only item
*/
public function offsetSet($key, $value)
{
throw new Exception\BadMethodCallException('Attempting to write to a read-only list');
}

/**
* ArrayAccess: unset offset
*
* @param mixed $key
* @throws Exception\BadMethodCallException when attemptingn to unset a read-only item
*/
public function offsetUnset($key)
{
throw new Exception\BadMethodCallException('Attempting to unset on a read-only list');
}
}
15 changes: 10 additions & 5 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/

namespace Zend\Dom;

use DOMDocument;
use DOMXPath;
use Zend\Stdlib\ErrorHandler;

/**
* Query DOM structures based on CSS selectors and/or XPath
*
* @package Zend_Dom
*/
class Query
{
Expand Down Expand Up @@ -314,6 +312,13 @@ protected function getNodeList($document, $xpathQuery)
: $xpath->registerPHPFunctions($this->xpathPhpFunctions);
}
$xpathQuery = (string) $xpathQuery;
return $xpath->query($xpathQuery);

ErrorHandler::start();
$nodeList = $xpath->query($xpathQuery);
$error = ErrorHandler::stop();
if ($error) {
throw $error;
}
return $nodeList;
}
}
2 changes: 1 addition & 1 deletion test/Css2XpathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/
Expand Down
2 changes: 1 addition & 1 deletion test/NodeListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/
Expand Down
46 changes: 45 additions & 1 deletion test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Dom
*/
Expand Down Expand Up @@ -356,4 +356,48 @@ public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttac
$this->setExpectedException("\Zend\Dom\Exception\RuntimeException");
$this->query->queryXpath('/');
}

public function testOffsetExists()
{
$this->loadHtml();
$results = $this->query->execute('input');

$this->assertEquals(3, $results->count());
$this->assertFalse($results->offsetExists(3));
$this->assertTrue($results->offsetExists(2));
}

public function testOffsetGet()
{
$this->loadHtml();
$results = $this->query->execute('input');

$this->assertEquals(3, $results->count());
$this->assertEquals('login', $results[2]->getAttribute('id'));
}

/**
* @expectedException Zend\Dom\Exception\BadMethodCallException
*/
public function testOffsetSet()
{
$this->loadHtml();
$results = $this->query->execute('input');
$this->assertEquals(3, $results->count());

$results[0] = '<foobar />';
}


/**
* @expectedException Zend\Dom\Exception\BadMethodCallException
*/
public function testOffsetUnset()
{
$this->loadHtml();
$results = $this->query->execute('input');
$this->assertEquals(3, $results->count());

unset($results[2]);
}
}

0 comments on commit accad73

Please sign in to comment.