diff --git a/index.html b/index.html index 3d4d773..5308410 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,7 @@ Base64 Encode/Decode JWT Decode URL Encode/Decode + HTML Encode/Decode JSON Formatter XML Formatter @@ -260,6 +261,50 @@

XML Formatter

                                 
+
+                            
+
+ + + +
+
+
+
+

HTML Encode/Decode

+
+
+
+
+ Input +
+
+
+
+ +
+
+
+
+ +
+
+
+ +
+
+
+   +
+
+
+ Output +
+
+
+
+
+                                
 
                             
diff --git a/js/scripts.js b/js/scripts.js index 873de35..54c90c0 100755 --- a/js/scripts.js +++ b/js/scripts.js @@ -6,6 +6,8 @@ $(document).ready(function(){ $('#btnUrlDecode').click(function (){urlDecode();}); $('#btnJsonFormat').click(function (){jsonFormat();}); $('#btnXmlFormat').click(function (){xmlFormat();}); + $('#btnHtmlEncode').click(function (){htmlEncode();}); + $('#btnHtmlDecode').click(function (){htmlDecode();}); }); function base64encode(){ @@ -112,6 +114,26 @@ function xmlFormat(sourceXml){ } }; +function htmlEncode(){ + var text = get('textAreaHtmlInput').value; + var encoded = text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); + get('preHtmlOutputCode').innerText = encoded; +} + +function htmlDecode(){ + var text = get('textAreaHtmlInput').value; + // Uses the browser's HTML parser for decoding entities, which may handle certain edge cases differently than manual replacements. + var parser = new DOMParser(); + var dom = parser.parseFromString(text, 'text/html'); + var decoded = dom.documentElement.textContent; + get('preHtmlOutputCode').innerText = decoded; +} + function get(id){ return document.getElementById(id); }