JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.6
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index 51f53e9..558ffd7 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -16,10 +16,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var doc = editor.document,\r
                        body = doc.getBody();\r
 \r
-               var enabled = 0;\r
+               var enabled = false;\r
                var onExec = function()\r
                {\r
-                       enabled = 1;\r
+                       enabled = true;\r
                };\r
 \r
                // The following seems to be the only reliable way to detect that\r
@@ -135,12 +135,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                var body = this.document.getBody();\r
 \r
-                               // Simulate 'beforepaste' event for all none-IEs.\r
-                               if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )\r
-                                       event.cancel();\r
-                               // Simulate 'paste' event for Opera/Firefox2.\r
-                               else if ( CKEDITOR.env.opera\r
-                                                || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
+                               // 1. Opera just misses the "paste" event.\r
+                               // 2. Firefox's "paste" event comes too late to have the plain\r
+                               // text paste bin to work.\r
+                               if ( CKEDITOR.env.opera || CKEDITOR.env.gecko )\r
                                        body.fire( 'paste' );\r
                                return;\r
 \r
@@ -214,17 +212,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                // Turn off design mode temporarily before give focus to the paste bin.\r
                if ( mode == 'text' )\r
-               {\r
-                       if ( CKEDITOR.env.ie )\r
-                       {\r
-                               var ieRange = doc.getBody().$.createTextRange();\r
-                               ieRange.moveToElementText( pastebin.$ );\r
-                               ieRange.execCommand( 'Paste' );\r
-                               evt.data.preventDefault();\r
-                       }\r
-                       else\r
-                               pastebin.$.focus();\r
-               }\r
+                       pastebin.$.focus();\r
                else\r
                {\r
                        range.setStartAt( pastebin, CKEDITOR.POSITION_AFTER_START );\r
@@ -236,10 +224,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                // Wait a while and grab the pasted contents\r
                window.setTimeout( function()\r
                {\r
-                       mode == 'text' && CKEDITOR.env.gecko && editor.focusGrabber.focus();\r
-                       pastebin.remove();\r
+                       // Restore properly the document focus. (#5684, #8849)\r
+                       editor.document.getBody().focus();\r
+\r
                        editor.removeListener( 'selectionChange', cancel );\r
 \r
+                       // IE7: selection must go before removing paste bin. (#8691)\r
+                       if ( CKEDITOR.env.ie7Compat )\r
+                       {\r
+                               sel.selectBookmarks( bms );\r
+                               pastebin.remove();\r
+                       }\r
+                       // Webkit: selection must go after removing paste bin. (#8921)\r
+                       else\r
+                       {\r
+                               pastebin.remove();\r
+                               sel.selectBookmarks( bms );\r
+                       }\r
+\r
                        // Grab the HTML contents.\r
                        // We need to look for a apple style wrapper on webkit it also adds\r
                        // a div wrapper if you copy/paste the body of the editor.\r
@@ -250,7 +252,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                 && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?\r
                                                        bogusSpan : pastebin );\r
 \r
-                       sel.selectBookmarks( bms );\r
                        callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );\r
                }, 0 );\r
        }\r
@@ -285,29 +286,50 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        }\r
 \r
-       var depressBeforeEvent;\r
+       var depressBeforeEvent,\r
+               inReadOnly;\r
        function stateFromNamedCommand( command, editor )\r
        {\r
-               // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',\r
-               // guard to distinguish from the ordinary sources( either\r
-               // keyboard paste or execCommand ) (#4874).\r
-               CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
-\r
-               var retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
-               depressBeforeEvent = 0;\r
-               return retval;\r
+               var retval;\r
+\r
+               if ( inReadOnly && command in { Paste : 1, Cut : 1 } )\r
+                       return CKEDITOR.TRISTATE_DISABLED;\r
+\r
+               if ( command == 'Paste' )\r
+               {\r
+                       // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',\r
+                       // guard to distinguish from the ordinary sources (either\r
+                       // keyboard paste or execCommand) (#4874).\r
+                       CKEDITOR.env.ie && ( depressBeforeEvent = 1 );\r
+                       try\r
+                       {\r
+                               // Always return true for Webkit (which always returns false).\r
+                               retval = editor.document.$.queryCommandEnabled( command ) || CKEDITOR.env.webkit;\r
+                       }\r
+                       catch( er ) {}\r
+                       depressBeforeEvent = 0;\r
+               }\r
+               // Cut, Copy - check if the selection is not empty\r
+               else\r
+               {\r
+                       var sel = editor.getSelection(),\r
+                               ranges = sel && sel.getRanges();\r
+                       retval = sel && !( ranges.length == 1 && ranges[ 0 ].collapsed );\r
+               }\r
+\r
+               return retval ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;\r
        }\r
 \r
-       var inReadOnly;\r
        function setToolbarStates()\r
        {\r
                if ( this.mode != 'wysiwyg' )\r
                        return;\r
 \r
-               this.getCommand( 'cut' ).setState( inReadOnly ? CKEDITOR.TRISTATE_DISABLED : stateFromNamedCommand( 'Cut', this ) );\r
+               var pasteState = stateFromNamedCommand( 'Paste', this );\r
+\r
+               this.getCommand( 'cut' ).setState( stateFromNamedCommand( 'Cut', this ) );\r
                this.getCommand( 'copy' ).setState( stateFromNamedCommand( 'Copy', this ) );\r
-               var pasteState = inReadOnly ? CKEDITOR.TRISTATE_DISABLED :\r
-                                               CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', this );\r
+               this.getCommand( 'paste' ).setState( pasteState );\r
                this.fire( 'pasteState', pasteState );\r
        }\r
 \r
@@ -377,33 +399,68 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                editor.on( 'key', onKey, editor );\r
 \r
-                               var mode = editor.config.forcePasteAsPlainText ? 'text' : 'html';\r
-\r
                                // We'll be catching all pasted content in one line, regardless of whether the\r
                                // it's introduced by a document command execution (e.g. toolbar buttons) or\r
                                // user paste behaviors. (e.g. Ctrl-V)\r
                                editor.on( 'contentDom', function()\r
                                {\r
                                        var body = editor.document.getBody();\r
-                                       body.on( ( ( mode == 'text' && CKEDITOR.env.ie ) || CKEDITOR.env.webkit ) ? 'paste' : 'beforepaste',\r
-                                               function( evt )\r
+\r
+                                       // Intercept the paste before it actually takes place.\r
+                                       body.on( !CKEDITOR.env.ie ? 'paste' : 'beforepaste', function( evt )\r
                                                {\r
                                                        if ( depressBeforeEvent )\r
                                                                return;\r
 \r
-                                                       getClipboardData.call( editor, evt, mode, function ( data )\r
+                                                       // Dismiss the (wrong) 'beforepaste' event fired on toolbar menu open.\r
+                                                       var domEvent = evt.data && evt.data.$;\r
+                                                       if ( CKEDITOR.env.ie && domEvent && !domEvent.ctrlKey )\r
+                                                               return;\r
+\r
+                                                       // Fire 'beforePaste' event so clipboard flavor get customized\r
+                                                       // by other plugins.\r
+                                                       var eventData =  { mode : 'html' };\r
+                                                       editor.fire( 'beforePaste', eventData );\r
+\r
+                                                       getClipboardData.call( editor, evt, eventData.mode, function ( data )\r
                                                        {\r
                                                                // The very last guard to make sure the\r
                                                                // paste has successfully happened.\r
-                                                               if ( !CKEDITOR.tools.trim( data.toLowerCase().replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/g,'' ) ) )\r
+                                                               if ( !( data = CKEDITOR.tools.trim( data.replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/ig,'' ) ) ) )\r
                                                                        return;\r
 \r
                                                                var dataTransfer = {};\r
-                                                               dataTransfer[ mode ] = data;\r
+                                                               dataTransfer[ eventData.mode ] = data;\r
                                                                editor.fire( 'paste', dataTransfer );\r
                                                        } );\r
                                                });\r
 \r
+                                       if ( CKEDITOR.env.ie )\r
+                                       {\r
+                                               // Dismiss the (wrong) 'beforepaste' event fired on context menu open. (#7953)\r
+                                               body.on( 'contextmenu', function()\r
+                                               {\r
+                                                       depressBeforeEvent = 1;\r
+                                                       // Important: The following timeout will be called only after menu closed.\r
+                                                       setTimeout( function() { depressBeforeEvent = 0; }, 0 );\r
+                                               } );\r
+\r
+                                               // Handle IE's late coming "paste" event when pasting from\r
+                                               // browser toolbar/context menu.\r
+                                               body.on( 'paste', function( evt )\r
+                                               {\r
+                                                       if ( !editor.document.getById( 'cke_pastebin' ) )\r
+                                                       {\r
+                                                               // Prevent native paste.\r
+                                                               evt.data.preventDefault();\r
+\r
+                                                               depressBeforeEvent = 0;\r
+                                                               // Resort to the paste command.\r
+                                                               pasteCmd.exec( editor );\r
+                                                       }\r
+                                               } );\r
+                                       }\r
+\r
                                        body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );\r
 \r
                                        body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );\r
@@ -424,9 +481,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                {\r
                                                        var readOnly = selection.getRanges()[ 0 ].checkReadOnly();\r
                                                        return {\r
-                                                               cut : !readOnly && stateFromNamedCommand( 'Cut', editor ),\r
+                                                               cut : stateFromNamedCommand( 'Cut', editor ),\r
                                                                copy : stateFromNamedCommand( 'Copy', editor ),\r
-                                                               paste : !readOnly && ( CKEDITOR.env.webkit ? CKEDITOR.TRISTATE_OFF : stateFromNamedCommand( 'Paste', editor ) )\r
+                                                               paste : stateFromNamedCommand( 'Paste', editor )\r
                                                        };\r
                                                });\r
                                }\r