2 * These plugins provide extra functionality for interaction with textareas.
6 if (document
.selection
&& document
.selection
.createRange
) {
7 // On IE, patch the focus() method to restore the windows' scroll position
10 focus
: (function ( _focus
) {
12 if ( arguments
.length
== 0 ) {
14 var state
= {top
: $w
.scrollTop(), left
: $w
.scrollLeft()};
15 var result
= _focus
.apply( this, arguments
);
16 window
.scrollTo( state
.top
, state
.left
);
19 return _focus
.apply( this, arguments
);
25 $.fn
.textSelection = function( command
, options
) {
28 * Helper function to get an IE TextRange object for an element
30 function rangeForElementIE( e
) {
31 if ( e
.nodeName
.toLowerCase() == 'input' ) {
32 return e
.createTextRange();
34 var sel
= document
.body
.createTextRange();
35 sel
.moveToElementText( e
);
42 * Get the contents of the textarea
44 getContents: function() {
48 * Get the currently selected text in this textarea. Will focus the textarea
49 * in some browsers (IE/Opera)
51 getSelection: function() {
52 var e
= this.get( 0 );
54 if ( $(e
).is( ':hidden' ) ) {
56 } else if ( document
.selection
&& document
.selection
.createRange
) {
58 var range
= document
.selection
.createRange();
60 } else if ( e
.selectionStart
|| e
.selectionStart
== '0' ) {
61 retval
= e
.value
.substring( e
.selectionStart
, e
.selectionEnd
);
66 * Ported from skins/common/edit.js by Trevor Parscal
67 * (c) 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org
69 * Inserts text at the begining and end of a text selection, optionally
70 * inserting text at the caret when selection is empty.
72 * @fixme document the options parameters
74 encapsulateSelection: function( options
) {
75 return this.each( function() {
76 var pre
= options
.pre
, post
= options
.post
;
79 * Check if the selected text is the same as the insert text
81 function checkSelectedText() {
83 selText
= options
.peri
;
85 } else if ( options
.replace
) {
86 selText
= options
.peri
;
88 while ( selText
.charAt( selText
.length
- 1 ) == ' ' ) {
89 // Exclude ending space char
90 selText
= selText
.substring( 0, selText
.length
- 1 );
93 while ( selText
.charAt( 0 ) == ' ' ) {
94 // Exclude prepending space char
95 selText
= selText
.substring( 1, selText
.length
);
102 * Do the splitlines stuff.
104 * Wrap each line of the selected text with pre and post
106 function doSplitLines( selText
, pre
, post
) {
108 var selTextArr
= selText
.split( '\n' );
109 for ( var i
= 0; i
< selTextArr
.length
; i
++ ) {
110 insertText
+= pre
+ selTextArr
[i
] + post
;
111 if ( i
!= selTextArr
.length
- 1 ) {
118 var isSample
= false;
119 if ( this.style
.display
== 'none' ) {
121 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
124 if ( options
.selectionStart
!== undefined ) {
125 $(this).textSelection( 'setSelection', { 'start': options
.selectionStart
, 'end': options
.selectionEnd
} );
128 var selText
= $(this).textSelection( 'getSelection' );
129 var startPos
= this.selectionStart
;
130 var endPos
= this.selectionEnd
;
131 var scrollTop
= this.scrollTop
;
133 if ( options
.selectionStart
!== undefined
134 && endPos
- startPos
!= options
.selectionEnd
- options
.selectionStart
)
136 // This means there is a difference in the selection range returned by browser and what we passed.
137 // This happens for Chrome in the case of composite characters. Ref bug #30130
138 // Set the startPos to the correct position.
139 startPos
= options
.selectionStart
;
142 var insertText
= pre
+ selText
+ post
;
143 if ( options
.splitlines
) {
144 insertText
= doSplitLines( selText
, pre
, post
);
146 if ( options
.ownline
) {
147 if ( startPos
!= 0 && this.value
.charAt( startPos
- 1 ) != "\n" && this.value
.charAt( startPos
- 1 ) != "\r" ) {
148 insertText
= "\n" + insertText
;
151 if ( this.value
.charAt( endPos
) != "\n" && this.value
.charAt( endPos
) != "\r" ) {
156 this.value
= this.value
.substring( 0, startPos
) + insertText
+
157 this.value
.substring( endPos
, this.value
.length
);
158 // Setting this.value scrolls the textarea to the top, restore the scroll position
159 this.scrollTop
= scrollTop
;
160 if ( window
.opera
) {
161 pre
= pre
.replace( /\r?\n/g, "\r\n" );
162 selText
= selText
.replace( /\r?\n/g, "\r\n" );
163 post
= post
.replace( /\r?\n/g, "\r\n" );
165 if ( isSample
&& options
.selectPeri
&& !options
.splitlines
) {
166 this.selectionStart
= startPos
+ pre
.length
;
167 this.selectionEnd
= startPos
+ pre
.length
+ selText
.length
;
169 this.selectionStart
= startPos
+ insertText
.length
;
170 this.selectionEnd
= this.selectionStart
;
172 } else if ( document
.selection
&& document
.selection
.createRange
) {
176 context
.fn
.restoreCursorAndScrollTop();
178 if ( options
.selectionStart
!== undefined ) {
179 $(this).textSelection( 'setSelection', { 'start': options
.selectionStart
, 'end': options
.selectionEnd
} );
182 var selText
= $(this).textSelection( 'getSelection' );
183 var scrollTop
= this.scrollTop
;
184 var range
= document
.selection
.createRange();
187 var insertText
= pre
+ selText
+ post
;
188 if ( options
.splitlines
) {
189 insertText
= doSplitLines( selText
, pre
, post
);
191 if ( options
.ownline
&& range
.moveStart
) {
192 var range2
= document
.selection
.createRange();
194 range2
.moveStart( 'character', -1 );
195 // FIXME: Which check is correct?
196 if ( range2
.text
!= "\r" && range2
.text
!= "\n" && range2
.text
!= "" ) {
197 insertText
= "\n" + insertText
;
200 var range3
= document
.selection
.createRange();
201 range3
.collapse( false );
202 range3
.moveEnd( 'character', 1 );
203 if ( range3
.text
!= "\r" && range3
.text
!= "\n" && range3
.text
!= "" ) {
209 range
.text
= insertText
;
210 if ( isSample
&& options
.selectPeri
&& range
.moveStart
) {
211 range
.moveStart( 'character', - post
.length
- selText
.length
);
212 range
.moveEnd( 'character', - post
.length
);
215 // Restore the scroll position
216 this.scrollTop
= scrollTop
;
218 $(this).trigger( 'encapsulateSelection', [ options
.pre
, options
.peri
, options
.post
, options
.ownline
,
219 options
.replace
, options
.spitlines
] );
223 * Ported from Wikia's LinkSuggest extension
224 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
225 * Some code copied from
226 * http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/
228 * Get the position (in resolution of bytes not nessecarily characters)
231 * Will focus the textarea in some browsers (IE/Opera)
233 * @fixme document the options parameters
235 getCaretPosition: function( options
) {
236 function getCaret( e
) {
237 var caretPos
= 0, endPos
= 0;
238 if ( document
.selection
&& document
.selection
.createRange
) {
239 // IE doesn't properly report non-selected caret position through
240 // the selection ranges when textarea isn't focused. This can
241 // lead to saving a bogus empty selection, which then screws up
242 // whatever we do later (bug 31847).
246 var preFinished
= false;
247 var periFinished
= false;
248 var postFinished
= false;
249 var preText
, rawPreText
, periText
;
250 var rawPeriText
, postText
, rawPostText
;
251 // Create range containing text in the selection
252 var periRange
= document
.selection
.createRange().duplicate();
253 // Create range containing text before the selection
254 var preRange
= rangeForElementIE( e
);
255 // Move the end where we need it
256 preRange
.setEndPoint("EndToStart", periRange
);
257 // Create range containing text after the selection
258 var postRange
= rangeForElementIE( e
);
259 // Move the start where we need it
260 postRange
.setEndPoint("StartToEnd", periRange
);
261 // Load the text values we need to compare
262 preText
= rawPreText
= preRange
.text
;
263 periText
= rawPeriText
= periRange
.text
;
264 postText
= rawPostText
= postRange
.text
;
266 * Check each range for trimmed newlines by shrinking the range by 1
267 * character and seeing if the text property has changed. If it has
268 * not changed then we know that IE has trimmed a \r\n from the end.
271 if ( !preFinished
) {
272 if ( preRange
.compareEndPoints( "StartToEnd", preRange
) == 0 ) {
275 preRange
.moveEnd( "character", -1 );
276 if ( preRange
.text
== preText
) {
277 rawPreText
+= "\r\n";
283 if ( !periFinished
) {
284 if ( periRange
.compareEndPoints( "StartToEnd", periRange
) == 0 ) {
287 periRange
.moveEnd( "character", -1 );
288 if ( periRange
.text
== periText
) {
289 rawPeriText
+= "\r\n";
295 if ( !postFinished
) {
296 if ( postRange
.compareEndPoints("StartToEnd", postRange
) == 0 ) {
299 postRange
.moveEnd( "character", -1 );
300 if ( postRange
.text
== postText
) {
301 rawPostText
+= "\r\n";
307 } while ( ( !preFinished
|| !periFinished
|| !postFinished
) );
308 caretPos
= rawPreText
.replace( /\r\n/g, "\n" ).length
;
309 endPos
= caretPos
+ rawPeriText
.replace( /\r\n/g, "\n" ).length
;
310 } else if ( e
.selectionStart
|| e
.selectionStart
== '0' ) {
312 caretPos
= e
.selectionStart
;
313 endPos
= e
.selectionEnd
;
315 return options
.startAndEnd
? [ caretPos
, endPos
] : caretPos
;
317 return getCaret( this.get( 0 ) );
320 * @fixme document the options parameters
322 setSelection: function( options
) {
323 return this.each( function() {
324 if ( $(this).is( ':hidden' ) ) {
326 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
327 // Opera 9.0 doesn't allow setting selectionStart past
328 // selectionEnd; any attempts to do that will be ignored
329 // Make sure to set them in the right order
330 if ( options
.start
> this.selectionEnd
) {
331 this.selectionEnd
= options
.end
;
332 this.selectionStart
= options
.start
;
334 this.selectionStart
= options
.start
;
335 this.selectionEnd
= options
.end
;
337 } else if ( document
.body
.createTextRange
) {
338 var selection
= rangeForElementIE( this );
339 var length
= this.value
.length
;
340 // IE doesn't count \n when computing the offset, so we won't either
341 var newLines
= this.value
.match( /\n/g );
342 if ( newLines
) length
= length
- newLines
.length
;
343 selection
.moveStart( 'character', options
.start
);
344 selection
.moveEnd( 'character', -length
+ options
.end
);
346 // This line can cause an error under certain circumstances (textarea empty, no selection)
347 // Silence that error
355 * Ported from Wikia's LinkSuggest extension
356 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
358 * Scroll a textarea to the current cursor position. You can set the cursor
359 * position with setSelection()
360 * @param options boolean Whether to force a scroll even if the caret position
361 * is already visible. Defaults to false
363 * @fixme document the options parameters (function body suggests options.force is a boolean, not options itself)
365 scrollToCaretPosition: function( options
) {
366 function getLineLength( e
) {
367 return Math
.floor( e
.scrollWidth
/ ( $.client
.profile().platform
== 'linux' ? 7 : 8 ) );
369 function getCaretScrollPosition( e
) {
370 // FIXME: This functions sucks and is off by a few lines most
371 // of the time. It should be replaced by something decent.
372 var text
= e
.value
.replace( /\r/g, "" );
373 var caret
= $( e
).textSelection( 'getCaretPosition' );
374 var lineLength
= getLineLength( e
);
377 var lastSpaceInLine
= 0;
378 for ( i
= 0; i
< caret
; i
++ ) {
380 if ( text
.charAt( i
) == " " ) {
381 lastSpaceInLine
= charInLine
;
382 } else if ( text
.charAt( i
) == "\n" ) {
387 if ( charInLine
> lineLength
) {
388 if ( lastSpaceInLine
> 0 ) {
389 charInLine
= charInLine
- lastSpaceInLine
;
396 for ( j
= caret
; j
< caret
+ lineLength
; j
++ ) {
398 text
.charAt( j
) == " " ||
399 text
.charAt( j
) == "\n" ||
406 if ( nextSpace
> lineLength
&& caret
<= lineLength
) {
407 charInLine
= caret
- lastSpaceInLine
;
410 return ( $.client
.profile().platform
== 'mac' ? 13 : ( $.client
.profile().platform
== 'linux' ? 15 : 16 ) ) * row
;
412 return this.each(function() {
413 if ( $(this).is( ':hidden' ) ) {
415 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
417 var scroll
= getCaretScrollPosition( this );
418 if ( options
.force
|| scroll
< $(this).scrollTop() ||
419 scroll
> $(this).scrollTop() + $(this).height() )
420 $(this).scrollTop( scroll
);
421 } else if ( document
.selection
&& document
.selection
.createRange
) {
424 * IE automatically scrolls the selected text to the
425 * bottom of the textarea at range.select() time, except
426 * if it was already in view and the cursor position
427 * wasn't changed, in which case it does nothing. To
428 * cover that case, we'll force it to act by moving one
429 * character back and forth.
431 var range
= document
.body
.createTextRange();
432 var savedRange
= document
.selection
.createRange();
433 var pos
= $(this).textSelection( 'getCaretPosition' );
434 var oldScrollTop
= this.scrollTop
;
435 range
.moveToElementText( this );
437 range
.move( 'character', pos
+ 1);
439 if ( this.scrollTop
!= oldScrollTop
)
440 this.scrollTop
+= range
.offsetTop
;
441 else if ( options
.force
) {
442 range
.move( 'character', -1 );
447 $(this).trigger( 'scrollToPosition' );
453 //case 'getContents': // no params
454 //case 'setContents': // no params with defaults
455 //case 'getSelection': // no params
456 case 'encapsulateSelection':
457 options
= $.extend( {
458 'pre': '', // Text to insert before the cursor/selection
459 'peri': '', // Text to insert between pre and post and select afterwards
460 'post': '', // Text to insert after the cursor/selection
461 'ownline': false, // Put the inserted text on a line of its own
462 'replace': false, // If there is a selection, replace it with peri instead of leaving it alone
463 'selectPeri': true, // Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true)
464 'splitlines': false, // If multiple lines are selected, encapsulate each line individually
465 'selectionStart': undefined, // Position to start selection at
466 'selectionEnd': undefined // Position to end selection at. Defaults to start
469 case 'getCaretPosition':
470 options
= $.extend( {
471 'startAndEnd': false // Return [start, end] instead of just start
473 // FIXME: We may not need character position-based functions if we insert markers in the right places
476 options
= $.extend( {
477 'start': undefined, // Position to start selection at
478 'end': undefined, // Position to end selection at. Defaults to start
479 'startContainer': undefined, // Element to start selection in (iframe only)
480 'endContainer': undefined // Element to end selection in (iframe only). Defaults to startContainer
482 if ( options
.end
=== undefined )
483 options
.end
= options
.start
;
484 if ( options
.endContainer
== undefined )
485 options
.endContainer
= options
.startContainer
;
486 // FIXME: We may not need character position-based functions if we insert markers in the right places
488 case 'scrollToCaretPosition':
489 options
= $.extend( {
490 'force': false // Force a scroll even if the caret position is already visible
494 var context
= $(this).data( 'wikiEditor-context' );
495 var hasIframe
= typeof context
!== 'undefined' && context
&& typeof context
.$iframe
!== 'undefined';
497 // IE selection restore voodoo
498 var needSave
= false;
499 if ( hasIframe
&& context
.savedSelection
!== null ) {
500 context
.fn
.restoreSelection();
503 var retval
= ( hasIframe
? context
.fn
: fn
)[command
].call( this, options
);
504 if ( hasIframe
&& needSave
) {
505 context
.fn
.saveSelection();