/vendor/PowerTools/HTML5/Parser/CharacterReference.php

https://github.com/PHPPowertools/DOM-Query · PHP · 142 lines · 20 code · 8 blank · 114 comment · 0 complexity · 4334194123dff8ea8457733b6c49b2a6 MD5 · raw file

  1. <?php
  2. /* !
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  5. *
  6. * PACKAGE : PHP POWERTOOLS
  7. *
  8. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  9. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  10. *
  11. * COMPONENT : HTML5
  12. *
  13. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  14. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  15. *
  16. * DESCRIPTION :
  17. *
  18. * A library for easy HTML5 parsing
  19. *
  20. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  21. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  22. *
  23. * REQUIREMENTS :
  24. *
  25. * PHP version 5.4+
  26. * PSR-0 compatibility
  27. *
  28. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  29. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  30. *
  31. * CREDITS :
  32. *
  33. * This library started out as a fork of Masterminds/html5-php
  34. *
  35. * Contributors of that Masterminds/html5-php :
  36. * ---------------------------------------------
  37. * Matt Butcher [technosophos]
  38. * Matt Farina [mattfarina]
  39. * Asmir Mustafic [goetas]
  40. * Edward Z. Yang [ezyang]
  41. * Geoffrey Sneddon [gsnedders]
  42. * Kukhar Vasily [ngreduce]
  43. * Rune Christensen [MrElectronic]
  44. * Mišo Belica [miso-belica]
  45. * Asmir Mustafic [goetas]
  46. * KITAITI Makoto [KitaitiMakoto]
  47. * Jacob Floyd [cognifloyd]
  48. *
  49. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  50. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  51. *
  52. * LICENSE :
  53. *
  54. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  55. * of this software and associated documentation files (the "Software"), to deal
  56. * in the Software without restriction, including without limitation the rights
  57. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  58. * copies of the Software, and to permit persons to whom the Software is
  59. * furnished to do so, subject to the following conditions:
  60. *
  61. * The above copyright notice and this permission notice shall be included in
  62. * all copies or substantial portions of the Software.
  63. *
  64. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  65. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  66. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  67. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  68. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  69. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  70. * THE SOFTWARE.
  71. *
  72. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  73. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  74. *
  75. * @category HTML5 parsing
  76. * @package HTML5
  77. * @author John Slegers
  78. * @copyright MMXIV John Slegers
  79. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  80. * @link https://github.com/jslegers
  81. *
  82. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  83. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  84. */
  85. namespace PowerTools;
  86. /**
  87. * Manage entity references.
  88. *
  89. * This is a simple resolver for HTML5 character reference entitites.
  90. * See HTML5_Entities for the list of supported entities.
  91. */
  92. class HTML5_Parser_CharacterReference {
  93. protected static $numeric_mask = array(
  94. 0x0,
  95. 0x2FFFF,
  96. 0,
  97. 0xFFFF
  98. );
  99. /**
  100. * Given a name (e.g.
  101. * 'amp'), lookup the UTF-8 character ('&')
  102. *
  103. * @param string $name
  104. * The name to look up.
  105. * @return string The character sequence. In UTF-8 this may be more than one byte.
  106. */
  107. public static function lookupName($name) {
  108. // Do we really want to return NULL here? or FFFD
  109. return isset(HTML5_Entities::$byName[$name]) ? HTML5_Entities::$byName[$name] : null;
  110. }
  111. /**
  112. * Given a Unicode codepoint, return the UTF-8 character.
  113. *
  114. * (NOT USED ANYWHERE)
  115. */
  116. /*
  117. * public static function lookupCode($codePoint) { return 'POINT'; }
  118. */
  119. /**
  120. * Given a decimal number, return the UTF-8 character.
  121. */
  122. public static function lookupDecimal($int) {
  123. $entity = '&#' . $int . ';';
  124. // UNTESTED: This may fail on some planes. Couldn't find full documentation
  125. // on the value of the mask array.
  126. return mb_decode_numericentity($entity, static::$numeric_mask, 'utf-8');
  127. }
  128. /**
  129. * Given a hexidecimal number, return the UTF-8 character.
  130. */
  131. public static function lookupHex($hexdec) {
  132. return static::lookupDecimal(hexdec($hexdec));
  133. }
  134. }