Index: trunk/phase3/resources/jquery/jquery.textSelection.js |
— | — | @@ -101,11 +101,23 @@ |
102 | 102 | } else if ( this.selectionStart || this.selectionStart == '0' ) { |
103 | 103 | // Mozilla/Opera |
104 | 104 | $(this).focus(); |
| 105 | + if ( options.selectionStart !== undefined ) { |
| 106 | + $(this).textSelection( 'setSelection', { 'start': options.selectionStart, 'end': options.selectionEnd } ); |
| 107 | + } |
| 108 | + |
105 | 109 | var selText = $(this).textSelection( 'getSelection' ); |
106 | 110 | var startPos = this.selectionStart; |
107 | 111 | var endPos = this.selectionEnd; |
108 | 112 | var scrollTop = this.scrollTop; |
109 | 113 | checkSelectedText(); |
| 114 | + if ( options.selectionStart !== undefined |
| 115 | + && endPos - startPos != options.selectionEnd - options.selectionStart ) |
| 116 | + { |
| 117 | + // This means there is a difference in the selection range returned by browser and what we passed. |
| 118 | + // This happens for Chrome in the case of composite characters. Ref bug #30130 |
| 119 | + // Set the startPos to the correct position. |
| 120 | + startPos = options.selectionStart; |
| 121 | + } |
110 | 122 | |
111 | 123 | var insertText = pre + selText + post; |
112 | 124 | if ( options.splitlines ) { |
— | — | @@ -143,6 +155,10 @@ |
144 | 156 | if ( context ) { |
145 | 157 | context.fn.restoreCursorAndScrollTop(); |
146 | 158 | } |
| 159 | + if ( options.selectionStart !== undefined ) { |
| 160 | + $(this).textSelection( 'setSelection', { 'start': options.selectionStart, 'end': options.selectionEnd } ); |
| 161 | + } |
| 162 | + |
147 | 163 | var selText = $(this).textSelection( 'getSelection' ); |
148 | 164 | var scrollTop = this.scrollTop; |
149 | 165 | var range = document.selection.createRange(); |
— | — | @@ -417,7 +433,9 @@ |
418 | 434 | 'ownline': false, // Put the inserted text on a line of its own |
419 | 435 | 'replace': false, // If there is a selection, replace it with peri instead of leaving it alone |
420 | 436 | 'selectPeri': true, // Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true) |
421 | | - 'splitlines': false // If multiple lines are selected, encapsulate each line individually |
| 437 | + 'splitlines': false, // If multiple lines are selected, encapsulate each line individually |
| 438 | + 'selectionStart': undefined, // Position to start selection at |
| 439 | + 'selectionEnd': undefined // Position to end selection at. Defaults to start |
422 | 440 | }, options ); |
423 | 441 | break; |
424 | 442 | case 'getCaretPosition': |
Index: trunk/phase3/tests/qunit/suites/resources/jquery/jquery.textSelection.test.js |
— | — | @@ -59,12 +59,11 @@ |
60 | 60 | start += newLinesBefore; |
61 | 61 | end += newLinesBefore + newLinesInside; |
62 | 62 | } |
63 | | - $textarea.textSelection( 'setSelection', { |
64 | | - start: start, |
65 | | - end: end |
66 | | - }); |
67 | 63 | |
68 | | - $textarea.textSelection( 'encapsulateSelection', opt.replace ); |
| 64 | + var options = $.extend( {}, opt.replace ); // Clone opt.replace |
| 65 | + options.selectionStart = start; |
| 66 | + options.selectionEnd = end; |
| 67 | + $textarea.textSelection( 'encapsulateSelection', options ); |
69 | 68 | |
70 | 69 | var text = $textarea.textSelection( 'getContents' ).replace( /\r\n/g, "\n" ); |
71 | 70 | |