Followup r93366: fix Opera bugs in textSelection. All tests pass in Opera now. Before...
authorRoan Kattouw <catrope@users.mediawiki.org>
Thu, 28 Jul 2011 06:40:31 +0000 (06:40 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Thu, 28 Jul 2011 06:40:31 +0000 (06:40 +0000)
* replace \r\n with \n in the obtained output from getSelection. This fixes one failure
* In continuation of r93366, which updated post for added newlines in insertText, do the same for pre and also do this in the non-IE branch. This fixes one failure
* Look for \r as well as \n where line endings are concerned in the non-IE branch, just like in the IE branch. This fixes one failure
* Have the test case generator remap selection positions for Opera to account for the fact that a newline is double-counted. This fixes the remaining two failures

resources/jquery/jquery.textSelection.js
tests/qunit/suites/resources/jquery/jquery.textSelection.js

index 7c39927..b16e755 100644 (file)
@@ -112,11 +112,13 @@ encapsulateSelection: function( options ) {
                                insertText = doSplitLines( selText, pre, post );
                        }
                        if ( options.ownline ) {
-                               if ( startPos != 0 && this.value.charAt( startPos - 1 ) != "\n" ) {
+                               if ( startPos != 0 && this.value.charAt( startPos - 1 ) != "\n" && this.value.charAt( startPos - 1 ) != "\r" ) {
                                        insertText = "\n" + insertText;
+                                       pre += "\n";
                                }
-                               if ( this.value.charAt( endPos ) != "\n" ) {
+                               if ( this.value.charAt( endPos ) != "\n" && this.value.charAt( endPos ) != "\r" ) {
                                        insertText += "\n";
+                                       post += "\n";
                                }
                        }
                        this.value = this.value.substring( 0, startPos ) + insertText +
@@ -157,6 +159,7 @@ encapsulateSelection: function( options ) {
                                // FIXME: Which check is correct?
                                if ( range2.text != "\r" && range2.text != "\n" && range2.text != "" ) {
                                        insertText = "\n" + insertText;
+                                       pre += "\n";
                                }
                                var range3 = document.selection.createRange();
                                range3.collapse( false );
@@ -292,7 +295,7 @@ setSelection: function( options ) {
                        var length = this.value.length;
                        // IE doesn't count \n when computing the offset, so we won't either
                        var newLines = this.value.match( /\n/g );
-                       if ( newLines) length = length - newLines.length;
+                       if ( newLines ) length = length - newLines.length;
                        selection.moveStart( 'character', options.start );
                        selection.moveEnd( 'character', -length + options.end );
                        
index d231af4..c684886 100644 (file)
@@ -50,14 +50,23 @@ var encapsulateTest = function( options ) {
                //$textarea.textSelection( 'setContents', opt.before.text); // this method is actually missing atm...
                $textarea.val( opt.before.text ); // won't work with the WikiEditor iframe?
 
+               var     start = opt.before.start,
+                       end = opt.before.end;
+               if ( window.opera ) {
+                       // Compensate for Opera's craziness converting "\n" to "\r\n" and counting that as two chars
+                       var     newLinesBefore = opt.before.text.substring( 0, start ).split( "\n" ).length - 1,
+                               newLinesInside = opt.before.text.substring( start, end ).split( "\n" ).length - 1;
+                       start += newLinesBefore;
+                       end += newLinesBefore + newLinesInside;
+               }
                $textarea.textSelection( 'setSelection', {
-                       start: opt.before.start,
-                       end: opt.before.end
+                       start: start,
+                       end: end
                });
 
                $textarea.textSelection( 'encapsulateSelection', opt.replace );
 
-               var text = $textarea.textSelection( 'getContents' );
+               var text = $textarea.textSelection( 'getContents' ).replace( /\r\n/g, "\n" );
 
                equal( text, opt.after.text, 'Checking full text after encapsulation' );