JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index 51f53e9..a285954 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, 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,11 +135,8 @@ 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
+                               if ( CKEDITOR.env.opera\r
                                                 || CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 )\r
                                        body.fire( 'paste' );\r
                                return;\r
@@ -214,17 +211,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,8 +223,9 @@ 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
                        // Grab the HTML contents.\r
@@ -250,7 +238,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                 && ( bogusSpan.is && bogusSpan.hasClass( 'Apple-style-span' ) ) ?\r
                                                        bogusSpan : pastebin );\r
 \r
+                       // IE7: selection must go before removing paste. (#8691)\r
                        sel.selectBookmarks( bms );\r
+                       pastebin.remove();\r
                        callback( pastebin[ 'get' + ( mode == 'text' ? 'Value' : 'Html' ) ]() );\r
                }, 0 );\r
        }\r
@@ -293,7 +283,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                // 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
+               var retval = CKEDITOR.TRISTATE_OFF;\r
+               try { retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; }catch( er ){}\r
+\r
                depressBeforeEvent = 0;\r
                return retval;\r
        }\r
@@ -377,33 +369,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