r61699 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61698‎ | r61699 | r61700 >
Date:23:55, 29 January 2010
Author:catrope
Status:deferred
Tags:
Comment:
UsabilityInitiative: Save trailing and leading whitespace in htmlToText() and restore it later. IE is *really* aggressive about stripping this
Modified paths:
  • /trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins.combined.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php
@@ -72,7 +72,7 @@
7373 array( 'src' => 'js/plugins/jquery.namespaceSelect.js', 'version' => 1 ),
7474 array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 7 ),
7575 array( 'src' => 'js/plugins/jquery.textSelection.js', 'version' => 26 ),
76 - array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 90 ),
 76+ array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 91 ),
7777 array( 'src' => 'js/plugins/jquery.wikiEditor.highlight.js', 'version' => 26 ),
7878 array( 'src' => 'js/plugins/jquery.wikiEditor.toolbar.js', 'version' => 45 ),
7979 array( 'src' => 'js/plugins/jquery.wikiEditor.dialogs.js', 'version' => 11 ),
@@ -82,10 +82,10 @@
8383 array( 'src' => 'js/plugins/jquery.wikiEditor.publish.js', 'version' => 2 ),
8484 ),
8585 'combined' => array(
86 - array( 'src' => 'js/plugins.combined.js', 'version' => 203 ),
 86+ array( 'src' => 'js/plugins.combined.js', 'version' => 204 ),
8787 ),
8888 'minified' => array(
89 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 203 ),
 89+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 204 ),
9090 ),
9191 ),
9292 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -364,15 +364,18 @@
365365 // IE does overzealous whitespace collapsing for $( '<pre />' ).html( html );
366366 // We also do the easy cases for <p> and <br> conversion here, complicated cases are handled
367367 // later
368 - var $pre = $( '<pre>' +
369 - html
 368+ html = html
370369 .replace( /\r?\n/g, "" ) // IE7 inserts newlines before block elements
371370 .replace( /&nbsp;/g, " " ) // We inserted these to prevent IE from collapsing spaces
372 - // Don't convert <br>s at the very start or end to prevent newline collapsing
373 - .replace( /(?!^)\<br[^\>]*\>(?!$)/gi, "\n" ) // Easy case for <br> conversion
 371+ .replace( /\<br[^\>]*\>/gi, "\n" ) // Easy case for <br> conversion
374372 .replace( /\<\/p\>\<p\>/gi, "\n" ) // Easy case for <p> conversion
375 - .replace( /\<\/p\>(\n*)\<p\>/gi, "$1\n" )
376 - + '</pre>' );
 373+ .replace( /\<\/p\>(\n*)\<p\>/gi, "$1\n" );
 374+ // Save leading and trailing whitespace now and restore it later. IE eats it all, and even Firefox
 375+ // won't leave everything alone
 376+ var leading = html.match( /^\s*/ )[0];
 377+ var trailing = html.match( /\s*$/ )[0];
 378+ html = html.substr( leading.length, html.length - leading.length - trailing.length );
 379+ var $pre = $( '<pre>' + html + '</pre>' );
377380 // TODO: Optimize this, maybe by converting <br>->\n when not at the beginning or end
378381 $pre.find( '.wikiEditor-noinclude' ).each( function() { $( this ).remove(); } );
379382 // Convert tabs, <p>s and <br>s back
@@ -409,13 +412,15 @@
410413 $( this ).text( text );
411414 }
412415 } );
413 - // IE aggressively collapses whitespace in .text() after having done DOM manipulation,
414 - // but for some crazy reason this does work
 416+ var retval;
415417 if ( $.browser.msie ) {
416 - return $( '<pre>' + $pre.html() + '</pre>' ).text().replace( /\r/g, '\n' );
 418+ // IE aggressively collapses whitespace in .text() after having done DOM manipulation,
 419+ // but for some crazy reason this does work. Also convert \r back to \n
 420+ retval = $( '<pre>' + $pre.html() + '</pre>' ).text().replace( /\r/g, '\n' );
417421 } else {
418 - return $pre.text();
 422+ retval = $pre.text();
419423 }
 424+ return leading + retval + trailing;
420425 },
421426
422427 /*
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -6788,15 +6788,18 @@
67896789 // IE does overzealous whitespace collapsing for $( '<pre />' ).html( html );
67906790 // We also do the easy cases for <p> and <br> conversion here, complicated cases are handled
67916791 // later
6792 - var $pre = $( '<pre>' +
6793 - html
 6792+ html = html
67946793 .replace( /\r?\n/g, "" ) // IE7 inserts newlines before block elements
67956794 .replace( /&nbsp;/g, " " ) // We inserted these to prevent IE from collapsing spaces
6796 - // Don't convert <br>s at the very start or end to prevent newline collapsing
6797 - .replace( /(?!^)\<br[^\>]*\>(?!$)/gi, "\n" ) // Easy case for <br> conversion
 6795+ .replace( /\<br[^\>]*\>/gi, "\n" ) // Easy case for <br> conversion
67986796 .replace( /\<\/p\>\<p\>/gi, "\n" ) // Easy case for <p> conversion
6799 - .replace( /\<\/p\>(\n*)\<p\>/gi, "$1\n" )
6800 - + '</pre>' );
 6797+ .replace( /\<\/p\>(\n*)\<p\>/gi, "$1\n" );
 6798+ // Save leading and trailing whitespace now and restore it later. IE eats it all, and even Firefox
 6799+ // won't leave everything alone
 6800+ var leading = html.match( /^\s*/ )[0];
 6801+ var trailing = html.match( /\s*$/ )[0];
 6802+ html = html.substr( leading.length, html.length - leading.length - trailing.length );
 6803+ var $pre = $( '<pre>' + html + '</pre>' );
68016804 // TODO: Optimize this, maybe by converting <br>->\n when not at the beginning or end
68026805 $pre.find( '.wikiEditor-noinclude' ).each( function() { $( this ).remove(); } );
68036806 // Convert tabs, <p>s and <br>s back
@@ -6833,13 +6836,15 @@
68346837 $( this ).text( text );
68356838 }
68366839 } );
6837 - // IE aggressively collapses whitespace in .text() after having done DOM manipulation,
6838 - // but for some crazy reason this does work
 6840+ var retval;
68396841 if ( $.browser.msie ) {
6840 - return $( '<pre>' + $pre.html() + '</pre>' ).text().replace( /\r/g, '\n' );
 6842+ // IE aggressively collapses whitespace in .text() after having done DOM manipulation,
 6843+ // but for some crazy reason this does work. Also convert \r back to \n
 6844+ retval = $( '<pre>' + $pre.html() + '</pre>' ).text().replace( /\r/g, '\n' );
68416845 } else {
6842 - return $pre.text();
 6846+ retval = $pre.text();
68436847 }
 6848+ return leading + retval + trailing;
68446849 },
68456850
68466851 /*
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -448,13 +448,12 @@
449449 for(module in context.modules){if(module in $.wikiEditor.modules&&'evt'in $.wikiEditor.modules[module]&&name in $.wikiEditor.modules[module].evt){$.wikiEditor.modules[module].evt[name](context,event);}}},'addButton':function(options){context.$controls.show();context.$buttons.show();return $('<button />').text($.wikiEditor.autoMsg(options,'caption')).click(options.action).appendTo(context.$buttons);},'addView':function(options){function addTab(options){context.$controls.show();context.$tabs.show();return $('<div></div>').attr('rel','wikiEditor-ui-view-'+options.name).addClass(context.view==options.name?'current':null).append($('<a></a>').attr('href','#').click(function(event){context.$ui.find('.wikiEditor-ui-view').hide();context.$ui.find('.'+$(this).parent().attr('rel')).show();context.$tabs.find('div').removeClass('current');$(this).parent().addClass('current');$(this).blur();if('init'in options&&typeof options.init=='function'){options.init(context);}
450450 event.preventDefault();return false;}).text($.wikiEditor.autoMsg(options,'title'))).appendTo(context.$tabs);}
451451 if(!context.$tabs.children().size()){addTab({'name':'wikitext','titleMsg':'wikieditor-wikitext-tab'});}
452 -addTab(options);return $('<div></div>').addClass('wikiEditor-ui-view wikiEditor-ui-view-'+options.name).hide().appendTo(context.$ui);},'htmlToText':function(html){var $pre=$('<pre>'+
453 -html.replace(/\r?\n/g,"").replace(/&nbsp;/g," ").replace(/(?!^)\<br[^\>]*\>(?!$)/gi,"\n").replace(/\<\/p\>\<p\>/gi,"\n").replace(/\<\/p\>(\n*)\<p\>/gi,"$1\n")
454 -+'</pre>');$pre.find('.wikiEditor-noinclude').each(function(){$(this).remove();});$pre.find('.wikiEditor-tab').each(function(){$(this).text("\t")});$pre.find('br').each(function(){$(this).replaceWith("\n");});$pre.find('p').each(function(){if(this.previousSibling||this.parentNode!=$pre.get(0)){var text=$(this).text();var t=new context.fn.rawTraverser(this.firstChild,-10,this).prev();while(t&&t.node.nodeName!='#text'&&t.node.nodeName!='BR'&&t.node.nodeName!='P'){t=t.prev();}
 452+addTab(options);return $('<div></div>').addClass('wikiEditor-ui-view wikiEditor-ui-view-'+options.name).hide().appendTo(context.$ui);},'htmlToText':function(html){html=html.replace(/\r?\n/g,"").replace(/&nbsp;/g," ").replace(/\<br[^\>]*\>/gi,"\n").replace(/\<\/p\>\<p\>/gi,"\n").replace(/\<\/p\>(\n*)\<p\>/gi,"$1\n");var leading=html.match(/^\s*/)[0];var trailing=html.match(/\s*$/)[0];html=html.substr(leading.length,html.length-leading.length-trailing.length);var $pre=$('<pre>'+html+'</pre>');$pre.find('.wikiEditor-noinclude').each(function(){$(this).remove();});$pre.find('.wikiEditor-tab').each(function(){$(this).text("\t")});$pre.find('br').each(function(){$(this).replaceWith("\n");});$pre.find('p').each(function(){if(this.previousSibling||this.parentNode!=$pre.get(0)){var text=$(this).text();var t=new context.fn.rawTraverser(this.firstChild,-10,this).prev();while(t&&t.node.nodeName!='#text'&&t.node.nodeName!='BR'&&t.node.nodeName!='P'){t=t.prev();}
455453 if(t){text="\n"+text;}
456454 t=new context.fn.rawTraverser(this.lastChild,-10,this).next();while(t&&t.node.nodeName!='#text'&&t.node.nodeName!='BR'&&t.node.nodeName!='P'){t=t.next();}
457455 if(t&&!t.inP&&t.node.nodeName=='#text'&&t.node.nodeValue.charAt(0)!='\n'&&t.node.nodeValue.charAt(0)!='\r'){text+="\n";}
458 -$(this).text(text);}});if($.browser.msie){return $('<pre>'+$pre.html()+'</pre>').text().replace(/\r/g,'\n');}else{return $pre.text();}},'getContents':function(){return context.fn.htmlToText(context.$content.html());},'getSelection':function(){var retval;if(context.$iframe[0].contentWindow.getSelection){retval=context.$iframe[0].contentWindow.getSelection();}else if(context.$iframe[0].contentWindow.document.selection){retval=context.$iframe[0].contentWindow.document.selection.createRange();}
 456+$(this).text(text);}});var retval;if($.browser.msie){retval=$('<pre>'+$pre.html()+'</pre>').text().replace(/\r/g,'\n');}else{retval=$pre.text();}
 457+return leading+retval+trailing;},'getContents':function(){return context.fn.htmlToText(context.$content.html());},'getSelection':function(){var retval;if(context.$iframe[0].contentWindow.getSelection){retval=context.$iframe[0].contentWindow.getSelection();}else if(context.$iframe[0].contentWindow.document.selection){retval=context.$iframe[0].contentWindow.document.selection.createRange();}
459458 if(typeof retval.text!='undefined'){retval=context.fn.htmlToText(retval.htmlText);}else if(retval.toString){retval=retval.toString();}
460459 return retval;},'encapsulateSelection':function(options){var selText=$(this).textSelection('getSelection');var selTextArr;var selectAfter=false;var pre=options.pre,post=options.post;if(!selText){selText=options.peri;selectAfter=true;}else if(options.replace){selText=options.peri;}else if(selText.charAt(selText.length-1)==' '){selText=selText.substring(0,selText.length-1);post+=' ';}
461460 if(options.splitlines){selTextArr=selText.split(/\n/);}

Status & tagging log