r63455 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63454‎ | r63455 | r63456 >
Date:03:26, 9 March 2010
Author:pdhanda
Status:resolved (Comments)
Tags:
Comment:
This should fix the some toc highlighting issues. Also bug 22642
Modified paths:
  • /trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -370,12 +370,7 @@
371371 context.oldHTML = newHTML;
372372 event.data.scope = 'realchange';
373373 }
374 - // Are we deleting a <p> with one keystroke? if so, either remove preceding <br> or merge <p>s
375 - switch ( event.which ) {
376 - case 8: // backspace
377 - // do something here...
378 - break;
379 - }
 374+
380375 return true;
381376 },
382377 'delayedChange': function( event ) {
@@ -385,6 +380,17 @@
386381 context.oldDelayedHTML = newHTML;
387382 event.data.scope = 'realchange';
388383 }
 384+
 385+ //surround by <p> if it does not already have it
 386+ var t = context.fn.getOffset();
 387+ if ( t.node.parentNode.nodeName.toLowerCase() == 'body' ) {
 388+ var cursorPos = context.fn.getCaretPosition()[0];
 389+ $( t.node ).wrap( "<p></p>" );
 390+ context.fn.refreshOffsets();
 391+ context.fn.setSelection( { start: cursorPos, end: cursorPos } );
 392+ }
 393+
 394+
389395 context.fn.updateHistory( event.data.scope == 'realchange' );
390396 return true;
391397 },
@@ -437,7 +443,7 @@
438444 }
439445 t = t.next();
440446 }
441 - // MS Word + webkit
 447+ // MS Word + webkit
442448 context.$content.find( 'p:not(.wikiEditor) p:not(.wikiEditor)' )
443449 .each( function(){
444450 var outerParent = $(this).parent();
@@ -1564,10 +1570,10 @@
15651571 end = e ? e.offset : null;
15661572 // Don't try to set the selection past the end of a node, causes errors
15671573 // Just put the selection at the end of the node in this case
1568 - if ( sc.nodeName == '#text' && start >= sc.nodeValue.length ) {
 1574+ if ( sc.nodeName == '#text' && start > sc.nodeValue.length ) {
15691575 start = sc.nodeValue.length - 1;
15701576 }
1571 - if ( ec.nodeName == '#text' && end >= ec.nodeValue.length ) {
 1577+ if ( ec.nodeName == '#text' && end > ec.nodeValue.length ) {
15721578 end = ec.nodeValue.length - 1;
15731579 }
15741580 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r63461UsabilityInitiative: Assorted fixes...catrope11:09, 9 March 2010
r63512Response to r63455. wrap with <p> only for content that is changed. Also fixe...pdhanda21:44, 9 March 2010

Comments

#Comment by Catrope (talk | contribs)   10:32, 9 March 2010
+			var t = context.fn.getOffset();

getOffset() takes a parameter; presumably this acts like getOffset(0), but I'm not even sure of that and I wrote that function. Instead of just doing this check for the first textnode in the document, why not do a general check for textnodes that are direct children of the body, something along the lines of:

if ( context.$content.children().length < context.$content.get( 0 ).childNodes.length ) { // textnodes count on the RHS but not on the LHS
	var $textnodes = $( context.$content.get( 0 ).childNodes ).not( '*' ); // textnodes don't match any selector, not even *
	$textnodes.each( function() { ..... } );
}
+				context.fn.refreshOffsets();

Please use purgeOffsets() instead, it purges the offset cache so offsets are regenerated the next time they're needed rather than right now. This matters in browsers whose setSelection() implementation doesn't use the offsets system (e.g. IE), and matters generally if you're not calling an offset-reliant function right after.

+				var cursorPos = context.fn.getCaretPosition()[0];
...
+				context.fn.setSelection( { start: cursorPos, end: cursorPos } );

This force-collapses the selection to the start. Is there a good reason to do this? If not, you should use start: cursorPos[0], end: cursorPos[1] and drop the [0] from the cursorPos assignment.

-					if ( sc.nodeName == '#text' && start >= sc.nodeValue.length ) {
+					if ( sc.nodeName == '#text' && start > sc.nodeValue.length ) {

This allows the start offset to be equal to the length. That's already out of bounds, the highest valid offset is length - 1, right?

#Comment by Pdhanda (talk | contribs)   00:23, 13 March 2010

The only issue I did not modify is the last one. From what I'm seeing I think length is a valid offset. Eg if length is 1, placing the cursor at 1 will place it after the first character.

Status & tagging log