CharacterReference.php
6 months ago
DOMTreeBuilder.php
6 months ago
EventHandler.php
6 months ago
FileInputStream.php
6 months ago
InputStream.php
6 months ago
ParseError.php
6 months ago
README.md
1 year ago
Scanner.php
6 months ago
StringInputStream.php
6 months ago
Tokenizer.php
6 months ago
TreeBuildingRules.php
6 months ago
UTF8Utils.php
6 months ago
UTF8Utils.php
178 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaVendor\Masterminds\HTML5\Parser; |
| 4 | |
| 5 | /* |
| 6 | Portions based on code from html5lib files with the following copyright: |
| 7 | |
| 8 | Copyright 2009 Geoffrey Sneddon <http://gsnedders.com/> |
| 9 | |
| 10 | Permission is hereby granted, free of charge, to any person obtaining a |
| 11 | copy of this software and associated documentation files (the |
| 12 | "Software"), to deal in the Software without restriction, including |
| 13 | without limitation the rights to use, copy, modify, merge, publish, |
| 14 | distribute, sublicense, and/or sell copies of the Software, and to |
| 15 | permit persons to whom the Software is furnished to do so, subject to |
| 16 | the following conditions: |
| 17 | |
| 18 | The above copyright notice and this permission notice shall be included |
| 19 | in all copies or substantial portions of the Software. |
| 20 | |
| 21 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 22 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 24 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 25 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 26 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 27 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | use AmeliaVendor\Masterminds\HTML5\Exception; |
| 31 | |
| 32 | class UTF8Utils |
| 33 | { |
| 34 | /** |
| 35 | * The Unicode replacement character. |
| 36 | */ |
| 37 | const FFFD = "\xEF\xBF\xBD"; |
| 38 | |
| 39 | /** |
| 40 | * Count the number of characters in a string. |
| 41 | * UTF-8 aware. This will try (in order) iconv, MB, and finally a custom counter. |
| 42 | * |
| 43 | * @param string $string |
| 44 | * |
| 45 | * @return int |
| 46 | */ |
| 47 | public static function countChars($string) |
| 48 | { |
| 49 | // Get the length for the string we need. |
| 50 | if (function_exists('mb_strlen')) { |
| 51 | return mb_strlen($string, 'utf-8'); |
| 52 | } |
| 53 | |
| 54 | if (function_exists('iconv_strlen')) { |
| 55 | return iconv_strlen($string, 'utf-8'); |
| 56 | } |
| 57 | |
| 58 | $count = count_chars($string); |
| 59 | |
| 60 | // 0x80 = 0x7F - 0 + 1 (one added to get inclusive range) |
| 61 | // 0x33 = 0xF4 - 0x2C + 1 (one added to get inclusive range) |
| 62 | return array_sum(array_slice($count, 0, 0x80)) + array_sum(array_slice($count, 0xC2, 0x33)); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Convert data from the given encoding to UTF-8. |
| 67 | * |
| 68 | * This has not yet been tested with charactersets other than UTF-8. |
| 69 | * It should work with ISO-8859-1/-13 and standard Latin Win charsets. |
| 70 | * |
| 71 | * @param string $data The data to convert |
| 72 | * @param string $encoding A valid encoding. Examples: http://www.php.net/manual/en/mbstring.supported-encodings.php |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public static function convertToUTF8($data, $encoding = 'UTF-8') |
| 77 | { |
| 78 | /* |
| 79 | * From the HTML5 spec: Given an encoding, the bytes in the input stream must be converted |
| 80 | * to Unicode characters for the tokeniser, as described by the rules for that encoding, |
| 81 | * except that the leading U+FEFF BYTE ORDER MARK character, if any, must not be stripped |
| 82 | * by the encoding layer (it is stripped by the rule below). Bytes or sequences of bytes |
| 83 | * in the original byte stream that could not be converted to Unicode characters must be |
| 84 | * converted to U+FFFD REPLACEMENT CHARACTER code points. |
| 85 | */ |
| 86 | |
| 87 | // mb_convert_encoding is chosen over iconv because of a bug. The best |
| 88 | // details for the bug are on http://us1.php.net/manual/en/function.iconv.php#108643 |
| 89 | // which contains links to the actual but reports as well as work around |
| 90 | // details. |
| 91 | if (function_exists('mb_convert_encoding')) { |
| 92 | // mb library has the following behaviors: |
| 93 | // - UTF-16 surrogates result in false. |
| 94 | // - Overlongs and outside Plane 16 result in empty strings. |
| 95 | |
| 96 | // Before we run mb_convert_encoding we need to tell it what to do with |
| 97 | // characters it does not know. This could be different than the parent |
| 98 | // application executing this library so we store the value, change it |
| 99 | // to our needs, and then change it back when we are done. This feels |
| 100 | // a little excessive and it would be great if there was a better way. |
| 101 | $save = mb_substitute_character(); |
| 102 | mb_substitute_character('none'); |
| 103 | $data = mb_convert_encoding($data, 'UTF-8', $encoding); |
| 104 | mb_substitute_character($save); |
| 105 | } |
| 106 | // @todo Get iconv running in at least some environments if that is possible. |
| 107 | elseif (function_exists('iconv') && 'auto' !== $encoding) { |
| 108 | // fprintf(STDOUT, "iconv found\n"); |
| 109 | // iconv has the following behaviors: |
| 110 | // - Overlong representations are ignored. |
| 111 | // - Beyond Plane 16 is replaced with a lower char. |
| 112 | // - Incomplete sequences generate a warning. |
| 113 | $data = @iconv($encoding, 'UTF-8//IGNORE', $data); |
| 114 | } else { |
| 115 | throw new Exception('Not implemented, please install mbstring or iconv'); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * One leading U+FEFF BYTE ORDER MARK character must be ignored if any are present. |
| 120 | */ |
| 121 | if ("\xEF\xBB\xBF" === substr($data, 0, 3)) { |
| 122 | $data = substr($data, 3); |
| 123 | } |
| 124 | |
| 125 | return $data; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Checks for Unicode code points that are not valid in a document. |
| 130 | * |
| 131 | * @param string $data A string to analyze |
| 132 | * |
| 133 | * @return array An array of (string) error messages produced by the scanning |
| 134 | */ |
| 135 | public static function checkForIllegalCodepoints($data) |
| 136 | { |
| 137 | // Vestigal error handling. |
| 138 | $errors = array(); |
| 139 | |
| 140 | /* |
| 141 | * All U+0000 null characters in the input must be replaced by U+FFFD REPLACEMENT CHARACTERs. |
| 142 | * Any occurrences of such characters is a parse error. |
| 143 | */ |
| 144 | for ($i = 0, $count = substr_count($data, "\0"); $i < $count; ++$i) { |
| 145 | $errors[] = 'null-character'; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Any occurrences of any characters in the ranges U+0001 to U+0008, U+000B, U+000E to U+001F, U+007F |
| 150 | * to U+009F, U+D800 to U+DFFF , U+FDD0 to U+FDEF, and characters U+FFFE, U+FFFF, U+1FFFE, U+1FFFF, |
| 151 | * U+2FFFE, U+2FFFF, U+3FFFE, U+3FFFF, U+4FFFE, U+4FFFF, U+5FFFE, U+5FFFF, U+6FFFE, U+6FFFF, U+7FFFE, |
| 152 | * U+7FFFF, U+8FFFE, U+8FFFF, U+9FFFE, U+9FFFF, U+AFFFE, U+AFFFF, U+BFFFE, U+BFFFF, U+CFFFE, U+CFFFF, |
| 153 | * U+DFFFE, U+DFFFF, U+EFFFE, U+EFFFF, U+FFFFE, U+FFFFF, U+10FFFE, and U+10FFFF are parse errors. |
| 154 | * (These are all control characters or permanently undefined Unicode characters.) |
| 155 | */ |
| 156 | // Check PCRE is loaded. |
| 157 | $count = preg_match_all( |
| 158 | '/(?: |
| 159 | [\x01-\x08\x0B\x0E-\x1F\x7F] # U+0001 to U+0008, U+000B, U+000E to U+001F and U+007F |
| 160 | | |
| 161 | \xC2[\x80-\x9F] # U+0080 to U+009F |
| 162 | | |
| 163 | \xED(?:\xA0[\x80-\xFF]|[\xA1-\xBE][\x00-\xFF]|\xBF[\x00-\xBF]) # U+D800 to U+DFFFF |
| 164 | | |
| 165 | \xEF\xB7[\x90-\xAF] # U+FDD0 to U+FDEF |
| 166 | | |
| 167 | \xEF\xBF[\xBE\xBF] # U+FFFE and U+FFFF |
| 168 | | |
| 169 | [\xF0-\xF4][\x8F-\xBF]\xBF[\xBE\xBF] # U+nFFFE and U+nFFFF (1 <= n <= 10_{16}) |
| 170 | )/x', $data, $matches); |
| 171 | for ($i = 0; $i < $count; ++$i) { |
| 172 | $errors[] = 'invalid-codepoint'; |
| 173 | } |
| 174 | |
| 175 | return $errors; |
| 176 | } |
| 177 | } |
| 178 |