4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\Parser\Tokenizer;
15 * CSS selector tokenizer escaping applier.
17 * This component is a port of the Python cssselect library,
18 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
20 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
24 class TokenizerEscaping
27 * @var TokenizerPatterns
32 * @param TokenizerPatterns $patterns
34 public function __construct(TokenizerPatterns $patterns)
36 $this->patterns = $patterns;
40 * @param string $value
44 public function escapeUnicode($value)
46 $value = $this->replaceUnicodeSequences($value);
48 return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value);
52 * @param string $value
56 public function escapeUnicodeAndNewLine($value)
58 $value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value);
60 return $this->escapeUnicode($value);
64 * @param string $value
68 private function replaceUnicodeSequences($value)
70 return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) {
71 $c = hexdec($match[1]);
73 if (0x80 > $c %= 0x200000) {
77 return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
80 return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);