From 12b763c3d9884e9a3db39b3e12f74b1b743e65ac Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Wed, 3 Feb 2016 12:24:57 +0900 Subject: [PATCH] encode: Add options for escape This allows to escape only characters to be escaped. --- lib/encode.js | 25 +++++++++++++++++++------ test/test.js | 34 +++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/lib/encode.js b/lib/encode.js index 10c0c36e..6c7b75be 100644 --- a/lib/encode.js +++ b/lib/encode.js @@ -61,13 +61,26 @@ function getInverse(inverse, re){ }; } -var re_xmlChars = getInverseReplacer(inverseXML); +function escapeXML(data, escQuot, escApos, outputUtf8){ + var k = ["&", ">", "<"]; -function escapeXML(data){ - return data - .replace(re_xmlChars, singleCharReplacer) - .replace(re_astralSymbols, astralReplacer) - .replace(re_nonASCII, singleCharReplacer); + if (escQuot) { + k.push("\""); + } + + if (escApos) { + k.push("'"); + } + + if (outputUtf8 === false) { + data = data + .replace(re_astralSymbols, astralReplacer) + .replace(re_nonASCII, singleCharReplacer); + } + + return data.replace(new RegExp(k.join("|"), "g"), function(name){ + return inverseXML[name]; + }); } exports.escape = escapeXML; diff --git a/test/test.js b/test/test.js index c61a20fc..f6321ac0 100644 --- a/test/test.js +++ b/test/test.js @@ -5,13 +5,17 @@ var assert = require("assert"), describe("Encode->decode test", function(){ var testcases = [ { - input: "asdf & ÿ ü '", - xml: "asdf & ÿ ü '", - html: "asdf & ÿ ü '" + input: "asdf & ÿ ü ' \"", + xml: "asdf & ÿ ü ' "", + html: "asdf & ÿ ü ' "", + special: "asdf & ÿ ü ' \"", + specialQuot: "asdf & ÿ ü ' "" }, { input: "&", xml: "&#38;", - html: "&#38;" + html: "&#38;", + special: "&#38;", + specialQuot: "&#38;" }, ]; testcases.forEach(function(tc) { @@ -145,10 +149,6 @@ describe("Astral entities", function(){ it("should encode " + astral[c], function(){ assert.equal(entities.encode(astral[c]), "&#x" + c + ";"); }); - - it("should escape " + astral[c], function(){ - assert.equal(entities.escape(astral[c]), "&#x" + c + ";"); - }); }); Object.keys(astralSpecial).forEach(function(c){ @@ -162,7 +162,23 @@ describe("Escape", function(){ it("should always decode ASCII chars", function(){ for(var i = 0; i < 0x7F; i++){ var c = String.fromCharCode(i); - assert.equal(entities.decodeXML(entities.escape(c)), c); + assert.equal(entities.decodeXML(entities.escape(c, true, true, true)), c); } }); + + it("should not escape \" if escQuot is false", function(){ + assert.equal(entities.decodeXML(entities.escape("\"", false, true, true)), "\""); + }); + + it("should not escape ' if escApos is false", function(){ + assert.equal(entities.decodeXML(entities.escape("'", true, false, true)), "'"); + }); + + it("should not escape non-ASCII if outputUtf8 is true", function(){ + assert.equal(entities.decodeXML(entities.escape("ÿ ü", true, true, true)), "ÿ ü"); + }); + + it("should escape non-ASCII if outputUtf8 is false", function(){ + assert.equal(entities.decodeXML(entities.escape("ÿ ü", true, true, false)), "ÿ ü"); + }); });