From c556c89627321c4d10610a210129ef32dd2a46af Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sat, 27 Jan 2007 18:33:03 +0000 Subject: [PATCH] prototype: Improve performance of String.prototype.escapeHTML by using a cached div and text node. Closes #6937. --- CHANGELOG | 2 ++ src/string.js | 14 ++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f3e18b8..2d19d2d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Improve performance of String.prototype.escapeHTML by using a cached div and text node. Closes #6937. [altblue] + * Make setStyle() with opacity: 0 in Internet Explorer work correctly. [Thomas Fuchs] * Form.Element.activate shouldn't raise an exception when the form or field is hidden. [sam] diff --git a/src/string.js b/src/string.js index 2cf17ef..8449934 100644 --- a/src/string.js +++ b/src/string.js @@ -66,10 +66,9 @@ Object.extend(String.prototype, { }, escapeHTML: function() { - var div = document.createElement('div'); - var text = document.createTextNode(this); - div.appendChild(text); - return div.innerHTML; + var self = arguments.callee; + self.text.data = this; + return self.div.innerHTML; }, unescapeHTML: function() { @@ -152,6 +151,13 @@ String.prototype.gsub.prepareReplacement = function(replacement) { String.prototype.parseQuery = String.prototype.toQueryParams; +Object.extend(String.prototype.escapeHTML, { + div: document.createElement('div'), + text: document.createTextNode('') +}); + +with (String.prototype.escapeHTML) div.appendChild(text); + var Template = Class.create(); Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; Template.prototype = {