Prevent linefeed normalisation in String.prototype.escapeHTML and unescapeHTML on IE for consistency with other browsers

This commit is contained in:
Thomas Fuchs 2007-03-28 11:35:05 +00:00
parent 5f2acb4be5
commit 9a7dcd54fb
4 changed files with 33 additions and 1 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Prevent linefeed normalisation in String.prototype.escapeHTML and unescapeHTML on IE for consistency with other browsers. [Thomas Fuchs]
* Make Hash.toQueryString serialize undefined values. Ensure consistency with String.prototype.toQueryParams. Closes #7806. [Mislav Marohnić]
Examples:
$H({a:'b',c:undefined}).toQueryString() -> 'a=b&c'

2
src/prototype.js vendored
View File

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

View File

@ -193,6 +193,21 @@ Object.extend(String.prototype, {
}
});
if (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');
},
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');
}
});
String.prototype.gsub.prepareReplacement = function(replacement) {
if (typeof replacement == 'function') return replacement;
var template = new Template(replacement);

View File

@ -76,6 +76,11 @@
source.gsub(/\s+/, ''));
assertEqual(' z',
source.gsub(/(.)(o+)/, ''));
assertEqual('ウィメンズ2007<br/>クルーズコレクション',
'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'<br/>'));
assertEqual('ウィメンズ2007<br/>クルーズコレクション',
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
}},
testGsubWithReplacementTemplateString: function() {with(this) {
@ -217,6 +222,7 @@
assertEqual('hello world', 'hello <span>world</span>'.stripTags());
assertEqual('hello world', '<a href="#" onclick="moo!">hello</a> world'.stripTags());
assertEqual('hello world', 'h<b><em>e</em></b>l<i>l</i>o w<span class="moo" id="x"><b>o</b></span>rld'.stripTags());
assertEqual('1\n2', '1\n2'.stripTags());
}},
testStripScripts: function() {with(this) {
@ -251,10 +257,15 @@
assertEqual('foo &lt;span&gt;bar&lt;/span&gt;', 'foo <span>bar</span>'.escapeHTML());
assertEqual('foo ß bar', 'foo ß bar'.escapeHTML());
assertEqual('ウィメンズ2007\nクルーズコレクション',
'ウィメンズ2007\nクルーズコレクション'.escapeHTML());
assertEqual('a&lt;a href="blah"&gt;blub&lt;/a&gt;b&lt;span&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;cdef&lt;strong&gt;!!!!&lt;/strong&gt;g',
'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.escapeHTML());
'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.escapeHTML());
assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML());
assertEqual('1\n2', '1\n2'.escapeHTML());
}},
testUnescapeHTML: function() {with(this) {
@ -266,6 +277,8 @@
'a&lt;a href="blah"&gt;blub&lt;/a&gt;b&lt;span&gt;&lt;div&gt;&lt;/div&gt;&lt;/span&gt;cdef&lt;strong&gt;!!!!&lt;/strong&gt;g'.unescapeHTML());
assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML());
assertEqual('1\n2', '1\n2'.unescapeHTML());
}},
testTemplateEvaluation: function() {with(this) {