PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/dompdf/dompdf/lib/html5lib/InputStream.php

https://gitlab.com/techniconline/kmc
PHP | 293 lines | 147 code | 22 blank | 124 comment | 26 complexity | b6bf4b30399953409b020515fb2885f0 MD5 | raw file
  1. <?php
  2. /*
  3. Copyright 2009 Geoffrey Sneddon <http://gsnedders.com/>
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  17. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  18. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  19. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. // Some conventions:
  22. // /* */ indicates verbatim text from the HTML 5 specification
  23. // // indicates regular comments
  24. class HTML5_InputStream
  25. {
  26. /**
  27. * The string data we're parsing.
  28. */
  29. private $data;
  30. /**
  31. * The current integer byte position we are in $data
  32. */
  33. private $char;
  34. /**
  35. * Length of $data; when $char === $data, we are at the end-of-file.
  36. */
  37. private $EOF;
  38. /**
  39. * Parse errors.
  40. */
  41. public $errors = array();
  42. /**
  43. * @param $data Data to parse
  44. */
  45. public function __construct($data)
  46. {
  47. /* Given an encoding, the bytes in the input stream must be
  48. converted to Unicode characters for the tokeniser, as
  49. described by the rules for that encoding, except that the
  50. leading U+FEFF BYTE ORDER MARK character, if any, must not
  51. be stripped by the encoding layer (it is stripped by the rule below).
  52. Bytes or sequences of bytes in the original byte stream that
  53. could not be converted to Unicode characters must be converted
  54. to U+FFFD REPLACEMENT CHARACTER code points. */
  55. // XXX currently assuming input data is UTF-8; once we
  56. // build encoding detection this will no longer be the case
  57. //
  58. // We previously had an mbstring implementation here, but that
  59. // implementation is heavily non-conforming, so it's been
  60. // omitted.
  61. if (extension_loaded('iconv')) {
  62. // non-conforming
  63. $data = @iconv('UTF-8', 'UTF-8//IGNORE', $data);
  64. } else {
  65. // we can make a conforming native implementation
  66. throw new Exception('Not implemented, please install mbstring or iconv');
  67. }
  68. /* One leading U+FEFF BYTE ORDER MARK character must be
  69. ignored if any are present. */
  70. if (substr($data, 0, 3) === "\xEF\xBB\xBF") {
  71. $data = substr($data, 3);
  72. }
  73. /* All U+0000 NULL characters in the input must be replaced
  74. by U+FFFD REPLACEMENT CHARACTERs. Any occurrences of such
  75. characters is a parse error. */
  76. for ($i = 0, $count = substr_count($data, "\0"); $i < $count; $i++) {
  77. $this->errors[] = array(
  78. 'type' => HTML5_Tokenizer::PARSEERROR,
  79. 'data' => 'null-character'
  80. );
  81. }
  82. /* U+000D CARRIAGE RETURN (CR) characters and U+000A LINE FEED
  83. (LF) characters are treated specially. Any CR characters
  84. that are followed by LF characters must be removed, and any
  85. CR characters not followed by LF characters must be converted
  86. to LF characters. Thus, newlines in HTML DOMs are represented
  87. by LF characters, and there are never any CR characters in the
  88. input to the tokenization stage. */
  89. $data = str_replace(
  90. array(
  91. "\0",
  92. "\r\n",
  93. "\r"
  94. ),
  95. array(
  96. "\xEF\xBF\xBD",
  97. "\n",
  98. "\n"
  99. ),
  100. $data
  101. );
  102. /* Any occurrences of any characters in the ranges U+0001 to
  103. U+0008, U+000B, U+000E to U+001F, U+007F to U+009F,
  104. U+D800 to U+DFFF , U+FDD0 to U+FDEF, and
  105. characters U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, U+2FFFE, U+2FFFF,
  106. U+3FFFE, U+3FFFF, U+4FFFE, U+4FFFF, U+5FFFE, U+5FFFF, U+6FFFE,
  107. U+6FFFF, U+7FFFE, U+7FFFF, U+8FFFE, U+8FFFF, U+9FFFE, U+9FFFF,
  108. U+AFFFE, U+AFFFF, U+BFFFE, U+BFFFF, U+CFFFE, U+CFFFF, U+DFFFE,
  109. U+DFFFF, U+EFFFE, U+EFFFF, U+FFFFE, U+FFFFF, U+10FFFE, and
  110. U+10FFFF are parse errors. (These are all control characters
  111. or permanently undefined Unicode characters.) */
  112. // Check PCRE is loaded.
  113. if (extension_loaded('pcre')) {
  114. $count = preg_match_all(
  115. '/(?:
  116. [\x01-\x08\x0B\x0E-\x1F\x7F] # U+0001 to U+0008, U+000B, U+000E to U+001F and U+007F
  117. |
  118. \xC2[\x80-\x9F] # U+0080 to U+009F
  119. |
  120. \xED(?:\xA0[\x80-\xFF]|[\xA1-\xBE][\x00-\xFF]|\xBF[\x00-\xBF]) # U+D800 to U+DFFFF
  121. |
  122. \xEF\xB7[\x90-\xAF] # U+FDD0 to U+FDEF
  123. |
  124. \xEF\xBF[\xBE\xBF] # U+FFFE and U+FFFF
  125. |
  126. [\xF0-\xF4][\x8F-\xBF]\xBF[\xBE\xBF] # U+nFFFE and U+nFFFF (1 <= n <= 10_{16})
  127. )/x',
  128. $data,
  129. $matches
  130. );
  131. for ($i = 0; $i < $count; $i++) {
  132. $this->errors[] = array(
  133. 'type' => HTML5_Tokenizer::PARSEERROR,
  134. 'data' => 'invalid-codepoint'
  135. );
  136. }
  137. } else {
  138. // XXX: Need non-PCRE impl, probably using substr_count
  139. }
  140. $this->data = $data;
  141. $this->char = 0;
  142. $this->EOF = strlen($data);
  143. }
  144. /**
  145. * Returns the current line that the tokenizer is at.
  146. */
  147. public function getCurrentLine()
  148. {
  149. // Check the string isn't empty
  150. if ($this->EOF) {
  151. // Add one to $this->char because we want the number for the next
  152. // byte to be processed.
  153. return substr_count($this->data, "\n", 0, min($this->char, $this->EOF)) + 1;
  154. } else {
  155. // If the string is empty, we are on the first line (sorta).
  156. return 1;
  157. }
  158. }
  159. /**
  160. * Returns the current column of the current line that the tokenizer is at.
  161. */
  162. public function getColumnOffset()
  163. {
  164. // strrpos is weird, and the offset needs to be negative for what we
  165. // want (i.e., the last \n before $this->char). This needs to not have
  166. // one (to make it point to the next character, the one we want the
  167. // position of) added to it because strrpos's behaviour includes the
  168. // final offset byte.
  169. $lastLine = strrpos($this->data, "\n", $this->char - 1 - strlen($this->data));
  170. // However, for here we want the length up until the next byte to be
  171. // processed, so add one to the current byte ($this->char).
  172. if ($lastLine !== false) {
  173. $findLengthOf = substr($this->data, $lastLine + 1, $this->char - 1 - $lastLine);
  174. } else {
  175. $findLengthOf = substr($this->data, 0, $this->char);
  176. }
  177. // Get the length for the string we need.
  178. if (extension_loaded('iconv')) {
  179. return iconv_strlen($findLengthOf, 'utf-8');
  180. } elseif (extension_loaded('mbstring')) {
  181. return mb_strlen($findLengthOf, 'utf-8');
  182. } elseif (extension_loaded('xml')) {
  183. return strlen(utf8_decode($findLengthOf));
  184. } else {
  185. $count = count_chars($findLengthOf);
  186. // 0x80 = 0x7F - 0 + 1 (one added to get inclusive range)
  187. // 0x33 = 0xF4 - 0x2C + 1 (one added to get inclusive range)
  188. return array_sum(array_slice($count, 0, 0x80)) +
  189. array_sum(array_slice($count, 0xC2, 0x33));
  190. }
  191. }
  192. /**
  193. * Retrieve the currently consume character.
  194. * @note This performs bounds checking
  195. */
  196. public function char()
  197. {
  198. return ($this->char++ < $this->EOF)
  199. ? $this->data[$this->char - 1]
  200. : false;
  201. }
  202. /**
  203. * Get all characters until EOF.
  204. * @note This performs bounds checking
  205. */
  206. public function remainingChars()
  207. {
  208. if ($this->char < $this->EOF) {
  209. $data = substr($this->data, $this->char);
  210. $this->char = $this->EOF;
  211. return $data;
  212. } else {
  213. return false;
  214. }
  215. }
  216. /**
  217. * Matches as far as possible until we reach a certain set of bytes
  218. * and returns the matched substring.
  219. * @param $bytes Bytes to match.
  220. */
  221. public function charsUntil($bytes, $max = null)
  222. {
  223. if ($this->char < $this->EOF) {
  224. if ($max === 0 || $max) {
  225. $len = strcspn($this->data, $bytes, $this->char, $max);
  226. } else {
  227. $len = strcspn($this->data, $bytes, $this->char);
  228. }
  229. $string = (string)substr($this->data, $this->char, $len);
  230. $this->char += $len;
  231. return $string;
  232. } else {
  233. return false;
  234. }
  235. }
  236. /**
  237. * Matches as far as possible with a certain set of bytes
  238. * and returns the matched substring.
  239. * @param $bytes Bytes to match.
  240. */
  241. public function charsWhile($bytes, $max = null)
  242. {
  243. if ($this->char < $this->EOF) {
  244. if ($max === 0 || $max) {
  245. $len = strspn($this->data, $bytes, $this->char, $max);
  246. } else {
  247. $len = strspn($this->data, $bytes, $this->char);
  248. }
  249. $string = (string)substr($this->data, $this->char, $len);
  250. $this->char += $len;
  251. return $string;
  252. } else {
  253. return false;
  254. }
  255. }
  256. /**
  257. * Unconsume one character.
  258. */
  259. public function unget()
  260. {
  261. if ($this->char <= $this->EOF) {
  262. $this->char--;
  263. }
  264. }
  265. }