r63230 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r63229‎ | r63230 | r63231 >
Date:23:15, 3 March 2010
Author:adam
Status:ok (Comments)
Tags:
Comment:
WikiEditor: Adding more cases for end of line detection in encapsulateSelection. Part of my work on bug 22347
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' => 7 ),
7575 array( 'src' => 'js/plugins/jquery.textSelection.js', 'version' => 27 ),
76 - array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 158 ),
 76+ array( 'src' => 'js/plugins/jquery.wikiEditor.js', 'version' => 159 ),
7777 array( 'src' => 'js/plugins/jquery.wikiEditor.highlight.js', 'version' => 36 ),
7878 array( 'src' => 'js/plugins/jquery.wikiEditor.toolbar.js', 'version' => 52 ),
7979 array( 'src' => 'js/plugins/jquery.wikiEditor.dialogs.js', 'version' => 19 ),
@@ -82,10 +82,10 @@
8383 array( 'src' => 'js/plugins/jquery.wikiEditor.publish.js', 'version' => 3 ),
8484 ),
8585 'combined' => array(
86 - array( 'src' => 'js/plugins.combined.js', 'version' => 303 ),
 86+ array( 'src' => 'js/plugins.combined.js', 'version' => 304 ),
8787 ),
8888 'minified' => array(
89 - array( 'src' => 'js/plugins.combined.min.js', 'version' => 303 ),
 89+ array( 'src' => 'js/plugins.combined.min.js', 'version' => 304 ),
9090 ),
9191 ),
9292 );
Index: trunk/extensions/UsabilityInitiative/js/plugins/jquery.wikiEditor.js
@@ -1257,9 +1257,11 @@
12581258 } else {
12591259 atEnd = true;
12601260 }
1261 - } else if ( range.startContainer.nodeName == '#text' &&
1262 - range.startOffset == range.startContainer.nodeValue.length ) {
1263 - // Apparently this happens when splitting text nodes
 1261+ }
 1262+ if ( ( range.endOffset == 0 && range.endContainer.nodeValue == null ) ||
 1263+ ( range.endContainer.nodeName == '#text' &&
 1264+ range.endOffset == range.endContainer.nodeValue.length ) ||
 1265+ ( range.endContainer.nodeName == 'P' && range.endContainer.nodeValue == null ) ) {
12641266 atEnd = true;
12651267 }
12661268 if ( !atStart ) {
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.js
@@ -7800,9 +7800,11 @@
78017801 } else {
78027802 atEnd = true;
78037803 }
7804 - } else if ( range.startContainer.nodeName == '#text' &&
7805 - range.startOffset == range.startContainer.nodeValue.length ) {
7806 - // Apparently this happens when splitting text nodes
 7804+ }
 7805+ if ( ( range.endOffset == 0 && range.endContainer.nodeValue == null ) ||
 7806+ ( range.endContainer.nodeName == '#text' &&
 7807+ range.endOffset == range.endContainer.nodeValue.length ) ||
 7808+ ( range.endContainer.nodeName == 'P' && range.endContainer.nodeValue == null ) ) {
78077809 atEnd = true;
78087810 }
78097811 if ( !atStart ) {
Index: trunk/extensions/UsabilityInitiative/js/plugins.combined.min.js
@@ -528,7 +528,8 @@
529529 if(context.$iframe[0].contentWindow.getSelection){var range=context.$iframe[0].contentWindow.getSelection().getRangeAt(0);if(collapseToEnd){if(range.endContainer.nodeName=='BR'){range.setEndBefore(range.endContainer);}
530530 range.collapse(false);}
531531 if(options.ownline){var atStart=false,atEnd=false;var body=context.$content.get(0);if(range.startOffset==0){atStart=true;}else if(range.startContainer==body){var n=body.firstChild;for(var i=0;i<range.startOffset-1&&n;i++){n=n.nextSibling;}
532 -if(n&&n.nodeName=='BR'){atStart=true;}else{atEnd=true;}}else if(range.startContainer.nodeName=='#text'&&range.startOffset==range.startContainer.nodeValue.length){atEnd=true;}
 532+if(n&&n.nodeName=='BR'){atStart=true;}else{atEnd=true;}}
 533+if((range.endOffset==0&&range.endContainer.nodeValue==null)||(range.endContainer.nodeName=='#text'&&range.endOffset==range.endContainer.nodeValue.length)||(range.endContainer.nodeName=='P'&&range.endContainer.nodeValue==null)){atEnd=true;}
533534 if(!atStart){pre="\n"+options.pre;}
534535 if(!atEnd){post+="\n";}}
535536 var insertText="";if(options.splitlines){for(var j=0;j<selTextArr.length;j++){insertText=insertText+pre+selTextArr[j]+post;if(j!=selTextArr.length-1){insertText+="\n";}}}else{insertText=pre+selText+post;}

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r62980WikiEditor: partial fix for bug 22347 and updating one image asset for the te...adam23:05, 25 February 2010

Comments

#Comment by Catrope (talk | contribs)   15:25, 4 March 2010
-					} else if ( range.startContainer.nodeName == '#text' &&
-							range.startOffset == range.startContainer.nodeValue.length ) {
-						// Apparently this happens when splitting text nodes
+					}
+					if ( ( range.endOffset == 0 && range.endContainer.nodeValue == null ) ||
+							( range.endContainer.nodeName == '#text' &&
+									range.endOffset == range.endContainer.nodeValue.length ) ||
+							( range.endContainer.nodeName == 'P' && range.endContainer.nodeValue == null ) ) {

This doesn't just add a case, it also changes one (from startContainer to endContainer; why was that)?

#Comment by Adammiller~mediawikiwiki (talk | contribs)   15:48, 4 March 2010

I changed it because it didn't seem to make sense the other way. We're looking to see if the selection is at the end of a line, so it seems you'd want to use endContainer to do your case checking. Is there a reason it was using startContainer before? I was testing against some particular cases, but this change didn't seem to negatively impact anything for me.


#Comment by Catrope (talk | contribs)   15:58, 4 March 2010

When checking whether the "selection" is at the start/end of the line, we're really checking whether the start of the selection is at the start/end of the line, so we should be using startContainer throughout. For zero-length selections (i.e. cursor positions without selections), startContainer and endContainer will be equal anyway.

#Comment by Adammiller~mediawikiwiki (talk | contribs)   22:28, 4 March 2010

My thinking was that if we do our checks for atStart using startOffset and startContainer, and use endOffset and endContainer for checking for atEnd we will be able to catch both collapsed and non-collapsed selections. As you said, for collapsed selections, the end and start variables will be the same, so this change shouldn't affect those cases at all, but gives us new support for non-collapsed selections.

This change was inspired by one specific case that was not working how I wanted it to. The case, starting with a blank article was:

1. Add a bulleted list item with the toolbar. 2. Add a numbered list item with the toolbar.

Before this change, you'd get "*
# Bulleted list item
" and the cursor would be on a line below the inserted text. After this change we get a much nicer "*
# Bulleted list item" with the cursor right at the end of the text, where it should be. Because encapsulateSelection correctly recognizes the selection being at the end of the line when you click the numbered list item button, the extra line break is not inserted after the text.


#Comment by Adammiller~mediawikiwiki (talk | contribs)   22:31, 4 March 2010

A better formatted version of what I just posted.

My thinking was that if we do our checks for atStart using startOffset and startContainer, and use endOffset and endContainer for checking for atEnd we will be able to catch both collapsed and non-collapsed selections. As you said, for collapsed selections, the end and start variables will be the same, so this change shouldn't affect those cases at all, but gives us new support for non-collapsed selections. This change was inspired by one specific case that was not working how I wanted it to. The case, starting with a blank article was:

  1. Add a bulleted list item with the toolbar.
  2. Add a numbered list item with the toolbar.

Before this change, you'd get:

"*<br /># Bulleted list item<br />"

and the cursor would be on a line below the inserted text. After this change we get a much nicer:

"*<br /># Bulleted list item"

with the cursor right at the end of the text, where it should be. Because encapsulateSelection correctly recognizes the selection being at the end of the line when you click the numbered list item button, the extra line break is not inserted after the text.

Status & tagging log