Ticket #7366: 7366.patch

File 7366.patch, 5.7 KB (added by Piotrek Koszuliński, 12 years ago)

Fix for Firefox&IE

  • _source/plugins/clipboard/plugin.js

     
    99
    1010(function()
    1111{
     12        // Safari doesn't like 'beforepaste' event - it sometimes doesn't
     13        // properly handles ctrl+c. Probably some race-condition between events.
     14        // Chrome and Firefox works well with both events, so better to use 'paste'
     15        // which will handle pasting from e.g. browsers' menu bars.
     16        // IE7/8 doesn't like 'paste' event for which it's throwing random errors.
     17        var mainPasteEvent = CKEDITOR.env.ie ? 'beforepaste' : 'paste';
     18
    1219        // Tries to execute any of the paste, cut or copy commands in IE. Returns a
    1320        // boolean indicating that the operation succeeded.
    1421        var execIECommand = function( editor, command )
     
    9299                                        // Prevent IE from pasting at the begining of the document.
    93100                                        editor.focus();
    94101
    95                                         if ( !editor.document.getBody().fire( 'beforepaste' )
     102                                        // Command will be handled by 'beforepaste', but as
     103                                        // execIECommand( 'paste' ) will fire also 'paste' event
     104                                        // we're canceling it.
     105                                        depressPasteEventNow();
     106
     107                                        if ( !editor.document.getBody().fire( mainPasteEvent )
    96108                                                 && !execIECommand( editor, 'paste' ) )
    97109                                        {
    98110                                                editor.fire( 'pasteDialog' );
     
    104116                                {
    105117                                        try
    106118                                        {
    107                                                 if ( !editor.document.getBody().fire( 'beforepaste' )
     119                                                if ( !editor.document.getBody().fire( mainPasteEvent )
    108120                                                         && !editor.document.$.execCommand( 'Paste', false, null ) )
    109121                                                {
    110122                                                        throw 0;
     
    134146                        case CKEDITOR.SHIFT + 45 :              // SHIFT+INS
    135147
    136148                                var body = this.document.getBody();
     149                                       
     150                                // Cancel 'paste' event because ctrl+v is for IE handled
     151                                // by 'beforepaste'.
     152                                depressPasteEventNow();
    137153
    138154                                // Simulate 'beforepaste' event for all none-IEs.
    139155                                if ( !CKEDITOR.env.ie && body.fire( 'beforepaste' ) )
     
    275291                }
    276292        }
    277293
    278         var depressBeforeEvent;
     294        var depressBeforePasteEvent;
    279295        function stateFromNamedCommand( command, editor )
    280296        {
    281297                // IE Bug: queryCommandEnabled('paste') fires also 'beforepaste(copy/cut)',
    282298                // guard to distinguish from the ordinary sources( either
    283299                // keyboard paste or execCommand ) (#4874).
    284                 CKEDITOR.env.ie && ( depressBeforeEvent = 1 );
     300                CKEDITOR.env.ie && ( depressBeforePasteEvent = 1 );
    285301
    286302                var retval = CKEDITOR.TRISTATE_OFF;
    287303                try { retval = editor.document.$.queryCommandEnabled( command ) ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED; }catch( er ){}
    288304
    289                 depressBeforeEvent = 0;
     305                depressBeforePasteEvent = 0;
    290306                return retval;
    291307        }
    292308
     
    303319                this.fire( 'pasteState', pasteState );
    304320        }
    305321
     322        var depressPasteEvent = 0;
     323        function depressPasteEventNow()
     324        {
     325                depressPasteEvent = 1;
     326                // For safety reason we should wait longer than 0/1ms.
     327                // We don't know how long execution of quite complex getClipboardData will take
     328                // and in for example 'paste' listner execCommand() (which fires 'paste') is called
     329                // after getClipboardData finishes.
     330                // Luckily, it's impossible to immediately fire another 'paste' event we want to handle,
     331                // because we only handle there native context menu and menu bar.
     332                setTimeout( function() { depressPasteEvent = 0; }, 100 );
     333        }
     334
    306335        // Register the plugin.
    307336        CKEDITOR.plugins.add( 'clipboard',
    308337                {
     
    375404                                editor.on( 'contentDom', function()
    376405                                {
    377406                                        var body = editor.document.getBody();
    378                                         body.on( CKEDITOR.env.webkit ? 'paste' : 'beforepaste', function( evt )
     407                                        body.on( mainPasteEvent, function( evt )
    379408                                                {
    380                                                         if ( depressBeforeEvent )
     409                                                        if ( CKEDITOR.env.ie && depressBeforePasteEvent )
    381410                                                                return;
    382411
    383412                                                        // Fire 'beforePaste' event so clipboard flavor get customized
     
    398427                                                        } );
    399428                                                });
    400429
     430                                        CKEDITOR.env.ie && body.on( 'paste', function( evt )
     431                                                {
     432                                                        if ( depressPasteEvent )
     433                                                                return;
     434
     435                                                        // Cancel next 'paste' event fired by execIECommand( 'paste' )
     436                                                        // at the end of this callback.
     437                                                        depressPasteEventNow();
     438
     439                                                        // Prevent native paste.
     440                                                        evt.data.preventDefault();
     441
     442                                                        // Fire 'beforePaste' event so clipboard flavor get customized
     443                                                        // by other plugins.
     444                                                        var eventData =  { mode : 'html' };
     445                                                        editor.fire( 'beforePaste', eventData );
     446
     447                                                        getClipboardData.call( editor, evt, eventData.mode, function ( data )
     448                                                        {
     449                                                                // The very last guard to make sure the
     450                                                                // paste has successfully happened.
     451                                                                if ( !( data = CKEDITOR.tools.trim( data.replace( /<span[^>]+data-cke-bookmark[^<]*?<\/span>/ig,'' ) ) ) )
     452                                                                        return;
     453
     454                                                                var dataTransfer = {};
     455                                                                dataTransfer[ eventData.mode ] = data;
     456                                                                editor.fire( 'paste', dataTransfer );
     457                                                        } );
     458
     459                                                        // Force IE to paste content into pastebin so pasteDataFromClipboard will work.
     460                                                        execIECommand( editor, 'paste' );
     461                                                });
     462
    401463                                        // Dismiss the (wrong) 'beforepaste' event fired on context menu open. (#7953)
    402464                                        body.on( 'contextmenu', function()
    403465                                        {
    404                                                 depressBeforeEvent = 1;
    405                                                 setTimeout( function() { depressBeforeEvent = 0; }, 10 );
     466                                                depressBeforePasteEvent = 1;
     467                                                setTimeout( function() { depressBeforePasteEvent = 0; }, 10 );
    406468                                        });
    407469
    408                                         body.on( 'beforecut', function() { !depressBeforeEvent && fixCut( editor ); } );
     470                                        body.on( 'beforecut', function() { !depressBeforePasteEvent && fixCut( editor ); } );
    409471
    410472                                        body.on( 'mouseup', function(){ setTimeout( function(){ setToolbarStates.call( editor ); }, 0 ); }, editor );
    411473                                        body.on( 'keyup', setToolbarStates, editor );
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy