Dieser Commit ist enthalten in:
Ursprung
2b21070b1a
Commit
f7a7c71f86
1583 geänderte Dateien mit 454759 neuen und 0 gelöschten Zeilen
169
library/Zend/Dom/Query/Css2Xpath.php
Normale Datei
169
library/Zend/Dom/Query/Css2Xpath.php
Normale Datei
|
|
@ -0,0 +1,169 @@
|
|||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Dom
|
||||
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Transform CSS selectors to XPath
|
||||
*
|
||||
* @package Zend_Dom
|
||||
* @subpackage Query
|
||||
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
class Zend_Dom_Query_Css2Xpath
|
||||
{
|
||||
/**
|
||||
* Transform CSS expression to XPath
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function transform($path)
|
||||
{
|
||||
$path = (string) $path;
|
||||
if (strstr($path, ',')) {
|
||||
$paths = explode(',', $path);
|
||||
$expressions = array();
|
||||
foreach ($paths as $path) {
|
||||
$xpath = self::transform(trim($path));
|
||||
if (is_string($xpath)) {
|
||||
$expressions[] = $xpath;
|
||||
} elseif (is_array($xpath)) {
|
||||
$expressions = array_merge($expressions, $xpath);
|
||||
}
|
||||
}
|
||||
return implode('|', $expressions);
|
||||
}
|
||||
|
||||
$paths = array('//');
|
||||
$path = preg_replace('|\s+>\s+|', '>', $path);
|
||||
$segments = preg_split('/\s+/', $path);
|
||||
foreach ($segments as $key => $segment) {
|
||||
$pathSegment = self::_tokenize($segment);
|
||||
if (0 == $key) {
|
||||
if (0 === strpos($pathSegment, '[contains(')) {
|
||||
$paths[0] .= '*' . ltrim($pathSegment, '*');
|
||||
} else {
|
||||
$paths[0] .= $pathSegment;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (0 === strpos($pathSegment, '[contains(')) {
|
||||
foreach ($paths as $key => $xpath) {
|
||||
$paths[$key] .= '//*' . ltrim($pathSegment, '*');
|
||||
$paths[] = $xpath . $pathSegment;
|
||||
}
|
||||
} else {
|
||||
foreach ($paths as $key => $xpath) {
|
||||
$paths[$key] .= '//' . $pathSegment;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (1 == count($paths)) {
|
||||
return $paths[0];
|
||||
}
|
||||
return implode('|', $paths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize CSS expressions to XPath
|
||||
*
|
||||
* @param string $expression
|
||||
* @return string
|
||||
*/
|
||||
protected static function _tokenize($expression)
|
||||
{
|
||||
// Child selectors
|
||||
$expression = str_replace('>', '/', $expression);
|
||||
|
||||
// IDs
|
||||
$expression = preg_replace('|#([a-z][a-z0-9_-]*)|i', '[@id=\'$1\']', $expression);
|
||||
$expression = preg_replace('|(?<![a-z0-9_-])(\[@id=)|i', '*$1', $expression);
|
||||
|
||||
// arbitrary attribute strict equality
|
||||
$expression = preg_replace_callback(
|
||||
'|\[([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i',
|
||||
array(__CLASS__, '_createEqualityExpression'),
|
||||
$expression
|
||||
);
|
||||
|
||||
// arbitrary attribute contains full word
|
||||
$expression = preg_replace_callback(
|
||||
'|\[([a-z0-9_-]+)~=[\'"]([^\'"]+)[\'"]\]|i',
|
||||
array(__CLASS__, '_normalizeSpaceAttribute'),
|
||||
$expression
|
||||
);
|
||||
|
||||
// arbitrary attribute contains specified content
|
||||
$expression = preg_replace_callback(
|
||||
'|\[([a-z0-9_-]+)\*=[\'"]([^\'"]+)[\'"]\]|i',
|
||||
array(__CLASS__, '_createContainsExpression'),
|
||||
$expression
|
||||
);
|
||||
|
||||
// Classes
|
||||
$expression = preg_replace(
|
||||
'|\.([a-z][a-z0-9_-]*)|i',
|
||||
"[contains(concat(' ', normalize-space(@class), ' '), ' \$1 ')]",
|
||||
$expression
|
||||
);
|
||||
|
||||
/** ZF-9764 -- remove double asterix */
|
||||
$expression = str_replace('**', '*', $expression);
|
||||
|
||||
return $expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for creating equality expressions
|
||||
*
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
protected static function _createEqualityExpression($matches)
|
||||
{
|
||||
return '[@' . strtolower($matches[1]) . "='" . $matches[2] . "']";
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for creating expressions to match one or more attribute values
|
||||
*
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
protected static function _normalizeSpaceAttribute($matches)
|
||||
{
|
||||
return "[contains(concat(' ', normalize-space(@" . strtolower($matches[1]) . "), ' '), ' "
|
||||
. $matches[2] . " ')]";
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for creating a strict "contains" expression
|
||||
*
|
||||
* @param array $matches
|
||||
* @return string
|
||||
*/
|
||||
protected static function _createContainsExpression($matches)
|
||||
{
|
||||
return "[contains(@" . strtolower($matches[1]) . ", '"
|
||||
. $matches[2] . "')]";
|
||||
}
|
||||
}
|
||||
183
library/Zend/Dom/Query/Result.php
Normale Datei
183
library/Zend/Dom/Query/Result.php
Normale Datei
|
|
@ -0,0 +1,183 @@
|
|||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Dom
|
||||
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/**
|
||||
* Results for DOM XPath query
|
||||
*
|
||||
* @package Zend_Dom
|
||||
* @subpackage Query
|
||||
* @uses Iterator
|
||||
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
class Zend_Dom_Query_Result implements Iterator,Countable
|
||||
{
|
||||
/**
|
||||
* Number of results
|
||||
* @var int
|
||||
*/
|
||||
protected $_count;
|
||||
|
||||
/**
|
||||
* CSS Selector query
|
||||
* @var string
|
||||
*/
|
||||
protected $_cssQuery;
|
||||
|
||||
/**
|
||||
* @var DOMDocument
|
||||
*/
|
||||
protected $_document;
|
||||
|
||||
/**
|
||||
* @var DOMNodeList
|
||||
*/
|
||||
protected $_nodeList;
|
||||
|
||||
/**
|
||||
* Current iterator position
|
||||
* @var int
|
||||
*/
|
||||
protected $_position = 0;
|
||||
|
||||
/**
|
||||
* @var DOMXPath
|
||||
*/
|
||||
protected $_xpath;
|
||||
|
||||
/**
|
||||
* XPath query
|
||||
* @var string
|
||||
*/
|
||||
protected $_xpathQuery;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $cssQuery
|
||||
* @param string|array $xpathQuery
|
||||
* @param DOMDocument $document
|
||||
* @param DOMNodeList $nodeList
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($cssQuery, $xpathQuery, DOMDocument $document, DOMNodeList $nodeList)
|
||||
{
|
||||
$this->_cssQuery = $cssQuery;
|
||||
$this->_xpathQuery = $xpathQuery;
|
||||
$this->_document = $document;
|
||||
$this->_nodeList = $nodeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve CSS Query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCssQuery()
|
||||
{
|
||||
return $this->_cssQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve XPath query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getXpathQuery()
|
||||
{
|
||||
return $this->_xpathQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve DOMDocument
|
||||
*
|
||||
* @return DOMDocument
|
||||
*/
|
||||
public function getDocument()
|
||||
{
|
||||
return $this->_document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: rewind to first element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->_position = 0;
|
||||
return $this->_nodeList->item(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: is current position valid?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
if (in_array($this->_position, range(0, $this->_nodeList->length - 1)) && $this->_nodeList->length > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: return current element
|
||||
*
|
||||
* @return DOMElement
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->_nodeList->item($this->_position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: return key of current element
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->_position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator: move to next element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
++$this->_position;
|
||||
return $this->_nodeList->item($this->_position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Countable: get count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return $this->_nodeList->length;
|
||||
}
|
||||
}
|
||||
Laden …
Tabelle hinzufügen
Einen Link hinzufügen
In neuem Issue referenzieren