react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / htmlparser2 / node_modules / domutils / node_modules / dom-serializer / node_modules / entities / test / test.js
81169 viewsvar assert = require("assert"),1path = require("path"),2entities = require("../");34describe("Encode->decode test", function(){5var testcases = [6{7input: "asdf & ÿ ü '",8xml: "asdf & ÿ ü '",9html: "asdf & ÿ ü '"10}, {11input: "&",12xml: "&#38;",13html: "&#38;"14},15];16testcases.forEach(function(tc) {17var encodedXML = entities.encodeXML(tc.input);18it("should XML encode " + tc.input, function(){19assert.equal(encodedXML, tc.xml);20});21it("should default to XML encode " + tc.input, function(){22assert.equal(entities.encode(tc.input), tc.xml);23});24it("should XML decode " + encodedXML, function(){25assert.equal(entities.decodeXML(encodedXML), tc.input);26});27it("should default to XML encode " + encodedXML, function(){28assert.equal(entities.decode(encodedXML), tc.input);29});30it("should default strict to XML encode " + encodedXML, function(){31assert.equal(entities.decodeStrict(encodedXML), tc.input);32});3334var encodedHTML5 = entities.encodeHTML5(tc.input);35it("should HTML5 encode " + tc.input, function(){36assert.equal(encodedHTML5, tc.html);37});38it("should HTML5 decode " + encodedHTML5, function(){39assert.equal(entities.decodeHTML(encodedHTML5), tc.input);40});41});4243it("should encode data URIs (issue 16)", function(){44var data = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7";45assert.equal(entities.decode(entities.encode(data)), data);46});47});4849describe("Decode test", function(){50var testcases = [51{ input: "&amp;", output: "&" },52{ input: "&#38;", output: "&" },53{ input: "&#x26;", output: "&" },54{ input: "&#X26;", output: "&" },55{ input: "&#38;", output: "&" },56{ input: "&#38;", output: "&" },57{ input: "&#38;", output: "&" },58{ input: ":", output: ":" },59{ input: ":", output: ":" },60{ input: ":", output: ":" },61{ input: ":", output: ":" }62];63testcases.forEach(function(tc) {64it("should XML decode " + tc.input, function(){65assert.equal(entities.decodeXML(tc.input), tc.output);66});67it("should HTML4 decode " + tc.input, function(){68assert.equal(entities.decodeHTML(tc.input), tc.output);69});70it("should HTML5 decode " + tc.input, function(){71assert.equal(entities.decodeHTML(tc.input), tc.output);72});73});74});7576var levels = ["xml", "entities"];7778describe("Documents", function(){79levels80.map(function(n){ return path.join("..", "maps", n); })81.map(require)82.forEach(function(doc, i){83describe("Decode", function(){84it(levels[i], function(){85Object.keys(doc).forEach(function(e){86for(var l = i; l < levels.length; l++){87assert.equal(entities.decode("&" + e + ";", l), doc[e]);88}89});90});91});9293describe("Decode strict", function(){94it(levels[i], function(){95Object.keys(doc).forEach(function(e){96for(var l = i; l < levels.length; l++){97assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);98}99});100});101});102103describe("Encode", function(){104it(levels[i], function(){105Object.keys(doc).forEach(function(e){106for(var l = i; l < levels.length; l++){107assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);108}109});110});111});112});113114var legacy = require("../maps/legacy.json");115116describe("Legacy", function(){117it("should decode", runLegacy);118});119120function runLegacy(){121Object.keys(legacy).forEach(function(e){122assert.equal(entities.decodeHTML("&" + e), legacy[e]);123});124}125});126127var astral = {128"1D306": "\uD834\uDF06",129"1D11E": "\uD834\uDD1E"130};131132var astralSpecial = {133"80": "\u20AC",134"110000": "\uFFFD"135};136137138describe("Astral entities", function(){139Object.keys(astral).forEach(function(c){140it("should decode " + astral[c], function(){141assert.equal(entities.decode("&#x" + c + ";"), astral[c]);142});143144it("should encode " + astral[c], function(){145assert.equal(entities.encode(astral[c]), "&#x" + c + ";");146});147148it("should escape " + astral[c], function(){149assert.equal(entities.escape(astral[c]), "&#x" + c + ";");150});151});152153Object.keys(astralSpecial).forEach(function(c){154it("special should decode \\u" + c, function(){155assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);156});157});158});159160describe("Escape", function(){161it("should always decode ASCII chars", function(){162for(var i = 0; i < 0x7F; i++){163var c = String.fromCharCode(i);164assert.equal(entities.decodeXML(entities.escape(c)), c);165}166});167});168169170