wp-user-avatar
/
third-party
/
vendor
/
symfony
/
css-selector
/
Parser
/
Tokenizer
/
TokenizerEscaping.php
wp-user-avatar
/
third-party
/
vendor
/
symfony
/
css-selector
/
Parser
/
Tokenizer
Last commit date
Tokenizer.php
2 months ago
TokenizerEscaping.php
2 months ago
TokenizerPatterns.php
2 months ago
TokenizerEscaping.php
57 lines
| 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 | namespace ProfilePressVendor\Symfony\Component\CssSelector\Parser\Tokenizer; |
| 12 | |
| 13 | /** |
| 14 | * CSS selector tokenizer escaping applier. |
| 15 | * |
| 16 | * This component is a port of the Python cssselect library, |
| 17 | * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. |
| 18 | * |
| 19 | * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> |
| 20 | * |
| 21 | * @internal |
| 22 | */ |
| 23 | class TokenizerEscaping |
| 24 | { |
| 25 | private $patterns; |
| 26 | public function __construct(TokenizerPatterns $patterns) |
| 27 | { |
| 28 | $this->patterns = $patterns; |
| 29 | } |
| 30 | public function escapeUnicode(string $value): string |
| 31 | { |
| 32 | $value = $this->replaceUnicodeSequences($value); |
| 33 | return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value); |
| 34 | } |
| 35 | public function escapeUnicodeAndNewLine(string $value): string |
| 36 | { |
| 37 | $value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value); |
| 38 | return $this->escapeUnicode($value); |
| 39 | } |
| 40 | private function replaceUnicodeSequences(string $value): string |
| 41 | { |
| 42 | return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) { |
| 43 | $c = hexdec($match[1]); |
| 44 | if (0x80 > $c %= 0x200000) { |
| 45 | return \chr($c); |
| 46 | } |
| 47 | if (0x800 > $c) { |
| 48 | return \chr(0xc0 | $c >> 6) . \chr(0x80 | $c & 0x3f); |
| 49 | } |
| 50 | if (0x10000 > $c) { |
| 51 | return \chr(0xe0 | $c >> 12) . \chr(0x80 | $c >> 6 & 0x3f) . \chr(0x80 | $c & 0x3f); |
| 52 | } |
| 53 | return ''; |
| 54 | }, $value); |
| 55 | } |
| 56 | } |
| 57 |