Merge "Add browser test for preview functionality to MediaWiki core"
[lhc/web/wiklou.git] / resources / src / jquery / jquery.textSelection.js
index 6b4ab97..cbbed99 100644 (file)
                        setContents: function ( content ) {
                                return this.each( function () {
                                        var scrollTop = this.scrollTop;
-                                       $( this ).val( content );
+                                       this.select();
+                                       if ( !document.execCommand( 'insertText', false, content ) ) {
+                                               $( this ).val( content );
+                                       }
                                        // Setting this.value may scroll the textarea, restore the scroll position
                                        this.scrollTop = scrollTop;
                                } );
                                return this.each( function () {
                                        var allText, currSelection, startPos, endPos;
 
-                                       allText = $( this ).textSelection( 'getContents' );
-                                       currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
-                                       startPos = currSelection[ 0 ];
-                                       endPos = currSelection[ 1 ];
+                                       if ( !document.execCommand( 'insertText', false, value ) ) {
+                                               allText = $( this ).textSelection( 'getContents' );
+                                               currSelection = $( this ).textSelection( 'getCaretPosition', { startAndEnd: true } );
+                                               startPos = currSelection[ 0 ];
+                                               endPos = currSelection[ 1 ];
 
-                                       $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
-                                               allText.slice( endPos ) );
-                                       $( this ).textSelection( 'setSelection', {
-                                               start: startPos,
-                                               end: startPos + value.length
-                                       } );
+                                               $( this ).textSelection( 'setContents', allText.slice( 0, startPos ) + value +
+                                                       allText.slice( endPos ) );
+                                               $( this ).textSelection( 'setSelection', {
+                                                       start: startPos,
+                                                       end: startPos + value.length
+                                               } );
+                                       }
                                } );
                        },