annotate vendor/masterminds/html5/src/HTML5/Parser/UTF8Utils.php @ 17:129ea1e6d783

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