PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/slow/ext_string/htmlspecialchars.php

http://github.com/facebook/hiphop-php
PHP | 88 lines | 73 code | 12 blank | 3 comment | 2 complexity | 8bd3c5f04a03e8c9d2f3a333fd6e15f8 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. function VS($x, $y) {
  3. var_dump($x === $y);
  4. if ($x !== $y) { echo "Failed: $y\n"; echo "Got: $x\n";
  5. var_dump(debug_backtrace()); }
  6. }
  7. function VERIFY($x) { VS($x != false, true); }
  8. // Php doesn't support \u escapes.
  9. function u($x) { return json_decode("\"" . $x . "\""); }
  10. //////////////////////////////////////////////////////////////////////
  11. function test_htmlspecialchars_decode() {
  12. $str = "<p>this -&gt; &quot;</p>";
  13. VS(htmlspecialchars_decode($str), "<p>this -> \"</p>");
  14. VS(htmlspecialchars_decode("&lt;"), "<");
  15. VS(htmlspecialchars_decode("&nbsp;"), "&nbsp;");
  16. VS(htmlspecialchars_decode("&amp; &Eacute; &Alpha; &#039;"),
  17. "& &Eacute; &Alpha; '");
  18. }
  19. function test_htmlspecialchars() {
  20. VS(htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES),
  21. "&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;");
  22. VS(bin2hex(htmlspecialchars("\xA0", ENT_COMPAT)), "");
  23. VS(bin2hex(htmlspecialchars("\xc2\xA0", ENT_COMPAT, "")), "c2a0");
  24. VS(bin2hex(htmlspecialchars("\xc2\xA0", ENT_COMPAT, "UTF-8")), "c2a0");
  25. $zfoo = "\0foo";
  26. VS(htmlspecialchars($zfoo, ENT_COMPAT), $zfoo);
  27. VS(fb_htmlspecialchars($zfoo, ENT_COMPAT), $zfoo);
  28. VS(fb_htmlspecialchars("abcdef'\"{}@gz", ENT_QUOTES,
  29. "", varray["z"]),
  30. "abcdef&#039;&quot;&#123;&#125;&#064;g&#122;");
  31. VS(fb_htmlspecialchars("abcdef'\"".u('\u00a1\uabcd'), ENT_FB_UTF8,
  32. "", varray["d"]),
  33. "abc&#100;ef&#039;&quot;&#xa1;&#xabcd;");
  34. VS(fb_htmlspecialchars("abcdef'\"".u('\u00a1\uabcd'), ENT_FB_UTF8_ONLY,
  35. "", varray["d"]),
  36. "abcdef'\"&#xa1;&#xabcd;");
  37. // The rest here expects RuntimeOption::Utf8izeReplace = true;
  38. $input =
  39. u('\u00a1')."\xc2\x41".
  40. u('\u0561')."\xd5\xe0".
  41. u('\u3862')."\xe3\x80\xf0".
  42. "\xf0\xa1\xa2\xa3".
  43. "\xf0\xa1\xa2\x41".
  44. "hello\x80world".
  45. "\xed\xa0\x80".
  46. "\xe0\x80\xbc".
  47. "\xc2";
  48. $tmp = $input;
  49. fb_utf8ize(inout $tmp);
  50. $sanitized = $tmp;
  51. VS(fb_htmlspecialchars($input, ENT_QUOTES, "UtF-8", varray[]),
  52. $sanitized);
  53. VS(fb_htmlspecialchars($input, ENT_FB_UTF8, "utf-8", varray[]),
  54. '&#xa1;&#xfffd;A'.
  55. '&#x561;&#xfffd;&#xfffd;'.
  56. '&#x3862;&#xfffd;&#xfffd;'.
  57. '&#x218a3;&#xfffd;A'.
  58. 'hello&#xfffd;world'.
  59. '&#xfffd;'.
  60. '&#xfffd;'.
  61. '&#xfffd;');
  62. VS(fb_htmlspecialchars($sanitized, ENT_QUOTES, "", varray[]),
  63. $sanitized);
  64. VS(fb_htmlspecialchars($zfoo, ENT_COMPAT, "UTF-8"), u('\ufffd')."foo");
  65. }
  66. <<__EntryPoint>>
  67. function main_htmlspecialchars() {
  68. test_htmlspecialchars_decode();
  69. test_htmlspecialchars();
  70. }