﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
5517	Extra <p>&nbsp;</p> when pasting in webkit	Pablo Vanwoerkom		"When pasting in Webkit, there is often an extra <p>&nbsp;</p> added when you look at it in the Source view. The reasons seem to be:

1. Webkit adds a <meta> tag on html paste.

2. Webkit adds a <span class='Apple-style-span'> wrapper to the pasted content.

3. When pasting onto a new paragraph, if the pasted html contains block elements such as <p> or <h1>, it seems the ckeditor tries to clean that the nonstandard DOM but an empty <p>&nbsp;</p> is added as a result.

4. When the current selection is on a block element and you paste other block elements at that point, an extra block element gets prepend before the pasted content. This extra block element has the same tagName as the existing block element (same attributes as well) and its inner HTML contains only ""&nbsp;"".

I've come up with a way to deal with issues 1-3, but not 4. Right now I apply the following clean up code on a paste event when it doesn't contain Microsoft word junk. If you have some feedback or know of a better way please let me know. I am not sure where this would go if it were to be a patch.

{{{

CKEDITOR.on('instanceCreated', function(e) {


  if(CKEDITOR.env.webkit){
    editorInstance.on('paste', function(evt) {
      var data = evt.data;
      data['html'] = data['html'].replace(/<(META|LINK)[^>]*>\s*/gi, '' );
      /* If not pasting from word and pasting into a new paragraph,
       * clean up code, replace paragraph element, and cancel paste action.
       * Otherwise continue with paste event. */
      if( !( /(class=\""?Mso|style=\""[^\""]*\bmso\-|w:WordDocument)/ ).test( data['html'] ) ){
        var temp_div = document.createElement(""div"");
        temp_div.innerHTML = data['html'];
        //remove Webkit span wrapper tag
        if( (temp_div.childElements().length == 1) && (temp_div.childElements()[0].className.indexOf('Apple-style-span') > -1 ) ){
            data['html'] = $(temp_div).childElements()[0].innerHTML;
            temp_div = document.createElement(""div"");
            temp_div.innerHTML = data['html'];
        }
        var selection = editorInstance.getSelection(),
                range = selection.getRanges()[0],
                    orgStart = range.startContainer,
                       orgEnd = range.endContainer,
                         nextElement = orgStart.getNext();

        if(/^\s*<br>\s*$/.match(orgStart.$.innerHTML) && (orgStart.$.tagName =='P') ){
            var newRange = new CKEDITOR.dom.range(range.document),
              fragment=document.createDocumentFragment();
              
            for(var mynode=0;mynode< temp_div.childNodes.length; ++mynode){
                if(typeof temp_div.childNodes[mynode] == 'object'){
                    if(temp_div.childNodes[mynode].nodeName == '#text'){
                      fragment.appendChild(document.createTextNode( temp_div.childNodes[mynode].data ) );
                    }else{
                      fragment.appendChild(temp_div.childNodes[mynode].cloneNode(true));
                    }
                }
            }

            orgStart.$.parentNode.replaceChild(fragment, orgStart.$);
            //Reset cursor to before next element or end of the editor's content
            if(nextElement){
                newRange.setStartAt(nextElement, CKEDITOR.POSITION_BEFORE_START);
                newRange.setEndAt(nextElement, CKEDITOR.POSITION_BEFORE_START);
            }else{
                newRange.setStartAt(editorInstance.document.getBody(), CKEDITOR.POSITION_BEFORE_END);
                newRange.setEndAt(editorInstance.document.getBody(), CKEDITOR.POSITION_BEFORE_END);
            }
            newRange.select();
            evt.cancel();
        }
      }
   });



});



}}}
"	Bug	closed	Normal		Core : Pasting	3.0	duplicate	HasPatch	
