Index: trunk/phase3/resources/jquery/jquery.textSelection.js |
— | — | @@ -3,6 +3,20 @@ |
4 | 4 | */ |
5 | 5 | ( function( $ ) { |
6 | 6 | $.fn.textSelection = function( command, options ) { |
| 7 | + |
| 8 | +/** |
| 9 | + * Helper function to get an IE TextRange object for an element |
| 10 | + */ |
| 11 | +function rangeForElementIE( e ) { |
| 12 | + if ( e.nodeName.toLowerCase() == 'input' ) { |
| 13 | + return e.createTextRange(); |
| 14 | + } else { |
| 15 | + var sel = document.body.createTextRange(); |
| 16 | + sel.moveToElementText( e ); |
| 17 | + return sel; |
| 18 | + } |
| 19 | +} |
| 20 | + |
7 | 21 | var fn = { |
8 | 22 | /** |
9 | 23 | * Get the contents of the textarea |
— | — | @@ -152,15 +166,11 @@ |
153 | 167 | // Create range containing text in the selection |
154 | 168 | var periRange = document.selection.createRange().duplicate(); |
155 | 169 | // Create range containing text before the selection |
156 | | - var preRange = document.body.createTextRange(); |
157 | | - // Select all the text |
158 | | - preRange.moveToElementText(e); |
| 170 | + var preRange = rangeForElementIE( e ); |
159 | 171 | // Move the end where we need it |
160 | 172 | preRange.setEndPoint("EndToStart", periRange); |
161 | 173 | // Create range containing text after the selection |
162 | | - var postRange = document.body.createTextRange(); |
163 | | - // Select all the text |
164 | | - postRange.moveToElementText(e); |
| 174 | + var postRange = rangeForElementIE( e ); |
165 | 175 | // Move the start where we need it |
166 | 176 | postRange.setEndPoint("StartToEnd", periRange); |
167 | 177 | // Load the text values we need to compare |
— | — | @@ -240,8 +250,7 @@ |
241 | 251 | this.selectionEnd = options.end; |
242 | 252 | } |
243 | 253 | } else if ( document.body.createTextRange ) { |
244 | | - var selection = document.body.createTextRange(); |
245 | | - selection.moveToElementText( this ); |
| 254 | + var selection = rangeForElementIE( this ); |
246 | 255 | var length = this.value.length; |
247 | 256 | // IE doesn't count \n when computing the offset, so we won't either |
248 | 257 | var newLines = this.value.match( /\n/g ); |