Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Dom / Css2Xpath.php
blob0b3a7752b530baa07e27e852bb6de90b4746125c
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Dom;
12 /**
13 * Transform CSS selectors to XPath
15 class Css2Xpath
17 /**
18 * Transform CSS expression to XPath
20 * @param string $path
21 * @return string
23 public static function transform($path)
25 $path = (string) $path;
26 if (strstr($path, ',')) {
27 $paths = explode(',', $path);
28 $expressions = array();
29 foreach ($paths as $path) {
30 $xpath = self::transform(trim($path));
31 if (is_string($xpath)) {
32 $expressions[] = $xpath;
33 } elseif (is_array($xpath)) {
34 $expressions = array_merge($expressions, $xpath);
37 return implode('|', $expressions);
40 $paths = array('//');
41 $path = preg_replace('|\s+>\s+|', '>', $path);
42 $segments = preg_split('/\s+/', $path);
43 foreach ($segments as $key => $segment) {
44 $pathSegment = static::_tokenize($segment);
45 if (0 == $key) {
46 if (0 === strpos($pathSegment, '[contains(')) {
47 $paths[0] .= '*' . ltrim($pathSegment, '*');
48 } else {
49 $paths[0] .= $pathSegment;
51 continue;
53 if (0 === strpos($pathSegment, '[contains(')) {
54 foreach ($paths as $pathKey => $xpath) {
55 $paths[$pathKey] .= '//*' . ltrim($pathSegment, '*');
56 $paths[] = $xpath . $pathSegment;
58 } else {
59 foreach ($paths as $pathKey => $xpath) {
60 $paths[$pathKey] .= '//' . $pathSegment;
65 if (1 == count($paths)) {
66 return $paths[0];
68 return implode('|', $paths);
71 /**
72 * Tokenize CSS expressions to XPath
74 * @param string $expression
75 * @return string
77 protected static function _tokenize($expression)
79 // Child selectors
80 $expression = str_replace('>', '/', $expression);
82 // IDs
83 $expression = preg_replace('|#([a-z][a-z0-9_-]*)|i', '[@id=\'$1\']', $expression);
84 $expression = preg_replace('|(?<![a-z0-9_-])(\[@id=)|i', '*$1', $expression);
86 // arbitrary attribute strict equality
87 $expression = preg_replace_callback(
88 '|\[@?([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i',
89 function ($matches) {
90 return '[@' . strtolower($matches[1]) . "='" . $matches[2] . "']";
92 $expression
95 // arbitrary attribute contains full word
96 $expression = preg_replace_callback(
97 '|\[([a-z0-9_-]+)~=[\'"]([^\'"]+)[\'"]\]|i',
98 function ($matches) {
99 return "[contains(concat(' ', normalize-space(@" . strtolower($matches[1]) . "), ' '), ' "
100 . $matches[2] . " ')]";
102 $expression
105 // arbitrary attribute contains specified content
106 $expression = preg_replace_callback(
107 '|\[([a-z0-9_-]+)\*=[\'"]([^\'"]+)[\'"]\]|i',
108 function ($matches) {
109 return "[contains(@" . strtolower($matches[1]) . ", '"
110 . $matches[2] . "')]";
112 $expression
115 // Classes
116 if(false === strpos($expression, "[@")) {
117 $expression = preg_replace(
118 '|\.([a-z][a-z0-9_-]*)|i',
119 "[contains(concat(' ', normalize-space(@class), ' '), ' \$1 ')]",
120 $expression
124 /** ZF-9764 -- remove double asterisk */
125 $expression = str_replace('**', '*', $expression);
127 return $expression;