From c0509c7f5f925949ae0f5e132c4e53b72bf70efa Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Thu, 29 Mar 2007 17:39:48 +0000 Subject: [PATCH] Prototype: clean (un)escapeHTML IE special casing and optimize speed for IE and Safari --- CHANGELOG | 2 +- src/prototype.js | 2 -- src/string.js | 12 +++--------- test/unit/string.html | 9 +++++++++ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 152e4b6..19f2cbd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ *SVN* -* Prevent linefeed normalisation in String.prototype.escapeHTML and unescapeHTML on IE for consistency with other browsers. [Thomas Fuchs] +* Prevent linefeed normalisation in String.prototype.escapeHTML and unescapeHTML on IE for consistency with other browsers. Speed optimizations for Safari and IE. [Thomas Fuchs] * Make Hash.toQueryString serialize undefined values. Ensure consistency with String.prototype.toQueryParams. Closes #7806. [Mislav Marohnić] Examples: diff --git a/src/prototype.js b/src/prototype.js index daa0f55..242cf30 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -18,8 +18,6 @@ var Prototype = { }, ScriptFragment: '(?:)((\n|\r|.)*?)(?:<\/script>)', - LinefeedFragment: '-PrOtOtYpE LiNeFeEd-', - emptyFunction: function() {}, K: function(x) { return x } } diff --git a/src/string.js b/src/string.js index 7af3e95..96fc361 100644 --- a/src/string.js +++ b/src/string.js @@ -193,18 +193,12 @@ Object.extend(String.prototype, { } }); -if (Prototype.Browser.IE) Object.extend(String.prototype, { +if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, { escapeHTML: function() { - var self = arguments.callee; - self.text.data = this.gsub(/\n/, Prototype.LinefeedFragment); - return self.div.innerHTML.gsub(Prototype.LinefeedFragment, '\n'); + return this.replace(/&/g,'&').replace(//g,'>'); }, unescapeHTML: function() { - var div = document.createElement('div'); - div.innerHTML = this.stripTags().gsub(/\n/, Prototype.LinefeedFragment); - return (div.childNodes[0] ? (div.childNodes.length > 1 ? - $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : - div.childNodes[0].nodeValue) : '').gsub(Prototype.LinefeedFragment, '\n'); + return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); } }); diff --git a/test/unit/string.html b/test/unit/string.html index ac243c2..6e09746 100644 --- a/test/unit/string.html +++ b/test/unit/string.html @@ -266,6 +266,10 @@ assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML()); assertEqual('1\n2', '1\n2'.escapeHTML()); + + benchmark(function(){ + largeTextUnescaped.escapeHTML(); + },1000); }}, testUnescapeHTML: function() {with(this) { @@ -279,6 +283,11 @@ assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML()); assertEqual('1\n2', '1\n2'.unescapeHTML()); + + benchmark(function(){ + largeTextEscaped.unescapeHTML(); + },1000); + }}, testTemplateEvaluation: function() {with(this) {