| 1 |
<?php |
| 2 |
|
| 3 |
namespace Masterminds\HTML5\Parser; |
| 4 |
|
| 5 |
use Masterminds\HTML5\Entities; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage entity references. |
| 9 |
* |
| 10 |
* This is a simple resolver for HTML5 character reference entitites. See Entities for the list of supported entities. |
| 11 |
*/ |
| 12 |
class CharacterReference |
| 13 |
{ |
| 14 |
protected static $numeric_mask = array( |
| 15 |
0x0, |
| 16 |
0x2FFFF, |
| 17 |
0, |
| 18 |
0xFFFF, |
| 19 |
); |
| 20 |
|
| 21 |
/** |
| 22 |
* Given a name (e.g. 'amp'), lookup the UTF-8 character ('&'). |
| 23 |
* |
| 24 |
* @param string $name The name to look up. |
| 25 |
* |
| 26 |
* @return string The character sequence. In UTF-8 this may be more than one byte. |
| 27 |
*/ |
| 28 |
public static function lookupName($name) |
| 29 |
{ |
| 30 |
// Do we really want to return NULL here? or FFFD |
| 31 |
return isset(Entities::$byName[$name]) ? Entities::$byName[$name] : null; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Given a decimal number, return the UTF-8 character. |
| 36 |
* |
| 37 |
* @param $int |
| 38 |
* |
| 39 |
* @return false|string|string[]|null |
| 40 |
*/ |
| 41 |
public static function lookupDecimal($int) |
| 42 |
{ |
| 43 |
$entity = '&#' . $int . ';'; |
| 44 |
|
| 45 |
// UNTESTED: This may fail on some planes. Couldn't find full documentation |
| 46 |
// on the value of the mask array. |
| 47 |
return mb_decode_numericentity($entity, static::$numeric_mask, 'utf-8'); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Given a hexadecimal number, return the UTF-8 character. |
| 52 |
* |
| 53 |
* @param $hexdec |
| 54 |
* |
| 55 |
* @return false|string|string[]|null |
| 56 |
*/ |
| 57 |
public static function lookupHex($hexdec) |
| 58 |
{ |
| 59 |
return static::lookupDecimal(hexdec($hexdec)); |
| 60 |
} |
| 61 |
} |
| 62 |
|