暂无描述

test.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. });
  42. describe("Decode test", function(){
  43. var testcases = [
  44. { input: "&", output: "&" },
  45. { input: "&", output: "&" },
  46. { input: "&", output: "&" },
  47. { input: "&", output: "&" },
  48. { input: "&", output: "&" },
  49. { input: "&", output: "&" },
  50. { input: "&", output: "&" },
  51. { input: ":", output: ":" },
  52. { input: ":", output: ":" },
  53. { input: ":", output: ":" },
  54. { input: ":", output: ":" }
  55. ];
  56. testcases.forEach(function(tc) {
  57. it("should XML decode " + tc.input, function(){
  58. assert.equal(entities.decodeXML(tc.input), tc.output);
  59. });
  60. it("should HTML4 decode " + tc.input, function(){
  61. assert.equal(entities.decodeHTML(tc.input), tc.output);
  62. });
  63. it("should HTML5 decode " + tc.input, function(){
  64. assert.equal(entities.decodeHTML(tc.input), tc.output);
  65. });
  66. });
  67. });
  68. var levels = ["xml", "entities"];
  69. describe("Documents", function(){
  70. levels
  71. .map(function(n){ return path.join("..", "maps", n); })
  72. .map(require)
  73. .forEach(function(doc, i){
  74. describe("Decode", function(){
  75. it(levels[i], function(){
  76. Object.keys(doc).forEach(function(e){
  77. for(var l = i; l < levels.length; l++){
  78. assert.equal(entities.decode("&" + e + ";", l), doc[e]);
  79. }
  80. });
  81. });
  82. });
  83. describe("Decode strict", function(){
  84. it(levels[i], function(){
  85. Object.keys(doc).forEach(function(e){
  86. for(var l = i; l < levels.length; l++){
  87. assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
  88. }
  89. });
  90. });
  91. });
  92. describe("Encode", function(){
  93. it(levels[i], function(){
  94. Object.keys(doc).forEach(function(e){
  95. for(var l = i; l < levels.length; l++){
  96. assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
  97. }
  98. });
  99. });
  100. });
  101. });
  102. var legacy = require("../maps/legacy.json");
  103. describe("Legacy", function(){
  104. it("should decode", runLegacy);
  105. });
  106. function runLegacy(){
  107. Object.keys(legacy).forEach(function(e){
  108. assert.equal(entities.decodeHTML("&" + e), legacy[e]);
  109. });
  110. }
  111. });
  112. var astral = {
  113. "1D306": "\uD834\uDF06",
  114. "1D11E": "\uD834\uDD1E"
  115. };
  116. var astralSpecial = {
  117. "80": "\u20AC",
  118. "110000": "\uFFFD"
  119. };
  120. describe("Astral entities", function(){
  121. Object.keys(astral).forEach(function(c){
  122. it("should decode " + astral[c], function(){
  123. assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
  124. });
  125. it("should encode " + astral[c], function(){
  126. assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
  127. });
  128. });
  129. Object.keys(astralSpecial).forEach(function(c){
  130. it("special should decode \\u" + c, function(){
  131. assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
  132. });
  133. });
  134. });