Css2Xpath.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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$
  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(@class')) {
  60. $paths[0] .= '*' . $pathSegment;
  61. } else {
  62. $paths[0] .= $pathSegment;
  63. }
  64. continue;
  65. }
  66. if (0 === strpos($pathSegment, '[contains(@class')) {
  67. foreach ($paths as $key => $xpath) {
  68. $paths[$key] .= '//*' . $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. if (preg_match('|([a-z]+)\[([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i', $expression)) {
  97. $expression = preg_replace_callback(
  98. '|([a-z]+)\[([a-z0-9_-]+)=[\'"]([^\'"]+)[\'"]\]|i',
  99. create_function(
  100. '$matches',
  101. 'return $matches[1] . "[@" . strtolower($matches[2]) . "=\'" . $matches[3] . "\']";'
  102. ),
  103. $expression
  104. );
  105. }
  106. // arbitrary attribute contains full word
  107. if (preg_match('|([a-z]+)\[([a-z0-9_-]+)~=[\'"]([^\'"]+)[\'"]\]|i', $expression)) {
  108. $expression = preg_replace_callback(
  109. '|([a-z]+)\[([a-z0-9_-]+)~=[\'"]([^\'"]+)[\'"]\]|i',
  110. create_function(
  111. '$matches',
  112. 'return $matches[1] . "[contains(@" . strtolower($matches[2]) . ", \' $matches[3] \')]";'
  113. ),
  114. $expression
  115. );
  116. }
  117. // arbitrary attribute contains specified content
  118. if (preg_match('|([a-z]+)\[([a-z0-9_-]+)\*=[\'"]([^\'"]+)[\'"]\]|i', $expression)) {
  119. $expression = preg_replace_callback(
  120. '|([a-z]+)\[([a-z0-9_-]+)\*=[\'"]([^\'"]+)[\'"]\]|i',
  121. create_function(
  122. '$matches',
  123. 'return $matches[1] . "[contains(@" . strtolower($matches[2]) . ", \'" . $matches[3] . "\')]";'
  124. ),
  125. $expression
  126. );
  127. }
  128. // Classes
  129. $expression = preg_replace('|\.([a-z][a-z0-9_-]*)|i', "[contains(@class, ' \$1 ')]", $expression);
  130. return $expression;
  131. }
  132. }