Kaido Kert | f585e26 | 2020-06-08 11:42:28 -0700 | [diff] [blame] | 1 | var test = require('tape'); |
| 2 | var punycode = require('punycode'); |
| 3 | var ent = require('../'); |
| 4 | |
| 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 | |
| 13 | test('html', function (t) { |
| 14 | var a = '<html> © π " \''; |
| 15 | var b = '<html> © π " ''; |
| 16 | t.equal(ent.encode(a), b); |
| 17 | t.equal(ent.decode(b), a); |
| 18 | t.end(); |
| 19 | }); |
| 20 | |
| 21 | test('html named', function (t) { |
| 22 | var a = '<html> © π " \' ∴ Β β'; |
| 23 | var b = '<html> © π " ' ∴ Β β'; |
| 24 | t.equal(ent.encode(a, { named: true }), b); |
| 25 | t.equal(ent.decode(b), a); |
| 26 | t.end(); |
| 27 | }); |
| 28 | |
| 29 | test('ambiguous ampersands', function (t) { |
| 30 | var a = '• &bullet'; |
| 31 | var b = '• &bullet'; |
| 32 | var c = '• &bullet'; |
| 33 | t.equal(ent.encode(a, { named: true }), c); |
| 34 | t.equal(ent.decode(b), a); |
| 35 | t.equal(ent.decode(c), a); |
| 36 | t.end(); |
| 37 | }); |
| 38 | |
| 39 | test('entities', function (t) { |
| 40 | var a = '\u2124'; |
| 41 | var b = 'ℤ'; |
| 42 | t.equal(ent.encode(a), b); |
| 43 | t.equal(ent.decode(b), a); |
| 44 | t.end(); |
| 45 | }); |
| 46 | |
| 47 | test('entities named', function (t) { |
| 48 | var a = '\u2124'; |
| 49 | var b = 'ℤ'; |
| 50 | t.equal(ent.encode(a, { named: true }), b); |
| 51 | t.equal(ent.decode(b), a); |
| 52 | t.end(); |
| 53 | }); |
| 54 | |
| 55 | test('num', function (t) { |
| 56 | var a = String.fromCharCode(1337); |
| 57 | var b = 'Թ'; |
| 58 | t.equal(ent.encode(a), b); |
| 59 | t.equal(ent.decode(b), a); |
| 60 | |
| 61 | t.equal(ent.encode(a + a), b + b); |
| 62 | t.equal(ent.decode(b + b), a + a); |
| 63 | t.end(); |
| 64 | }); |
| 65 | |
| 66 | test('astral num', function (t) { |
| 67 | var a = punycode.ucs2.encode([0x1d306]); |
| 68 | var b = '𝌆'; |
| 69 | t.equal(ent.encode(a), b); |
| 70 | t.equal(ent.decode(b), a); |
| 71 | |
| 72 | t.equal(ent.encode(a + a), b + b); |
| 73 | t.equal(ent.decode(b + b), a + a); |
| 74 | t.end(); |
| 75 | }); |
| 76 | |
| 77 | test('nested escapes', function (t) { |
| 78 | var a = '&'; |
| 79 | var b = '&amp;'; |
| 80 | t.equal(ent.encode(a), b); |
| 81 | t.equal(ent.decode(b), a); |
| 82 | |
| 83 | t.equal(ent.encode(a + a), b + b); |
| 84 | t.equal(ent.decode(b + b), a + a); |
| 85 | t.end(); |
| 86 | }); |
| 87 | |
| 88 | test('encode `special` option', function (t) { |
| 89 | var a = '<>\'"&'; |
| 90 | var b = '<>\'"&'; |
| 91 | t.equal(ent.encode(a, { |
| 92 | named: true, |
| 93 | special: { |
| 94 | '<': true, |
| 95 | '>': true, |
| 96 | '&': true |
| 97 | } |
| 98 | }), b); |
| 99 | |
| 100 | t.end(); |
| 101 | }); |