Drupal investigation

TokenizerEscaping.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Parser\Tokenizer;
  11. /**
  12. * CSS selector tokenizer escaping applier.
  13. *
  14. * This component is a port of the Python cssselect library,
  15. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  16. *
  17. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  18. *
  19. * @internal
  20. */
  21. class TokenizerEscaping
  22. {
  23. /**
  24. * @var TokenizerPatterns
  25. */
  26. private $patterns;
  27. /**
  28. * @param TokenizerPatterns $patterns
  29. */
  30. public function __construct(TokenizerPatterns $patterns)
  31. {
  32. $this->patterns = $patterns;
  33. }
  34. /**
  35. * @param string $value
  36. *
  37. * @return string
  38. */
  39. public function escapeUnicode($value)
  40. {
  41. $value = $this->replaceUnicodeSequences($value);
  42. return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value);
  43. }
  44. /**
  45. * @param string $value
  46. *
  47. * @return string
  48. */
  49. public function escapeUnicodeAndNewLine($value)
  50. {
  51. $value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value);
  52. return $this->escapeUnicode($value);
  53. }
  54. /**
  55. * @param string $value
  56. *
  57. * @return string
  58. */
  59. private function replaceUnicodeSequences($value)
  60. {
  61. return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) {
  62. $c = hexdec($match[1]);
  63. if (0x80 > $c %= 0x200000) {
  64. return chr($c);
  65. }
  66. if (0x800 > $c) {
  67. return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
  68. }
  69. if (0x10000 > $c) {
  70. return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
  71. }
  72. }, $value);
  73. }
  74. }