From feb7939488c501733837edf0549d1b34c519e7ee Mon Sep 17 00:00:00 2001 From: Manuel Friedli Date: Thu, 26 Mar 2015 22:06:54 +0100 Subject: [PATCH] add HTML entity de- and encoding --- dencode.js | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/dencode.js b/dencode.js index f7305ba..315cae6 100644 --- a/dencode.js +++ b/dencode.js @@ -3,7 +3,7 @@ { "id": "CHOOSE", "name": "Please choose your conversion ...", - "convert": function(input) { + "convert": function (input) { return { "status": "OK", "content": "" @@ -13,7 +13,7 @@ { "id": "base64encode", "name": "Base64 encode", - "convert": function(input) { + "convert": function (input) { return { "status": "OK", "content": btoa(input) @@ -23,7 +23,7 @@ { "id": "base64decode", "name": "Base64 decode", - "convert": function(input) { + "convert": function (input) { try { return { "status": "OK", @@ -40,7 +40,7 @@ { "id": "encodeuri", "name": "Encode URI", - "convert": function(input) { + "convert": function (input) { return { "status": "OK", "content": encodeURI(input).replace(/%5B/g, '[').replace(/%5D/g, ']') @@ -50,7 +50,7 @@ { "id": "decodeuri", "name": "Decode URI", - "convert": function(input) { + "convert": function (input) { try { return { "status": "OK", @@ -67,7 +67,7 @@ { "id": "encodeuricomponent", "name": "Encode URI component", - "convert": function(input) { + "convert": function (input) { return { "status": "OK", "content": encodeURIComponent(input).replace(/[!'()*]/g, function(c) { @@ -79,7 +79,7 @@ { "id": "decodeuricomponent", "name": "Decode URI component", - "convert": function(input) { + "convert": function (input) { try { return { "status": "OK", @@ -92,6 +92,41 @@ }; } } + }, + { + "id": "encodehtmlentities", + "name": "Encode HTML entities", + "convert": function (input) { + return { + "status": "OK", + "content": input + .replace(/\&/g, "&") + .replace(/\/g, ">") + .replace(/\"/g, """) + }; + } + }, + { + "id": "decodehtmlentities", + "name": "Decode HTML entities", + "convert": function (input) { + try { + return { + "status": "OK", + "content": input + .replace(/\"\;/g, "\"") + .replace(/\>\;/g, ">") + .replace(/\<\;/g, "<") + .replace(/\&\;/g, "&") + }; + } catch (exception) { + return { + "status": "ERROR", + "content": "Invalid HTML entity string." + }; + } + } } ]; @@ -151,7 +186,7 @@ } if (appendNewOutput) { if (output !== "") { - $output = $(template.replace(/\{index\}/g, "" + outputIndex).replace(/\{content\}/g, output)); + $output = $(template.replace(/\{index\}/g, "" + outputIndex).replace(/\{content\}/g, output.replace(/\&/g, "&"))); if (status === "ERROR") { $output.find("textarea").addClass("error"); }