JSEscape.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * @group formatting
  4. */
  5. class Tests_Formatting_JSEscape extends WP_UnitTestCase {
  6. function test_js_escape_simple() {
  7. $out = esc_js('foo bar baz();');
  8. $this->assertEquals('foo bar baz();', $out);
  9. }
  10. function test_js_escape_quotes() {
  11. $out = esc_js('foo "bar" \'baz\'');
  12. // does it make any sense to change " into &quot;? Why not \"?
  13. $this->assertEquals("foo &quot;bar&quot; \'baz\'", $out);
  14. }
  15. function test_js_escape_backslash() {
  16. $bs = '\\';
  17. $out = esc_js('foo '.$bs.'t bar '.$bs.$bs.' baz');
  18. // \t becomes t - bug?
  19. $this->assertEquals('foo t bar '.$bs.$bs.' baz', $out);
  20. }
  21. function test_js_escape_amp() {
  22. $out = esc_js('foo & bar &baz; &apos;');
  23. $this->assertEquals("foo &amp; bar &amp;baz; &apos;", $out);
  24. }
  25. function test_js_escape_quote_entity() {
  26. $out = esc_js('foo &#x27; bar &#39; baz &#x26;');
  27. $this->assertEquals("foo \\' bar \\' baz &amp;", $out);
  28. }
  29. function test_js_no_carriage_return() {
  30. $out = esc_js("foo\rbar\nbaz\r");
  31. // \r is stripped
  32. $this->assertequals("foobar\\nbaz", $out);
  33. }
  34. function test_js_escape_rn() {
  35. $out = esc_js("foo\r\nbar\nbaz\r\n");
  36. // \r is stripped
  37. $this->assertequals("foo\\nbar\\nbaz\\n", $out);
  38. }
  39. }