EntityParserTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. class HTMLPurifier_EntityParserTest extends HTMLPurifier_Harness
  3. {
  4. protected $EntityParser;
  5. protected $_entity_lookup;
  6. public function setUp()
  7. {
  8. $this->EntityParser = new HTMLPurifier_EntityParser();
  9. $this->_entity_lookup = HTMLPurifier_EntityLookup::instance();
  10. }
  11. public function test_substituteNonSpecialEntities()
  12. {
  13. $char_theta = $this->_entity_lookup->table['theta'];
  14. $this->assertIdentical($char_theta,
  15. $this->EntityParser->substituteNonSpecialEntities('&theta;') );
  16. $this->assertIdentical($char_theta,
  17. $this->EntityParser->substituteTextEntities('&theta;') );
  18. $this->assertIdentical('"',
  19. $this->EntityParser->substituteNonSpecialEntities('"') );
  20. $this->assertIdentical('"',
  21. $this->EntityParser->substituteTextEntities('"') );
  22. // numeric tests, adapted from Feyd
  23. $args = array();
  24. $args[] = array(1114112,false );
  25. $args[] = array(1114111,'F48FBFBF'); // 0x0010FFFF
  26. $args[] = array(1048576,'F4808080'); // 0x00100000
  27. $args[] = array(1048575,'F3BFBFBF'); // 0x000FFFFF
  28. $args[] = array(262144, 'F1808080'); // 0x00040000
  29. $args[] = array(262143, 'F0BFBFBF'); // 0x0003FFFF
  30. $args[] = array(65536, 'F0908080'); // 0x00010000
  31. $args[] = array(65535, 'EFBFBF' ); // 0x0000FFFF
  32. $args[] = array(57344, 'EE8080' ); // 0x0000E000
  33. $args[] = array(57343, false ); // 0x0000DFFF these are ill-formed
  34. $args[] = array(56040, false ); // 0x0000DAE8 these are ill-formed
  35. $args[] = array(55296, false ); // 0x0000D800 these are ill-formed
  36. $args[] = array(55295, 'ED9FBF' ); // 0x0000D7FF
  37. $args[] = array(53248, 'ED8080' ); // 0x0000D000
  38. $args[] = array(53247, 'ECBFBF' ); // 0x0000CFFF
  39. $args[] = array(4096, 'E18080' ); // 0x00001000
  40. $args[] = array(4095, 'E0BFBF' ); // 0x00000FFF
  41. $args[] = array(2048, 'E0A080' ); // 0x00000800
  42. $args[] = array(2047, 'DFBF' ); // 0x000007FF
  43. $args[] = array(128, 'C280' ); // 0x00000080 invalid SGML char
  44. $args[] = array(127, '7F' ); // 0x0000007F invalid SGML char
  45. $args[] = array(0, '00' ); // 0x00000000 invalid SGML char
  46. $args[] = array(20108, 'E4BA8C' ); // 0x00004E8C
  47. $args[] = array(77, '4D' ); // 0x0000004D
  48. $args[] = array(66306, 'F0908C82'); // 0x00010302
  49. $args[] = array(1072, 'D0B0' ); // 0x00000430
  50. foreach ($args as $arg) {
  51. $string = '&#' . $arg[0] . ';' . // decimal
  52. '&#x' . dechex($arg[0]) . ';'; // hex
  53. $expect = '';
  54. if ($arg[1] !== false) {
  55. // this is only for PHP 5, the below is PHP 5 and PHP 4
  56. //$chars = str_split($arg[1], 2);
  57. $chars = array();
  58. // strlen must be called in loop because strings size changes
  59. for ($i = 0; strlen($arg[1]) > $i; $i += 2) {
  60. $chars[] = $arg[1][$i] . $arg[1][$i+1];
  61. }
  62. foreach ($chars as $char) {
  63. $expect .= chr(hexdec($char));
  64. }
  65. $expect .= $expect; // double it
  66. }
  67. $this->assertIdentical(
  68. $this->EntityParser->substituteNonSpecialEntities($string),
  69. $expect,
  70. 'Identical expectation [Hex: '. dechex($arg[0]) .']'
  71. );
  72. $this->assertIdentical(
  73. $this->EntityParser->substituteTextEntities($string),
  74. $expect,
  75. 'Identical expectation [Hex: '. dechex($arg[0]) .']'
  76. );
  77. }
  78. }
  79. public function test_substituteSpecialEntities()
  80. {
  81. $this->assertIdentical(
  82. "'",
  83. $this->EntityParser->substituteSpecialEntities('&#39;')
  84. );
  85. $this->assertIdentical(
  86. "'",
  87. $this->EntityParser->substituteTextEntities('&#39;')
  88. );
  89. }
  90. }
  91. // vim: et sw=4 sts=4