Menu

IE Copy Bug Fix

2008-04-28
2013-04-24
  • Mark Lubischer

    Mark Lubischer - 2008-04-28

    Regarding the known defect listed on the CodePress home page:
    "Copy from CodePress and paste somewhere else is not working correctly on Internet Explorer. Fixed for Firefox on version 0.9.6."

    I've put together a fix for the defect that in my spot testing appears to work properly.

    msie.js - replace the existing versions of the following two functions with these:

        metaHandler : function(evt) {
            keyCode = evt.keyCode;
           
            if(keyCode==9 || evt.tabKey) {
                CodePress.snippets();
            }
            else if((keyCode==122||keyCode==121||keyCode==90) && evt.ctrlKey) { // undo and redo
                (keyCode==121||evt.shiftKey) ? CodePress.actions.redo() :  CodePress.actions.undo();
                evt.returnValue = false;
            }
            else if(keyCode==34||keyCode==33) { // handle page up/down for IE
                self.scrollBy(0, (keyCode==34) ? 200 : -200);
                evt.returnValue = false;
            }
            else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed
                 CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML;
            }
            else if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && keyCode!=90)  { // shortcuts = ctrl||appleKey+shift+key!=z(undo)
                CodePress.shortcuts(keyCode);
                evt.returnValue = false;
            }
            else if(keyCode==86 && evt.ctrlKey)  { // handle paste
                window.clipboardData.setData('Text',window.clipboardData.getData('Text').replace(/\t/g,'\u2008'));
                 top.setTimeout(function(){CodePress.syntaxHighlight('paste');},10);
            }
            else if(keyCode==67 && evt.ctrlKey)  { // handle copy

                //send it through the getCode function
                var output = CodePress.getCode(document.selection.createRange().htmlText);
               
                //trim off a bad char at the end
                output = output.substring(0, output.length - 1);
               
                //set it to the clipboard
                window.clipboardData.setData('Text', output);
               
                //make sure it doesn't get overridden with the browser default handling
                return false;
            }
        },

        getCode : function() {
            var code = editor.innerHTML;
            if (arguments[0])
            {
                code = arguments[0];
            }
            code = code.replace(/<br>/g,'\n');
            code = code.replace(/<\/p>/gi,'\r');
            code = code.replace(/<p>/i,''); // IE first line fix
            code = code.replace(/<p>/gi,'\n');
            code = code.replace(/&nbsp;/gi,'');
            code = code.replace(/\u2009/g,'');
            code = code.replace(/<.*?>/g,'');
            code = code.replace(/&lt;/g,'<');
            code = code.replace(/&gt;/g,'>');
            code = code.replace(/&amp;/gi,'&');
            return code;
        },

    If anyone has some comprehensive tests they can run against it and provide feedback it would be much appreciated.

    Thanks,
    Mark

     
    • brad laney

      brad laney - 2008-10-31

      Using window.clipboardData.setData in anywhere but the "onpaste" event will cause an IE popup.

      It is better to register the onpaste function and do all your work there and have codepress ignore pasting from the rest of the application.

      So far the only issue I've found with pasting in IE is tab indents.

      Also there is an issue when copying one line, it copies the new line character (from inside codepress to codepress). I'm currently working on this issue as well.

       

Log in to post a comment.