Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[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     /**
27      * @var TokenizerPatterns
28      */
29     private $patterns;
30
31     /**
32      * @param TokenizerPatterns $patterns
33      */
34     public function __construct(TokenizerPatterns $patterns)
35     {
36         $this->patterns = $patterns;
37     }
38
39     /**
40      * @param string $value
41      *
42      * @return string
43      */
44     public function escapeUnicode($value)
45     {
46         $value = $this->replaceUnicodeSequences($value);
47
48         return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value);
49     }
50
51     /**
52      * @param string $value
53      *
54      * @return string
55      */
56     public function escapeUnicodeAndNewLine($value)
57     {
58         $value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value);
59
60         return $this->escapeUnicode($value);
61     }
62
63     /**
64      * @param string $value
65      *
66      * @return string
67      */
68     private function replaceUnicodeSequences($value)
69     {
70         return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) {
71             $c = hexdec($match[1]);
72
73             if (0x80 > $c %= 0x200000) {
74                 return chr($c);
75             }
76             if (0x800 > $c) {
77                 return chr(0xC0 | $c >> 6).chr(0x80 | $c & 0x3F);
78             }
79             if (0x10000 > $c) {
80                 return chr(0xE0 | $c >> 12).chr(0x80 | $c >> 6 & 0x3F).chr(0x80 | $c & 0x3F);
81             }
82         }, $value);
83     }
84 }