CssSelector.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector;
  11. @trigger_error('The '.__NAMESPACE__.'\CssSelector class is deprecated since version 2.8 and will be removed in 3.0. Use directly the \Symfony\Component\CssSelector\CssSelectorConverter class instead.', E_USER_DEPRECATED);
  12. /**
  13. * CssSelector is the main entry point of the component and can convert CSS
  14. * selectors to XPath expressions.
  15. *
  16. * $xpath = CssSelector::toXpath('h1.foo');
  17. *
  18. * This component is a port of the Python cssselect library,
  19. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  20. *
  21. * Copyright (c) 2007-2012 Ian Bicking and contributors. See AUTHORS
  22. * for more details.
  23. *
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions are
  28. * met:
  29. *
  30. * 1. Redistributions of source code must retain the above copyright
  31. * notice, this list of conditions and the following disclaimer.
  32. *
  33. * 2. Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in
  35. * the documentation and/or other materials provided with the
  36. * distribution.
  37. *
  38. * 3. Neither the name of Ian Bicking nor the names of its contributors may
  39. * be used to endorse or promote products derived from this software
  40. * without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  43. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  44. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  45. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IAN BICKING OR
  46. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  47. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  48. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  49. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  50. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  51. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  52. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. *
  54. * @author Fabien Potencier <fabien@symfony.com>
  55. *
  56. * @deprecated as of 2.8, will be removed in 3.0. Use the \Symfony\Component\CssSelector\CssSelectorConverter class instead.
  57. */
  58. class CssSelector
  59. {
  60. private static $html = true;
  61. /**
  62. * Translates a CSS expression to its XPath equivalent.
  63. * Optionally, a prefix can be added to the resulting XPath
  64. * expression with the $prefix parameter.
  65. *
  66. * @param mixed $cssExpr The CSS expression.
  67. * @param string $prefix An optional prefix for the XPath expression.
  68. *
  69. * @return string
  70. */
  71. public static function toXPath($cssExpr, $prefix = 'descendant-or-self::')
  72. {
  73. $converter = new CssSelectorConverter(self::$html);
  74. return $converter->toXPath($cssExpr, $prefix);
  75. }
  76. /**
  77. * Enables the HTML extension.
  78. */
  79. public static function enableHtmlExtension()
  80. {
  81. self::$html = true;
  82. }
  83. /**
  84. * Disables the HTML extension.
  85. */
  86. public static function disableHtmlExtension()
  87. {
  88. self::$html = false;
  89. }
  90. }