Prototype: clean (un)escapeHTML IE special casing and optimize speed for IE and Safari

This commit is contained in:
Thomas Fuchs 2007-03-29 17:39:48 +00:00
parent 9a7dcd54fb
commit c0509c7f5f
4 changed files with 13 additions and 12 deletions

View File

@ -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:

2
src/prototype.js vendored
View File

@ -18,8 +18,6 @@ var Prototype = {
},
ScriptFragment: '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)',
LinefeedFragment: '-PrOtOtYpE LiNeFeEd-',
emptyFunction: function() {},
K: function(x) { return x }
}

View File

@ -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,'>');
}
});

View File

@ -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) {