r65554 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65553‎ | r65554 | r65555 >
Date:20:26, 26 April 2010
Author:pdhanda
Status:ok
Tags:
Comment:
More paste fixes: Cursor position, minor refactors
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.delayedBind.js', 'version' => 1 ),
7474 array( 'src' => 'js/plugins/jquery.suggestions.js', 'version' => 11 ),
7575 array( 'src' => 'js/plugins/jquery.textSelection.js', 'version' => 30 ),
76 - array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 181 ),
 76+ array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 182 ),
7777 array( 'src' => 'js/plugins/jquery.wikiEditor.highlight.js', 'version' => 52 ),
7878 array( 'src' => 'js/plugins/jquery.wikiEditor.toolbar.js', 'version' => 58 ),
7979 array( 'src' => 'js/plugins/jquery.wikiEditor.dialogs.js', 'version' => 23 ),
@@ -82,10 +82,10 @@
8383 array( 'src' => 'js/plugins/jquery.wikiEditor.publish.js', 'version' => 5 ),
8484 ),
8585 'combined' => array(
86 - array( 'src' => 'js/plugins.combined.js', 'version' => 384 ),
 86+ array( 'src' => 'js/plugins.combined.js', 'version' => 385 ),
8787 ),
8888 'minified' => array(
89 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 384 ),
 89+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 385 ),
9090 ),
9191 ),
9292 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -451,10 +451,21 @@
452452 pasteContent = pasteContent.nextSibling;
453453 if ( currentNode.nodeName == '#text' && currentNode.nodeValue == currentNode.wholeText ) {
454454 $( currentNode ).wrap( $( '<p></p>' ) );
 455+ $( currentNode ).addClass( 'wikiEditor' );
455456 removeNextBR = true;
456 - } else if ( currentNode.nodeName == 'BR' && removeNextBR ) {
457 - $( currentNode ).remove();
 457+ } else if ( currentNode.nodeName == 'SPAN' ) {
 458+ var text = $( currentNode ).text();
 459+ if ( text.length == 0 ) {
 460+ $( currentNode ).remove();
 461+ }
458462 removeNextBR = false;
 463+ } else if ( currentNode.nodeName == 'BR' ) {
 464+ if (removeNextBR ) {
 465+ $( currentNode ).remove();
 466+ } else {
 467+ $( currentNode ).addClass( 'wikiEditor' );
 468+ }
 469+ removeNextBR = false;
459470 } else {
460471 removeNextBR = false;
461472 }
@@ -467,12 +478,7 @@
468479 $currentElement = $currentElement.parent();
469480 }
470481 var html = $( '<div></div>' ).text( $currentElement.text().replace( /\r|\n/g, ' ' ) ).html();
471 - if ( $currentElement.is( 'br' ) ) {
472 - $currentElement.addClass( 'wikiEditor' );
473 - } else if ( $currentElement.is( 'span' ) && html.length == 0 ) {
474 - // Markers!
475 - $currentElement.remove();
476 - } else if ( $currentElement.is( 'p' ) || $currentElement.is( 'div' ) ) {
 482+ if ( $currentElement.is( 'p' ) || $currentElement.is( 'div' ) ) {
477483 $newElement = $( '<p></p>' )
478484 .addClass( 'wikiEditor' )
479485 .insertAfter( $currentElement );
@@ -496,7 +502,8 @@
497503
498504 // Restore cursor position
499505 context.fn.purgeOffsets();
500 - var restoreTo = cursorPos[0] + context.fn.getContents().length - oldLength;
 506+ var newLength = context.$content.html().length;
 507+ var restoreTo = cursorPos[0] + newLength - oldLength;
501508 context.fn.setSelection( { start: restoreTo, end: restoreTo } );
502509 }, 0 );
503510 return true;
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -7003,7 +7003,7 @@
70047004 'paste': function( event ) {
70057005 // Save the cursor position to restore it after all this voodoo
70067006 var cursorPos = context.fn.getCaretPosition();
7007 - var oldLength = context.fn.getContents().length;
 7007+ var oldLength = context.fn.getContents().length - ( cursorPos[1] - cursorPos[0] );
70087008 context.$content.find( ':not(.wikiEditor)' ).addClass( 'wikiEditor' );
70097009 if ( $.layout.name !== 'webkit' ) {
70107010 context.$content.addClass( 'pasting' );
@@ -7018,26 +7018,6 @@
70197019 $(this).text( $(this).text() );
70207020 }
70217021 } );
7022 - // Remove newlines from all text nodes
7023 - var t = context.fn.traverser( context.$content );
7024 - while ( t ) {
7025 - if ( t.node.nodeName == '#text' ) {
7026 - // Text nodes that are nothing but blank lines need to be converted to full line breaks
7027 - if ( t.node.nodeValue === '\n' ) {
7028 - $( '<p><br></p>' ).insertAfter( $( t.node ) );
7029 - var oldNode = t.node;
7030 - t = t.next();
7031 - $( oldNode ).remove();
7032 - // We already advanced, so let's finish now
7033 - continue;
7034 - }
7035 - // Text nodes containing new lines just need conversion to spaces
7036 - else if ( ( t.node.nodeValue.indexOf( '\n' ) != 1 || t.node.nodeValue.indexOf( '\r' ) != -1 ) ) {
7037 - t.node.nodeValue = t.node.nodeValue.replace( /\r|\n/g, ' ' );
7038 - }
7039 - }
7040 - t = t.next();
7041 - }
70427022 // MS Word + webkit
70437023 context.$content.find( 'p:not(.wikiEditor) p:not(.wikiEditor)' )
70447024 .each( function(){
@@ -7048,6 +7028,34 @@
70497029 context.$content.find( 'span.Apple-style-span' ).each( function() {
70507030 $(this).replaceWith( this.childNodes );
70517031 } );
 7032+
 7033+ var pasteContent = context.fn.getOffset( cursorPos[0] ).node;
 7034+ var removeNextBR = false
 7035+ while ( pasteContent != null && ! $( pasteContent ).hasClass( 'wikiEditor' ) ) {
 7036+ var currentNode = pasteContent;
 7037+ pasteContent = pasteContent.nextSibling;
 7038+ if ( currentNode.nodeName == '#text' && currentNode.nodeValue == currentNode.wholeText ) {
 7039+ $( currentNode ).wrap( $( '<p></p>' ) );
 7040+ $( currentNode ).addClass( 'wikiEditor' );
 7041+ removeNextBR = true;
 7042+ } else if ( currentNode.nodeName == 'SPAN' ) {
 7043+ var text = $( currentNode ).text();
 7044+ if ( text.length == 0 ) {
 7045+ $( currentNode ).remove();
 7046+ }
 7047+ removeNextBR = false;
 7048+ } else if ( currentNode.nodeName == 'BR' ) {
 7049+ if (removeNextBR ) {
 7050+ $( currentNode ).remove();
 7051+ } else {
 7052+ $( currentNode ).addClass( 'wikiEditor' );
 7053+ }
 7054+ removeNextBR = false;
 7055+ } else {
 7056+ removeNextBR = false;
 7057+ }
 7058+ }
 7059+
70527060 var $selection = context.$content.find( ':not(.wikiEditor)' );
70537061 while ( $selection.length && $selection.length > 0 ) {
70547062 var $currentElement = $selection.eq( 0 );
@@ -7055,12 +7063,7 @@
70567064 $currentElement = $currentElement.parent();
70577065 }
70587066 var html = $( '<div></div>' ).text( $currentElement.text().replace( /\r|\n/g, ' ' ) ).html();
7059 - if ( $currentElement.is( 'br' ) ) {
7060 - $currentElement.addClass( 'wikiEditor' );
7061 - } else if ( $currentElement.is( 'span' ) && html.length == 0 ) {
7062 - // Markers!
7063 - $currentElement.remove();
7064 - } else if ( $currentElement.is( 'p' ) || $currentElement.is( 'div' ) ) {
 7067+ if ( $currentElement.is( 'p' ) || $currentElement.is( 'div' ) ) {
70657068 $newElement = $( '<p></p>' )
70667069 .addClass( 'wikiEditor' )
70677070 .insertAfter( $currentElement );
@@ -7084,7 +7087,8 @@
70857088
70867089 // Restore cursor position
70877090 context.fn.purgeOffsets();
7088 - var restoreTo = cursorPos[1] + context.fn.getContents().length - oldLength;
 7091+ var newLength = context.$content.html().length;
 7092+ var restoreTo = cursorPos[0] + newLength - oldLength;
70897093 context.fn.setSelection( { start: restoreTo, end: restoreTo } );
70907094 }, 0 );
70917095 return true;
@@ -9351,7 +9355,7 @@
93529356 /* TemplateEditor module for wikiEditor */
93539357 ( function( $ ) { $.wikiEditor.modules.templateEditor = {
93549358 /**
9355 - * Name mappings, dirty hack which will be reomved once "TemplateInfo" extension is more fully supported
 9359+ * Name mappings, dirty hack which will be removed once "TemplateInfo" extension is more fully supported
93569360 */
93579361 'nameMappings': { //keep these all lowercase to navigate web of redirects
93589362 "infobox skyscraper": "building_name",
@@ -9599,7 +9603,9 @@
96009604 bindTemplateEvents: function( $wrapper ) {
96019605 var $template = $wrapper.parent( '.wikiEditor-template' );
96029606
9603 - $template.parent().attr('contentEditable', 'false');
 9607+ if ( typeof ( opera ) == "undefined" ) {
 9608+ $template.parent().attr('contentEditable', 'false');
 9609+ }
96049610
96059611 $template.click( function(event) {event.preventDefault(); return false;} )
96069612
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -466,16 +466,17 @@
467467 return true;},'change':function(event){event.data.scope='division';var newHTML=context.$content.html();if(context.oldHTML!=newHTML){context.fn.purgeOffsets();context.oldHTML=newHTML;event.data.scope='realchange';}
468468 if(context.$content.children().length==0){context.$content.append('<p></p>');}
469469 return true;},'delayedChange':function(event){event.data.scope='division';var newHTML=context.$content.html();if(context.oldDelayedHTML!=newHTML){context.oldDelayedHTML=newHTML;event.data.scope='realchange';var cursorPos=context.fn.getCaretPosition();var t=context.fn.getOffset(cursorPos[0]);if(t&&t.node.nodeName=='#text'&&t.node.parentNode.nodeName.toLowerCase()=='body'){$(t.node).wrap("<p></p>");context.fn.purgeOffsets();context.fn.setSelection({start:cursorPos[0],end:cursorPos[1]});}}
470 -context.fn.updateHistory(event.data.scope=='realchange');return true;},'cut':function(event){setTimeout(function(){context.$content.find('br').each(function(){if($(this).parent().is('body')){$(this).wrap($('<p></p>'));}});},100);return true;},'paste':function(event){var cursorPos=context.fn.getCaretPosition();var oldLength=context.fn.getContents().length;context.$content.find(':not(.wikiEditor)').addClass('wikiEditor');if($.layout.name!=='webkit'){context.$content.addClass('pasting');}
471 -setTimeout(function(){context.$content.find('script,style,img,input,select,textarea,hr,button,link,meta').remove();context.$content.find('*').each(function(){if($(this).children().length==0&&this.childNodes.length>0){$(this).text($(this).text());}});var t=context.fn.traverser(context.$content);while(t){if(t.node.nodeName=='#text'){if(t.node.nodeValue==='\n'){$('<p><br></p>').insertAfter($(t.node));var oldNode=t.node;t=t.next();$(oldNode).remove();continue;}
472 -else if((t.node.nodeValue.indexOf('\n')!=1||t.node.nodeValue.indexOf('\r')!=-1)){t.node.nodeValue=t.node.nodeValue.replace(/\r|\n/g,' ');}}
473 -t=t.next();}
474 -context.$content.find('p:not(.wikiEditor) p:not(.wikiEditor)').each(function(){var outerParent=$(this).parent();outerParent.replaceWith(outerParent.childNodes);});context.$content.find('span.Apple-style-span').each(function(){$(this).replaceWith(this.childNodes);});var $selection=context.$content.find(':not(.wikiEditor)');while($selection.length&&$selection.length>0){var $currentElement=$selection.eq(0);while(!$currentElement.parent().is('body')&&!$currentElement.parent().is('.wikiEditor')){$currentElement=$currentElement.parent();}
475 -var html=$('<div></div>').text($currentElement.text().replace(/\r|\n/g,' ')).html();if($currentElement.is('br')){$currentElement.addClass('wikiEditor');}else if($currentElement.is('span')&&html.length==0){$currentElement.remove();}else if($currentElement.is('p')||$currentElement.is('div')){$newElement=$('<p></p>').addClass('wikiEditor').insertAfter($currentElement);if(html.length){$newElement.html(html);}else{$newElement.append($('<br>').addClass('wikiEditor'));}
 470+context.fn.updateHistory(event.data.scope=='realchange');return true;},'cut':function(event){setTimeout(function(){context.$content.find('br').each(function(){if($(this).parent().is('body')){$(this).wrap($('<p></p>'));}});},100);return true;},'paste':function(event){var cursorPos=context.fn.getCaretPosition();var oldLength=context.fn.getContents().length-(cursorPos[1]-cursorPos[0]);context.$content.find(':not(.wikiEditor)').addClass('wikiEditor');if($.layout.name!=='webkit'){context.$content.addClass('pasting');}
 471+setTimeout(function(){context.$content.find('script,style,img,input,select,textarea,hr,button,link,meta').remove();context.$content.find('*').each(function(){if($(this).children().length==0&&this.childNodes.length>0){$(this).text($(this).text());}});context.$content.find('p:not(.wikiEditor) p:not(.wikiEditor)').each(function(){var outerParent=$(this).parent();outerParent.replaceWith(outerParent.childNodes);});context.$content.find('span.Apple-style-span').each(function(){$(this).replaceWith(this.childNodes);});var pasteContent=context.fn.getOffset(cursorPos[0]).node;var removeNextBR=false
 472+while(pasteContent!=null&&!$(pasteContent).hasClass('wikiEditor')){var currentNode=pasteContent;pasteContent=pasteContent.nextSibling;if(currentNode.nodeName=='#text'&&currentNode.nodeValue==currentNode.wholeText){$(currentNode).wrap($('<p></p>'));$(currentNode).addClass('wikiEditor');removeNextBR=true;}else if(currentNode.nodeName=='SPAN'){var text=$(currentNode).text();if(text.length==0){$(currentNode).remove();}
 473+removeNextBR=false;}else if(currentNode.nodeName=='BR'){if(removeNextBR){$(currentNode).remove();}else{$(currentNode).addClass('wikiEditor');}
 474+removeNextBR=false;}else{removeNextBR=false;}}
 475+var $selection=context.$content.find(':not(.wikiEditor)');while($selection.length&&$selection.length>0){var $currentElement=$selection.eq(0);while(!$currentElement.parent().is('body')&&!$currentElement.parent().is('.wikiEditor')){$currentElement=$currentElement.parent();}
 476+var html=$('<div></div>').text($currentElement.text().replace(/\r|\n/g,' ')).html();if($currentElement.is('p')||$currentElement.is('div')){$newElement=$('<p></p>').addClass('wikiEditor').insertAfter($currentElement);if(html.length){$newElement.html(html);}else{$newElement.append($('<br>').addClass('wikiEditor'));}
476477 $currentElement.remove();}else{$newElement=$('<span></span>').html(html).insertAfter($currentElement);$newElement.replaceWith($newElement[0].childNodes);$currentElement.remove();}
477478 $selection=context.$content.find(':not(.wikiEditor)');}
478479 context.$content.find('.wikiEditor').removeClass('wikiEditor');if($.layout.name!=='webkit'){context.$content.removeClass('pasting');}
479 -context.fn.purgeOffsets();var restoreTo=cursorPos[1]+context.fn.getContents().length-oldLength;context.fn.setSelection({start:restoreTo,end:restoreTo});},0);return true;},'ready':function(event){context.history.push({'html':context.$content.html(),'sel':context.fn.getCaretPosition()});return true;}};context.fn={'trigger':function(name,event){if(typeof event=='undefined'){event={'type':'custom'};}
 480+context.fn.purgeOffsets();var newLength=context.$content.html().length;var restoreTo=cursorPos[0]+newLength-oldLength;context.fn.setSelection({start:restoreTo,end:restoreTo});},0);return true;},'ready':function(event){context.history.push({'html':context.$content.html(),'sel':context.fn.getCaretPosition()});return true;}};context.fn={'trigger':function(name,event){if(typeof event=='undefined'){event={'type':'custom'};}
480481 if(typeof event.data=='undefined'){event.data={};}
481482 if(name in context.evt){if(!context.evt[name](event)){return false;}}
482483 var returnFromModules=null;for(var module in context.modules){if(module in $.wikiEditor.modules&&'evt'in $.wikiEditor.modules[module]&&name in $.wikiEditor.modules[module].evt){var ret=$.wikiEditor.modules[module].evt[name](context,event);if(ret!=null){if(returnFromModules==null){returnFromModules=ret;}else{returnFromModules=returnFromModules&&ret;}}}}
@@ -660,7 +661,8 @@
661662 if($(node).parent().hasClass('wikiEditor-template')&&!model.isCollapsible()){$.wikiEditor.modules.templateEditor.fn.unwrapTemplate($(node));}else if(!$(node).parent().hasClass('wikiEditor-template')&&model.isCollapsible()){$.wikiEditor.modules.templateEditor.fn.wrapTemplate($(node));$.wikiEditor.modules.templateEditor.fn.bindTemplateEvents($(node));}},getAnchor:function(ca1,ca2){return $(ca1.parentNode).is('span.wikiEditor-template-text')?ca1.parentNode:null;},context:context,skipDivision:'realchange'});}else{tokenArray[beginIndex].label='TEMPLATE_FALSE_BEGIN';tokenIndex=beginIndex;}}}},keydown:function(context,event){if(context.$iframe.data('ignoreKeypress')){context.$iframe.data('ignoreKeypress',false);}
662663 var $evtElem=event.jQueryNode;if($evtElem.hasClass('wikiEditor-template-label')){if(event.ctrlKey||event.metaKey)return true;switch(event.which){case 13:$evtElem.click();event.preventDefault();return false;case 32:$evtElem.parent().siblings('.wikiEditor-template-expand').click();event.preventDefault();return false;case 37:case 38:case 39:case 40:return true;default:context.$iframe.data('ignoreKeypress',true);event.preventDefault();return false;}}else if($evtElem.hasClass('wikiEditor-template-text')){switch(event.which){case 13:context.$iframe.data('ignoreKeypress',true);context.fn.encapsulateSelection({'pre':'\n','peri':'','post':''});event.preventDefault();return false;default:return true;}}},keyup:function(context,event){if(context.$iframe.data('ignoreKeypress')){context.$iframe.data('ignoreKeypress',false);}
663664 return true;},keypress:function(context,event){return(context.$iframe.data('ignoreKeypress')?false:true);}},exp:[{'regex':/{{/,'label':"TEMPLATE_BEGIN"},{'regex':/}}/,'label':"TEMPLATE_END",'markAfter':true}],cfg:{},fn:{create:function(context,config){context.modules.templateEditor={};},wrapTemplate:function($wrapper){var model=$wrapper.data('model');var context=$wrapper.data('marker').context;var $template=$wrapper.wrap('<span class="wikiEditor-template"></span>').addClass('wikiEditor-template-text wikiEditor-template-text-shrunken').parent().addClass('wikiEditor-template-collapsed').prepend('<span class="wikiEditor-template-expand wikiEditor-noinclude"></span>'+'<span class="wikiEditor-template-name wikiEditor-noinclude">'+'<span class="wikiEditor-template-label wikiEditor-noinclude">'+
664 -$.wikiEditor.modules.templateEditor.fn.getTemplateDisplayName(model)+'</span>'+'<span class="wikiEditor-template-dialog wikiEditor-noinclude"></span>'+'</span>');},unwrapTemplate:function($wrapper){$wrapper.parent().replaceWith($wrapper);},bindTemplateEvents:function($wrapper){var $template=$wrapper.parent('.wikiEditor-template');$template.parent().attr('contentEditable','false');$template.click(function(event){event.preventDefault();return false;})
 665+$.wikiEditor.modules.templateEditor.fn.getTemplateDisplayName(model)+'</span>'+'<span class="wikiEditor-template-dialog wikiEditor-noinclude"></span>'+'</span>');},unwrapTemplate:function($wrapper){$wrapper.parent().replaceWith($wrapper);},bindTemplateEvents:function($wrapper){var $template=$wrapper.parent('.wikiEditor-template');if(typeof(opera)=="undefined"){$template.parent().attr('contentEditable','false');}
 666+$template.click(function(event){event.preventDefault();return false;})
665667 $template.find('.wikiEditor-template-name').click(function(event){$.wikiEditor.modules.templateEditor.fn.createDialog($wrapper);event.stopPropagation();return false;}).mousedown(function(event){event.stopPropagation();return false;});$template.find('.wikiEditor-template-expand').click(function(event){$.wikiEditor.modules.templateEditor.fn.toggleWikiTextEditor($wrapper);event.stopPropagation();return false;}).mousedown(function(event){event.stopPropagation();return false;});},toggleWikiTextEditor:function($wrapper){var context=$wrapper.data('marker').context;var $template=$wrapper.parent('.wikiEditor-template');context.fn.purgeOffsets();$template.toggleClass('wikiEditor-template-expanded').toggleClass('wikiEditor-template-collapsed');var $templateText=$template.find('.wikiEditor-template-text');$templateText.toggleClass('wikiEditor-template-text-shrunken');$templateText.toggleClass('wikiEditor-template-text-visible');if($templateText.hasClass('wikiEditor-template-text-shrunken')){if($templateText.html()!=$templateText.data('oldHTML')){var templateModel=$.wikiEditor.modules.templateEditor.fn.updateModel($templateText);var $tLabel=$template.find('.wikiEditor-template-label');$tLabel.text($.wikiEditor.modules.templateEditor.fn.getTemplateDisplayName(templateModel));}}},createDialog:function($wrapper){var context=$wrapper.data('marker').context;var $template=$wrapper.parent('.wikiEditor-template');var dialog={'titleMsg':'wikieditor-template-editor-dialog-title','id':'wikiEditor-template-dialog','html':'\
666668 <fieldset>\
667669 <div class="wikiEditor-template-dialog-title" />\

Status & tagging log