2 * These plugins provide extra functionality for interaction with textareas.
5 $.fn
.textSelection = function( command
, options
) {
8 * Get the contents of the textarea
10 getContents: function() {
14 * Get the currently selected text in this textarea. Will focus the textarea
15 * in some browsers (IE/Opera)
17 getSelection: function() {
18 var e
= this.get( 0 );
20 if ( $(e
).is( ':hidden' ) ) {
22 } else if ( document
.selection
&& document
.selection
.createRange
) {
24 var range
= document
.selection
.createRange();
26 } else if ( e
.selectionStart
|| e
.selectionStart
== '0' ) {
27 retval
= e
.value
.substring( e
.selectionStart
, e
.selectionEnd
);
32 * Ported from skins/common/edit.js by Trevor Parscal
33 * (c) 2009 Wikimedia Foundation (GPLv2) - http://www.wikimedia.org
35 * Inserts text at the begining and end of a text selection, optionally
36 * inserting text at the caret when selection is empty.
38 encapsulateSelection: function( options
) {
39 return this.each( function() {
41 * Check if the selected text is the same as the insert text
43 function checkSelectedText() {
45 selText
= options
.peri
;
47 } else if ( options
.replace
) {
48 selText
= options
.peri
;
49 } else if ( selText
.charAt( selText
.length
- 1 ) == ' ' ) {
50 // Exclude ending space char
51 selText
= selText
.substring(0, selText
.length
- 1);
56 if ( this.style
.display
== 'none' ) {
58 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
61 var selText
= $(this).textSelection( 'getSelection' );
62 var startPos
= this.selectionStart
;
63 var endPos
= this.selectionEnd
;
64 var scrollTop
= this.scrollTop
;
66 if ( options
.ownline
) {
67 if ( startPos
!= 0 && this.value
.charAt( startPos
- 1 ) != "\n" ) {
68 options
.pre
= "\n" + options
.pre
;
70 if ( this.value
.charAt( endPos
) != "\n" ) {
74 this.value
= this.value
.substring( 0, startPos
) + options
.pre
+ selText
+ options
.post
+
75 this.value
.substring( endPos
, this.value
.length
);
76 // Setting this.value scrolls the textarea to the top, restore the scroll position
77 this.scrollTop
= scrollTop
;
79 options
.pre
= options
.pre
.replace( /\r?\n/g, "\r\n" );
80 selText
= selText
.replace( /\r?\n/g, "\r\n" );
81 options
.post
= options
.post
.replace( /\r?\n/g, "\r\n" );
83 if ( isSample
&& options
.selectPeri
) {
84 this.selectionStart
= startPos
+ options
.pre
.length
;
85 this.selectionEnd
= startPos
+ options
.pre
.length
+ selText
.length
;
87 this.selectionStart
= startPos
+ options
.pre
.length
+ selText
.length
+
89 this.selectionEnd
= this.selectionStart
;
91 } else if ( document
.selection
&& document
.selection
.createRange
) {
95 context
.fn
.restoreCursorAndScrollTop();
97 var selText
= $(this).textSelection( 'getSelection' );
98 var scrollTop
= this.scrollTop
;
99 var range
= document
.selection
.createRange();
100 if ( options
.ownline
&& range
.moveStart
) {
101 var range2
= document
.selection
.createRange();
103 range2
.moveStart( 'character', -1 );
104 // FIXME: Which check is correct?
105 if ( range2
.text
!= "\r" && range2
.text
!= "\n" && range2
.text
!= "" ) {
106 options
.pre
= "\n" + options
.pre
;
108 var range3
= document
.selection
.createRange();
109 range3
.collapse( false );
110 range3
.moveEnd( 'character', 1 );
111 if ( range3
.text
!= "\r" && range3
.text
!= "\n" && range3
.text
!= "" ) {
112 options
.post
+= "\n";
116 range
.text
= options
.pre
+ selText
+ options
.post
;
117 if ( isSample
&& options
.selectPeri
&& range
.moveStart
) {
118 range
.moveStart( 'character', - options
.post
.length
- selText
.length
);
119 range
.moveEnd( 'character', - options
.post
.length
);
122 // Restore the scroll position
123 this.scrollTop
= scrollTop
;
125 $(this).trigger( 'encapsulateSelection', [ options
.pre
, options
.peri
, options
.post
, options
.ownline
,
130 * Ported from Wikia's LinkSuggest extension
131 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
132 * Some code copied from
133 * http://www.dedestruct.com/2008/03/22/howto-cross-browser-cursor-position-in-textareas/
135 * Get the position (in resolution of bytes not nessecarily characters)
138 getCaretPosition: function( options
) {
139 function getCaret( e
) {
140 var caretPos
= 0, endPos
= 0;
141 if ( $.browser
.msie
) {
143 var preFinished
= false;
144 var periFinished
= false;
145 var postFinished
= false;
146 var preText
, rawPreText
, periText
;
147 var rawPeriText
, postText
, rawPostText
;
148 // Create range containing text in the selection
149 var periRange
= document
.selection
.createRange().duplicate();
150 // Create range containing text before the selection
151 var preRange
= document
.body
.createTextRange();
152 // Select all the text
153 preRange
.moveToElementText(e
);
154 // Move the end where we need it
155 preRange
.setEndPoint("EndToStart", periRange
);
156 // Create range containing text after the selection
157 var postRange
= document
.body
.createTextRange();
158 // Select all the text
159 postRange
.moveToElementText(e
);
160 // Move the start where we need it
161 postRange
.setEndPoint("StartToEnd", periRange
);
162 // Load the text values we need to compare
163 preText
= rawPreText
= preRange
.text
;
164 periText
= rawPeriText
= periRange
.text
;
165 postText
= rawPostText
= postRange
.text
;
167 * Check each range for trimmed newlines by shrinking the range by 1
168 * character and seeing if the text property has changed. If it has
169 * not changed then we know that IE has trimmed a \r\n from the end.
172 if ( !preFinished
) {
173 if ( preRange
.compareEndPoints( "StartToEnd", preRange
) == 0 ) {
176 preRange
.moveEnd( "character", -1 );
177 if ( preRange
.text
== preText
) {
178 rawPreText
+= "\r\n";
184 if ( !periFinished
) {
185 if ( periRange
.compareEndPoints( "StartToEnd", periRange
) == 0 ) {
188 periRange
.moveEnd( "character", -1 );
189 if ( periRange
.text
== periText
) {
190 rawPeriText
+= "\r\n";
196 if ( !postFinished
) {
197 if ( postRange
.compareEndPoints("StartToEnd", postRange
) == 0 ) {
200 postRange
.moveEnd( "character", -1 );
201 if ( postRange
.text
== postText
) {
202 rawPostText
+= "\r\n";
208 } while ( ( !preFinished
|| !periFinished
|| !postFinished
) );
209 caretPos
= rawPreText
.replace( /\r\n/g, "\n" ).length
;
210 endPos
= caretPos
+ rawPeriText
.replace( /\r\n/g, "\n" ).length
;
211 } else if ( e
.selectionStart
|| e
.selectionStart
== '0' ) {
213 caretPos
= e
.selectionStart
;
214 endPos
= e
.selectionEnd
;
216 return options
.startAndEnd
? [ caretPos
, endPos
] : caretPos
;
218 return getCaret( this.get( 0 ) );
220 setSelection: function( options
) {
221 return this.each( function() {
222 if ( $(this).is( ':hidden' ) ) {
224 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
225 // Opera 9.0 doesn't allow setting selectionStart past
226 // selectionEnd; any attempts to do that will be ignored
227 // Make sure to set them in the right order
228 if ( options
.start
> this.selectionEnd
) {
229 this.selectionEnd
= options
.end
;
230 this.selectionStart
= options
.start
;
232 this.selectionStart
= options
.start
;
233 this.selectionEnd
= options
.end
;
235 } else if ( document
.body
.createTextRange
) {
236 var selection
= document
.body
.createTextRange();
237 selection
.moveToElementText( this );
238 var length
= this.value
.length
;
239 // IE doesn't count \n when computing the offset, so we won't either
240 var newLines
= this.value
.match( /\n/g );
241 if ( newLines
) length
= length
- newLines
.length
;
242 selection
.moveStart( 'character', options
.start
);
243 selection
.moveEnd( 'character', -length
+ options
.end
);
245 // This line can cause an error under certain circumstances (textarea empty, no selection)
246 // Silence that error
254 * Ported from Wikia's LinkSuggest extension
255 * https://svn.wikia-code.com/wikia/trunk/extensions/wikia/LinkSuggest
257 * Scroll a textarea to the current cursor position. You can set the cursor
258 * position with setSelection()
259 * @param options boolean Whether to force a scroll even if the caret position
260 * is already visible. Defaults to false
262 scrollToCaretPosition: function( options
) {
263 function getLineLength( e
) {
264 return Math
.floor( e
.scrollWidth
/ ( $.client
.profile().platform
== 'linux' ? 7 : 8 ) );
266 function getCaretScrollPosition( e
) {
267 // FIXME: This functions sucks and is off by a few lines most
268 // of the time. It should be replaced by something decent.
269 var text
= e
.value
.replace( /\r/g, "" );
270 var caret
= $( e
).textSelection( 'getCaretPosition' );
271 var lineLength
= getLineLength( e
);
274 var lastSpaceInLine
= 0;
275 for ( i
= 0; i
< caret
; i
++ ) {
277 if ( text
.charAt( i
) == " " ) {
278 lastSpaceInLine
= charInLine
;
279 } else if ( text
.charAt( i
) == "\n" ) {
284 if ( charInLine
> lineLength
) {
285 if ( lastSpaceInLine
> 0 ) {
286 charInLine
= charInLine
- lastSpaceInLine
;
293 for ( j
= caret
; j
< caret
+ lineLength
; j
++ ) {
295 text
.charAt( j
) == " " ||
296 text
.charAt( j
) == "\n" ||
303 if ( nextSpace
> lineLength
&& caret
<= lineLength
) {
304 charInLine
= caret
- lastSpaceInLine
;
307 return ( $.client
.profile().platform
== 'mac' ? 13 : ( $.client
.profile().platform
== 'linux' ? 15 : 16 ) ) * row
;
309 return this.each(function() {
310 if ( $(this).is( ':hidden' ) ) {
312 } else if ( this.selectionStart
|| this.selectionStart
== '0' ) {
314 var scroll
= getCaretScrollPosition( this );
315 if ( options
.force
|| scroll
< $(this).scrollTop() ||
316 scroll
> $(this).scrollTop() + $(this).height() )
317 $(this).scrollTop( scroll
);
318 } else if ( document
.selection
&& document
.selection
.createRange
) {
321 * IE automatically scrolls the selected text to the
322 * bottom of the textarea at range.select() time, except
323 * if it was already in view and the cursor position
324 * wasn't changed, in which case it does nothing. To
325 * cover that case, we'll force it to act by moving one
326 * character back and forth.
328 var range
= document
.body
.createTextRange();
329 var savedRange
= document
.selection
.createRange();
330 var pos
= $(this).textSelection( 'getCaretPosition' );
331 var oldScrollTop
= this.scrollTop
;
332 range
.moveToElementText( this );
334 range
.move( 'character', pos
+ 1);
336 if ( this.scrollTop
!= oldScrollTop
)
337 this.scrollTop
+= range
.offsetTop
;
338 else if ( options
.force
) {
339 range
.move( 'character', -1 );
344 $(this).trigger( 'scrollToPosition' );
350 //case 'getContents': // no params
351 //case 'setContents': // no params with defaults
352 //case 'getSelection': // no params
353 case 'encapsulateSelection':
354 options
= $.extend( {
355 'pre': '', // Text to insert before the cursor/selection
356 'peri': '', // Text to insert between pre and post and select afterwards
357 'post': '', // Text to insert after the cursor/selection
358 'ownline': false, // Put the inserted text on a line of its own
359 'replace': false, // If there is a selection, replace it with peri instead of leaving it alone
360 'selectPeri': true // Select the peri text if it was inserted (but not if there was a selection and replace==false)
363 case 'getCaretPosition':
364 options
= $.extend( {
365 'startAndEnd': false // Return [start, end] instead of just start
367 // FIXME: We may not need character position-based functions if we insert markers in the right places
370 options
= $.extend( {
371 'start': undefined, // Position to start selection at
372 'end': undefined, // Position to end selection at. Defaults to start
373 'startContainer': undefined, // Element to start selection in (iframe only)
374 'endContainer': undefined // Element to end selection in (iframe only). Defaults to startContainer
376 if ( options
.end
=== undefined )
377 options
.end
= options
.start
;
378 if ( options
.endContainer
== undefined )
379 options
.endContainer
= options
.startContainer
;
380 // FIXME: We may not need character position-based functions if we insert markers in the right places
382 case 'scrollToCaretPosition':
383 options
= $.extend( {
384 'force': false // Force a scroll even if the caret position is already visible
388 var context
= $(this).data( 'wikiEditor-context' );
389 var hasIframe
= typeof context
!== 'undefined' && context
&& typeof context
.$iframe
!== 'undefined';
391 // IE selection restore voodoo
392 var needSave
= false;
393 if ( hasIframe
&& context
.savedSelection
!== null ) {
394 context
.fn
.restoreSelection();
397 retval
= ( hasIframe
? context
.fn
: fn
)[command
].call( this, options
);
398 if ( hasIframe
&& needSave
) {
399 context
.fn
.saveSelection();