JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 5a0aca8..0a35305 100644 (file)
@@ -14,17 +14,30 @@ 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 checkReadOnly( selection )\r
+       {\r
+               if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT )\r
+                       return selection.getSelectedElement().isReadOnly();\r
+               else\r
+                       return selection.getCommonAncestor().isReadOnly();\r
+       }\r
 \r
        function onInsertHtml( evt )\r
        {\r
                if ( this.mode == 'wysiwyg' )\r
                {\r
                        this.focus();\r
-                       this.fire( 'saveSnapshot' );\r
 \r
-                       var selection = this.getSelection(),\r
-                               data = evt.data;\r
+                       var selection = this.getSelection();\r
+                       if ( checkReadOnly( selection ) )\r
+                               return;\r
+\r
+                       var data = evt.data;\r
+                       this.fire( 'saveSnapshot' );\r
 \r
                        if ( this.dataProcessor )\r
                                data = this.dataProcessor.toHtml( data );\r
@@ -37,8 +50,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        selection.unlock();\r
 \r
                                var $sel = selection.getNative();\r
+\r
+                               // Delete control selections to avoid IE bugs on pasteHTML.\r
                                if ( $sel.type == 'Control' )\r
                                        $sel.clear();\r
+                               else if  ( selection.getType() == CKEDITOR.SELECTION_TEXT )\r
+                               {\r
+                                       // Due to IE bugs on handling contenteditable=false blocks\r
+                                       // (#6005), we need to make some checks and eventually\r
+                                       // delete the selection first.\r
+\r
+                                       var range = selection.getRanges()[0],\r
+                                               endContainer = range && range.endContainer;\r
+\r
+                                       if ( endContainer &&\r
+                                                endContainer.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                endContainer.getAttribute( 'contenteditable' ) == 'false' &&\r
+                                                range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )\r
+                                       {\r
+                                               range.setEndAfter( range.endContainer );\r
+                                               range.deleteContents();\r
+                                       }\r
+                               }\r
+\r
                                $sel.createRange().pasteHTML( data );\r
 \r
                                if ( selIsLocked )\r
@@ -47,6 +81,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
@@ -59,15 +102,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( this.mode == 'wysiwyg' )\r
                {\r
                        this.focus();\r
+\r
+                       var selection = this.getSelection();\r
+                       if ( checkReadOnly( selection ) )\r
+                               return;\r
+\r
                        this.fire( 'saveSnapshot' );\r
 \r
-                       var element = evt.data,\r
+                       var ranges = selection.getRanges(),\r
+                               element = evt.data,\r
                                elementName = element.getName(),\r
                                isBlock = CKEDITOR.dtd.$block[ elementName ];\r
 \r
-                       var selection = this.getSelection(),\r
-                               ranges = selection.getRanges();\r
-\r
                        var selIsLocked = selection.isLocked;\r
 \r
                        if ( selIsLocked )\r
@@ -121,9 +167,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 +231,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 +273,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 +300,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
@@ -323,6 +412,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        ' allowTransparency="true"' +\r
                                                        '></iframe>' );\r
 \r
+                                               // #5689 Running inside of Firefox chrome the load event doesn't bubble like in a normal page\r
+                                               if (document.location.protocol == 'chrome:')\r
+                                                       CKEDITOR.event.useCapture = true;\r
+\r
                                                // With FF, it's better to load the data on iframe.load. (#3894,#4058)\r
                                                iframe.on( 'load', function( ev )\r
                                                        {\r
@@ -337,6 +430,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                doc.close();\r
                                                        });\r
 \r
+                                               // #5689 Reset adjustment back to default\r
+                                               if (document.location.protocol == 'chrome:')\r
+                                                       CKEDITOR.event.useCapture = false;\r
+\r
                                                mainElement.append( iframe );\r
                                        };\r
 \r
@@ -395,33 +492,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 +516,34 @@ 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
+                                               // Prevent the browser opening links in read-only blocks. (#6032)\r
+                                               domDocument.on( 'click', function( ev )\r
+                                                       {\r
+                                                               ev = ev.data;\r
+                                                               if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )\r
+                                                                       ev.preventDefault();\r
+                                                       });\r
+\r
                                                // Webkit: avoid from editing form control elements content.\r
                                                if ( CKEDITOR.env.webkit )\r
                                                {\r
@@ -498,17 +597,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 +684,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 +869,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
@@ -792,7 +891,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                if ( isLoadingData )\r
                                                                        isPendingFocus = true;\r
-                                                               else if ( editor.window )\r
+                                                               // Temporary solution caused by #6025, supposed be unified by #6154.\r
+                                                               else if ( CKEDITOR.env.opera && editor.document )\r
+                                                               {\r
+                                                                       editor.document.getBody().focus();\r
+\r
+                                                                       editor.selectionChange();\r
+                                                               }\r
+                                                               else if ( !CKEDITOR.env.opera && editor.window )\r
                                                                {\r
                                                                        editor.window.focus();\r
 \r
@@ -882,10 +988,16 @@ 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
+                                       if ( !element.isReadOnly() )\r
+                                       {\r
+                                               element.setAttribute( 'contentEditable', false );\r
+                                               // We should flag that the element was locked by our code so\r
+                                               // it'll be editable by the editor functions (#6046).\r
+                                               element.setCustomData( '_cke_notReadOnly', 1 );\r
+                                       }\r
                                }\r
                        });\r
 \r