diff --git a/CHANGELOG b/CHANGELOG index 13f894e..152e4b6 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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' diff --git a/src/prototype.js b/src/prototype.js index 242cf30..daa0f55 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -18,6 +18,8 @@ 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 fa3ebd0..7af3e95 100644 --- a/src/string.js +++ b/src/string.js @@ -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); diff --git a/test/unit/string.html b/test/unit/string.html index 63ca6b7..ac243c2 100644 --- a/test/unit/string.html +++ b/test/unit/string.html @@ -76,6 +76,11 @@ source.gsub(/\s+/, '')); assertEqual(' z', source.gsub(/(.)(o+)/, '')); + + assertEqual('ウィメンズ2007
クルーズコレクション', + 'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'
')); + assertEqual('ウィメンズ2007
クルーズコレクション', + 'ウィメンズ2007\nクルーズコレクション'.gsub('\n','
')); }}, testGsubWithReplacementTemplateString: function() {with(this) { @@ -217,6 +222,7 @@ assertEqual('hello world', 'hello world'.stripTags()); assertEqual('hello world', 'hello world'.stripTags()); assertEqual('hello world', 'hello world'.stripTags()); + assertEqual('1\n2', '1\n2'.stripTags()); }}, testStripScripts: function() {with(this) { @@ -251,10 +257,15 @@ assertEqual('foo <span>bar</span>', 'foo bar'.escapeHTML()); assertEqual('foo ß bar', 'foo ß bar'.escapeHTML()); + assertEqual('ウィメンズ2007\nクルーズコレクション', + 'ウィメンズ2007\nクルーズコレクション'.escapeHTML()); + assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g', - 'ablubb
cdef!!!!g'.escapeHTML()); + 'ablubb
cdef!!!!g'.escapeHTML()); assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML()); + + assertEqual('1\n2', '1\n2'.escapeHTML()); }}, testUnescapeHTML: function() {with(this) { @@ -266,6 +277,8 @@ 'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.unescapeHTML()); assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML()); + + assertEqual('1\n2', '1\n2'.unescapeHTML()); }}, testTemplateEvaluation: function() {with(this) {