From: Roan Kattouw Date: Thu, 28 Jul 2011 06:40:31 +0000 (+0000) Subject: Followup r93366: fix Opera bugs in textSelection. All tests pass in Opera now. Before... X-Git-Tag: 1.31.0-rc.0~28571 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=bc72a007253338342fa9a6497e8a3e1606b4fac5;p=lhc%2Fweb%2Fwiklou.git Followup r93366: fix Opera bugs in textSelection. All tests pass in Opera now. Before, there were five failures * 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 --- diff --git a/resources/jquery/jquery.textSelection.js b/resources/jquery/jquery.textSelection.js index 7c39927cbf..b16e755663 100644 --- a/resources/jquery/jquery.textSelection.js +++ b/resources/jquery/jquery.textSelection.js @@ -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 ); diff --git a/tests/qunit/suites/resources/jquery/jquery.textSelection.js b/tests/qunit/suites/resources/jquery/jquery.textSelection.js index d231af4937..c6848863f8 100644 --- a/tests/qunit/suites/resources/jquery/jquery.textSelection.js +++ b/tests/qunit/suites/resources/jquery/jquery.textSelection.js @@ -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' );