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

Commit

Permalink
Merge branch 'master' of https://github.com/zendframework/zf2 into ad…
Browse files Browse the repository at this point in the history
…d_maxlength_to_param_container
  • Loading branch information
Show file tree
Hide file tree
Showing 16 changed files with 1,190 additions and 560 deletions.
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
}
},
"require": {
"php": ">=5.3.3",
"zendframework/zend-stdlib": "self.version"
"php": ">=5.3.23"
},
"extra": {
"branch-alias": {
"dev-master": "2.2-dev",
"dev-develop": "2.3-dev"
"dev-master": "2.3-dev",
"dev-develop": "2.4-dev"
}
},
"autoload-dev": {
Expand Down
110 changes: 8 additions & 102 deletions src/Css2Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,31 @@
* 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)
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Dom;

/**
* Transform CSS selectors to XPath
*
* @deprecated
* @see Document\Query
*/
class Css2Xpath
{
/**
* Transform CSS expression to XPath
*
* @deprecated
* @see Document\Query
* @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 = static::_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 $pathKey => $xpath) {
$paths[$pathKey] .= '//*' . ltrim($pathSegment, '*');
$paths[] = $xpath . $pathSegment;
}
} else {
foreach ($paths as $pathKey => $xpath) {
$paths[$pathKey] .= '//' . $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',
function ($matches) {
return '[@' . strtolower($matches[1]) . "='" . $matches[2] . "']";
},
$expression
);

// arbitrary attribute contains full word
$expression = preg_replace_callback(
'|\[([a-z0-9_-]+)~=[\'"]([^\'"]+)[\'"]\]|i',
function ($matches) {
return "[contains(concat(' ', normalize-space(@" . strtolower($matches[1]) . "), ' '), ' "
. $matches[2] . " ')]";
},
$expression
);

// arbitrary attribute contains specified content
$expression = preg_replace_callback(
'|\[([a-z0-9_-]+)\*=[\'"]([^\'"]+)[\'"]\]|i',
function ($matches) {
return "[contains(@" . strtolower($matches[1]) . ", '"
. $matches[2] . "')]";
},
$expression
);

// Classes
$expression = preg_replace(
'|\.([a-z][a-z0-9_-]*)|i',
"[contains(concat(' ', normalize-space(@class), ' '), ' \$1 ')]",
$expression
);

/** ZF-9764 -- remove double asterisk */
$expression = str_replace('**', '*', $expression);

return $expression;
trigger_error(sprintf('%s is deprecated; please use %s\Document\Query::cssToXpath instead', __METHOD__, __NAMESPACE__), E_USER_DEPRECATED);
return Document\Query::cssToXpath($path);
}
}
70 changes: 70 additions & 0 deletions src/DOMXPath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Dom;

use ErrorException;

/**
* Extends DOMXpath to throw ErrorExceptions instead of raising errors.
*/
class DOMXPath extends \DOMXPath
{
/**
* A stack of ErrorExceptions created via addError()
*
* @var array
*/
protected $errors = array(null);

/**
* Evaluates an XPath expression; throws an ErrorException instead of
* raising an error
*
* @param string $expression The XPath expression to evaluate.
* @return \DOMNodeList
* @throws ErrorException
*/
public function queryWithErrorException($expression)
{
$this->errors = array(null);

set_error_handler(array($this, 'addError'), \E_WARNING);
$nodeList = $this->query($expression);
restore_error_handler();

$exception = array_pop($this->errors);
if ($exception) {
throw $exception;
}

return $nodeList;
}

/**
* Adds an error to the stack of errors
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
*/
public function addError($errno, $errstr = '', $errfile = '', $errline = 0)
{
$last_error = end($this->errors);
$this->errors[] = new ErrorException(
$errstr,
0,
$errno,
$errfile,
$errline,
$last_error
);
}
}
Loading

0 comments on commit c825927

Please sign in to comment.