* These plugins provide extra functionality for interaction with textareas.
*/
( function( $ ) {
+
+if (document.selection && document.selection.createRange) {
+ // On IE, patch the focus() method to restore the windows' scroll position
+ // (bug 32241)
+ $.fn.extend({
+ focus : (function ( _focus ) {
+ return function () {
+ if ( arguments.length == 0 ) {
+ var $w = $( window );
+ var state = {top: $w.scrollTop(), left: $w.scrollLeft()};
+ var result = _focus.apply( this, arguments );
+ window.scrollTo( state.top, state.left );
+ return result;
+ }
+ return _focus.apply( this, arguments );
+ };
+ })( $.fn.focus )
+ });
+}
+
$.fn.textSelection = function( command, options ) {
/**
}
}
+/**
+ * Helper function for IE for activating the textarea. Called only in the
+ * IE-specific code paths below; makes use of IE-specific non-standard
+ * function setActive() if possible to avoid screen flicker.
+ */
+function activateElementOnIE( element ) {
+ if ( element.setActive ) {
+ element.setActive(); // bug 32241: doesn't scroll
+ } else {
+ $( element ).focus(); // may scroll (but we patched it above)
+ }
+}
+
var fn = {
/**
* Get the contents of the textarea
if ( $(e).is( ':hidden' ) ) {
// Do nothing
} else if ( document.selection && document.selection.createRange ) {
- e.focus();
+ activateElementOnIE( e );
var range = document.selection.createRange();
retval = range.text;
} else if ( e.selectionStart || e.selectionStart == '0' ) {
encapsulateSelection: function( options ) {
return this.each( function() {
var pre = options.pre, post = options.post;
-
+
/**
* Check if the selected text is the same as the insert text
*/
}
}
}
-
+
/**
* Do the splitlines stuff.
- *
+ *
* Wrap each line of the selected text with pre and post
*/
function doSplitLines( selText, pre, post ) {
}
return insertText;
}
-
+
var isSample = false;
if ( this.style.display == 'none' ) {
// Do nothing
- } else if ( this.selectionStart || this.selectionStart == '0' ) {
- // Mozilla/Opera
- $(this).focus();
- var selText = $(this).textSelection( 'getSelection' );
- var startPos = this.selectionStart;
- var endPos = this.selectionEnd;
- var scrollTop = this.scrollTop;
- checkSelectedText();
-
- var insertText = pre + selText + post;
- if ( options.splitlines ) {
- insertText = doSplitLines( selText, pre, post );
- }
- if ( options.ownline ) {
- if ( startPos != 0 && this.value.charAt( startPos - 1 ) != "\n" && this.value.charAt( startPos - 1 ) != "\r" ) {
- insertText = "\n" + insertText;
- pre += "\n";
- }
- if ( this.value.charAt( endPos ) != "\n" && this.value.charAt( endPos ) != "\r" ) {
- insertText += "\n";
- post += "\n";
- }
- }
- this.value = this.value.substring( 0, startPos ) + insertText +
- this.value.substring( endPos, this.value.length );
- // Setting this.value scrolls the textarea to the top, restore the scroll position
- this.scrollTop = scrollTop;
- if ( window.opera ) {
- pre = pre.replace( /\r?\n/g, "\r\n" );
- selText = selText.replace( /\r?\n/g, "\r\n" );
- post = post.replace( /\r?\n/g, "\r\n" );
- }
- if ( isSample && options.selectPeri && !options.splitlines ) {
- this.selectionStart = startPos + pre.length;
- this.selectionEnd = startPos + pre.length + selText.length;
- } else {
- this.selectionStart = startPos + insertText.length;
- this.selectionEnd = this.selectionStart;
- }
} else if ( document.selection && document.selection.createRange ) {
// IE
- $(this).focus();
+
+ // Note that IE9 will trigger the next section unless we check this first.
+ // See bug 35201.
+
+ activateElementOnIE( this );
if ( context ) {
context.fn.restoreCursorAndScrollTop();
}
+ if ( options.selectionStart !== undefined ) {
+ $(this).textSelection( 'setSelection', { 'start': options.selectionStart, 'end': options.selectionEnd } );
+ }
+
var selText = $(this).textSelection( 'getSelection' );
var scrollTop = this.scrollTop;
var range = document.selection.createRange();
-
+
checkSelectedText();
- var insertText = pre + selText + post;
+ var insertText = pre + selText + post;
if ( options.splitlines ) {
insertText = doSplitLines( selText, pre, post );
}
post += "\n";
}
}
-
+
range.text = insertText;
if ( isSample && options.selectPeri && range.moveStart ) {
range.moveStart( 'character', - post.length - selText.length );
range.select();
// Restore the scroll position
this.scrollTop = scrollTop;
+ } else if ( this.selectionStart || this.selectionStart == '0' ) {
+ // Mozilla/Opera
+
+ $(this).focus();
+ if ( options.selectionStart !== undefined ) {
+ $(this).textSelection( 'setSelection', { 'start': options.selectionStart, 'end': options.selectionEnd } );
+ }
+
+ var selText = $(this).textSelection( 'getSelection' );
+ var startPos = this.selectionStart;
+ var endPos = this.selectionEnd;
+ var scrollTop = this.scrollTop;
+ checkSelectedText();
+ if ( options.selectionStart !== undefined
+ && endPos - startPos != options.selectionEnd - options.selectionStart )
+ {
+ // This means there is a difference in the selection range returned by browser and what we passed.
+ // This happens for Chrome in the case of composite characters. Ref bug #30130
+ // Set the startPos to the correct position.
+ startPos = options.selectionStart;
+ }
+
+ var insertText = pre + selText + post;
+ if ( options.splitlines ) {
+ insertText = doSplitLines( selText, pre, post );
+ }
+ if ( options.ownline ) {
+ if ( startPos != 0 && this.value.charAt( startPos - 1 ) != "\n" && this.value.charAt( startPos - 1 ) != "\r" ) {
+ insertText = "\n" + insertText;
+ pre += "\n";
+ }
+ if ( this.value.charAt( endPos ) != "\n" && this.value.charAt( endPos ) != "\r" ) {
+ insertText += "\n";
+ post += "\n";
+ }
+ }
+ this.value = this.value.substring( 0, startPos ) + insertText +
+ this.value.substring( endPos, this.value.length );
+ // Setting this.value scrolls the textarea to the top, restore the scroll position
+ this.scrollTop = scrollTop;
+ if ( window.opera ) {
+ pre = pre.replace( /\r?\n/g, "\r\n" );
+ selText = selText.replace( /\r?\n/g, "\r\n" );
+ post = post.replace( /\r?\n/g, "\r\n" );
+ }
+ if ( isSample && options.selectPeri && !options.splitlines ) {
+ this.selectionStart = startPos + pre.length;
+ this.selectionEnd = startPos + pre.length + selText.length;
+ } else {
+ this.selectionStart = startPos + insertText.length;
+ this.selectionEnd = this.selectionStart;
+ }
}
$(this).trigger( 'encapsulateSelection', [ options.pre, options.peri, options.post, options.ownline,
options.replace, options.spitlines ] );
* Get the position (in resolution of bytes not nessecarily characters)
* in a textarea
*
+ * Will focus the textarea in some browsers (IE/Opera)
+ *
* @fixme document the options parameters
*/
getCaretPosition: function( options ) {
function getCaret( e ) {
var caretPos = 0, endPos = 0;
- if ( $.browser.msie ) {
+ if ( document.selection && document.selection.createRange ) {
+ // IE doesn't properly report non-selected caret position through
+ // the selection ranges when textarea isn't focused. This can
+ // lead to saving a bogus empty selection, which then screws up
+ // whatever we do later (bug 31847).
+ activateElementOnIE( e );
+
// IE Support
var preFinished = false;
var periFinished = false;
if ( newLines ) length = length - newLines.length;
selection.moveStart( 'character', options.start );
selection.moveEnd( 'character', -length + options.end );
-
+
// This line can cause an error under certain circumstances (textarea empty, no selection)
// Silence that error
try {
'ownline': false, // Put the inserted text on a line of its own
'replace': false, // If there is a selection, replace it with peri instead of leaving it alone
'selectPeri': true, // Select the peri text if it was inserted (but not if there was a selection and replace==false, or if splitlines==true)
- 'splitlines': false // If multiple lines are selected, encapsulate each line individually
+ 'splitlines': false, // If multiple lines are selected, encapsulate each line individually
+ 'selectionStart': undefined, // Position to start selection at
+ 'selectionEnd': undefined // Position to end selection at. Defaults to start
}, options );
break;
case 'getCaretPosition':
}
var context = $(this).data( 'wikiEditor-context' );
var hasIframe = typeof context !== 'undefined' && context && typeof context.$iframe !== 'undefined';
-
+
// IE selection restore voodoo
var needSave = false;
if ( hasIframe && context.savedSelection !== null ) {