r61521 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61520‎ | r61521 | r61522 >
Date:16:48, 26 January 2010
Author:catrope
Status:deferred
Tags:
Comment:
UsabilityInitiative: Fix for r61215: IE7 collapses all whitespace, even non-leading whitespace, so we need to convert spaces to   throughout. Also fix some JS errors and improve some comments
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.highlight.js (modified) (history)
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php
@@ -135,12 +135,12 @@
136136 array(
137137 'src' => 'js/plugins/jquery.wikiEditor.js',
138138 'class' => 'j.wikiEditor',
139 - 'version' => 66
 139+ 'version' => 67
140140 ),
141141 array(
142142 'src' => 'js/plugins/jquery.wikiEditor.highlight.js',
143143 'class' => 'j.wikiEditor.modules.highlight',
144 - 'version' => 21
 144+ 'version' => 22
145145 ),
146146 array(
147147 'src' => 'js/plugins/jquery.wikiEditor.toolbar.js',
@@ -173,10 +173,10 @@
174174 'version' => 1 ),
175175 ),
176176 'combined' => array(
177 - array( 'src' => 'js/plugins.combined.js', 'version' => 176 ),
 177+ array( 'src' => 'js/plugins.combined.js', 'version' => 177 ),
178178 ),
179179 'minified' => array(
180 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 176 ),
 180+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 177 ),
181181 ),
182182 ),
183183 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -368,17 +368,17 @@
369369 .appendTo( context.$ui );
370370 },
371371 'htmlToText': function( html ) {
372 - // We use .html() instead of .text() so HTML entities are handled right - setting the HTML of the textarea
373 - // doesn't work on all browsers, use a dummy <div> instead
374 - // Get rid of the noincludes when getting text
 372+ // We use this elaborate trickery for cross-browser compatibility
 373+ // IE does overzealous whitespace collapsing for $( '<pre />' ).html( html );
375374 var $pre = $( '<pre>' +
376375 html
377 - .replace( /\r?\n/g, "" )
 376+ .replace( /\r?\n/g, "" ) // IE7 inserts newlines before block elements
378377 .replace( /\<br[^\>]*\>/gi, "\n" )
379 - .replace( /&nbsp;/g, " " )
380 - .replace( /\<p[^\>]*\>/gi, "\n" )
 378+ .replace( /&nbsp;/g, " " ) // We inserted these to prevent IE from collapsing spaces
 379+ .replace( /\<p[^\>]*\>/gi, "\n" ) // IE uses </p><p> for user-inserted line breaks
381380 .replace( /\<\/p[^\>]*\>/gi, "" )
382381 + '</pre>' );
 382+ // Get rid of the noincludes when getting text
383383 $pre.find( '.wikiEditor-noinclude' ).each( function() { $( this ).remove(); } );
384384 $pre.find( '.wikiEditor-tab' ).each( function() { $( this ).text( "\t" ) } );
385385 return $pre.text();
@@ -674,8 +674,8 @@
675675 }
676676 var seekPos = context.fn.htmlToText( range2.htmlText ).length;
677677 var offsets = context.fn.getOffsets();
678 - e = offsets[seekPos].node;
679 - offset = offsets[seekPos].offset;
 678+ e = offsets[seekPos] ? offsets[seekPos].node : null;
 679+ offset = offsets[seekPos] ? offsets[seekPos].offset : null;
680680 if ( !e )
681681 return $( [] );
682682 }
@@ -903,26 +903,15 @@
904904 if ( $.browser.msie ) {
905905 // Browser sniffing is not ideal, but executing this code on a non-broken browser doesn't cause harm
906906 if ( $.browser.versionNumber <= 7 ) {
907 - // Replace all spaces matching /(^|n) +/ with &nbsp; - IE <= 7 needs this because of its overzealous
 907+ // Replace all spaces matching &nbsp; - IE <= 7 needs this because of its overzealous
908908 // whitespace collapsing;
909 - var prefix = '', suffix = html;
910 - while ( suffix ) {
911 - var match = suffix.match( /(^|\n) / );
912 - if ( match ) {
913 - prefix += suffix.substr( 0, match.index + match[0].length - 1 ) + '&nbsp;';
914 - suffix = suffix.substr( match.index + match[0].length );
915 - } else {
916 - break;
917 - }
918 - }
919 - html = prefix + suffix;
 909+ html = html.replace( / /g, "&nbsp;" );
920910 } else {
921911 // IE8 is happy if we just convert the first leading space to &nbsp;
922912 html = html.replace( /(^|\n) /g, "$1&nbsp;" );
923913 }
924914 html = html.replace( /\t/g, '<span class="wikiEditor-tab"></span>' );
925915 }
926 - // We must append, because IE will crash if we set html() - which is the same as empty() and append()
927916 context.$content.html( html.replace( /\r?\n/g, '<br />' ) );
928917 // Reflect direction of parent frame into child
929918 if ( $( 'body' ).is( '.rtl' ) ) {
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.highlight.js
@@ -282,7 +282,7 @@
283283
284284 // Remove markers that were previously inserted but weren't passed to this function
285285 context.$content.find( 'div.wikiEditor-highlight:not(.wikiEditor-highlight-tmp)' ).each( function() {
286 - if ( typeof $(this).data( 'marker' ).unwrap == 'function' )
 286+ if ( $(this).data( 'marker' ) && typeof $(this).data( 'marker' ).unwrap == 'function' )
287287 $(this).data( 'marker' ).unwrap( this );
288288 if ( $(this).children().size() > 0 ) {
289289 $(this).replaceWith( $(this).children() );
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -6730,17 +6730,17 @@
67316731 .appendTo( context.$ui );
67326732 },
67336733 'htmlToText': function( html ) {
6734 - // We use .html() instead of .text() so HTML entities are handled right - setting the HTML of the textarea
6735 - // doesn't work on all browsers, use a dummy <div> instead
6736 - // Get rid of the noincludes when getting text
 6734+ // We use this elaborate trickery for cross-browser compatibility
 6735+ // IE does overzealous whitespace collapsing for $( '<pre />' ).html( html );
67376736 var $pre = $( '<pre>' +
67386737 html
6739 - .replace( /\r?\n/g, "" )
 6738+ .replace( /\r?\n/g, "" ) // IE7 inserts newlines before block elements
67406739 .replace( /\<br[^\>]*\>/gi, "\n" )
6741 - .replace( /&nbsp;/g, " " )
6742 - .replace( /\<p[^\>]*\>/gi, "\n" )
 6740+ .replace( /&nbsp;/g, " " ) // We inserted these to prevent IE from collapsing spaces
 6741+ .replace( /\<p[^\>]*\>/gi, "\n" ) // IE uses </p><p> for user-inserted line breaks
67436742 .replace( /\<\/p[^\>]*\>/gi, "" )
67446743 + '</pre>' );
 6744+ // Get rid of the noincludes when getting text
67456745 $pre.find( '.wikiEditor-noinclude' ).each( function() { $( this ).remove(); } );
67466746 $pre.find( '.wikiEditor-tab' ).each( function() { $( this ).text( "\t" ) } );
67476747 return $pre.text();
@@ -7036,8 +7036,8 @@
70377037 }
70387038 var seekPos = context.fn.htmlToText( range2.htmlText ).length;
70397039 var offsets = context.fn.getOffsets();
7040 - e = offsets[seekPos].node;
7041 - offset = offsets[seekPos].offset;
 7040+ e = offsets[seekPos] ? offsets[seekPos].node : null;
 7041+ offset = offsets[seekPos] ? offsets[seekPos].offset : null;
70427042 if ( !e )
70437043 return $( [] );
70447044 }
@@ -7265,26 +7265,15 @@
72667266 if ( $.browser.msie ) {
72677267 // Browser sniffing is not ideal, but executing this code on a non-broken browser doesn't cause harm
72687268 if ( $.browser.versionNumber <= 7 ) {
7269 - // Replace all spaces matching /(^|n) +/ with &nbsp; - IE <= 7 needs this because of its overzealous
 7269+ // Replace all spaces matching &nbsp; - IE <= 7 needs this because of its overzealous
72707270 // whitespace collapsing;
7271 - var prefix = '', suffix = html;
7272 - while ( suffix ) {
7273 - var match = suffix.match( /(^|\n) / );
7274 - if ( match ) {
7275 - prefix += suffix.substr( 0, match.index + match[0].length - 1 ) + '&nbsp;';
7276 - suffix = suffix.substr( match.index + match[0].length );
7277 - } else {
7278 - break;
7279 - }
7280 - }
7281 - html = prefix + suffix;
 7271+ html = html.replace( / /g, "&nbsp;" );
72827272 } else {
72837273 // IE8 is happy if we just convert the first leading space to &nbsp;
72847274 html = html.replace( /(^|\n) /g, "$1&nbsp;" );
72857275 }
72867276 html = html.replace( /\t/g, '<span class="wikiEditor-tab"></span>' );
72877277 }
7288 - // We must append, because IE will crash if we set html() - which is the same as empty() and append()
72897278 context.$content.html( html.replace( /\r?\n/g, '<br />' ) );
72907279 // Reflect direction of parent frame into child
72917280 if ( $( 'body' ).is( '.rtl' ) ) {
@@ -7764,7 +7753,7 @@
77657754
77667755 // Remove markers that were previously inserted but weren't passed to this function
77677756 context.$content.find( 'div.wikiEditor-highlight:not(.wikiEditor-highlight-tmp)' ).each( function() {
7768 - if ( typeof $(this).data( 'marker' ).unwrap == 'function' )
 7757+ if ( $(this).data( 'marker' ) && typeof $(this).data( 'marker' ).unwrap == 'function' )
77697758 $(this).data( 'marker' ).unwrap( this );
77707759 if ( $(this).children().size() > 0 ) {
77717760 $(this).replaceWith( $(this).children() );
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -464,7 +464,7 @@
465465 var topBound=html.scrollTop()>body.scrollTop()?html.scrollTop():body.scrollTop(),bottomBound=topBound+context.$iframe.height();if(force||y<topBound||y>bottomBound){html.scrollTop(y);body.scrollTop(y);}
466466 $element.trigger('scrollToTop');},'beforeSelection':function(selector,strict){if(typeof selector=='undefined'){selector='*';}
467467 var e,offset;if(context.$iframe[0].contentWindow.getSelection){var selection=context.$iframe[0].contentWindow.getSelection();if(selection.baseNode!==null){e=selection.getRangeAt(0).startContainer;offset=selection.startOffset;}else{return $([]);}}else if(context.$iframe[0].contentWindow.document.selection){var range=context.$iframe[0].contentWindow.document.selection.createRange();var range2=context.$iframe[0].contentWindow.document.body.createTextRange();try{range2.setEndPoint('EndToStart',range);}catch(e){return $([]);}
468 -var seekPos=context.fn.htmlToText(range2.htmlText).length;var offsets=context.fn.getOffsets();e=offsets[seekPos].node;offset=offsets[seekPos].offset;if(!e)
 468+var seekPos=context.fn.htmlToText(range2.htmlText).length;var offsets=context.fn.getOffsets();e=offsets[seekPos]?offsets[seekPos].node:null;offset=offsets[seekPos]?offsets[seekPos].offset:null;if(!e)
469469 return $([]);}
470470 if(e.nodeName!='#text'){var newE=e.firstChild;for(var i=0;i<offset-1&&newE;i++){newE=newE.nextSibling;}
471471 while(newE&&newE.lastChild){newE=newE.lastChild;}
@@ -483,8 +483,7 @@
484484 if(leavingP){context.offsets[nextPos]={'node':t.node,'offset':nextPos-pos,'length':nextPos-pos+1,'depth':t.depth,'lastTextNode':lastTextNode,'lastTextNodeDepth':lastTextNodeDepth};}
485485 pos=nextPos+(leavingP?1:0);if(t.node.nodeName=='#text'){lastTextNode=t.node;lastTextNodeDepth=t.depth;}
486486 t=nextT;}}};context.$textarea.wrap($('<div></div>').addClass('wikiEditor-ui')).wrap($('<div></div>').addClass('wikiEditor-ui-view wikiEditor-ui-view-wikitext')).wrap($('<div></div>').addClass('wikiEditor-ui-left')).wrap($('<div></div>').addClass('wikiEditor-ui-bottom')).wrap($('<div></div>').addClass('wikiEditor-ui-text'));context.$ui=context.$textarea.parent().parent().parent().parent().parent();context.$wikitext=context.$textarea.parent().parent().parent().parent();context.$wikitext.before($('<div></div>').addClass('wikiEditor-ui-controls').append($('<div></div>').addClass('wikiEditor-ui-tabs').hide()).append($('<div></div>').addClass('wikiEditor-ui-buttons'))).before($('<div style="clear:both;"></div>'));context.$controls=context.$ui.find('.wikiEditor-ui-buttons').hide();context.$buttons=context.$ui.find('.wikiEditor-ui-buttons');context.$tabs=context.$ui.find('.wikiEditor-ui-tabs');context.$ui.after($('<div style="clear:both;"></div>'));context.$wikitext.append($('<div></div>').addClass('wikiEditor-ui-right'));context.$wikitext.find('.wikiEditor-ui-left').prepend($('<div></div>').addClass('wikiEditor-ui-top'));context.view='wikitext';$(window).resize(function(event){context.fn.trigger('resize',event)});context.$iframe=$('<iframe></iframe>').attr({'frameBorder':0,'border':0,'src':wgScriptPath+'/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.html?'+'instance='+context.instance+'&ts='+(new Date()).getTime(),'id':'wikiEditor-iframe-'+context.instance}).css({'backgroundColor':'white','width':'100%','height':context.$textarea.height(),'display':'none','overflow-y':'scroll','overflow-x':'hidden'}).insertAfter(context.$textarea).load(function(){if(!this.isSecondRun){context.$iframe[0].contentWindow.document.designMode='on';if($.browser.msie){this.isSecondRun=true;return;}}
487 -context.$content=$(context.$iframe[0].contentWindow.document.body);var html=context.$textarea.val().replace(/\</g,'&lt;').replace(/\>/g,'&gt;');if($.browser.msie){if($.browser.versionNumber<=7){var prefix='',suffix=html;while(suffix){var match=suffix.match(/(^|\n) /);if(match){prefix+=suffix.substr(0,match.index+match[0].length-1)+'&nbsp;';suffix=suffix.substr(match.index+match[0].length);}else{break;}}
488 -html=prefix+suffix;}else{html=html.replace(/(^|\n) /g,"$1&nbsp;");}
 487+context.$content=$(context.$iframe[0].contentWindow.document.body);var html=context.$textarea.val().replace(/\</g,'&lt;').replace(/\>/g,'&gt;');if($.browser.msie){if($.browser.versionNumber<=7){html=html.replace(/ /g,"&nbsp;");}else{html=html.replace(/(^|\n) /g,"$1&nbsp;");}
489488 html=html.replace(/\t/g,'<span class="wikiEditor-tab"></span>');}
490489 context.$content.html(html.replace(/\r?\n/g,'<br />'));if($('body').is('.rtl')){context.$content.addClass('rtl').attr('dir','rtl');}
491490 context.$textarea.attr('disabled',true);context.$textarea.hide();context.$iframe.show();context.fn.trigger('ready');$(context.$iframe[0].contentWindow.document).bind('keyup mouseup paste cut encapsulateSelection',function(event){context.fn.trigger('change',event);}).delayedBind(250,'keyup mouseup paste cut encapsulateSelection',function(event){context.fn.trigger('delayedChange',event);});});context.$textarea.closest('form').submit(function(){context.$textarea.attr('disabled',false);context.$textarea.val(context.$textarea.textSelection('getContents'));});}
@@ -510,7 +509,7 @@
511510 if(ca1&&ca2&&ca1.parentNode&&ca2.nextSibling){var anchor=markers[i].getAnchor(ca1,ca2);if(!anchor){var newNode=ca1.ownerDocument.createElement('div');var commonAncestor=ca1.parentNode;var nextNode=ca2.nextSibling;if(markers[i].anchor=='wrap'){var n=ca1;while(n!=nextNode){var ns=n.nextSibling;newNode.appendChild(n);n=ns;}
512511 if(nextNode){commonAncestor.insertBefore(newNode,nextNode);}else{commonAncestor.appendChild(newNode);}}else if(markers[i].anchor=='before'){commonAncestor.insertBefore(newNode,ca1);}else if(markers[i].anchor=='after'){if(nextNode){commonAncestor.insertBefore(newNode,nextNode);}else{commonAncestor.appendChild(newNode);}}
513512 $(newNode).data('marker',markers[i]).addClass('wikiEditor-highlight wikiEditor-highlight-tmp');markers[i].afterWrap(newNode,markers[i]);}else{$(anchor).addClass('wikiEditor-highlight-tmp').data('marker',markers[i]);markers[i].onSkip(anchor);}}}
514 -context.$content.find('div.wikiEditor-highlight:not(.wikiEditor-highlight-tmp)').each(function(){if(typeof $(this).data('marker').unwrap=='function')
 513+context.$content.find('div.wikiEditor-highlight:not(.wikiEditor-highlight-tmp)').each(function(){if($(this).data('marker')&&typeof $(this).data('marker').unwrap=='function')
515514 $(this).data('marker').unwrap(this);if($(this).children().size()>0){$(this).replaceWith($(this).children());}else{$(this).replaceWith($(this).html());}});context.$content.find('div.wikiEditor-highlight-tmp').removeClass('wikiEditor-highlight-tmp');}}};})(jQuery);(function($){$.wikiEditor.modules.preview={fn:{create:function(context,config){if('initialized'in context.modules.preview){return;}
516515 context.modules.preview={'initialized':true,'previewText':null,'changesText':null};context.modules.preview.$preview=context.fn.addView({'name':'preview','titleMsg':'wikieditor-preview-tab','init':function(context){var wikitext=context.fn.getContents();if(context.modules.preview.previewText==wikitext){return;}
517516 context.modules.preview.$preview.find('.wikiEditor-preview-contents').empty();context.modules.preview.$preview.find('.wikiEditor-preview-loading').show();$.post(wgScriptPath+'/api.php',{'action':'parse','title':wgPageName,'text':wikitext,'prop':'text','pst':'','format':'json'},function(data){if(typeof data.parse=='undefined'||typeof data.parse.text=='undefined'||typeof data.parse.text['*']=='undefined'){return;}

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r61215Added whitespace support for IE.tparscal21:16, 18 January 2010

Status & tagging log