Skip to content

Commit

Permalink
Prototype: clean (un)escapeHTML IE special casing and optimize speed …
Browse files Browse the repository at this point in the history
…for IE and Safari
  • Loading branch information
madrobby committed Mar 29, 2007
1 parent 9a7dcd5 commit c0509c7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 0 additions & 2 deletions src/prototype.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ var Prototype = {
},

ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
LinefeedFragment: '-PrOtOtYpE LiNeFeEd-',

emptyFunction: function() {},
K: function(x) { return x }
}
Expand Down
12 changes: 3 additions & 9 deletions src/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
},
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(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}
});

Expand Down
9 changes: 9 additions & 0 deletions test/unit/string.html
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ <h1>Prototype Unit test file</h1>
assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML());

assertEqual('1\n2', '1\n2'.escapeHTML());

benchmark(function(){
largeTextUnescaped.escapeHTML();
},1000);
}},

testUnescapeHTML: function() {with(this) {
Expand All @@ -279,6 +283,11 @@ <h1>Prototype Unit test file</h1>
assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML());

assertEqual('1\n2', '1\n2'.unescapeHTML());

benchmark(function(){
largeTextEscaped.unescapeHTML();
},1000);

}},

testTemplateEvaluation: function() {with(this) {
Expand Down

0 comments on commit c0509c7

Please sign in to comment.