From b214744a5d3a4c548f2903caace5bc352b48d985 Mon Sep 17 00:00:00 2001 From: Tobie Langel Date: Wed, 22 Jul 2009 02:17:55 +0200 Subject: [PATCH] Greatly simplify String#(un)escapeHTML and remove their DOM dependencies. --- src/lang/string.js | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/src/lang/string.js b/src/lang/string.js index f89cd13..3327546 100644 --- a/src/lang/string.js +++ b/src/lang/string.js @@ -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,'>'); } /** 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,'>'); - }; -} - -if ('<\n>'.unescapeHTML() !== '<\n>') { - String.prototype.unescapeHTML = function() { - return this.stripTags().replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&'); - }; -}