JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.3.2
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 5a0aca8..deff975 100644 (file)
@@ -14,7 +14,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        var nonExitableElementNames = { table:1,pre:1 };\r
 \r
        // Matching an empty paragraph at the end of document.\r
-       var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
+       var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center|li)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
+\r
+       var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );\r
 \r
        function onInsertHtml( evt )\r
        {\r
@@ -47,6 +49,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        else\r
                                this.document.$.execCommand( 'inserthtml', false, data );\r
 \r
+                       // Webkit does not scroll to the cursor position after pasting (#5558)\r
+                       if ( CKEDITOR.env.webkit )\r
+                       {\r
+                               this.document.$.execCommand( 'inserthtml', false, '<span id="cke_paste_marker" cke_temp="1"></span>' );\r
+                               var marker = this.document.getById( 'cke_paste_marker' );\r
+                               marker.scrollIntoView();\r
+                               marker.remove();\r
+                       }\r
+\r
                        CKEDITOR.tools.setTimeout( function()\r
                                {\r
                                        this.fire( 'saveSnapshot' );\r
@@ -121,9 +132,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
 \r
-                       var next = lastElement.getNextSourceNode( true );\r
-                       if ( next && next.type == CKEDITOR.NODE_ELEMENT )\r
-                               range.moveToElementEditStart( next );\r
+                       // If we're inserting a block element immediatelly followed by\r
+                       // another block element, the selection must move there. (#3100,#5436)\r
+                       if ( isBlock )\r
+                       {\r
+                               var next = lastElement.getNext( notWhitespaceEval ),\r
+                                       nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();\r
+\r
+                               // Check if it's a block element that accepts text.\r
+                               if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )\r
+                                       range.moveToElementEditStart( next );\r
+                       }\r
 \r
                        selection.selectRanges( [ range ] );\r
 \r
@@ -177,6 +196,34 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );\r
 \r
+       // Gecko need a key event to 'wake up' the editing\r
+       // ability when document is empty.(#3864, #5781)\r
+       function activateEditing( editor )\r
+       {\r
+               var win = editor.window,\r
+                       doc = editor.document,\r
+                       body = editor.document.getBody(),\r
+                       bodyChildsNum = body.getChildren().count();\r
+\r
+               if ( !bodyChildsNum || ( bodyChildsNum == 1&& body.getFirst().hasAttribute( '_moz_editor_bogus_node' ) ) )\r
+               {\r
+                       restoreDirty( editor );\r
+\r
+                       // Simulating keyboard character input by dispatching a keydown of white-space text.\r
+                       var keyEventSimulate = doc.$.createEvent( "KeyEvents" );\r
+                       keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,\r
+                               false, false, false, 0, 32 );\r
+                       doc.$.dispatchEvent( keyEventSimulate );\r
+\r
+                       // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
+                       bodyChildsNum && body.getFirst().remove();\r
+                       doc.getBody().appendBogus();\r
+                       var nativeRange = new CKEDITOR.dom.range( doc );\r
+                       nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );\r
+                       nativeRange.select();\r
+               }\r
+       }\r
+\r
        /**\r
         *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
         *  non-exitable-block by padding extra br.(#3189)\r
@@ -191,6 +238,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        body = editor.document.getBody(),\r
                        enterMode = editor.config.enterMode;\r
 \r
+               CKEDITOR.env.gecko && activateEditing( editor );\r
+\r
                // When enterMode set to block, we'll establing new paragraph only if we're\r
                // selecting inline contents right under body. (#3657)\r
                if ( enterMode != CKEDITOR.ENTER_BR\r
@@ -216,20 +265,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // block, we should revert the fix and move into the existed one. (#3684)\r
                        if ( isBlankParagraph( fixedBlock ) )\r
                        {\r
-                               var previousElement = fixedBlock.getPrevious( isNotWhitespace ),\r
-                                       nextElement = fixedBlock.getNext( isNotWhitespace );\r
-\r
-                               if ( previousElement && previousElement.getName\r
-                                        && !( previousElement.getName() in nonExitableElementNames )\r
-                                        && isBlankParagraph( previousElement )\r
-                                        && range.moveToElementEditStart( previousElement )\r
-                                        || nextElement && nextElement.getName\r
-                                               && !( nextElement.getName() in nonExitableElementNames )\r
-                                               && isBlankParagraph( nextElement )\r
-                                               && range.moveToElementEditStart( nextElement ) )\r
+                               var element = fixedBlock.getNext( isNotWhitespace );\r
+                               if ( element &&\r
+                                        element.type == CKEDITOR.NODE_ELEMENT &&\r
+                                        !nonExitableElementNames[ element.getName() ] )\r
                                {\r
+                                       range.moveToElementEditStart( element );\r
                                        fixedBlock.remove();\r
                                }\r
+                               else\r
+                               {\r
+                                       element = fixedBlock.getPrevious( isNotWhitespace );\r
+                                       if ( element &&\r
+                                                element.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                !nonExitableElementNames[ element.getName() ] )\r
+                                       {\r
+                                               range.moveToElementEditEnd( element );\r
+                                               fixedBlock.remove();\r
+                                       }\r
+                               }\r
                        }\r
 \r
                        range.select();\r
@@ -395,33 +449,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        }, 0 );\r
                                                }\r
 \r
-                                               // Gecko need a key event to 'wake up' the editing\r
-                                               // ability when document is empty.(#3864)\r
-                                               if ( CKEDITOR.env.gecko && !body.childNodes.length )\r
-                                               {\r
-                                                       setTimeout( function()\r
-                                                       {\r
-                                                               restoreDirty( editor );\r
-\r
-                                                               // Simulating keyboard character input by dispatching a keydown of white-space text.\r
-                                                               var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );\r
-                                                               keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,\r
-                                                                       false, false, false, 0, 32 );\r
-                                                               domDocument.$.dispatchEvent( keyEventSimulate );\r
-\r
-                                                               // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
-                                                               domDocument.createElement( 'br', { attributes: { '_moz_editor_bogus_node' : 'TRUE', '_moz_dirty' : "" } } )\r
-                                                                       .replace( domDocument.getBody().getFirst() );\r
-                                                               var nativeRange = new CKEDITOR.dom.range( domDocument );\r
-                                                               nativeRange.setStartAt( new CKEDITOR.dom.element( body ) , CKEDITOR.POSITION_AFTER_START );\r
-                                                               nativeRange.select();\r
-                                                       }, 0 );\r
-                                               }\r
-\r
-                                               // IE, Opera and Safari may not support it and throw\r
-                                               // errors.\r
-                                               try { domDocument.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}\r
-                                               try { domDocument.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}\r
+                                               CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
 \r
                                                domWindow       = editor.window         = new CKEDITOR.dom.window( domWindow );\r
                                                domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
@@ -445,6 +473,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        } );\r
                                                }\r
 \r
+                                               if ( CKEDITOR.env.gecko )\r
+                                               {\r
+                                                       domDocument.on( 'mouseup', function( ev )\r
+                                                       {\r
+                                                               if ( ev.data.$.button == 2 )\r
+                                                               {\r
+                                                                       var target = ev.data.getTarget();\r
+\r
+                                                                       // Prevent right click from selecting an empty block even\r
+                                                                       // when selection is anchored inside it. (#5845)\r
+                                                                       if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )\r
+                                                                       {\r
+                                                                               var range = new CKEDITOR.dom.range( domDocument );\r
+                                                                               range.moveToElementEditStart( target );\r
+                                                                               range.select( true );\r
+                                                                       }\r
+                                                               }\r
+                                                       } );\r
+                                               }\r
+\r
                                                // Webkit: avoid from editing form control elements content.\r
                                                if ( CKEDITOR.env.webkit )\r
                                                {\r
@@ -498,17 +546,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        blinkCursor();\r
                                                                else if ( CKEDITOR.env.opera )\r
                                                                        doc.getBody().focus();\r
-                                                               else if ( CKEDITOR.env.webkit )\r
-                                                               {\r
-                                                                       // Selection will get lost after move focus\r
-                                                                       // to document element, save it first.\r
-                                                                       var sel = editor.getSelection(),\r
-                                                                                       type = sel.getType(),\r
-                                                                                       range = ( type != CKEDITOR.SELECTION_NONE ) && sel.getRanges()[ 0 ];\r
-\r
-                                                                       doc.getDocumentElement().focus();\r
-                                                                       range && range.select();\r
-                                                               }\r
 \r
                                                                editor.focusManager.focus();\r
                                                        });\r
@@ -596,6 +633,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        editor.fire( 'dataReady' );\r
                                                                }, 0 );\r
 \r
+                                                               // IE, Opera and Safari may not support it and throw errors.\r
+                                                               try { editor.document.$.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}\r
+                                                               try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}\r
+\r
                                                                /*\r
                                                                 * IE BUG: IE might have rendered the iframe with invisible contents.\r
                                                                 * (#3623). Push some inconsequential CSS style changes to force IE to\r
@@ -777,6 +818,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                editor.document.clearCustomData();\r
 \r
                                                                iframe.clearCustomData();\r
+\r
+                                                               /*\r
+                                                               * IE BUG: When destroying editor DOM with the selection remains inside\r
+                                                               * editing area would break IE7/8's selection system, we have to put the editing\r
+                                                               * iframe offline first. (#3812 and #5441)\r
+                                                               */\r
+                                                               iframe.remove();\r
                                                        },\r
 \r
                                                        unload : function( holderElement )\r
@@ -882,7 +930,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        editor.on( 'insertElement', function ( evt )\r
                        {\r
                                var element = evt.data;\r
-                               if ( element.type = CKEDITOR.NODE_ELEMENT\r
+                               if ( element.type == CKEDITOR.NODE_ELEMENT\r
                                                && ( element.is( 'input' ) || element.is( 'textarea' ) ) )\r
                                {\r
                                        element.setAttribute( 'contentEditable', false );\r