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

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
Promote develop to master, becoming the next stable release.
  • Loading branch information
weierophinney committed Mar 12, 2014
148 parents 83a8226 + 52a7c3f + a6e83c4 + 1ddadb6 + 2a36660 + b606864 + 41467b4 + b11a11a + de9f81e + 5737317 + c7cdc55 + a50e2ae + 80fc529 + ba58ad8 + a20b034 + f3031d6 + f095b61 + ee7be4f + 5cd5b6f + 560bbb2 + 211c08a + 164a230 + 1fa8900 + 2b3577e + 495bb6c + ab5c324 + 2e456c8 + 63bff97 + f7e139b + 3cb454e + 22f9181 + 93a693f + 2a458de + 1c142d8 + 7f95140 + be3c33f + 7ab0a73 + 8cb2a33 + 56ac953 + 4157e43 + 0ec8c93 + 4bea232 + 26b2403 + 11d87a1 + fb85298 + 6bda27e + b73658e + 1020b71 + 58956c7 + 4de672f + 79fe77b + 7624c04 + cfbc839 + 49e4d7c + ff81391 + 21cca29 + f0671be + 7c4ff71 + 98d530a + 8d88cc5 + 390535a + 00e3a5a + 1debe09 + 90714d6 + b60de46 + 239a3c3 + 623bc45 + caef534 + 2df5656 + 43268cd + a75eec7 + 2567e8f + ea9335d + 464392f + 7fd0ba3 + 1e64d87 + c8ba509 + ca73950 + c8dc1c3 + bb4f382 + c7e8658 + 7182b57 + 7681cd3 + 9e9880e + e68c740 + a5f5145 + e24bcf7 + 0cf2624 + 74f8e99 + 9dc42d7 + 6918467 + 75ec452 + 7032642 + 79c38ce + 995e0a7 + bb2d896 + 1b246ad + b74beb0 + d10206d + 242a79a + 8a250b9 + 537a949 + 9269d97 + 2698f19 + 4cf6c67 + 5866634 + 6ef9027 + af148c0 + e8a3d3b + 769eee2 + db12820 + 0b7828e + 1da1921 + 9643cb4 + c09dfeb + 831c808 + 346e46a + 09df463 + a6b3a5c + 81271a1 + a27453a + 64fe065 + 042e1cd + abd6687 + 0dcab85 + b47ae92 + 630648b + 3a97e95 + 1a3b527 + 53e423c + 854a12d + accad73 + 4a1dcdf + 702a810 + 27b5402 + 2e72915 + 5418731 + 05f12a3 + e88fa1b + 75453b8 + daca924 + 406cf1d + 0581343 + 6dbf09b + 47fd133 + 6a7e413 + 0c8699d + eec62e5 commit 11755e5
Show file tree
Hide file tree
Showing 12 changed files with 1,170 additions and 554 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
}
},
"require": {
"php": ">=5.3.3",
"zendframework/zend-stdlib": "self.version"
"php": ">=5.3.23"
},
"extra": {
"branch-alias": {
Expand Down
110 changes: 7 additions & 103 deletions src/Css2Xpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,119 +11,23 @@

/**
* 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
if(false === strpos($expression, "[@")) {
$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 11755e5

Please sign in to comment.