Last active
March 4, 2016 21:04
-
-
Save tigerhawkvok/285b8631ed6ebef4446d to your computer and use it in GitHub Desktop.
Unescape even fairly mutilated escaped string in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String::unescape = (strict = false) -> | |
### | |
# Take escaped text, and return the unescaped version | |
# | |
# @param string str | String to be used | |
# @param bool strict | Stict mode will remove all HTML | |
# | |
# Test it here: | |
# https://jsfiddle.net/tigerhawkvok/t9pn1dn5/ | |
# | |
# Code: https://gist.github.com/tigerhawkvok/285b8631ed6ebef4446d | |
### | |
# Create a dummy element | |
element = document.createElement("div") | |
decodeHTMLEntities = (str) -> | |
if str? and typeof str is "string" | |
unless strict is true | |
# escape HTML tags | |
str = escape(str).replace(/%26/g,'&').replace(/%23/g,'#').replace(/%3B/g,';') | |
else | |
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '') | |
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '') | |
element.innerHTML = str | |
if element.innerText | |
# Do we support innerText? | |
str = element.innerText | |
element.innerText = "" | |
else | |
# Firefox | |
str = element.textContent | |
element.textContent = "" | |
unescape(str) | |
# Remove encoded or double-encoded tags | |
fixHtmlEncodings = (string) -> | |
string = string.replace(/\&#/mg, '&#') # The rest, for double-encodings | |
string = string.replace(/\"/mg, '"') | |
string = string.replace(/\"e;/mg, '"') | |
string = string.replace(/\_/mg, '_') | |
string = string.replace(/\'/mg, "'") | |
string = string.replace(/\"/mg, '"') | |
string = string.replace(/\>/mg, '>') | |
string = string.replace(/\</mg, '<') | |
string | |
# Run it | |
tmp = fixHtmlEncodings(this) | |
decodeHTMLEntities(tmp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.unescape = function(strict) { | |
var decodeHTMLEntities, element, fixHtmlEncodings, tmp; | |
if (strict == null) { | |
strict = false; | |
} | |
/* | |
* Take escaped text, and return the unescaped version | |
* | |
* @param string str | String to be used | |
* @param bool strict | Stict mode will remove all HTML | |
* | |
* Test it here: | |
* https://jsfiddle.net/tigerhawkvok/t9pn1dn5/ | |
* | |
* Code: https://gist.github.com/tigerhawkvok/285b8631ed6ebef4446d | |
*/ | |
element = document.createElement("div"); | |
decodeHTMLEntities = function(str) { | |
if ((str != null) && typeof str === "string") { | |
if (strict !== true) { | |
str = escape(str).replace(/%26/g, '&').replace(/%23/g, '#').replace(/%3B/g, ';'); | |
} else { | |
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, ''); | |
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, ''); | |
} | |
element.innerHTML = str; | |
if (element.innerText) { | |
str = element.innerText; | |
element.innerText = ""; | |
} else { | |
str = element.textContent; | |
element.textContent = ""; | |
} | |
} | |
return unescape(str); | |
}; | |
fixHtmlEncodings = function(string) { | |
string = string.replace(/\&#/mg, '&#'); | |
string = string.replace(/\"/mg, '"'); | |
string = string.replace(/\"e;/mg, '"'); | |
string = string.replace(/\_/mg, '_'); | |
string = string.replace(/\'/mg, "'"); | |
string = string.replace(/\"/mg, '"'); | |
string = string.replace(/\>/mg, '>'); | |
string = string.replace(/\</mg, '<'); | |
return string; | |
}; | |
tmp = fixHtmlEncodings(this); | |
return decodeHTMLEntities(tmp); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment