TokenizerPatterns.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\CssSelector\Parser\Tokenizer;
  11. /**
  12. * CSS selector tokenizer patterns builder.
  13. *
  14. * This component is a port of the Python cssselect library,
  15. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  16. *
  17. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  18. *
  19. * @internal
  20. */
  21. class TokenizerPatterns
  22. {
  23. /**
  24. * @var string
  25. */
  26. private $unicodeEscapePattern;
  27. /**
  28. * @var string
  29. */
  30. private $simpleEscapePattern;
  31. /**
  32. * @var string
  33. */
  34. private $newLineEscapePattern;
  35. /**
  36. * @var string
  37. */
  38. private $escapePattern;
  39. /**
  40. * @var string
  41. */
  42. private $stringEscapePattern;
  43. /**
  44. * @var string
  45. */
  46. private $nonAsciiPattern;
  47. /**
  48. * @var string
  49. */
  50. private $nmCharPattern;
  51. /**
  52. * @var string
  53. */
  54. private $nmStartPattern;
  55. /**
  56. * @var string
  57. */
  58. private $identifierPattern;
  59. /**
  60. * @var string
  61. */
  62. private $hashPattern;
  63. /**
  64. * @var string
  65. */
  66. private $numberPattern;
  67. /**
  68. * @var string
  69. */
  70. private $quotedStringPattern;
  71. /**
  72. * Constructor.
  73. */
  74. public function __construct()
  75. {
  76. $this->unicodeEscapePattern = '\\\\([0-9a-f]{1,6})(?:\r\n|[ \n\r\t\f])?';
  77. $this->simpleEscapePattern = '\\\\(.)';
  78. $this->newLineEscapePattern = '\\\\(?:\n|\r\n|\r|\f)';
  79. $this->escapePattern = $this->unicodeEscapePattern.'|\\\\[^\n\r\f0-9a-f]';
  80. $this->stringEscapePattern = $this->newLineEscapePattern.'|'.$this->escapePattern;
  81. $this->nonAsciiPattern = '[^\x00-\x7F]';
  82. $this->nmCharPattern = '[_a-z0-9-]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
  83. $this->nmStartPattern = '[_a-z]|'.$this->escapePattern.'|'.$this->nonAsciiPattern;
  84. $this->identifierPattern = '(?:'.$this->nmStartPattern.')(?:'.$this->nmCharPattern.')*';
  85. $this->hashPattern = '#((?:'.$this->nmCharPattern.')+)';
  86. $this->numberPattern = '[+-]?(?:[0-9]*\.[0-9]+|[0-9]+)';
  87. $this->quotedStringPattern = '([^\n\r\f%s]|'.$this->stringEscapePattern.')*';
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function getNewLineEscapePattern()
  93. {
  94. return '~^'.$this->newLineEscapePattern.'~';
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function getSimpleEscapePattern()
  100. {
  101. return '~^'.$this->simpleEscapePattern.'~';
  102. }
  103. /**
  104. * @return string
  105. */
  106. public function getUnicodeEscapePattern()
  107. {
  108. return '~^'.$this->unicodeEscapePattern.'~i';
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function getIdentifierPattern()
  114. {
  115. return '~^'.$this->identifierPattern.'~i';
  116. }
  117. /**
  118. * @return string
  119. */
  120. public function getHashPattern()
  121. {
  122. return '~^'.$this->hashPattern.'~i';
  123. }
  124. /**
  125. * @return string
  126. */
  127. public function getNumberPattern()
  128. {
  129. return '~^'.$this->numberPattern.'~';
  130. }
  131. /**
  132. * @param string $quote
  133. *
  134. * @return string
  135. */
  136. public function getQuotedStringPattern($quote)
  137. {
  138. return '~^'.sprintf($this->quotedStringPattern, $quote).'~i';
  139. }
  140. }