1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
9
|
|
|
* - (c) John MacFarlane |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark\Util; |
16
|
|
|
|
17
|
|
|
final class Html5EntityDecoder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param string $entity |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
66 |
|
public static function decode(string $entity): string |
25
|
|
|
{ |
26
|
66 |
|
if (\substr($entity, -1) !== ';') { |
27
|
6 |
|
return $entity; |
28
|
|
|
} |
29
|
|
|
|
30
|
66 |
|
if (\substr($entity, 0, 2) === '&#') { |
31
|
33 |
|
if (\strtolower(\substr($entity, 2, 1)) === 'x') { |
32
|
3 |
|
return self::fromHex(\substr($entity, 3, -1)); |
33
|
|
|
} |
34
|
|
|
|
35
|
30 |
|
return self::fromDecimal(\substr($entity, 2, -1)); |
36
|
|
|
} |
37
|
|
|
|
38
|
48 |
|
return \html_entity_decode($entity, \ENT_QUOTES | \ENT_HTML5, 'UTF-8'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $number |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
33 |
|
private static function fromDecimal($number): string |
47
|
|
|
{ |
48
|
|
|
// Only convert code points within planes 0-2, excluding NULL |
49
|
33 |
|
if (empty($number) || $number > 0x2FFFF) { |
50
|
3 |
|
return self::fromHex('fffd'); |
51
|
|
|
} |
52
|
|
|
|
53
|
33 |
|
$entity = '&#' . $number . ';'; |
54
|
|
|
|
55
|
33 |
|
$converted = \mb_decode_numericentity($entity, [0x0, 0x2FFFF, 0, 0xFFFF], 'UTF-8'); |
56
|
|
|
|
57
|
33 |
|
if ($converted === $entity) { |
58
|
|
|
return self::fromHex('fffd'); |
59
|
|
|
} |
60
|
|
|
|
61
|
33 |
|
return $converted; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $hexChars |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
6 |
|
private static function fromHex(string $hexChars): string |
70
|
|
|
{ |
71
|
6 |
|
return self::fromDecimal(\hexdec($hexChars)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|