Greatly simplify String#(un)escapeHTML and remove their DOM dependencies.
This commit is contained in:
parent
b039a4791c
commit
b214744a5d
|
@ -170,8 +170,7 @@ Object.extend(String.prototype, (function() {
|
|||
* Converts HTML special characters to their entity equivalents.
|
||||
**/
|
||||
function escapeHTML() {
|
||||
escapeHTML.text.data = this;
|
||||
return escapeHTML.div.innerHTML;
|
||||
return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||||
}
|
||||
|
||||
/** related to: String#escapeHTML
|
||||
|
@ -181,11 +180,8 @@ Object.extend(String.prototype, (function() {
|
|||
* to their normal form.
|
||||
**/
|
||||
function unescapeHTML() {
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = this.stripTags();
|
||||
return div.childNodes[0] ? (div.childNodes.length > 1 ?
|
||||
$A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) :
|
||||
div.childNodes[0].nodeValue) : '';
|
||||
// Warning: In 1.7 String#unescapeHTML will no longer call String#stripTags.
|
||||
return this.stripTags().replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -469,21 +465,3 @@ Object.extend(String.prototype, (function() {
|
|||
};
|
||||
})());
|
||||
|
||||
Object.extend(String.prototype.escapeHTML, {
|
||||
div: document.createElement('div'),
|
||||
text: document.createTextNode('')
|
||||
});
|
||||
|
||||
String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text);
|
||||
|
||||
if ('<\n>'.escapeHTML() !== '<\n>') {
|
||||
String.prototype.escapeHTML = function() {
|
||||
return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
||||
};
|
||||
}
|
||||
|
||||
if ('<\n>'.unescapeHTML() !== '<\n>') {
|
||||
String.prototype.unescapeHTML = function() {
|
||||
return this.stripTags().replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue