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
26 private $unicodeEscapePattern;
27 private $simpleEscapePattern;
28 private $newLineEscapePattern;
29 private $escapePattern;
30 private $stringEscapePattern;
31 private $nonAsciiPattern;
32 private $nmCharPattern;
33 private $nmStartPattern;
34 private $identifierPattern;
36 private $numberPattern;
37 private $quotedStringPattern;
39 public function __construct()
41 $this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?';
42 $this->simpleEscapePattern = '\\\\(.)';
43 $this->newLineEscapePattern = '\\\\(?:\n|\r\n|\r|\f)';
44 $this->escapePattern = $this->unicodeEscapePattern.'|\\\\[^\n\r\f0-9a-f]';
45 $this->stringEscapePattern = $this->newLineEscapePattern.'|'.$this->escapePattern;
46 $this->nonAsciiPattern = '[^\x00-\x7F]';
47 $this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
48 $this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
49 $this->identifierPattern = '-?(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
50 $this->hashPattern = '#((?:'.$this->nmCharPattern.')+)';
51 $this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)';
52 $this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*';
58 public function getNewLineEscapePattern()
60 return '~^'.$this->newLineEscapePattern.'~';
66 public function getSimpleEscapePattern()
68 return '~^'.$this->simpleEscapePattern.'~';
74 public function getUnicodeEscapePattern()
76 return '~^'.$this->unicodeEscapePattern.'~i';
82 public function getIdentifierPattern()
84 return '~^'.$this->identifierPattern.'~i';
90 public function getHashPattern()
92 return '~^'.$this->hashPattern.'~i';
98 public function getNumberPattern()
100 return '~^'.$this->numberPattern.'~';
104 * @param string $quote
108 public function getQuotedStringPattern($quote)
110 return '~^'.sprintf($this->quotedStringPattern, $quote).'~i';