2 * These plugins provide extra functionality for interaction with textareas.
5 $.fn
.textSelection = function( command
, options
) {
8 * Helper function to get an IE TextRange object for an element
10 function rangeForElementIE( e
) {
11 if ( e
.nodeName
.toLowerCase() == 'input' ) {
12 return e
.createTextRange();
14 var sel
= document
.body
.createTextRange();
15 sel
.moveToElementText( e
);
22 * Get the contents of the textarea
24 getContents: function() {
28 * Get the currently selected text in this textarea. Will focus the textarea
29 * in some browsers (IE/Opera)
31 getSelection: function() {
32 var e
= this.get( 0 );
34 if ( $(e
).is( ':hidden' ) ) {
36 } else if ( document
.selection
&& document
.selection
.createRange
) {
38 var range
= document
.selection
.createRange();
40 } else if ( e
.selectionStart
|| e
.selectionStart
== '0' ) {
41 retval
= e
.value
.substring( e
.selectionStart
, e
.selectionEnd
);
46 * Ported from skins/common/edit.js by Trevor Parscal
47 * (c) 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org
49 * Inserts text at the begining and end of a text selection, optionally
50 * inserting text at the caret when selection is empty.
52 * @fixme document the options parameters
54 encapsulateSelection: function( options
) {
55 return this.each( function() {
56 var pre
= options
.pre
, post
= options
.post
;
59 * Check if the selected text is the same as the insert text
61 function checkSelectedText() {
63 selText
= options
.peri
;
65 } else if ( options
.replace
) {
66 selText
= options
.peri
;
68 while ( selText
.charAt( selText
.length
- 1 ) == ' ' ) {
69 // Exclude ending space char
70 selText
= selText
.substring( 0, selText
.length
- 1 );
73 while ( selText
.charAt( 0 ) == ' ' ) {
74 // Exclude prepending space char
75 selText
= selText
.substring( 1, selText
.length
);
82 * Do the splitlines stuff.
84 * Wrap each line of the selected text with pre and post
86 function doSplitLines( selText
, pre
, post
) {
88 var selTextArr
= selText
.split( '\n' );
89 for ( var i
= 0; i
< selTextArr
.length
; i
++ ) {
90 insertText
+= pre
+ selTextArr
[i
] + post
;
91 if ( i
!= selTextArr
.length
- 1 ) {
99 if ( this.style
.display
== 'none' ) {
101 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
104 var selText
= $(this).textSelection( 'getSelection' );
105 var startPos
= this.selectionStart
;
106 var endPos
= this.selectionEnd
;
107 var scrollTop
= this.scrollTop
;
110 var insertText
= pre
+ selText
+ post
;
111 if ( options
.splitlines
) {
112 insertText
= doSplitLines( selText
, pre
, post
);
114 if ( options
.ownline
) {
115 if ( startPos
!= 0 && this.value
.charAt( startPos
- 1 ) != "\n" ) {
116 insertText
= "\n" + insertText
;
118 if ( this.value
.charAt( endPos
) != "\n" ) {
122 this.value
= this.value
.substring( 0, startPos
) + insertText
+
123 this.value
.substring( endPos
, this.value
.length
);
124 // Setting this.value scrolls the textarea to the top, restore the scroll position
125 this.scrollTop
= scrollTop
;
126 if ( window
.opera
) {
127 pre
= pre
.replace( /\r?\n/g, "\r\n" );
128 selText
= selText
.replace( /\r?\n/g, "\r\n" );
129 post
= post
.replace( /\r?\n/g, "\r\n" );
131 if ( isSample
&& options
.selectPeri
) {
132 this.selectionStart
= startPos
+ pre
.length
;
133 this.selectionEnd
= startPos
+ pre
.length
+ selText
.length
;
135 this.selectionStart
= startPos
+ pre
.length
+ selText
.length
+
137 this.selectionEnd
= this.selectionStart
;
139 } else if ( document
.selection
&& document
.selection
.createRange
) {
143 context
.fn
.restoreCursorAndScrollTop();
145 var selText
= $(this).textSelection( 'getSelection' );
146 var scrollTop
= this.scrollTop
;
147 var range
= document
.selection
.createRange();
150 var insertText
= pre
+ selText
+ post
;
151 if ( options
.splitlines
) {
152 insertText
= doSplitLines( selText
, pre
, post
);
154 if ( options
.ownline
&& range
.moveStart
) {
155 var range2
= document
.selection
.createRange();
157 range2
.moveStart( 'character', -1 );
158 // FIXME: Which check is correct?
159 if ( range2
.text
!= "\r" && range2
.text
!= "\n" && range2
.text
!= "" ) {
160 insertText
= "\n" + insertText
;
162 var range3
= document
.selection
.createRange();
163 range3
.collapse( false );
164 range3
.moveEnd( 'character', 1 );
165 if ( range3
.text
!= "\r" && range3
.text
!= "\n" && range3
.text
!= "" ) {
170 range
.text
= insertText
;
171 if ( isSample
&& options
.selectPeri
&& range
.moveStart
) {
172 range
.moveStart( 'character', - post
.length
- selText
.length
);
173 range
.moveEnd( 'character', - post
.length
);
176 // Restore the scroll position
177 this.scrollTop
= scrollTop
;
179 $(this).trigger( 'encapsulateSelection', [ options
.pre
, options
.peri
, options
.post
, options
.ownline
,
180 options
.replace
, options
.spitlines
] );
184 * Ported from Wikia's LinkSuggest extension
185 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
186 * Some code copied from
187 * http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/
189 * Get the position (in resolution of bytes not nessecarily characters)
192 * @fixme document the options parameters
194 getCaretPosition: function( options
) {
195 function getCaret( e
) {
196 var caretPos
= 0, endPos
= 0;
197 if ( $.browser
.msie
) {
199 var preFinished
= false;
200 var periFinished
= false;
201 var postFinished
= false;
202 var preText
, rawPreText
, periText
;
203 var rawPeriText
, postText
, rawPostText
;
204 // Create range containing text in the selection
205 var periRange
= document
.selection
.createRange().duplicate();
206 // Create range containing text before the selection
207 var preRange
= rangeForElementIE( e
);
208 // Move the end where we need it
209 preRange
.setEndPoint("EndToStart", periRange
);
210 // Create range containing text after the selection
211 var postRange
= rangeForElementIE( e
);
212 // Move the start where we need it
213 postRange
.setEndPoint("StartToEnd", periRange
);
214 // Load the text values we need to compare
215 preText
= rawPreText
= preRange
.text
;
216 periText
= rawPeriText
= periRange
.text
;
217 postText
= rawPostText
= postRange
.text
;
219 * Check each range for trimmed newlines by shrinking the range by 1
220 * character and seeing if the text property has changed. If it has
221 * not changed then we know that IE has trimmed a \r\n from the end.
224 if ( !preFinished
) {
225 if ( preRange
.compareEndPoints( "StartToEnd", preRange
) == 0 ) {
228 preRange
.moveEnd( "character", -1 );
229 if ( preRange
.text
== preText
) {
230 rawPreText
+= "\r\n";
236 if ( !periFinished
) {
237 if ( periRange
.compareEndPoints( "StartToEnd", periRange
) == 0 ) {
240 periRange
.moveEnd( "character", -1 );
241 if ( periRange
.text
== periText
) {
242 rawPeriText
+= "\r\n";
248 if ( !postFinished
) {
249 if ( postRange
.compareEndPoints("StartToEnd", postRange
) == 0 ) {
252 postRange
.moveEnd( "character", -1 );
253 if ( postRange
.text
== postText
) {
254 rawPostText
+= "\r\n";
260 } while ( ( !preFinished
|| !periFinished
|| !postFinished
) );
261 caretPos
= rawPreText
.replace( /\r\n/g, "\n" ).length
;
262 endPos
= caretPos
+ rawPeriText
.replace( /\r\n/g, "\n" ).length
;
263 } else if ( e
.selectionStart
|| e
.selectionStart
== '0' ) {
265 caretPos
= e
.selectionStart
;
266 endPos
= e
.selectionEnd
;
268 return options
.startAndEnd
? [ caretPos
, endPos
] : caretPos
;
270 return getCaret( this.get( 0 ) );
273 * @fixme document the options parameters
275 setSelection: function( options
) {
276 return this.each( function() {
277 if ( $(this).is( ':hidden' ) ) {
279 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
280 // Opera 9.0 doesn't allow setting selectionStart past
281 // selectionEnd; any attempts to do that will be ignored
282 // Make sure to set them in the right order
283 if ( options
.start
> this.selectionEnd
) {
284 this.selectionEnd
= options
.end
;
285 this.selectionStart
= options
.start
;
287 this.selectionStart
= options
.start
;
288 this.selectionEnd
= options
.end
;
290 } else if ( document
.body
.createTextRange
) {
291 var selection
= rangeForElementIE( this );
292 var length
= this.value
.length
;
293 // IE doesn't count \n when computing the offset, so we won't either
294 var newLines
= this.value
.match( /\n/g );
295 if ( newLines
) length
= length
- newLines
.length
;
296 selection
.moveStart( 'character', options
.start
);
297 selection
.moveEnd( 'character', -length
+ options
.end
);
299 // This line can cause an error under certain circumstances (textarea empty, no selection)
300 // Silence that error
308 * Ported from Wikia's LinkSuggest extension
309 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
311 * Scroll a textarea to the current cursor position. You can set the cursor
312 * position with setSelection()
313 * @param options boolean Whether to force a scroll even if the caret position
314 * is already visible. Defaults to false
316 * @fixme document the options parameters (function body suggests options.force is a boolean, not options itself)
318 scrollToCaretPosition: function( options
) {
319 function getLineLength( e
) {
320 return Math
.floor( e
.scrollWidth
/ ( $.client
.profile().platform
== 'linux' ? 7 : 8 ) );
322 function getCaretScrollPosition( e
) {
323 // FIXME: This functions sucks and is off by a few lines most
324 // of the time. It should be replaced by something decent.
325 var text
= e
.value
.replace( /\r/g, "" );
326 var caret
= $( e
).textSelection( 'getCaretPosition' );
327 var lineLength
= getLineLength( e
);
330 var lastSpaceInLine
= 0;
331 for ( i
= 0; i
< caret
; i
++ ) {
333 if ( text
.charAt( i
) == " " ) {
334 lastSpaceInLine
= charInLine
;
335 } else if ( text
.charAt( i
) == "\n" ) {
340 if ( charInLine
> lineLength
) {
341 if ( lastSpaceInLine
> 0 ) {
342 charInLine
= charInLine
- lastSpaceInLine
;
349 for ( j
= caret
; j
< caret
+ lineLength
; j
++ ) {
351 text
.charAt( j
) == " " ||
352 text
.charAt( j
) == "\n" ||
359 if ( nextSpace
> lineLength
&& caret
<= lineLength
) {
360 charInLine
= caret
- lastSpaceInLine
;
363 return ( $.client
.profile().platform
== 'mac' ? 13 : ( $.client
.profile().platform
== 'linux' ? 15 : 16 ) ) * row
;
365 return this.each(function() {
366 if ( $(this).is( ':hidden' ) ) {
368 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
370 var scroll
= getCaretScrollPosition( this );
371 if ( options
.force
|| scroll
< $(this).scrollTop() ||
372 scroll
> $(this).scrollTop() + $(this).height() )
373 $(this).scrollTop( scroll
);
374 } else if ( document
.selection
&& document
.selection
.createRange
) {
377 * IE automatically scrolls the selected text to the
378 * bottom of the textarea at range.select() time, except
379 * if it was already in view and the cursor position
380 * wasn't changed, in which case it does nothing. To
381 * cover that case, we'll force it to act by moving one
382 * character back and forth.
384 var range
= document
.body
.createTextRange();
385 var savedRange
= document
.selection
.createRange();
386 var pos
= $(this).textSelection( 'getCaretPosition' );
387 var oldScrollTop
= this.scrollTop
;
388 range
.moveToElementText( this );
390 range
.move( 'character', pos
+ 1);
392 if ( this.scrollTop
!= oldScrollTop
)
393 this.scrollTop
+= range
.offsetTop
;
394 else if ( options
.force
) {
395 range
.move( 'character', -1 );
400 $(this).trigger( 'scrollToPosition' );
406 //case 'getContents': // no params
407 //case 'setContents': // no params with defaults
408 //case 'getSelection': // no params
409 case 'encapsulateSelection':
410 options
= $.extend( {
411 'pre': '', // Text to insert before the cursor/selection
412 'peri': '', // Text to insert between pre and post and select afterwards
413 'post': '', // Text to insert after the cursor/selection
414 'ownline': false, // Put the inserted text on a line of its own
415 'replace': false, // If there is a selection, replace it with peri instead of leaving it alone
416 'selectPeri': true, // Select the peri text if it was inserted (but not if there was a selection and replace==false)
417 'splitlines': true // If multiple lines are selected, encapsulate each line individually
420 case 'getCaretPosition':
421 options
= $.extend( {
422 'startAndEnd': false // Return [start, end] instead of just start
424 // FIXME: We may not need character position-based functions if we insert markers in the right places
427 options
= $.extend( {
428 'start': undefined, // Position to start selection at
429 'end': undefined, // Position to end selection at. Defaults to start
430 'startContainer': undefined, // Element to start selection in (iframe only)
431 'endContainer': undefined // Element to end selection in (iframe only). Defaults to startContainer
433 if ( options
.end
=== undefined )
434 options
.end
= options
.start
;
435 if ( options
.endContainer
== undefined )
436 options
.endContainer
= options
.startContainer
;
437 // FIXME: We may not need character position-based functions if we insert markers in the right places
439 case 'scrollToCaretPosition':
440 options
= $.extend( {
441 'force': false // Force a scroll even if the caret position is already visible
445 var context
= $(this).data( 'wikiEditor-context' );
446 var hasIframe
= typeof context
!== 'undefined' && context
&& typeof context
.$iframe
!== 'undefined';
448 // IE selection restore voodoo
449 var needSave
= false;
450 if ( hasIframe
&& context
.savedSelection
!== null ) {
451 context
.fn
.restoreSelection();
454 retval
= ( hasIframe
? context
.fn
: fn
)[command
].call( this, options
);
455 if ( hasIframe
&& needSave
) {
456 context
.fn
.saveSelection();