JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 6e4eacb..7657a19 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -30,10 +30,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                this.focus();\r
 \r
+                               // Since the insertion might happen from within dialog or menu\r
+                               // where the editor selection might be locked at the moment,\r
+                               // update the locked selection.\r
+                               var selection = this.getSelection(),\r
+                               selIsLocked = selection.isLocked;\r
+\r
+                               selIsLocked && selection.unlock();\r
+\r
                                this.fire( 'saveSnapshot' );\r
 \r
                                insertFunc.call( this, evt.data );\r
 \r
+                               selIsLocked && this.getSelection().lock();\r
+\r
                                // Save snaps after the whole execution completed.\r
                                // This's a workaround for make DOM modification's happened after\r
                                // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'\r
@@ -83,11 +93,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                if ( CKEDITOR.env.ie )\r
                {\r
-                       var selIsLocked = selection.isLocked;\r
-\r
-                       if ( selIsLocked )\r
-                               selection.unlock();\r
-\r
                        var $sel = selection.getNative();\r
 \r
                        // Delete control selections to avoid IE bugs on pasteHTML.\r
@@ -112,14 +117,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }\r
                        }\r
 \r
-                       try\r
-                       {\r
-                               $sel.createRange().pasteHTML( data );\r
-                       }\r
-                       catch (e) {}\r
-\r
-                       if ( selIsLocked )\r
-                               this.getSelection().lock();\r
+                       $sel.createRange().pasteHTML( data );\r
                }\r
                else\r
                        this.document.$.execCommand( 'inserthtml', false, data );\r
@@ -398,8 +396,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        && !pathBlock.is( 'pre' )\r
                                        && !pathBlock.getBogus() )\r
                        {\r
-                               editor.fire( 'updateSnapshot' );\r
-                               restoreDirty( editor );\r
                                pathBlock.appendBogus();\r
                        }\r
                }\r
@@ -412,10 +408,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                && blockLimit.getName() == 'body'\r
                                && !path.block )\r
                {\r
-                       editor.fire( 'updateSnapshot' );\r
-                       restoreDirty( editor );\r
-                       CKEDITOR.env.ie && restoreSelection( selection );\r
-\r
                        var fixedBlock = range.fixBlock( true,\r
                                        editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'  );\r
 \r
@@ -464,10 +456,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );\r
                if ( !testPath.blockLimit.is( 'body') )\r
                {\r
-                       editor.fire( 'updateSnapshot' );\r
-                       restoreDirty( editor );\r
-                       CKEDITOR.env.ie && restoreSelection( selection );\r
-\r
                        var paddingBlock;\r
                        if ( enterMode != CKEDITOR.ENTER_BR )\r
                                paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
@@ -490,6 +478,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );\r
 \r
+                       var win = CKEDITOR.document.getWindow();\r
                        var contentDomReadyHandler;\r
                        editor.on( 'editingBlockReady', function()\r
                                {\r
@@ -498,7 +487,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                isLoadingData,\r
                                                isPendingFocus,\r
                                                frameLoaded,\r
-                                               fireMode;\r
+                                               fireMode,\r
+                                               onResize;\r
 \r
 \r
                                        // Support for custom document.domain in IE.\r
@@ -560,6 +550,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        CKEDITOR.event.useCapture = false;\r
 \r
                                                mainElement.append( iframe );\r
+\r
+                                               // Webkit: iframe size doesn't auto fit well. (#7360)\r
+                                               if ( CKEDITOR.env.webkit )\r
+                                               {\r
+                                                       onResize = function()\r
+                                                       {\r
+                                                               iframe.hide();\r
+                                                               iframe.setSize( 'width', mainElement.getSize( 'width' ) );\r
+                                                               iframe.show();\r
+                                                       };\r
+\r
+                                                       win.on( 'resize', onResize );\r
+                                               }\r
                                        };\r
 \r
                                        // The script that launches the bootstrap logic on 'domReady', so the document\r
@@ -703,28 +706,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        } );\r
                                                }\r
 \r
-                                               // IE standard compliant in editing frame doesn't focus the editor when\r
-                                               // clicking outside actual content, manually apply the focus. (#1659)\r
-                                               if ( editable &&\r
-                                                               CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat'\r
-                                                               || CKEDITOR.env.gecko\r
-                                                               || CKEDITOR.env.opera )\r
-                                               {\r
-                                                       var htmlElement = domDocument.getDocumentElement();\r
-                                                       htmlElement.on( 'mousedown', function( evt )\r
-                                                       {\r
-                                                               // Setting focus directly on editor doesn't work, we\r
-                                                               // have to use here a temporary element to 'redirect'\r
-                                                               // the focus.\r
-                                                               if ( evt.data.getTarget().equals( htmlElement ) )\r
-                                                               {\r
-                                                                       if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
-                                                                               blinkCursor();\r
-                                                                       focusGrabber.focus();\r
-                                                               }\r
-                                                       } );\r
-                                               }\r
-\r
                                                var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;\r
                                                focusTarget.on( 'blur', function()\r
                                                        {\r
@@ -759,80 +740,105 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;\r
                                                keystrokeHandler.attach( domDocument );\r
 \r
-                                               if ( CKEDITOR.env.ie )\r
+                                               domDocument.getDocumentElement().addClass( domDocument.$.compatMode );\r
+                                               // Override keystroke behaviors.\r
+                                               editable && domDocument.on( 'keydown', function( evt )\r
                                                {\r
-                                                       domDocument.getDocumentElement().addClass( domDocument.$.compatMode );\r
-                                                       // Override keystrokes which should have deletion behavior\r
-                                                       //  on control types in IE . (#4047)\r
-                                                       editable && domDocument.on( 'keydown', function( evt )\r
+                                                       var keyCode = evt.data.getKeystroke();\r
+\r
+                                                       // Backspace OR Delete.\r
+                                                       if ( keyCode in { 8 : 1, 46 : 1 } )\r
                                                        {\r
-                                                               var keyCode = evt.data.getKeystroke();\r
+                                                               var sel = editor.getSelection(),\r
+                                                                       selected = sel.getSelectedElement(),\r
+                                                                       range  = sel.getRanges()[ 0 ];\r
 \r
-                                                               // Backspace OR Delete.\r
-                                                               if ( keyCode in { 8 : 1, 46 : 1 } )\r
+                                                               // Override keystrokes which should have deletion behavior\r
+                                                               //  on fully selected element . (#4047) (#7645)\r
+                                                               if ( selected )\r
                                                                {\r
-                                                                       var sel = editor.getSelection(),\r
-                                                                               control = sel.getSelectedElement();\r
-\r
-                                                                       if ( control )\r
-                                                                       {\r
-                                                                               // Make undo snapshot.\r
-                                                                               editor.fire( 'saveSnapshot' );\r
+                                                                       // Make undo snapshot.\r
+                                                                       editor.fire( 'saveSnapshot' );\r
 \r
-                                                                               // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will\r
-                                                                               // break up the selection, safely manage it here. (#4795)\r
-                                                                               var bookmark = sel.getRanges()[ 0 ].createBookmark();\r
-                                                                               // Remove the control manually.\r
-                                                                               control.remove();\r
-                                                                               sel.selectBookmarks( [ bookmark ] );\r
+                                                                       // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will\r
+                                                                       // break up the selection, safely manage it here. (#4795)\r
+                                                                       range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );\r
+                                                                       // Remove the control manually.\r
+                                                                       selected.remove();\r
+                                                                       range.select();\r
 \r
-                                                                               editor.fire( 'saveSnapshot' );\r
+                                                                       editor.fire( 'saveSnapshot' );\r
 \r
-                                                                               evt.data.preventDefault();\r
-                                                                       }\r
+                                                                       evt.data.preventDefault();\r
+                                                                       return;\r
                                                                }\r
-                                                       } );\r
+                                                       }\r
 \r
-                                                       // PageUp/PageDown scrolling is broken in document\r
-                                                       // with standard doctype, manually fix it. (#4736)\r
-                                                       if ( domDocument.$.compatMode == 'CSS1Compat' )\r
+                                                       // PageUp OR PageDown\r
+                                                       if ( keyCode == 33 || keyCode == 34 )\r
                                                        {\r
-                                                               var pageUpDownKeys = { 33 : 1, 34 : 1 };\r
-                                                               domDocument.on( 'keydown', function( evt )\r
+                                                               if ( CKEDITOR.env.gecko )\r
                                                                {\r
-                                                                       if ( evt.data.getKeystroke() in pageUpDownKeys )\r
+                                                                       var body = domDocument.getBody();\r
+\r
+                                                                       // Page up/down cause editor selection to leak\r
+                                                                       // outside of editable thus we try to intercept\r
+                                                                       // the behavior, while it affects only happen\r
+                                                                       // when editor contents are not overflowed. (#7955)\r
+                                                                       if ( domWindow.$.innerHeight > body.$.offsetHeight )\r
                                                                        {\r
-                                                                               setTimeout( function ()\r
-                                                                               {\r
-                                                                                       editor.getSelection().scrollIntoView();\r
-                                                                               }, 0 );\r
+                                                                               range = new CKEDITOR.dom.range( domDocument );\r
+                                                                               range[ keyCode == 33 ? 'moveToElementEditStart' : 'moveToElementEditEnd']( body );\r
+                                                                               range.select();\r
+                                                                               evt.data.preventDefault();\r
                                                                        }\r
-                                                               } );\r
+                                                               }\r
+\r
                                                        }\r
+                                               } );\r
 \r
-                                                       // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)\r
-                                                       editor.config.enterMode != CKEDITOR.ENTER_P\r
-                                                               && domDocument.on( 'selectionchange', function()\r
+                                               // PageUp/PageDown scrolling is broken in document\r
+                                               // with standard doctype, manually fix it. (#4736)\r
+                                               if ( CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat' )\r
+                                               {\r
+                                                       var pageUpDownKeys = { 33 : 1, 34 : 1 };\r
+                                                       domDocument.on( 'keydown', function( evt )\r
+                                                       {\r
+                                                               if ( evt.data.getKeystroke() in pageUpDownKeys )\r
                                                                {\r
-                                                                       var body = domDocument.getBody(),\r
-                                                                               range = editor.getSelection().getRanges()[ 0 ];\r
+                                                                       setTimeout( function ()\r
+                                                                       {\r
+                                                                               editor.getSelection().scrollIntoView();\r
+                                                                       }, 0 );\r
+                                                               }\r
+                                                       } );\r
+                                               }\r
+\r
+                                               // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)\r
+                                               if ( CKEDITOR.env.ie && editor.config.enterMode != CKEDITOR.ENTER_P )\r
+                                               {\r
+                                                       domDocument.on( 'selectionchange', function()\r
+                                                       {\r
+                                                               var body = domDocument.getBody(),\r
+                                                                       sel = editor.getSelection(),\r
+                                                                       range = sel && sel.getRanges()[ 0 ];\r
 \r
-                                                                       if ( body.getHtml().match( /^<p>&nbsp;<\/p>$/i )\r
-                                                                               && range.startContainer.equals( body ) )\r
+                                                               if ( range && body.getHtml().match( /^<p>&nbsp;<\/p>$/i )\r
+                                                                       && range.startContainer.equals( body ) )\r
+                                                               {\r
+                                                                       // Avoid the ambiguity from a real user cursor position.\r
+                                                                       setTimeout( function ()\r
                                                                        {\r
-                                                                               // Avoid the ambiguity from a real user cursor position.\r
-                                                                               setTimeout( function ()\r
+                                                                               range = editor.getSelection().getRanges()[ 0 ];\r
+                                                                               if ( !range.startContainer.equals ( 'body' ) )\r
                                                                                {\r
-                                                                                       range = editor.getSelection().getRanges()[ 0 ];\r
-                                                                                       if ( !range.startContainer.equals ( 'body' ) )\r
-                                                                                       {\r
-                                                                                               body.getFirst().remove( 1 );\r
-                                                                                               range.moveToElementEditEnd( body );\r
-                                                                                               range.select( 1 );\r
-                                                                                       }\r
-                                                                               }, 0 );\r
-                                                                       }\r
-                                                               });\r
+                                                                                       body.getFirst().remove( 1 );\r
+                                                                                       range.moveToElementEditEnd( body );\r
+                                                                                       range.select( 1 );\r
+                                                                               }\r
+                                                                       }, 0 );\r
+                                                               }\r
+                                                       });\r
                                                }\r
 \r
                                                // Adds the document body as a context menu target.\r
@@ -846,7 +852,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                if ( fireMode )\r
                                                                {\r
                                                                        editor.mode = 'wysiwyg';\r
-                                                                       editor.fire( 'mode' );\r
+                                                                       editor.fire( 'mode', { previousMode : editor._.previousMode } );\r
                                                                        fireMode = false;\r
                                                                }\r
 \r
@@ -1090,6 +1096,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                this.onDispose();\r
 \r
+                                                               if ( onResize )\r
+                                                                       win.removeListener( 'resize', onResize );\r
+\r
                                                                editor.window = editor.document = iframe = mainElement = isPendingFocus = null;\r
 \r
                                                                editor.fire( 'contentDomUnload' );\r
@@ -1101,18 +1110,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                if ( isLoadingData )\r
                                                                        isPendingFocus = true;\r
-                                                               // Temporary solution caused by #6025, supposed be unified by #6154.\r
-                                                               else if ( CKEDITOR.env.opera && editor.document )\r
-                                                               {\r
-                                                                       // Required for Opera when switching focus\r
-                                                                       // from another iframe, e.g. panels. (#6444)\r
-                                                                       var iframe = editor.window.$.frameElement;\r
-                                                                       iframe.blur(), iframe.focus();\r
-                                                                       editor.document.getBody().focus();\r
-\r
-                                                                       editor.selectionChange();\r
-                                                               }\r
-                                                               else if ( !CKEDITOR.env.opera && win )\r
+                                                               else if ( win )\r
                                                                {\r
                                                                        // AIR needs a while to focus when moving from a link.\r
                                                                        CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();\r
@@ -1125,7 +1123,23 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );\r
                                        editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );\r
                                        // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)\r
-                                       editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );\r
+                                       editor.on( 'selectionChange', function( evt )\r
+                                       {\r
+                                               if ( editor.readOnly )\r
+                                                       return;\r
+\r
+                                               var sel = editor.getSelection();\r
+                                               // Do it only when selection is not locked. (#8222)\r
+                                               if ( sel && !sel.isLocked )\r
+                                               {\r
+                                                       var isDirty = editor.checkDirty();\r
+                                                       editor.fire( 'saveSnapshot', { contentOnly : 1 } );\r
+                                                       onSelectionChangeFixBody.call( this, evt );\r
+                                                       editor.fire( 'updateSnapshot' );\r
+                                                       !isDirty && editor.resetDirty();\r
+                                               }\r
+\r
+                                       }, null, null, 1 );\r
                                });\r
 \r
                        var titleBackup;\r
@@ -1165,6 +1179,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                editor.addCss( 'html { height: 100% !important; }' );\r
                                editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1;     width : 24px; height : 24px; }' );\r
                        }\r
+                       // Remove the margin to avoid mouse confusion. (#8835)\r
+                       else if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 && editor.config.contentsLangDirection == 'ltr' )\r
+                               editor.addCss( 'body{margin-right:0;}' );\r
 \r
                        /* #3658: [IE6] Editor document has horizontal scrollbar on long lines\r
                        To prevent this misbehavior, we show the scrollbar always */\r
@@ -1205,31 +1222,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        });\r
                        }\r
 \r
-                       // Create an invisible element to grab focus.\r
-                       if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )\r
-                       {\r
-                               var focusGrabber;\r
-                               editor.on( 'uiReady', function()\r
-                               {\r
-                                       focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(\r
-                                               // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)\r
-                                               '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );\r
-\r
-                                       focusGrabber.on( 'focus', function()\r
-                                               {\r
-                                                       editor.focus();\r
-                                               } );\r
-\r
-                                       editor.focusGrabber = focusGrabber;\r
-                               } );\r
-                               editor.on( 'destroy', function()\r
-                               {\r
-                                       CKEDITOR.tools.removeFunction( contentDomReadyHandler );\r
-                                       focusGrabber.clearCustomData();\r
-                                       delete editor.focusGrabber;\r
-                               } );\r
-                       }\r
-\r
                        // Disable form elements editing mode provided by some browers. (#5746)\r
                        editor.on( 'insertElement', function ( evt )\r
                        {\r
@@ -1239,9 +1231,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                {\r
                                        // We should flag that the element was locked by our code so\r
                                        // it'll be editable by the editor functions (#6046).\r
-                                       if ( !element.isReadOnly() )\r
+                                       var readonly = element.getAttribute( 'contenteditable' ) == 'false';\r
+                                       if ( !readonly )\r
+                                       {\r
                                                element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );\r
-                                       element.setAttribute( 'contentEditable', false );\r
+                                               element.setAttribute( 'contenteditable', false );\r
+                                       }\r
                                }\r
                        });\r
 \r