Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\CssSelector\Parser\Tokenizer; Chris@0: Chris@0: /** Chris@0: * CSS selector tokenizer escaping applier. Chris@0: * Chris@0: * This component is a port of the Python cssselect library, Chris@0: * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class TokenizerEscaping Chris@0: { Chris@0: private $patterns; Chris@0: Chris@0: public function __construct(TokenizerPatterns $patterns) Chris@0: { Chris@0: $this->patterns = $patterns; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $value Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function escapeUnicode($value) Chris@0: { Chris@0: $value = $this->replaceUnicodeSequences($value); Chris@0: Chris@0: return preg_replace($this->patterns->getSimpleEscapePattern(), '$1', $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $value Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function escapeUnicodeAndNewLine($value) Chris@0: { Chris@0: $value = preg_replace($this->patterns->getNewLineEscapePattern(), '', $value); Chris@0: Chris@0: return $this->escapeUnicode($value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $value Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: private function replaceUnicodeSequences($value) Chris@0: { Chris@0: return preg_replace_callback($this->patterns->getUnicodeEscapePattern(), function ($match) { Chris@0: $c = hexdec($match[1]); Chris@0: Chris@0: if (0x80 > $c %= 0x200000) { Chris@17: return \chr($c); Chris@0: } Chris@0: if (0x800 > $c) { Chris@17: return \chr(0xC0 | $c >> 6).\chr(0x80 | $c & 0x3F); Chris@0: } Chris@0: if (0x10000 > $c) { Chris@17: return \chr(0xE0 | $c >> 12).\chr(0x80 | $c >> 6 & 0x3F).\chr(0x80 | $c & 0x3F); Chris@0: } Chris@0: }, $value); Chris@0: } Chris@0: }