Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encode: Add options for escape #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions lib/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
34 changes: 25 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ var assert = require("assert"),
describe("Encode->decode test", function(){
var testcases = [
{
input: "asdf & ÿ ü '",
xml: "asdf &amp; &#xFF; &#xFC; &apos;",
html: "asdf &amp; &yuml; &uuml; &apos;"
input: "asdf & ÿ ü ' \"",
xml: "asdf &amp; &#xFF; &#xFC; &apos; &quot;",
html: "asdf &amp; &yuml; &uuml; &apos; &quot;",
special: "asdf &amp; ÿ ü ' \"",
specialQuot: "asdf &amp; ÿ ü &apos; &quot;"
}, {
input: "&#38;",
xml: "&amp;#38;",
html: "&amp;&num;38&semi;"
html: "&amp;&num;38&semi;",
special: "&amp;#38;",
specialQuot: "&amp;#38;"
},
];
testcases.forEach(function(tc) {
Expand Down Expand Up @@ -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){
Expand All @@ -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)), "&#xFF; &#xFC;");
});
});