JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.6.1
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 76501cc..ed284f9 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -10,19 +10,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
 (function()\r
 {\r
-       // List of elements in which has no way to move editing focus outside.\r
-       var nonExitableElementNames = { table:1,pre:1 };\r
-\r
        // Matching an empty paragraph at the end of document.\r
-       var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;\r
+       var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center|pre)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;\r
 \r
-       var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );\r
+       var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true ),\r
+         notBogus = CKEDITOR.dom.walker.bogus( true ),\r
+         notEmpty = function( node ) { return notWhitespaceEval( node ) && notBogus( node ); };\r
 \r
-       // Elements that could have empty new line around, including table, pre-formatted block, hr, page-break. (#6554)\r
-       function nonExitable( element )\r
+       // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)\r
+       function nonEditable( element )\r
        {\r
-               return ( element.getName() in nonExitableElementNames )\r
-                               || element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];\r
+               return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];\r
        }\r
 \r
 \r
@@ -34,18 +32,31 @@ 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
+                               var that = this;\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
                                // call.\r
-                               CKEDITOR.tools.setTimeout( function()\r
+                               setTimeout( function()\r
                                   {\r
-                                          this.fire( 'saveSnapshot' );\r
-                                  }, 0, this );\r
+                                                try { that.fire( 'saveSnapshot' ); }\r
+                                                // IEs < 9 may requires a further delay to save snapshot, after pasting. (#9132)\r
+                                                catch ( e ) { setTimeout( function(){ that.fire( 'saveSnapshot' ); }, 200 ); }\r
+                                        }, 0 );\r
                        }\r
                };\r
        }\r
@@ -55,6 +66,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( this.dataProcessor )\r
                        data = this.dataProcessor.toHtml( data );\r
 \r
+               if ( !data )\r
+                       return;\r
+\r
                // HTML insertion only considers the first range.\r
                var selection = this.getSelection(),\r
                        range = selection.getRanges()[ 0 ];\r
@@ -62,13 +76,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( range.checkReadOnly() )\r
                        return;\r
 \r
-               if ( CKEDITOR.env.ie )\r
+               // Opera: force block splitting when pasted content contains block. (#7801)\r
+               if ( CKEDITOR.env.opera )\r
                {\r
-                       var selIsLocked = selection.isLocked;\r
-\r
-                       if ( selIsLocked )\r
-                               selection.unlock();\r
+                       var path = new CKEDITOR.dom.elementPath( range.startContainer );\r
+                       if ( path.block )\r
+                       {\r
+                               var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;\r
+                               for ( var i = 0, count = nodes.length; i < count; i++ )\r
+                               {\r
+                                       if ( nodes[ i ]._.isBlockLike )\r
+                                       {\r
+                                               range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
+                                               range.insertNode( range.document.createText( '' ) );\r
+                                               range.select();\r
+                                               break;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
 \r
+               if ( CKEDITOR.env.ie )\r
+               {\r
                        var $sel = selection.getNative();\r
 \r
                        // Delete control selections to avoid IE bugs on pasteHTML.\r
@@ -93,14 +122,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
@@ -250,16 +272,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
 \r
-                               // If we're inserting a block element immediatelly followed by\r
-                               // another block element, the selection must move there. (#3100,#5436)\r
+                               // If we're inserting a block element immediately followed by\r
+                               // another block element, the selection must be optimized. (#3100,#5436,#8950)\r
                                if ( isBlock )\r
                                {\r
-                                       var next = lastElement.getNext( notWhitespaceEval ),\r
+                                       var next = lastElement.getNext( notEmpty ),\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
+                                       // If the next one is a text block, move cursor to the start of it's content.\r
+                                       if ( nextName && CKEDITOR.dtd.$block[ nextName ] )\r
+                                       {\r
+                                               if ( CKEDITOR.dtd[ nextName ][ '#' ] )\r
+                                                       range.moveToElementEditStart( next );\r
+                                               // Otherwise move cursor to the before end of the last element.\r
+                                               else\r
+                                                       range.moveToElementEditEnd( lastElement );\r
+                                       }\r
+                                       // Open a new line if the block is inserted at the end of parent.\r
+                                       else if ( !next )\r
+                                       {\r
+                                               next = range.fixBlock( true, this.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
                                                range.moveToElementEditStart( next );\r
+                                       }\r
                                }\r
                        }\r
 \r
@@ -307,15 +341,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        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
+       // ability when document is empty.(#3864)\r
        function activateEditing( editor )\r
        {\r
                var win = editor.window,\r
                        doc = editor.document,\r
                        body = editor.document.getBody(),\r
+                       bodyFirstChild = body.getFirst(),\r
                        bodyChildsNum = body.getChildren().count();\r
 \r
-               if ( !bodyChildsNum || ( bodyChildsNum == 1&& body.getFirst().hasAttribute( '_moz_editor_bogus_node' ) ) )\r
+               if ( !bodyChildsNum\r
+                       || bodyChildsNum == 1\r
+                               && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT\r
+                               && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )\r
                {\r
                        restoreDirty( editor );\r
 \r
@@ -359,29 +397,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                if ( CKEDITOR.env.gecko )\r
                {\r
-                       activateEditing( editor );\r
-\r
                        // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)\r
-                       var pathBlock = path.block || path.blockLimit;\r
-                       if ( pathBlock && !pathBlock.getBogus() )\r
+                       var pathBlock = path.block || path.blockLimit,\r
+                               lastNode = pathBlock && pathBlock.getLast( isNotEmpty );\r
+\r
+                       // Check some specialities of the current path block:\r
+                       // 1. It is really displayed as block; (#7221)\r
+                       // 2. It doesn't end with one inner block; (#7467)\r
+                       // 3. It doesn't have bogus br yet.\r
+                       if ( pathBlock\r
+                                       && pathBlock.isBlockBoundary()\r
+                                       && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )\r
+                                       && !pathBlock.is( 'pre' )\r
+                                       && !pathBlock.getBogus() )\r
                        {\r
-                               editor.fire( 'updateSnapshot' );\r
-                               restoreDirty( editor );\r
                                pathBlock.appendBogus();\r
                        }\r
                }\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
-                    && range.collapsed\r
-                        && blockLimit.getName() == 'body'\r
-                        && !path.block )\r
+               // When we're in block enter mode, a new paragraph will be established\r
+               // to encapsulate inline contents right under body. (#3657)\r
+               if ( editor.config.autoParagraph !== false\r
+                               && enterMode != CKEDITOR.ENTER_BR\r
+                               && range.collapsed\r
+                               && 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
@@ -399,7 +440,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                var element = fixedBlock.getNext( isNotWhitespace );\r
                                if ( element &&\r
                                         element.type == CKEDITOR.NODE_ELEMENT &&\r
-                                        !nonExitable( element ) )\r
+                                        !nonEditable( element ) )\r
                                {\r
                                        range.moveToElementEditStart( element );\r
                                        fixedBlock.remove();\r
@@ -409,7 +450,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        element = fixedBlock.getPrevious( isNotWhitespace );\r
                                        if ( element &&\r
                                                 element.type == CKEDITOR.NODE_ELEMENT &&\r
-                                                !nonExitable( element ) )\r
+                                                !nonEditable( element ) )\r
                                        {\r
                                                range.moveToElementEditEnd( element );\r
                                                fixedBlock.remove();\r
@@ -418,39 +459,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
 \r
                        range.select();\r
-                       // Notify non-IE that selection has changed.\r
-                       if ( !CKEDITOR.env.ie )\r
-                       {\r
-                               // Make sure next selection change is correct.  (#6811)\r
-                               editor.forceNextSelectionCheck();\r
-                               editor.selectionChange();\r
-                       }\r
+                       // Cancel this selection change in favor of the next (correct).  (#6811)\r
+                       evt.cancel();\r
                }\r
 \r
-               // All browsers are incapable to moving cursor out of certain non-exitable\r
-               // blocks (e.g. table, list, pre) at the end of document, make this happen by\r
-               // place a bogus node there, which would be later removed by dataprocessor.\r
-               var walkerRange = new CKEDITOR.dom.range( editor.document ),\r
-                       walker = new CKEDITOR.dom.walker( walkerRange );\r
-               walkerRange.selectNodeContents( body );\r
-               walker.evaluator = function( node )\r
-               {\r
-                       return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in nonExitableElementNames );\r
-               };\r
-               walker.guard = function( node, isMoveout )\r
+               // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)\r
+               // at the end of document, makes it unable to continue adding content, we have to make this\r
+               // easier by opening an new empty paragraph.\r
+               var testRange = new CKEDITOR.dom.range( editor.document );\r
+               testRange.moveToElementEditEnd( editor.document.getBody() );\r
+               var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );\r
+               if ( !testPath.blockLimit.is( 'body') )\r
                {\r
-                       return !( ( node.type == CKEDITOR.NODE_TEXT && isNotWhitespace( node ) ) || isMoveout );\r
-               };\r
-\r
-               if ( walker.previous() )\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( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
+                               paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
                        else\r
                                paddingBlock = body;\r
 \r
@@ -465,11 +488,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                init : function( editor )\r
                {\r
-                       var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )\r
+                       var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )\r
                                ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;\r
 \r
-                       var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );\r
+                       var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name ),\r
+                               frameDesc = editor.lang.editorHelp;\r
+\r
+                       if ( CKEDITOR.env.ie )\r
+                               frameLabel += ', ' + frameDesc;\r
 \r
+                       var win = CKEDITOR.document.getWindow();\r
                        var contentDomReadyHandler;\r
                        editor.on( 'editingBlockReady', function()\r
                                {\r
@@ -478,7 +506,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
@@ -510,10 +539,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        :\r
                                                                '';\r
 \r
+                                               var labelId = CKEDITOR.tools.getNextId();\r
                                                iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
                                                        ' style="width:100%;height:100%"' +\r
                                                        ' frameBorder="0"' +\r
-                                                       ' title="' + frameLabel + '"' +\r
+                                                       ' aria-describedby="' + labelId + '"' +\r
+                                                       ' title="' + frameLabel + '"' +\r
                                                        ' src="' + src + '"' +\r
                                                        ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +\r
                                                        ' allowTransparency="true"' +\r
@@ -539,29 +570,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                if ( document.location.protocol == 'chrome:' )\r
                                                        CKEDITOR.event.useCapture = false;\r
 \r
-                                               // The container must be visible when creating the iframe in FF (#5956)\r
-                                               var element = editor.element,\r
-                                                       isHidden = CKEDITOR.env.gecko && !element.isVisible(),\r
-                                                       previousStyles = {};\r
-                                               if ( isHidden )\r
-                                               {\r
-                                                       element.show();\r
-                                                       previousStyles = {\r
-                                                               position : element.getStyle( 'position' ),\r
-                                                               top : element.getStyle( 'top' )\r
-                                                       };\r
-                                                       element.setStyles( { position : 'absolute', top : '-3000px' } );\r
-                                               }\r
+                                               mainElement.append( CKEDITOR.dom.element.createFromHtml(\r
+                                                       '<span id="' + labelId + '" class="cke_voice_label">' + frameDesc + '</span>'));\r
 \r
                                                mainElement.append( iframe );\r
 \r
-                                               if ( isHidden )\r
+                                               // Webkit: iframe size doesn't auto fit well. (#7360)\r
+                                               if ( CKEDITOR.env.webkit )\r
                                                {\r
-                                                       setTimeout( function()\r
+                                                       onResize = function()\r
                                                        {\r
-                                                               element.hide();\r
-                                                               element.setStyles( previousStyles );\r
-                                                       }, 1000 );\r
+                                                               // Hide the iframe to get real size of the holder. (#8941)\r
+                                                               mainElement.setStyle( 'width', '100%' );\r
+                                                               iframe.hide();\r
+\r
+                                                               iframe.setSize( 'width', mainElement.getSize( 'width' ) );\r
+                                                               mainElement.removeStyle( 'width' );\r
+                                                               iframe.show();\r
+                                                       };\r
+\r
+                                                       win.on( 'resize', onResize );\r
                                                }\r
                                        };\r
 \r
@@ -592,6 +620,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                body.spellcheck = !editor.config.disableNativeSpellChecker;\r
 \r
+                                               var editable = !editor.readOnly;\r
+\r
                                                if ( CKEDITOR.env.ie )\r
                                                {\r
                                                        // Don't display the focus border.\r
@@ -600,7 +630,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // Disable and re-enable the body to avoid IE from\r
                                                        // taking the editing focus at startup. (#141 / #523)\r
                                                        body.disabled = true;\r
-                                                       body.contentEditable = true;\r
+                                                       body.contentEditable = editable;\r
                                                        body.removeAttribute( 'disabled' );\r
                                                }\r
                                                else\r
@@ -612,20 +642,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // Prefer 'contentEditable' instead of 'designMode'. (#3593)\r
                                                                if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900\r
                                                                                || CKEDITOR.env.opera )\r
-                                                                       domDocument.$.body.contentEditable = true;\r
+                                                                       domDocument.$.body.contentEditable = editable;\r
                                                                else if ( CKEDITOR.env.webkit )\r
-                                                                       domDocument.$.body.parentNode.contentEditable = true;\r
+                                                                       domDocument.$.body.parentNode.contentEditable = editable;\r
                                                                else\r
-                                                                       domDocument.$.designMode = 'on';\r
+                                                                       domDocument.$.designMode = editable? 'off' : 'on';\r
                                                        }, 0 );\r
                                                }\r
 \r
-                                               CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
+                                               editable && 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
 \r
-                                               domDocument.on( 'dblclick', function( evt )\r
+                                               editable && domDocument.on( 'dblclick', function( evt )\r
                                                {\r
                                                        var element = evt.data.getTarget(),\r
                                                                data = { element : element, dialog : '' };\r
@@ -687,6 +717,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                // Webkit: avoid from editing form control elements content.\r
                                                if ( CKEDITOR.env.webkit )\r
                                                {\r
+                                                       // Mark that cursor will right blinking (#7113).\r
+                                                       domDocument.on( 'mousedown', function() { wasFocused = 1; } );\r
                                                        // Prevent from tick checkbox/radiobox/select\r
                                                        domDocument.on( 'click', function( ev )\r
                                                        {\r
@@ -702,28 +734,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 ( CKEDITOR.env.ie\r
-                                                       && 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
@@ -736,9 +746,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                var doc = editor.document;\r
 \r
-                                                               if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
-                                                                       blinkCursor();\r
-                                                               else if ( CKEDITOR.env.opera )\r
+                                                               if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )\r
                                                                        doc.getBody().focus();\r
                                                                // Webkit needs focus for the first time on the HTML element. (#6153)\r
                                                                else if ( CKEDITOR.env.webkit )\r
@@ -754,59 +762,162 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        });\r
 \r
                                                var keystrokeHandler = editor.keystrokeHandler;\r
-                                               if ( keystrokeHandler )\r
-                                                       keystrokeHandler.attach( domDocument );\r
+                                               // Prevent backspace from navigating off the page.\r
+                                               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
+                                               editor.on( 'key', 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
-                                                       domDocument.on( 'keydown', function( evt )\r
+                                                       if ( editor.mode != 'wysiwyg' )\r
+                                                               return;\r
+\r
+                                                       var keyCode = evt.data.keyCode;\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
+                                                                       path = new CKEDITOR.dom.elementPath( range.startContainer ),\r
+                                                                       block,\r
+                                                                       parent,\r
+                                                                       next,\r
+                                                                       rtl = keyCode == 8;\r
+\r
+                                                               // Override keystrokes which should have deletion behavior\r
+                                                               //  on fully selected element . (#4047) (#7645)\r
+                                                               if ( selected )\r
+                                                               {\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
+                                                                       range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );\r
+                                                                       // Remove the control manually.\r
+                                                                       selected.remove();\r
+                                                                       range.select();\r
 \r
-                                                               // Backspace OR Delete.\r
-                                                               if ( keyCode in { 8 : 1, 46 : 1 } )\r
+                                                                       editor.fire( 'saveSnapshot' );\r
+\r
+                                                                       evt.cancel();\r
+                                                               }\r
+                                                               else if ( range.collapsed )\r
                                                                {\r
-                                                                       var sel = editor.getSelection(),\r
-                                                                               control = sel.getSelectedElement();\r
+                                                                       // Handle the following special cases: (#6217)\r
+                                                                       // 1. Del/Backspace key before/after table;\r
+                                                                       // 2. Backspace Key after start of table.\r
+                                                                       if ( ( block = path.block ) &&\r
+                                                                                range[ rtl ? 'checkStartOfBlock' : 'checkEndOfBlock' ]() &&\r
+                                                                                ( next = block[ rtl ? 'getPrevious' : 'getNext' ]( notWhitespaceEval ) ) &&\r
+                                                                                next.is( 'table' ) )\r
+                                                                       {\r
+                                                                               editor.fire( 'saveSnapshot' );\r
+\r
+                                                                               // Remove the current empty block.\r
+                                                                               if ( range[ rtl ? 'checkEndOfBlock' : 'checkStartOfBlock' ]() )\r
+                                                                                       block.remove();\r
+\r
+                                                                               // Move cursor to the beginning/end of table cell.\r
+                                                                               range[ 'moveToElementEdit' + ( rtl ? 'End' : 'Start' ) ]( next );\r
+                                                                               range.select();\r
 \r
-                                                                       if ( control )\r
+                                                                               editor.fire( 'saveSnapshot' );\r
+\r
+                                                                               evt.cancel();\r
+                                                                       }\r
+                                                                       else if ( path.blockLimit.is( 'td' ) &&\r
+                                                                                         ( parent = path.blockLimit.getAscendant( 'table' ) ) &&\r
+                                                                                         range.checkBoundaryOfElement( parent, rtl ? CKEDITOR.START : CKEDITOR.END ) &&\r
+                                                                                         ( next = parent[ rtl ? 'getPrevious' : 'getNext' ]( notWhitespaceEval ) ) )\r
                                                                        {\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
+                                                                               // Move cursor to the end of previous block.\r
+                                                                               range[ 'moveToElementEdit' + ( rtl ? 'End' : 'Start' ) ]( next );\r
+\r
+                                                                               // Remove any previous empty block.\r
+                                                                               if ( range.checkStartOfBlock() && range.checkEndOfBlock() )\r
+                                                                                       next.remove();\r
+                                                                               else\r
+                                                                                       range.select();\r
 \r
                                                                                editor.fire( 'saveSnapshot' );\r
 \r
-                                                                               evt.data.preventDefault();\r
+                                                                               evt.cancel();\r
                                                                        }\r
+\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.cancel();\r
                                                                        }\r
-                                                               } );\r
+                                                               }\r
+\r
                                                        }\r
+                                               } );\r
+\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
+                                                                       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 ( 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
+                                                                               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
                                                }\r
 \r
                                                // Adds the document body as a context menu target.\r
@@ -820,7 +931,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
@@ -836,6 +947,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        editor.fire( 'dataReady' );\r
                                                                }, 0 );\r
 \r
+                                                               // Enable dragging of position:absolute elements in IE.\r
+                                                               try { editor.document.$.execCommand ( '2D-position', false, true); } catch(e) {}\r
+\r
                                                                // IE, Opera and Safari may not support it and throw errors.\r
                                                                try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}\r
                                                                if ( editor.config.disableObjectResizing )\r
@@ -902,6 +1016,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        loadData : function( data )\r
                                                        {\r
                                                                isLoadingData = true;\r
+                                                               editor._.dataStore = { id : 1 };\r
 \r
                                                                var config = editor.config,\r
                                                                        fullPage = config.fullPage,\r
@@ -926,6 +1041,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                {\r
                                                                                        editor.docType = docType = match;\r
                                                                                        return '';\r
+                                                                               }).replace( /<\?xml\s[^\?]*\?>/i, function( match )\r
+                                                                               {\r
+                                                                                       editor.xmlDeclaration = match;\r
+                                                                                       return '';\r
                                                                                });\r
                                                                }\r
 \r
@@ -981,6 +1100,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                '</html>';\r
                                                                }\r
 \r
+                                                               // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).\r
+                                                               if ( CKEDITOR.env.gecko )\r
+                                                                       data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );\r
+\r
                                                                data += activationScript;\r
 \r
 \r
@@ -994,12 +1117,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                var config = editor.config,\r
                                                                        fullPage = config.fullPage,\r
                                                                        docType = fullPage && editor.docType,\r
+                                                                       xmlDeclaration = fullPage && editor.xmlDeclaration,\r
                                                                        doc = iframe.getFrameDocument();\r
 \r
                                                                var data = fullPage\r
                                                                        ? doc.getDocumentElement().getOuterHtml()\r
                                                                        : doc.getBody().getHtml();\r
 \r
+                                                               // BR at the end of document is bogus node for Mozilla. (#5293).\r
+                                                               if ( CKEDITOR.env.gecko )\r
+                                                                       data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );\r
+\r
                                                                if ( editor.dataProcessor )\r
                                                                        data = editor.dataProcessor.toDataFormat( data, fixForBody );\r
 \r
@@ -1007,6 +1135,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                if ( config.ignoreEmptyParagraph )\r
                                                                        data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );\r
 \r
+                                                               if ( xmlDeclaration )\r
+                                                                       data = xmlDeclaration + '\n' + data;\r
                                                                if ( docType )\r
                                                                        data = docType + '\n' + data;\r
 \r
@@ -1048,6 +1178,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
@@ -1059,19 +1192,17 @@ 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
+                                                               else if ( win )\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
+                                                                       var sel = editor.getSelection(),\r
+                                                                               ieSel = sel && sel.getNative();\r
+\r
+                                                                       // IE considers control-type element as separate\r
+                                                                       // focus host when selected, avoid destroying the\r
+                                                                       // selection in such case. (#5812) (#8949)\r
+                                                                       if ( ieSel && ieSel.type == 'Control' )\r
+                                                                               return;\r
 \r
-                                                                       editor.selectionChange();\r
-                                                               }\r
-                                                               else if ( !CKEDITOR.env.opera && 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
                                                                        editor.selectionChange();\r
@@ -1083,22 +1214,49 @@ 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
-                       // Setting voice label as window title, backup the original one\r
-                       // and restore it before running into use.\r
                        editor.on( 'contentDom', function()\r
                                {\r
                                        var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );\r
                                        title.data( 'cke-title', editor.document.$.title );\r
-                                       editor.document.$.title = frameLabel;\r
+\r
+                                       // [IE] JAWS will not recognize the aria label we used on the iframe\r
+                                       // unless the frame window title string is used as the voice label,\r
+                                       // backup the original one and restore it on output.\r
+                                       CKEDITOR.env.ie && ( editor.document.$.title = frameLabel );\r
+                               });\r
+\r
+                       editor.on( 'readOnly', function()\r
+                               {\r
+                                       if ( editor.mode == 'wysiwyg' )\r
+                                       {\r
+                                               // Symply reload the wysiwyg area. It'll take care of read-only.\r
+                                               var wysiwyg = editor.getMode();\r
+                                               wysiwyg.loadData( wysiwyg.getData() );\r
+                                       }\r
                                });\r
 \r
-                       // IE8 stricts mode doesn't have 'contentEditable' in effect\r
+                       // IE>=8 stricts mode doesn't have 'contentEditable' in effect\r
                        // on element unless it has layout. (#5562)\r
-                       if ( CKEDITOR.env.ie8Compat )\r
+                       if ( CKEDITOR.document.$.documentMode >= 8 )\r
                        {\r
                                editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );\r
 \r
@@ -1109,60 +1267,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                        // Set the HTML style to 100% to have the text cursor in affect (#6341)\r
                        else if ( CKEDITOR.env.gecko )\r
-                               editor.addCss( 'html { height: 100% !important; }' );\r
-\r
-                       // Switch on design mode for a short while and close it after then.\r
-                       function blinkCursor( retry )\r
                        {\r
-                               CKEDITOR.tools.tryThese(\r
-                                       function()\r
-                                       {\r
-                                               editor.document.$.designMode = 'on';\r
-                                               setTimeout( function()\r
-                                               {\r
-                                                       editor.document.$.designMode = 'off';\r
-                                                       if ( CKEDITOR.currentInstance == editor )\r
-                                                               editor.document.getBody().focus();\r
-                                               }, 50 );\r
-                                       },\r
-                                       function()\r
-                                       {\r
-                                               // The above call is known to fail when parent DOM\r
-                                               // tree layout changes may break design mode. (#5782)\r
-                                               // Refresh the 'contentEditable' is a cue to this.\r
-                                               editor.document.$.designMode = 'off';\r
-                                               var body = editor.document.getBody();\r
-                                               body.setAttribute( 'contentEditable', false );\r
-                                               body.setAttribute( 'contentEditable', true );\r
-                                               // Try it again once..\r
-                                               !retry && blinkCursor( 1 );\r
-                                       });\r
+                               editor.addCss( 'html { height: 100% !important; }' );\r
+                               editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1;     min-width : 24px; min-height : 24px; }' );\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
+                       /* #3658: [IE6] Editor document has horizontal scrollbar on long lines\r
+                       To prevent this misbehavior, we show the scrollbar always */\r
+                       /* #6341: The text cursor must be set on the editor area. */\r
+                       /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/\r
+                       editor.addCss( 'html {  _overflow-y: scroll; cursor: text;      *cursor:auto;}' );\r
+                       // Use correct cursor for these elements\r
+                       editor.addCss( 'img, input, textarea { cursor: default;}' );\r
 \r
                        // Disable form elements editing mode provided by some browers. (#5746)\r
                        editor.on( 'insertElement', function ( evt )\r
@@ -1173,9 +1289,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
@@ -1236,14 +1355,12 @@ CKEDITOR.config.disableObjectResizing = false;
 CKEDITOR.config.disableNativeTableHandles = true;\r
 \r
 /**\r
- * Disables the built-in spell checker while typing natively available in the\r
- * browser (currently Firefox and Safari only).<br /><br />\r
+ * Disables the built-in words spell checker if browser provides one.<br /><br />\r
  *\r
- * Even if word suggestions will not appear in the CKEditor context menu, this\r
- * feature is useful to help quickly identifying misspelled words.<br /><br />\r
+ * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,\r
+ * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}\r
+ * is enabled or you're simply not using the context menu plugin.\r
  *\r
- * This setting is currently compatible with Firefox only due to limitations in\r
- * other browsers.\r
  * @type Boolean\r
  * @default true\r
  * @example\r
@@ -1268,6 +1385,18 @@ CKEDITOR.config.ignoreEmptyParagraph = true;
  */\r
 \r
 /**\r
+ * Whether automatically create wrapping blocks around inline contents inside document body,\r
+ * this helps to ensure the integrality of the block enter mode.\r
+ * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.\r
+ * @name CKEDITOR.config.autoParagraph\r
+ * @since 3.6\r
+ * @type Boolean\r
+ * @default true\r
+ * @example\r
+ * config.autoParagraph = false;\r
+ */\r
+\r
+/**\r
  * Fired when some elements are added to the document\r
  * @name CKEDITOR.editor#ariaWidget\r
  * @event\r