comparison vendor/masterminds/html5/src/HTML5/Parser/CharacterReference.php @ 0:4c8ae668cc8c

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