From: Roan Kattouw Date: Thu, 8 Sep 2011 16:05:50 +0000 (+0000) Subject: (bug 30130) Add selectionStart and selectionEnd parameters to encapsulateSelection... X-Git-Tag: 1.31.0-rc.0~27791 X-Git-Url: http://git.cyclocoop.org/ecrire?a=commitdiff_plain;h=6a59be46273e9f1f962817cf1ec6cf6fb1707105;p=lhc%2Fweb%2Fwiklou.git (bug 30130) Add selectionStart and selectionEnd parameters to encapsulateSelection in jquery.textSelection. Modified patch by Santosh (removed whitespace and debugging code, added tests). --- diff --git a/resources/jquery/jquery.textSelection.js b/resources/jquery/jquery.textSelection.js index fbdeb38898..8a1f44bb81 100644 --- a/resources/jquery/jquery.textSelection.js +++ b/resources/jquery/jquery.textSelection.js @@ -101,11 +101,23 @@ encapsulateSelection: function( options ) { } else if ( this.selectionStart || this.selectionStart == '0' ) { // Mozilla/Opera $(this).focus(); + if ( options.selectionStart !== undefined ) { + $(this).textSelection( 'setSelection', { 'start': options.selectionStart, 'end': options.selectionEnd } ); + } + var selText = $(this).textSelection( 'getSelection' ); var startPos = this.selectionStart; var endPos = this.selectionEnd; var scrollTop = this.scrollTop; checkSelectedText(); + if ( options.selectionStart !== undefined + && endPos - startPos != options.selectionEnd - options.selectionStart ) + { + // This means there is a difference in the selection range returned by browser and what we passed. + // This happens for Chrome in the case of composite characters. Ref bug #30130 + // Set the startPos to the correct position. + startPos = options.selectionStart; + } var insertText = pre + selText + post; if ( options.splitlines ) { @@ -143,6 +155,10 @@ encapsulateSelection: function( options ) { if ( context ) { context.fn.restoreCursorAndScrollTop(); } + if ( options.selectionStart !== undefined ) { + $(this).textSelection( 'setSelection', { 'start': options.selectionStart, 'end': options.selectionEnd } ); + } + var selText = $(this).textSelection( 'getSelection' ); var scrollTop = this.scrollTop; var range = document.selection.createRange(); @@ -417,7 +433,9 @@ scrollToCaretPosition: function( options ) { 'ownline': false, // Put the inserted text on a line of its own 'replace': false, // If there is a selection, replace it with peri instead of leaving it alone 'selectPeri': true, // Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true) - 'splitlines': false // If multiple lines are selected, encapsulate each line individually + 'splitlines': false, // If multiple lines are selected, encapsulate each line individually + 'selectionStart': undefined, // Position to start selection at + 'selectionEnd': undefined // Position to end selection at. Defaults to start }, options ); break; case 'getCaretPosition': diff --git a/tests/qunit/suites/resources/jquery/jquery.textSelection.test.js b/tests/qunit/suites/resources/jquery/jquery.textSelection.test.js index 132da276dd..5926409d73 100644 --- a/tests/qunit/suites/resources/jquery/jquery.textSelection.test.js +++ b/tests/qunit/suites/resources/jquery/jquery.textSelection.test.js @@ -59,12 +59,11 @@ var encapsulateTest = function( options ) { start += newLinesBefore; end += newLinesBefore + newLinesInside; } - $textarea.textSelection( 'setSelection', { - start: start, - end: end - }); - $textarea.textSelection( 'encapsulateSelection', opt.replace ); + var options = $.extend( {}, opt.replace ); // Clone opt.replace + options.selectionStart = start; + options.selectionEnd = end; + $textarea.textSelection( 'encapsulateSelection', options ); var text = $textarea.textSelection( 'getContents' ).replace( /\r\n/g, "\n" );