/ Published in: JavaScript
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/***************************************** * String encoding/decoding functions * * This work is licensed under a Creative Commons Attribution 3.0 Unported License * http://creativecommons.org/licenses/by/3.0/ * * Author: Andy Harrison, http://dragonzreef.com/ * Date: 16 September 2011 *****************************************/ String.prototype.encodeToUrlPart = function(){ return encodeURIComponent(this); }; String.prototype.decodeFromUrlPart = function(){ return decodeURIComponent(this); }; String.prototype.encodeToUrl = function(){ return encodeURI(this); }; String.prototype.decodeFromUrl = function(){ return decodeURI(this); }; //HTML-escapes amphersands that do not designate a character reference String.prototype.encodeIncongruousAmphersands = function() { var str = this.toString(); if(!str) return ""; //escape amphersands that are obviously not designating character references str = str.replace(/&#x([A-F0-9]*(?:[^A-F0-9;]|$))/ig, "&#x$1"); str = str.replace(/&#(?!x)([0-9]*(?:[^0-9;]|$))/ig, "&#$1"); str = str.replace(/&(?!#)([a-z0-9]*(?:[^a-z0-9;]|$))/ig, "&$1"); //for substrings formatted as character entity references, make sure entity names are valid //see http://www.w3.org/TR/html401/sgml/entities.html var validEntityNames = ""+ //markup-significant and internationalization characters "quot|amp|lt|gt|OElig|oelig|Scaron|scaron|Yuml|circ|tilde|ensp|emsp|thinsp|zwnj|zwj|lrm|"+ "rlm|ndash|mdash|lsquo|rsquo|sbquo|ldquo|rdquo|bdquo|dagger|Dagger|permil|lsaquo|rsaquo|euro|"+ //ISO 8859-1 characters "nbsp|iexcl|cent|pound|curren|yen|brvbar|sect|uml|copy|ordf|laquo|not|shy|reg|macr|deg|"+ "plusmn|sup2|sup3|acute|micro|para|middot|cedil|sup1|ordm|raquo|frac14|frac12|frac34|iquest|"+ "Agrave|Aacute|Acirc|Atilde|Auml|Aring|AElig|Ccedil|Egrave|Eacute|Ecirc|Euml|Igrave|Iacute|"+ "Icirc|Iuml|ETH|Ntilde|Ograve|Oacute|Ocirc|Otilde|Ouml|times|Oslash|Ugrave|Uacute|Ucirc|Uuml|"+ "Yacute|THORN|szlig|agrave|aacute|acirc|atilde|auml|aring|aelig|ccedil|egrave|eacute|ecirc|euml|"+ "igrave|iacute|icirc|iuml|eth|ntilde|ograve|oacute|ocirc|otilde|ouml|divide|oslash|ugrave|"+ "uacute|ucirc|uuml|yacute|thorn|yuml|"+ //symbols, mathematical symbols, and Greek letters "fnof|Alpha|Beta|Gamma|Delta|Epsilon|Zeta|Eta|Theta|Iota|Kappa|Lambda|Mu|Nu|Xi|Omicron|Pi|"+ "Rho|Sigma|Tau|Upsilon|Phi|Chi|Psi|Omega|alpha|beta|gamma|delta|epsilon|zeta|eta|theta|iota|"+ "kappa|lambda|mu|nu|xi|omicron|pi|rho|sigmaf|sigma|tau|upsilon|phi|chi|psi|omega|thetasym|"+ "upsih|piv|bull|hellip|prime|Prime|oline|frasl|weierp|image|real|trade|alefsym|larr|uarr|rarr|"+ "darr|harr|crarr|lArr|uArr|rArr|dArr|hArr|forall|part|exist|empty|nabla|isin|notin|ni|prod|"+ "sum|minus|lowast|radic|prop|infin|ang|and|or|cap|cup|int|there4|sim|cong|asymp|ne|equiv|"+ "le|ge|sub|sup|nsub|sube|supe|oplus|otimes|perp|sdot|lceil|rceil|lfloor|rfloor|lang|rang|"+ "loz|spades|clubs|hearts|diams"; var rxp = new RegExp("&(?!("+validEntityNames+");)([a-zA-Z0-9]+;)", "g"); str.replace(rxp, "&$1"); return str; }; String.prototype.encodeToHtml = function(keepValidEntities) { var str = this.toString(); if(!str) return ""; str = keepValidEntities ? str.encodeIncongruousAmphersands() : str.replace(/&/g, "&"); str = str.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'"); return str; }; //unescapes all entities in the string, not just the markup-significant characters String.prototype.decodeFromHtml = function() { var str = this.toString(); if(!str) return ""; var tmp = document.createElement("div"); tmp.innerHTML = str; return tmp.firstChild.nodeValue; }; //escapes the string for use as a JavaScript string in embedded or inline code //See http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript String.prototype.encodeToJavaScriptString = function() { var str = this.toString(); if(!str) return ""; str = str.replace(/\\/g, "\\\\"); //prevent "escape from the quote" attacks by escaping quotes and line feed characters str = str.replace(/'/g, "\\u0027").replace(/"/g, "\\u0022"); str = str.replace(/\u0009/ig, "\\t").replace(/\u000A/ig, "\\n").replace(/\u000D/ig, "\\r").replace(/\u0085/ig, "\\u0085"); //tab, line feed, carriage return, next line str = str.replace(/\u2028/ig, "\\u2028").replace(/\u2029/ig, "\\u2029"); //line separator, paragraph separator //prevent string from closing the tag str = str.replace(/</g, "\\u003C").replace(/>/g, "\\u003E"); //these are escaped just in case ("defense-in-depth") str = str.replace(/&/g, "\\u0026").replace(/=/g, "\\u003D"); return str; };