PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/php-css-parser/Parsing/ParserState.php

https://github.com/mackensen/moodle
PHP | 310 lines | 272 code | 31 blank | 7 comment | 55 complexity | 2b4c47613a0d2cbc83c5eda62435ec3a MD5 | raw file
  1. <?php
  2. namespace Sabberworm\CSS\Parsing;
  3. use Sabberworm\CSS\Comment\Comment;
  4. use Sabberworm\CSS\Parsing\UnexpectedTokenException;
  5. use Sabberworm\CSS\Settings;
  6. class ParserState {
  7. private $oParserSettings;
  8. private $sText;
  9. private $aText;
  10. private $iCurrentPosition;
  11. private $sCharset;
  12. private $iLength;
  13. private $iLineNo;
  14. public function __construct($sText, Settings $oParserSettings, $iLineNo = 1) {
  15. $this->oParserSettings = $oParserSettings;
  16. $this->sText = $sText;
  17. $this->iCurrentPosition = 0;
  18. $this->iLineNo = $iLineNo;
  19. $this->setCharset($this->oParserSettings->sDefaultCharset);
  20. }
  21. public function setCharset($sCharset) {
  22. $this->sCharset = $sCharset;
  23. $this->aText = $this->strsplit($this->sText);
  24. $this->iLength = count($this->aText);
  25. }
  26. public function getCharset() {
  27. $this->oParserHelper->getCharset();
  28. return $this->sCharset;
  29. }
  30. public function currentLine() {
  31. return $this->iLineNo;
  32. }
  33. public function getSettings() {
  34. return $this->oParserSettings;
  35. }
  36. public function parseIdentifier($bIgnoreCase = true) {
  37. $sResult = $this->parseCharacter(true);
  38. if ($sResult === null) {
  39. throw new UnexpectedTokenException($sResult, $this->peek(5), 'identifier', $this->iLineNo);
  40. }
  41. $sCharacter = null;
  42. while (($sCharacter = $this->parseCharacter(true)) !== null) {
  43. $sResult .= $sCharacter;
  44. }
  45. if ($bIgnoreCase) {
  46. $sResult = $this->strtolower($sResult);
  47. }
  48. return $sResult;
  49. }
  50. public function parseCharacter($bIsForIdentifier) {
  51. if ($this->peek() === '\\') {
  52. if ($bIsForIdentifier && $this->oParserSettings->bLenientParsing && ($this->comes('\0') || $this->comes('\9'))) {
  53. // Non-strings can contain \0 or \9 which is an IE hack supported in lenient parsing.
  54. return null;
  55. }
  56. $this->consume('\\');
  57. if ($this->comes('\n') || $this->comes('\r')) {
  58. return '';
  59. }
  60. if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
  61. return $this->consume(1);
  62. }
  63. $sUnicode = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
  64. if ($this->strlen($sUnicode) < 6) {
  65. //Consume whitespace after incomplete unicode escape
  66. if (preg_match('/\\s/isSu', $this->peek())) {
  67. if ($this->comes('\r\n')) {
  68. $this->consume(2);
  69. } else {
  70. $this->consume(1);
  71. }
  72. }
  73. }
  74. $iUnicode = intval($sUnicode, 16);
  75. $sUtf32 = "";
  76. for ($i = 0; $i < 4; ++$i) {
  77. $sUtf32 .= chr($iUnicode & 0xff);
  78. $iUnicode = $iUnicode >> 8;
  79. }
  80. return iconv('utf-32le', $this->sCharset, $sUtf32);
  81. }
  82. if ($bIsForIdentifier) {
  83. $peek = ord($this->peek());
  84. // Ranges: a-z A-Z 0-9 - _
  85. if (($peek >= 97 && $peek <= 122) ||
  86. ($peek >= 65 && $peek <= 90) ||
  87. ($peek >= 48 && $peek <= 57) ||
  88. ($peek === 45) ||
  89. ($peek === 95) ||
  90. ($peek > 0xa1)) {
  91. return $this->consume(1);
  92. }
  93. } else {
  94. return $this->consume(1);
  95. }
  96. return null;
  97. }
  98. public function consumeWhiteSpace() {
  99. $comments = array();
  100. do {
  101. while (preg_match('/\\s/isSu', $this->peek()) === 1) {
  102. $this->consume(1);
  103. }
  104. if($this->oParserSettings->bLenientParsing) {
  105. try {
  106. $oComment = $this->consumeComment();
  107. } catch(UnexpectedTokenException $e) {
  108. // When we can’t find the end of a comment, we assume the document is finished.
  109. $this->iCurrentPosition = $this->iLength;
  110. return;
  111. }
  112. } else {
  113. $oComment = $this->consumeComment();
  114. }
  115. if ($oComment !== false) {
  116. $comments[] = $oComment;
  117. }
  118. } while($oComment !== false);
  119. return $comments;
  120. }
  121. public function comes($sString, $bCaseInsensitive = false) {
  122. $sPeek = $this->peek(strlen($sString));
  123. return ($sPeek == '')
  124. ? false
  125. : $this->streql($sPeek, $sString, $bCaseInsensitive);
  126. }
  127. public function peek($iLength = 1, $iOffset = 0) {
  128. $iOffset += $this->iCurrentPosition;
  129. if ($iOffset >= $this->iLength) {
  130. return '';
  131. }
  132. return $this->substr($iOffset, $iLength);
  133. }
  134. public function consume($mValue = 1) {
  135. if (is_string($mValue)) {
  136. $iLineCount = substr_count($mValue, "\n");
  137. $iLength = $this->strlen($mValue);
  138. if (!$this->streql($this->substr($this->iCurrentPosition, $iLength), $mValue)) {
  139. throw new UnexpectedTokenException($mValue, $this->peek(max($iLength, 5)), $this->iLineNo);
  140. }
  141. $this->iLineNo += $iLineCount;
  142. $this->iCurrentPosition += $this->strlen($mValue);
  143. return $mValue;
  144. } else {
  145. if ($this->iCurrentPosition + $mValue > $this->iLength) {
  146. throw new UnexpectedTokenException($mValue, $this->peek(5), 'count', $this->iLineNo);
  147. }
  148. $sResult = $this->substr($this->iCurrentPosition, $mValue);
  149. $iLineCount = substr_count($sResult, "\n");
  150. $this->iLineNo += $iLineCount;
  151. $this->iCurrentPosition += $mValue;
  152. return $sResult;
  153. }
  154. }
  155. public function consumeExpression($mExpression, $iMaxLength = null) {
  156. $aMatches = null;
  157. $sInput = $iMaxLength !== null ? $this->peek($iMaxLength) : $this->inputLeft();
  158. if (preg_match($mExpression, $sInput, $aMatches, PREG_OFFSET_CAPTURE) === 1) {
  159. return $this->consume($aMatches[0][0]);
  160. }
  161. throw new UnexpectedTokenException($mExpression, $this->peek(5), 'expression', $this->iLineNo);
  162. }
  163. /**
  164. * @return false|Comment
  165. */
  166. public function consumeComment() {
  167. $mComment = false;
  168. if ($this->comes('/*')) {
  169. $iLineNo = $this->iLineNo;
  170. $this->consume(1);
  171. $mComment = '';
  172. while (($char = $this->consume(1)) !== '') {
  173. $mComment .= $char;
  174. if ($this->comes('*/')) {
  175. $this->consume(2);
  176. break;
  177. }
  178. }
  179. }
  180. if ($mComment !== false) {
  181. // We skip the * which was included in the comment.
  182. return new Comment(substr($mComment, 1), $iLineNo);
  183. }
  184. return $mComment;
  185. }
  186. public function isEnd() {
  187. return $this->iCurrentPosition >= $this->iLength;
  188. }
  189. public function consumeUntil($aEnd, $bIncludeEnd = false, $consumeEnd = false, array &$comments = array()) {
  190. $aEnd = is_array($aEnd) ? $aEnd : array($aEnd);
  191. $out = '';
  192. $start = $this->iCurrentPosition;
  193. while (($char = $this->consume(1)) !== '') {
  194. if (in_array($char, $aEnd)) {
  195. if ($bIncludeEnd) {
  196. $out .= $char;
  197. } elseif (!$consumeEnd) {
  198. $this->iCurrentPosition -= $this->strlen($char);
  199. }
  200. return $out;
  201. }
  202. $out .= $char;
  203. if ($comment = $this->consumeComment()) {
  204. $comments[] = $comment;
  205. }
  206. }
  207. $this->iCurrentPosition = $start;
  208. throw new UnexpectedTokenException('One of ("'.implode('","', $aEnd).'")', $this->peek(5), 'search', $this->iLineNo);
  209. }
  210. private function inputLeft() {
  211. return $this->substr($this->iCurrentPosition, -1);
  212. }
  213. public function streql($sString1, $sString2, $bCaseInsensitive = true) {
  214. if($bCaseInsensitive) {
  215. return $this->strtolower($sString1) === $this->strtolower($sString2);
  216. } else {
  217. return $sString1 === $sString2;
  218. }
  219. }
  220. public function backtrack($iAmount) {
  221. $this->iCurrentPosition -= $iAmount;
  222. }
  223. public function strlen($sString) {
  224. if ($this->oParserSettings->bMultibyteSupport) {
  225. return mb_strlen($sString, $this->sCharset);
  226. } else {
  227. return strlen($sString);
  228. }
  229. }
  230. private function substr($iStart, $iLength) {
  231. if ($iLength < 0) {
  232. $iLength = $this->iLength - $iStart + $iLength;
  233. }
  234. if ($iStart + $iLength > $this->iLength) {
  235. $iLength = $this->iLength - $iStart;
  236. }
  237. $sResult = '';
  238. while ($iLength > 0) {
  239. $sResult .= $this->aText[$iStart];
  240. $iStart++;
  241. $iLength--;
  242. }
  243. return $sResult;
  244. }
  245. private function strtolower($sString) {
  246. if ($this->oParserSettings->bMultibyteSupport) {
  247. return mb_strtolower($sString, $this->sCharset);
  248. } else {
  249. return strtolower($sString);
  250. }
  251. }
  252. private function strsplit($sString) {
  253. if ($this->oParserSettings->bMultibyteSupport) {
  254. if ($this->streql($this->sCharset, 'utf-8')) {
  255. return preg_split('//u', $sString, null, PREG_SPLIT_NO_EMPTY);
  256. } else {
  257. $iLength = mb_strlen($sString, $this->sCharset);
  258. $aResult = array();
  259. for ($i = 0; $i < $iLength; ++$i) {
  260. $aResult[] = mb_substr($sString, $i, 1, $this->sCharset);
  261. }
  262. return $aResult;
  263. }
  264. } else {
  265. if($sString === '') {
  266. return array();
  267. } else {
  268. return str_split($sString);
  269. }
  270. }
  271. }
  272. private function strpos($sString, $sNeedle, $iOffset) {
  273. if ($this->oParserSettings->bMultibyteSupport) {
  274. return mb_strpos($sString, $sNeedle, $iOffset, $this->sCharset);
  275. } else {
  276. return strpos($sString, $sNeedle, $iOffset);
  277. }
  278. }
  279. }