codes.js 2.3 KB

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