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 patterns builder.
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 TokenizerPatterns
29 private $unicodeEscapePattern;
34 private $simpleEscapePattern;
39 private $newLineEscapePattern;
44 private $escapePattern;
49 private $stringEscapePattern;
54 private $nonAsciiPattern;
59 private $nmCharPattern;
64 private $nmStartPattern;
69 private $identifierPattern;
79 private $numberPattern;
84 private $quotedStringPattern;
89 public function __construct()
91 $this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?';
92 $this->simpleEscapePattern = '\\\\(.)';
93 $this->newLineEscapePattern = '\\\\(?:\n|\r\n|\r|\f)';
94 $this->escapePattern = $this->unicodeEscapePattern.'|\\\\[^\n\r\f0-9a-f]';
95 $this->stringEscapePattern = $this->newLineEscapePattern.'|'.$this->escapePattern;
96 $this->nonAsciiPattern = '[^\x00-\x7F]';
97 $this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
98 $this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
99 $this->identifierPattern = '(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
100 $this->hashPattern = '#((?:'.$this->nmCharPattern.')+)';
101 $this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)';
102 $this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*';
108 public function getNewLineEscapePattern()
110 return '~^'.$this->newLineEscapePattern.'~';
116 public function getSimpleEscapePattern()
118 return '~^'.$this->simpleEscapePattern.'~';
124 public function getUnicodeEscapePattern()
126 return '~^'.$this->unicodeEscapePattern.'~i';
132 public function getIdentifierPattern()
134 return '~^'.$this->identifierPattern.'~i';
140 public function getHashPattern()
142 return '~^'.$this->hashPattern.'~i';
148 public function getNumberPattern()
150 return '~^'.$this->numberPattern.'~';
154 * @param string $quote
158 public function getQuotedStringPattern($quote)
160 return '~^'.sprintf($this->quotedStringPattern, $quote).'~i';