c6f289aa11439355c66ba431a4f0265330c2810c
[yaffs-website] / vendor / symfony / css-selector / Parser / Tokenizer / TokenizerEscaping.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\CssSelector\Parser\Tokenizer;
13
14 /**
15  * CSS selector tokenizer escaping applier.
16  *
17  * This component is a port of the Python cssselect library,
18  * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
19  *
20  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
21  *
22  * @internal
23  */
24 class TokenizerEscaping
25 {
26     private $patterns;
27
28     public function __construct(TokenizerPatterns $patterns)
29     {
30         $this->patterns = $patterns;
31     }
32
33     /**
34      * @param string $value
35      *
36      * @return string
37      */
38     public function escapeUnicode($value)
39     {
40         $value = $this->replaceUnicodeSequences($value);
41
42         return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value);
43     }
44
45     /**
46      * @param string $value
47      *
48      * @return string
49      */
50     public function escapeUnicodeAndNewLine($value)
51     {
52         $value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value);
53
54         return $this->escapeUnicode($value);
55     }
56
57     /**
58      * @param string $value
59      *
60      * @return string
61      */
62     private function replaceUnicodeSequences($value)
63     {
64         return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) {
65             $c = hexdec($match[1]);
66
67             if (0x80 > $c %= 0x200000) {
68                 return chr($c);
69             }
70             if (0x800 > $c) {
71                 return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
72             }
73             if (0x10000 > $c) {
74                 return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
75             }
76         }, $value);
77     }
78 }