PageRenderTime 68ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Dom/Query/Css2Xpath.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 169 lines | 91 code | 14 blank | 64 comment | 10 complexity | 436b17459f82643919c1d93c75b4106f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dom
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Transform CSS selectors to XPath
  22. *
  23. * @package Zend_Dom
  24. * @subpackage Query
  25. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. * @version $Id: Css2Xpath.php 22045 2010-04-28 19:59:49Z matthew $
  28. */
  29. class Zend_Dom_Query_Css2Xpath
  30. {
  31. /**
  32. * Transform CSS expression to XPath
  33. *
  34. * @param string $path
  35. * @return string
  36. */
  37. public static function transform($path)
  38. {
  39. $path = (string) $path;
  40. if (strstr($path, ',')) {
  41. $paths = explode(',', $path);
  42. $expressions = array();
  43. foreach ($paths as $path) {
  44. $xpath = self::transform(trim($path));
  45. if (is_string($xpath)) {
  46. $expressions[] = $xpath;
  47. } elseif (is_array($xpath)) {
  48. $expressions = array_merge($expressions, $xpath);
  49. }
  50. }
  51. return implode('|', $expressions);
  52. }
  53. $paths = array('//');
  54. $path = preg_replace('|\s+>\s+|', '>', $path);
  55. $segments = preg_split('/\s+/', $path);
  56. foreach ($segments as $key => $segment) {
  57. $pathSegment = self::_tokenize($segment);
  58. if (0 == $key) {
  59. if (0 === strpos($pathSegment, '[contains(')) {
  60. $paths[0] .= '*' . ltrim($pathSegment, '*');
  61. } else {
  62. $paths[0] .= $pathSegment;
  63. }
  64. continue;
  65. }
  66. if (0 === strpos($pathSegment, '[contains(')) {
  67. foreach ($paths as $key => $xpath) {
  68. $paths[$key] .= '//*' . ltrim($pathSegment, '*');
  69. $paths[] = $xpath . $pathSegment;
  70. }
  71. } else {
  72. foreach ($paths as $key => $xpath) {
  73. $paths[$key] .= '//' . $pathSegment;
  74. }
  75. }
  76. }
  77. if (1 == count($paths)) {
  78. return $paths[0];
  79. }
  80. return implode('|', $paths);
  81. }
  82. /**
  83. * Tokenize CSS expressions to XPath
  84. *
  85. * @param string $expression
  86. * @return string
  87. */
  88. protected static function _tokenize($expression)
  89. {
  90. // Child selectors
  91. $expression = str_replace('>', '/', $expression);
  92. // IDs
  93. $expression = preg_replace('|#([a-z][a-z0-9_-]*)|i', '[@id=\'$1\']', $expression);
  94. $expression = preg_replace('|(?<![a-z0-9_-])(\[@id=)|i', '*$1', $expression);
  95. // arbitrary attribute strict equality
  96. $expression = preg_replace_callback(
  97. '|\[([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i',
  98. array(__CLASS__, '_createEqualityExpression'),
  99. $expression
  100. );
  101. // arbitrary attribute contains full word
  102. $expression = preg_replace_callback(
  103. '|\[([a-z0-9_-]+)~=[\'"]([^\'"]+)[\'"]\]|i',
  104. array(__CLASS__, '_normalizeSpaceAttribute'),
  105. $expression
  106. );
  107. // arbitrary attribute contains specified content
  108. $expression = preg_replace_callback(
  109. '|\[([a-z0-9_-]+)\*=[\'"]([^\'"]+)[\'"]\]|i',
  110. array(__CLASS__, '_createContainsExpression'),
  111. $expression
  112. );
  113. // Classes
  114. $expression = preg_replace(
  115. '|\.([a-z][a-z0-9_-]*)|i',
  116. "[contains(concat(' ', normalize-space(@class), ' '), ' \$1 ')]",
  117. $expression
  118. );
  119. /** ZF-9764 -- remove double asterix */
  120. $expression = str_replace('**', '*', $expression);
  121. return $expression;
  122. }
  123. /**
  124. * Callback for creating equality expressions
  125. *
  126. * @param array $matches
  127. * @return string
  128. */
  129. protected static function _createEqualityExpression($matches)
  130. {
  131. return '[@' . strtolower($matches[1]) . "='" . $matches[2] . "']";
  132. }
  133. /**
  134. * Callback for creating expressions to match one or more attribute values
  135. *
  136. * @param array $matches
  137. * @return string
  138. */
  139. protected static function _normalizeSpaceAttribute($matches)
  140. {
  141. return "[contains(concat(' ', normalize-space(@" . strtolower($matches[1]) . "), ' '), ' "
  142. . $matches[2] . " ')]";
  143. }
  144. /**
  145. * Callback for creating a strict "contains" expression
  146. *
  147. * @param array $matches
  148. * @return string
  149. */
  150. protected static function _createContainsExpression($matches)
  151. {
  152. return "[contains(@" . strtolower($matches[1]) . ", '"
  153. . $matches[2] . "')]";
  154. }
  155. }