Ticket #8452: Fix-String-escapeHTML-and-String-unescapeHTML.patch
| File Fix-String-escapeHTML-and-String-unescapeHTML.patch, 4.2 kB (added by jdalton, 5 months ago) |
|---|
-
a/src/string.js
old new 78 78 escapeHTML: function() { 79 79 var self = arguments.callee; 80 80 self.text.data = this; 81 return self. div.innerHTML;81 return self.wrapper.innerHTML; 82 82 }, 83 83 84 84 unescapeHTML: function() { 85 85 var div = new Element('div'); 86 div.innerHTML = this.stripTags(); 87 return div.childNodes[0] ? (div.childNodes.length > 1 ? 88 $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : 86 div.innerHTML = '<pre>'+this.stripTags()+'</pre>'; 87 div = div.firstChild; 88 return div.childNodes[0] ? (div.childNodes.length > 1 ? 89 $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : 89 90 div.childNodes[0].nodeValue) : ''; 90 91 }, 91 92 … … 206 207 } 207 208 }); 208 209 209 if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, {210 escapeHTML: function() {211 return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');212 },213 unescapeHTML: function() {214 return this.stripTags().replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');215 }216 });217 218 210 String.prototype.gsub.prepareReplacement = function(replacement) { 219 211 if (Object.isFunction(replacement)) return replacement; 220 212 var template = new Template(replacement); … … 223 215 224 216 String.prototype.parseQuery = String.prototype.toQueryParams; 225 217 218 if (Prototype.Browser.IE) 219 String.prototype.unescapeHTML = String.prototype.unescapeHTML.wrap(function(proceed){ 220 return proceed().replace(/\r/g,'\n') 221 }); 222 if (Prototype.Browser.WebKit && Prototype.BrowserFeatures.SelectorsAPI) 223 String.prototype.escapeHTML = new Function('', 224 String.prototype.escapeHTML.toString().substring(14).replace(/;?\s*}$/,'') + '.replace(/>/g,">")' 225 ); 226 226 227 Object.extend(String.prototype.escapeHTML, { 227 div: document.createElement('div'),228 wrapper: document.createElement('pre'), 228 229 text: document.createTextNode('') 229 230 }); 230 231 231 String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text); 232 String.prototype.escapeHTML.wrapper.appendChild(String.prototype.escapeHTML.text); 233 234 if('&'.escapeHTML() !== '&'){ 235 with(String.prototype.escapeHTML){ 236 text = wrapper.removeChild(text); 237 wrapper = document.createElement('xmp'); 238 wrapper.appendChild(text); 239 } 240 } 241 242 (function(){ 243 Event.observe(window, 'unload', function(){ 244 with(String.prototype.escapeHTML) wrapper = text = null; 245 }) 246 }).defer(); 232 247 233 248 var Template = Class.create({ 234 249 initialize: function(template, pattern) { -
a/test/unit/string.html
old new 277 277 'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.unescapeHTML()); 278 278 279 279 this.assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML()); 280 280 281 this.assertEqual('test ú', 'test ú'.unescapeHTML()); 281 282 this.assertEqual('1\n2', '1\n2'.unescapeHTML()); 282 this.assertEqual('Pride & Prejudice', '<h1>Pride & Prejudice</h1>'.unescapeHTML()); 283 this.assertEqual('Pride & Prejudice', '<h1>Pride & Prejudice</h1>'.unescapeHTML()); 284 285 var escapedTest = '"<" means "<" in HTML'; 286 this.assertEqual(escapedTest, escapedTest.escapeHTML().unescapeHTML()); 283 287 284 288 this.benchmark(function() { largeTextEscaped.unescapeHTML() }, 1000); 285 289