test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var assert = require("assert"),
  2. path = require("path"),
  3. entities = require("../");
  4. describe("Encode->decode test", function(){
  5. var testcases = [
  6. {
  7. input: "asdf & ÿ ü '",
  8. xml: "asdf & ÿ ü '",
  9. html: "asdf & ÿ ü '"
  10. }, {
  11. input: "&",
  12. xml: "&",
  13. html: "&"
  14. },
  15. ];
  16. testcases.forEach(function(tc) {
  17. var encodedXML = entities.encodeXML(tc.input);
  18. it("should XML encode " + tc.input, function(){
  19. assert.equal(encodedXML, tc.xml);
  20. });
  21. it("should default to XML encode " + tc.input, function(){
  22. assert.equal(entities.encode(tc.input), tc.xml);
  23. });
  24. it("should XML decode " + encodedXML, function(){
  25. assert.equal(entities.decodeXML(encodedXML), tc.input);
  26. });
  27. it("should default to XML encode " + encodedXML, function(){
  28. assert.equal(entities.decode(encodedXML), tc.input);
  29. });
  30. it("should default strict to XML encode " + encodedXML, function(){
  31. assert.equal(entities.decodeStrict(encodedXML), tc.input);
  32. });
  33. var encodedHTML5 = entities.encodeHTML5(tc.input);
  34. it("should HTML5 encode " + tc.input, function(){
  35. assert.equal(encodedHTML5, tc.html);
  36. });
  37. it("should HTML5 decode " + encodedHTML5, function(){
  38. assert.equal(entities.decodeHTML(encodedHTML5), tc.input);
  39. });
  40. });
  41. it("should encode data URIs (issue 16)", function(){
  42. var data = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7";
  43. assert.equal(entities.decode(entities.encode(data)), data);
  44. });
  45. });
  46. describe("Decode test", function(){
  47. var testcases = [
  48. { input: "&", output: "&" },
  49. { input: "&", output: "&" },
  50. { input: "&", output: "&" },
  51. { input: "&", output: "&" },
  52. { input: "&", output: "&" },
  53. { input: "&", output: "&" },
  54. { input: "&", output: "&" },
  55. { input: ":", output: ":" },
  56. { input: ":", output: ":" },
  57. { input: ":", output: ":" },
  58. { input: ":", output: ":" }
  59. ];
  60. testcases.forEach(function(tc) {
  61. it("should XML decode " + tc.input, function(){
  62. assert.equal(entities.decodeXML(tc.input), tc.output);
  63. });
  64. it("should HTML4 decode " + tc.input, function(){
  65. assert.equal(entities.decodeHTML(tc.input), tc.output);
  66. });
  67. it("should HTML5 decode " + tc.input, function(){
  68. assert.equal(entities.decodeHTML(tc.input), tc.output);
  69. });
  70. });
  71. });
  72. var levels = ["xml", "entities"];
  73. describe("Documents", function(){
  74. levels
  75. .map(function(n){ return path.join("..", "maps", n); })
  76. .map(require)
  77. .forEach(function(doc, i){
  78. describe("Decode", function(){
  79. it(levels[i], function(){
  80. Object.keys(doc).forEach(function(e){
  81. for(var l = i; l < levels.length; l++){
  82. assert.equal(entities.decode("&" + e + ";", l), doc[e]);
  83. }
  84. });
  85. });
  86. });
  87. describe("Decode strict", function(){
  88. it(levels[i], function(){
  89. Object.keys(doc).forEach(function(e){
  90. for(var l = i; l < levels.length; l++){
  91. assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
  92. }
  93. });
  94. });
  95. });
  96. describe("Encode", function(){
  97. it(levels[i], function(){
  98. Object.keys(doc).forEach(function(e){
  99. for(var l = i; l < levels.length; l++){
  100. assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
  101. }
  102. });
  103. });
  104. });
  105. });
  106. var legacy = require("../maps/legacy.json");
  107. describe("Legacy", function(){
  108. it("should decode", runLegacy);
  109. });
  110. function runLegacy(){
  111. Object.keys(legacy).forEach(function(e){
  112. assert.equal(entities.decodeHTML("&" + e), legacy[e]);
  113. });
  114. }
  115. });
  116. var astral = {
  117. "1D306": "\uD834\uDF06",
  118. "1D11E": "\uD834\uDD1E"
  119. };
  120. var astralSpecial = {
  121. "80": "\u20AC",
  122. "110000": "\uFFFD"
  123. };
  124. describe("Astral entities", function(){
  125. Object.keys(astral).forEach(function(c){
  126. it("should decode " + astral[c], function(){
  127. assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
  128. });
  129. it("should encode " + astral[c], function(){
  130. assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
  131. });
  132. it("should escape " + astral[c], function(){
  133. assert.equal(entities.escape(astral[c]), "&#x" + c + ";");
  134. });
  135. });
  136. Object.keys(astralSpecial).forEach(function(c){
  137. it("special should decode \\u" + c, function(){
  138. assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
  139. });
  140. });
  141. });
  142. describe("Escape", function(){
  143. it("should always decode ASCII chars", function(){
  144. for(var i = 0; i < 0x7F; i++){
  145. var c = String.fromCharCode(i);
  146. assert.equal(entities.decodeXML(entities.escape(c)), c);
  147. }
  148. });
  149. });