codes.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var test = require('tape');
  2. var punycode = require('punycode');
  3. var ent = require('../');
  4. test('amp', function (t) {
  5. var a = 'a & b & c';
  6. var b = 'a & b & c';
  7. t.equal(ent.encode(a), b);
  8. t.equal(ent.decode(b), a);
  9. t.end();
  10. });
  11. test('html', function (t) {
  12. var a = '<html> © π " \'';
  13. var b = '&#60;html&#62; &#169; &#960; &#34; &#39;';
  14. t.equal(ent.encode(a), b);
  15. t.equal(ent.decode(b), a);
  16. t.end();
  17. });
  18. test('html named', function (t) {
  19. var a = '<html> © π " \' ∴ Β β';
  20. var b = '&lt;html&gt; &copy; &pi; &quot; &apos; &therefore; &Beta; &beta;';
  21. t.equal(ent.encode(a, { named: true }), b);
  22. t.equal(ent.decode(b), a);
  23. t.end();
  24. });
  25. test('ambiguous ampersands', function (t) {
  26. var a = '• &bullet';
  27. var b = '&bullet; &bullet';
  28. var c = '&bullet; &amp;bullet';
  29. t.equal(ent.encode(a, { named: true }), c);
  30. t.equal(ent.decode(b), a);
  31. t.equal(ent.decode(c), a);
  32. t.end();
  33. });
  34. test('entities', function (t) {
  35. var a = '\u2124';
  36. var b = '&#8484;';
  37. t.equal(ent.encode(a), b);
  38. t.equal(ent.decode(b), a);
  39. t.end();
  40. });
  41. test('entities named', function (t) {
  42. var a = '\u2124';
  43. var b = '&Zopf;';
  44. t.equal(ent.encode(a, { named: true }), b);
  45. t.equal(ent.decode(b), a);
  46. t.end();
  47. });
  48. test('num', function (t) {
  49. var a = String.fromCharCode(1337);
  50. var b = '&#1337;';
  51. t.equal(ent.encode(a), b);
  52. t.equal(ent.decode(b), a);
  53. t.equal(ent.encode(a + a), b + b);
  54. t.equal(ent.decode(b + b), a + a);
  55. t.end();
  56. });
  57. test('astral num', function (t) {
  58. var a = punycode.ucs2.encode([0x1d306]);
  59. var b = '&#119558;';
  60. t.equal(ent.encode(a), b);
  61. t.equal(ent.decode(b), a);
  62. t.equal(ent.encode(a + a), b + b);
  63. t.equal(ent.decode(b + b), a + a);
  64. t.end();
  65. });
  66. test('nested escapes', function (t) {
  67. var a = '&amp;';
  68. var b = '&#38;amp;';
  69. t.equal(ent.encode(a), b);
  70. t.equal(ent.decode(b), a);
  71. t.equal(ent.encode(a + a), b + b);
  72. t.equal(ent.decode(b + b), a + a);
  73. t.end();
  74. });
  75. test('encode `special` option', function (t) {
  76. var a = '<>\'"&';
  77. var b = '&lt;&gt;\'"&amp;';
  78. t.equal(ent.encode(a, {
  79. named: true,
  80. special: {
  81. '<': true,
  82. '>': true,
  83. '&': true
  84. }
  85. }), b);
  86. t.end();
  87. });