JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.4
[ckeditor.git] / _source / plugins / clipboard / plugin.js
index a285954..6c21f55 100644 (file)
@@ -135,9 +135,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                var body = this.document.getBody();\r
 \r
-                               // Simulate 'paste' event for Opera/Firefox2.\r
-                               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
@@ -228,6 +229,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \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
@@ -238,9 +252,6 @@ 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
@@ -275,31 +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
+               var retval;\r
+\r
+               if ( inReadOnly && command in { Paste : 1, Cut : 1 } )\r
+                       return CKEDITOR.TRISTATE_DISABLED;\r
 \r
-               var retval = CKEDITOR.TRISTATE_OFF;\r
-               try { retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; }catch( er ){}\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
-               depressBeforeEvent = 0;\r
-               return retval;\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
@@ -451,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