JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 225f2f3..6e4eacb 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -10,115 +10,359 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
 (function()\r
 {\r
-       /**\r
-        * List of elements in which has no way to move editing focus outside.\r
-        */\r
-       var nonExitableElementNames = { table:1,pre:1 };\r
        // Matching an empty paragraph at the end of document.\r
-       var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|&#160;)\s*(:?<\/\1>)?\s*$/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
-       function onInsertHtml( evt )\r
-       {\r
-               if ( this.mode == 'wysiwyg' )\r
-               {\r
-                       this.focus();\r
+       var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );\r
 \r
-                       var selection = this.getSelection(),\r
-                               data = evt.data;\r
+       // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)\r
+       function nonEditable( element )\r
+       {\r
+               return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];\r
+       }\r
 \r
-                       if ( this.dataProcessor )\r
-                               data = this.dataProcessor.toHtml( data );\r
 \r
-                       if ( CKEDITOR.env.ie )\r
+       function onInsert( insertFunc )\r
+       {\r
+               return function( evt )\r
+               {\r
+                       if ( this.mode == 'wysiwyg' )\r
                        {\r
-                               var selIsLocked = selection.isLocked;\r
+                               this.focus();\r
 \r
-                               if ( selIsLocked )\r
-                                       selection.unlock();\r
+                               this.fire( 'saveSnapshot' );\r
 \r
-                               var $sel = selection.getNative();\r
-                               if ( $sel.type == 'Control' )\r
-                                       $sel.clear();\r
-                               $sel.createRange().pasteHTML( data );\r
+                               insertFunc.call( this, evt.data );\r
 \r
-                               if ( selIsLocked )\r
-                                       this.getSelection().lock();\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
+                                  {\r
+                                          this.fire( 'saveSnapshot' );\r
+                                  }, 0, this );\r
                        }\r
-                       else\r
-                               this.document.$.execCommand( 'inserthtml', false, data );\r
-               }\r
+               };\r
        }\r
 \r
-       function onInsertElement( evt )\r
+       function doInsertHtml( data )\r
        {\r
-               if ( this.mode == 'wysiwyg' )\r
-               {\r
-                       this.focus();\r
-                       this.fire( 'saveSnapshot' );\r
+               if ( this.dataProcessor )\r
+                       data = this.dataProcessor.toHtml( data );\r
 \r
-                       var element = evt.data,\r
-                               elementName = element.getName(),\r
-                               isBlock = CKEDITOR.dtd.$block[ elementName ];\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
+\r
+               if ( range.checkReadOnly() )\r
+                       return;\r
 \r
-                       var selection = this.getSelection(),\r
-                               ranges = selection.getRanges();\r
+               // Opera: force block splitting when pasted content contains block. (#7801)\r
+               if ( CKEDITOR.env.opera )\r
+               {\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 selIsLocked = selection.isLocked;\r
 \r
                        if ( selIsLocked )\r
                                selection.unlock();\r
 \r
-                       var range, clone, lastElement, bookmark;\r
+                       var $sel = selection.getNative();\r
 \r
-                       for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
+                       // Delete control selections to avoid IE bugs on pasteHTML.\r
+                       if ( $sel.type == 'Control' )\r
+                               $sel.clear();\r
+                       else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )\r
                        {\r
-                               range = ranges[ i ];\r
+                               // Due to IE bugs on handling contenteditable=false blocks\r
+                               // (#6005), we need to make some checks and eventually\r
+                               // delete the selection first.\r
+\r
+                               range = selection.getRanges()[ 0 ];\r
+                               var endContainer = range && range.endContainer;\r
+\r
+                               if ( endContainer &&\r
+                                               endContainer.type == CKEDITOR.NODE_ELEMENT &&\r
+                                               endContainer.getAttribute( 'contenteditable' ) == 'false' &&\r
+                                               range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )\r
+                               {\r
+                                       range.setEndAfter( range.endContainer );\r
+                                       range.deleteContents();\r
+                               }\r
+                       }\r
 \r
-                               // Remove the original contents.\r
-                               range.deleteContents();\r
+                       try\r
+                       {\r
+                               $sel.createRange().pasteHTML( data );\r
+                       }\r
+                       catch (e) {}\r
 \r
-                               clone = !i && element || element.clone( true );\r
+                       if ( selIsLocked )\r
+                               this.getSelection().lock();\r
+               }\r
+               else\r
+                       this.document.$.execCommand( 'inserthtml', false, data );\r
 \r
-                               // If we're inserting a block at dtd-violated position, split\r
-                               // the parent blocks until we reach blockLimit.\r
-                               var parent, dtd;\r
-                               if ( this.config.enterMode != CKEDITOR.ENTER_BR && isBlock )\r
+               // Webkit does not scroll to the cursor position after pasting (#5558)\r
+               if ( CKEDITOR.env.webkit )\r
+               {\r
+                       selection = this.getSelection();\r
+                       selection.scrollIntoView();\r
+               }\r
+       }\r
+\r
+       function doInsertText( text )\r
+       {\r
+               var selection = this.getSelection(),\r
+                       mode = selection.getStartElement().hasAscendant( 'pre', true ) ?\r
+                                  CKEDITOR.ENTER_BR : this.config.enterMode,\r
+                       isEnterBrMode = mode == CKEDITOR.ENTER_BR;\r
+\r
+               var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );\r
+\r
+               // Convert leading and trailing whitespaces into &nbsp;\r
+               html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )\r
+                       {\r
+                               if ( match.length == 1 )        // one space, preserve it\r
+                                       return '&nbsp;';\r
+                               else if ( !offset )             // beginning of block\r
+                                       return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';\r
+                               else                            // end of block\r
+                                       return ' ' + CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 );\r
+                       } );\r
+\r
+               // Convert subsequent whitespaces into &nbsp;\r
+               html = html.replace( /[ \t]{2,}/g, function ( match )\r
+                  {\r
+                          return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';\r
+                  } );\r
+\r
+               var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';\r
+\r
+               // Two line-breaks create one paragraph.\r
+               if ( !isEnterBrMode )\r
+               {\r
+                       html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,\r
+                               function( match, group1, text )\r
                                {\r
-                                       while( ( parent = range.getCommonAncestor( false, true ) )\r
-                                                       && ( dtd = CKEDITOR.dtd[ parent.getName() ] )\r
-                                                       && !( dtd && dtd [ elementName ] ) )\r
+                                       return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';\r
+                               });\r
+               }\r
+\r
+               // One <br> per line-break.\r
+               html = html.replace( /\n/g, '<br>' );\r
+\r
+               // Compensate padding <br> for non-IE.\r
+               if ( !( isEnterBrMode || CKEDITOR.env.ie ) )\r
+               {\r
+                       html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )\r
+                       {\r
+                               return CKEDITOR.tools.repeat( match, 2 );\r
+                       } );\r
+               }\r
+\r
+               // Inline styles have to be inherited in Firefox.\r
+               if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )\r
+               {\r
+                       var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),\r
+                               context = [];\r
+\r
+                       for ( var i = 0; i < path.elements.length; i++ )\r
+                       {\r
+                               var tag = path.elements[ i ].getName();\r
+                               if ( tag in CKEDITOR.dtd.$inline )\r
+                                       context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );\r
+                               else if ( tag in CKEDITOR.dtd.$block )\r
+                                       break;\r
+                       }\r
+\r
+                       // Reproduce the context  by preceding the pasted HTML with opening inline tags.\r
+                       html = context.join( '' ) + html;\r
+               }\r
+\r
+               doInsertHtml.call( this, html );\r
+       }\r
+\r
+       function doInsertElement( element )\r
+       {\r
+               var selection = this.getSelection(),\r
+                               ranges = selection.getRanges(),\r
+                               elementName = element.getName(),\r
+                               isBlock = CKEDITOR.dtd.$block[ elementName ];\r
+\r
+               var selIsLocked = selection.isLocked;\r
+\r
+               if ( selIsLocked )\r
+                       selection.unlock();\r
+\r
+               var range, clone, lastElement, bookmark;\r
+\r
+               for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
+               {\r
+                       range = ranges[ i ];\r
+\r
+                               if ( !range.checkReadOnly() )\r
+                               {\r
+                                       // Remove the original contents, merge splitted nodes.\r
+                                       range.deleteContents( 1 );\r
+\r
+                                       clone = !i && element || element.clone( 1 );\r
+\r
+                                       // If we're inserting a block at dtd-violated position, split\r
+                                       // the parent blocks until we reach blockLimit.\r
+                                       var current, dtd;\r
+                                       if ( isBlock )\r
                                        {\r
-                                               range.splitBlock();\r
+                                               while ( ( current = range.getCommonAncestor( 0, 1 ) )\r
+                                                               && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
+                                                               && !( dtd && dtd [ elementName ] ) )\r
+                                               {\r
+                                                       // Split up inline elements.\r
+                                                       if ( current.getName() in CKEDITOR.dtd.span )\r
+                                                               range.splitElement( current );\r
+                                                       // If we're in an empty block which indicate a new paragraph,\r
+                                                       // simply replace it with the inserting block.(#3664)\r
+                                                       else if ( range.checkStartOfBlock()\r
+                                                                       && range.checkEndOfBlock() )\r
+                                                       {\r
+                                                               range.setStartBefore( current );\r
+                                                               range.collapse( true );\r
+                                                               current.remove();\r
+                                                       }\r
+                                                       else\r
+                                                               range.splitBlock();\r
+                                               }\r
                                        }\r
-                               }\r
 \r
-                               // Insert the new node.\r
-                               range.insertNode( clone );\r
+                                       // Insert the new node.\r
+                                       range.insertNode( clone );\r
 \r
-                               // Save the last element reference so we can make the\r
-                               // selection later.\r
-                               if ( !lastElement )\r
-                                       lastElement = clone;\r
+                                       // Save the last element reference so we can make the\r
+                                       // selection later.\r
+                                       if ( !lastElement )\r
+                                               lastElement = clone;\r
+                               }\r
                        }\r
 \r
-                       range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
+                       if ( lastElement )\r
+                       {\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 ( isBlock )\r
+                               {\r
+                                       var next = lastElement.getNext( notWhitespaceEval ),\r
+                                               nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();\r
 \r
-                       var next = lastElement.getNextSourceNode( true );\r
-                       if ( next && next.type == CKEDITOR.NODE_ELEMENT )\r
-                               range.moveToElementEditStart( next );\r
+                                       // Check if it's a block element that accepts text.\r
+                                       if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )\r
+                                               range.moveToElementEditStart( next );\r
+                               }\r
+                       }\r
 \r
                        selection.selectRanges( [ range ] );\r
 \r
-                       if ( selIsLocked )\r
-                               this.getSelection().lock();\r
+               if ( selIsLocked )\r
+                       this.getSelection().lock();\r
+       }\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
-                       // call.\r
-                       CKEDITOR.tools.setTimeout( function(){\r
-                               this.fire( 'saveSnapshot' );\r
-                       }, 0, this );\r
+       // DOM modification here should not bother dirty flag.(#4385)\r
+       function restoreDirty( editor )\r
+       {\r
+               if ( !editor.checkDirty() )\r
+                       setTimeout( function(){ editor.resetDirty(); }, 0 );\r
+       }\r
+\r
+       var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),\r
+               isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );\r
+\r
+       function isNotEmpty( node )\r
+       {\r
+               return isNotWhitespace( node ) && isNotBookmark( node );\r
+       }\r
+\r
+       function isNbsp( node )\r
+       {\r
+               return node.type == CKEDITOR.NODE_TEXT\r
+                          && CKEDITOR.tools.trim( node.getText() ).match( /^(?:&nbsp;|\xa0)$/ );\r
+       }\r
+\r
+       function restoreSelection( selection )\r
+       {\r
+               if ( selection.isLocked )\r
+               {\r
+                       selection.unlock();\r
+                       setTimeout( function() { selection.lock(); }, 0 );\r
+               }\r
+       }\r
+\r
+       function isBlankParagraph( block )\r
+       {\r
+               return block.getOuterHtml().match( emptyParagraphRegexp );\r
+       }\r
+\r
+       isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );\r
+\r
+       // Gecko need a key event to 'wake up' the editing\r
+       // ability when document is empty.(#3864, #5781)\r
+       function activateEditing( editor )\r
+       {\r
+               var win = editor.window,\r
+                       doc = editor.document,\r
+                       body = editor.document.getBody(),\r
+                       bodyFirstChild = body.getFirst(),\r
+                       bodyChildsNum = body.getChildren().count();\r
+\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
+                       // Memorize scroll position to restore it later (#4472).\r
+                       var hostDocument = editor.element.getDocument();\r
+                       var hostDocumentElement = hostDocument.getDocumentElement();\r
+                       var scrollTop = hostDocumentElement.$.scrollTop;\r
+                       var scrollLeft = hostDocumentElement.$.scrollLeft;\r
+\r
+                       // Simulating keyboard character input by dispatching a keydown of white-space text.\r
+                       var keyEventSimulate = doc.$.createEvent( "KeyEvents" );\r
+                       keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,\r
+                               false, false, false, 0, 32 );\r
+                       doc.$.dispatchEvent( keyEventSimulate );\r
+\r
+                       if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )\r
+                               hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );\r
+\r
+                       // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
+                       bodyChildsNum && body.getFirst().remove();\r
+                       doc.getBody().appendBogus();\r
+                       var nativeRange = new CKEDITOR.dom.range( doc );\r
+                       nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );\r
+                       nativeRange.select();\r
                }\r
        }\r
 \r
@@ -136,67 +380,102 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        body = editor.document.getBody(),\r
                        enterMode = editor.config.enterMode;\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
+               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
+                               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 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
-                       var bms = selection.createBookmarks(),\r
-                               fixedBlock = range.fixBlock( true,\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
-                       // For IE, we'll be removing any bogus br ( introduce by fixing body )\r
-                       // right now to prevent it introducing visual line break.\r
+                       // For IE, we should remove any filler node which was introduced before.\r
                        if ( CKEDITOR.env.ie )\r
                        {\r
-                               var brNodeList = fixedBlock.getElementsByTag( 'br' ), brNode;\r
-                               for ( var i = 0 ; i < brNodeList.count() ; i++ )\r
-                               {\r
-                                       if( ( brNode = brNodeList.getItem( i ) ) && brNode.hasAttribute( '_cke_bogus' ) )\r
-                                               brNode.remove();\r
-                               }\r
+                               var first = fixedBlock.getFirst( isNotEmpty );\r
+                               first && isNbsp( first ) && first.remove();\r
                        }\r
 \r
-                       selection.selectBookmarks( bms );\r
-\r
-                       // If the fixed block is blank and is already followed by a exitable\r
-                       // block, we should drop it and move to the exist block(#3684).\r
-                       var children = fixedBlock.getChildren(),\r
-                               count = children.count(),\r
-                               firstChild,\r
-                               whitespaceGuard = CKEDITOR.dom.walker.whitespaces( true ),\r
-                               previousElement = fixedBlock.getPrevious( whitespaceGuard ),\r
-                               nextElement = fixedBlock.getNext( whitespaceGuard ),\r
-                               enterBlock;\r
-                       if ( previousElement && previousElement.getName\r
-                                && !( previousElement.getName() in nonExitableElementNames ) )\r
-                               enterBlock = previousElement;\r
-                       else if ( nextElement && nextElement.getName\r
-                                         && !( nextElement.getName() in nonExitableElementNames ) )\r
-                               enterBlock = nextElement;\r
-\r
-                       // Not all blocks are editable, e.g. <hr />, further checking it.(#3994)\r
-                       if( ( !count\r
-                                 || ( firstChild = children.getItem( 0 ) ) && firstChild.is && firstChild.is( 'br' ) )\r
-                               && enterBlock\r
-                               && range.moveToElementEditStart( enterBlock ) )\r
+                       // If the fixed block is actually blank and is already followed by an exitable blank\r
+                       // block, we should revert the fix and move into the existed one. (#3684)\r
+                       if ( isBlankParagraph( fixedBlock ) )\r
                        {\r
-                               fixedBlock.remove();\r
-                               range.select();\r
+                               var element = fixedBlock.getNext( isNotWhitespace );\r
+                               if ( element &&\r
+                                        element.type == CKEDITOR.NODE_ELEMENT &&\r
+                                        !nonEditable( element ) )\r
+                               {\r
+                                       range.moveToElementEditStart( element );\r
+                                       fixedBlock.remove();\r
+                               }\r
+                               else\r
+                               {\r
+                                       element = fixedBlock.getPrevious( isNotWhitespace );\r
+                                       if ( element &&\r
+                                                element.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                !nonEditable( element ) )\r
+                                       {\r
+                                               range.moveToElementEditEnd( element );\r
+                                               fixedBlock.remove();\r
+                                       }\r
+                               }\r
                        }\r
+\r
+                       range.select();\r
+                       // Cancel this selection change in favor of the next (correct).  (#6811)\r
+                       evt.cancel();\r
                }\r
 \r
-               // Inserting the padding-br before body if it's preceded by an\r
-               // unexitable block.\r
-               var lastNode = body.getLast( CKEDITOR.dom.walker.whitespaces( true ) );\r
-               if ( lastNode && lastNode.getName && ( lastNode.getName() in nonExitableElementNames ) )\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
-                       var paddingBlock = editor.document.createElement(\r
-                                       ( CKEDITOR.env.ie && enterMode != CKEDITOR.ENTER_BR ) ?\r
-                                               '<br _cke_bogus="true" />' : 'br' );\r
-                       body.append( paddingBlock );\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
+                       else\r
+                               paddingBlock = body;\r
+\r
+                       if ( !CKEDITOR.env.ie )\r
+                               paddingBlock.appendBogus();\r
                }\r
        }\r
 \r
@@ -206,141 +485,112 @@ 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
+\r
+                       var contentDomReadyHandler;\r
                        editor.on( 'editingBlockReady', function()\r
                                {\r
                                        var mainElement,\r
-                                               fieldset,\r
                                                iframe,\r
                                                isLoadingData,\r
                                                isPendingFocus,\r
                                                frameLoaded,\r
                                                fireMode;\r
 \r
+\r
                                        // Support for custom document.domain in IE.\r
                                        var isCustomDomain = CKEDITOR.env.isCustomDomain();\r
 \r
                                        // Creates the iframe that holds the editable document.\r
-                                       var createIFrame = function()\r
+                                       var createIFrame = function( data )\r
                                        {\r
                                                if ( iframe )\r
                                                        iframe.remove();\r
-                                               if ( fieldset )\r
-                                                       fieldset.remove();\r
 \r
-                                               frameLoaded = 0;\r
-                                               // The document domain must be set within the src\r
-                                               // attribute;\r
-                                               // Defer the script execution until iframe\r
-                                               // has been added to main window, this is needed for some\r
-                                               // browsers which will begin to load the frame content\r
-                                               // prior to it's presentation in DOM.(#3894)\r
-                                               var src = 'void( '\r
-                                                               + ( CKEDITOR.env.gecko ? 'setTimeout' : '' ) + '( function(){' +\r
-                                                               'document.open();' +\r
-                                                               ( CKEDITOR.env.ie && isCustomDomain ? 'document.domain="' + document.domain + '";' : '' ) +\r
-                                                               'document.write( window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] );' +\r
-                                                               'document.close();' +\r
-                                                               'window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] = null;' +\r
-                                                               '}'\r
-                                                               + ( CKEDITOR.env.gecko ? ', 0 )' : ')()' )\r
-                                                               + ' )';\r
-\r
-                                               // Loading via src attribute does not work in Opera.\r
-                                               if ( CKEDITOR.env.opera )\r
-                                                       src = 'void(0);';\r
+                                               var src =\r
+                                                       'document.open();' +\r
 \r
-                                               iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
-                                                               ' style="width:100%;height:100%"' +\r
-                                                               ' frameBorder="0"' +\r
-                                                               ' tabIndex="-1"' +\r
-                                                               ' allowTransparency="true"' +\r
-                                                               ' src="javascript:' + encodeURIComponent( src ) + '"' +\r
-                                                               '></iframe>' );\r
+                                                       // The document domain must be set any time we\r
+                                                       // call document.open().\r
+                                                       ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
 \r
-                                               var accTitle = editor.lang.editorTitle.replace( '%1', editor.name );\r
+                                                       'document.close();';\r
 \r
-                                               if ( CKEDITOR.env.gecko )\r
-                                               {\r
-                                                       // Double checking the iframe will be loaded properly(#4058).\r
-                                                       iframe.on( 'load', function( ev )\r
+                                               // With IE, the custom domain has to be taken care at first,\r
+                                               // for other browers, the 'src' attribute should be left empty to\r
+                                               // trigger iframe's 'load' event.\r
+                                               src =\r
+                                                       CKEDITOR.env.air ?\r
+                                                               'javascript:void(0)' :\r
+                                                       CKEDITOR.env.ie ?\r
+                                                               'javascript:void(function(){' + encodeURIComponent( src ) + '}())'\r
+                                                       :\r
+                                                               '';\r
+\r
+                                               iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
+                                                       ' style="width:100%;height:100%"' +\r
+                                                       ' frameBorder="0"' +\r
+                                                       ' title="' + frameLabel + '"' +\r
+                                                       ' src="' + src + '"' +\r
+                                                       ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +\r
+                                                       ' allowTransparency="true"' +\r
+                                                       '></iframe>' );\r
+\r
+                                               // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)\r
+                                               if ( document.location.protocol == 'chrome:' )\r
+                                                       CKEDITOR.event.useCapture = true;\r
+\r
+                                               // With FF, it's better to load the data on iframe.load. (#3894,#4058)\r
+                                               iframe.on( 'load', function( ev )\r
                                                        {\r
+                                                               frameLoaded = 1;\r
                                                                ev.removeListener();\r
-                                                               contentDomReady( iframe.$.contentWindow );\r
-                                                       } );\r
 \r
-                                                       // Accessibility attributes for Firefox.\r
-                                                       mainElement.setAttributes(\r
-                                                               {\r
-                                                                       role : 'region',\r
-                                                                       title : accTitle\r
-                                                               } );\r
-                                                       iframe.setAttributes(\r
-                                                               {\r
-                                                                       role : 'region',\r
-                                                                       title : ' '\r
-                                                               } );\r
-                                               }\r
-                                               else if ( CKEDITOR.env.webkit )\r
-                                               {\r
-                                                       iframe.setAttribute( 'title', accTitle );       // Safari 4\r
-                                                       iframe.setAttribute( 'name', accTitle );        // Safari 3\r
-                                               }\r
-                                               else if ( CKEDITOR.env.ie )\r
-                                               {\r
-                                                       // Accessibility label for IE.\r
-                                                       fieldset = CKEDITOR.dom.element.createFromHtml(\r
-                                                               '<fieldset style="height:100%' +\r
-                                                               ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? ';position:relative' : '' ) +\r
-                                                               '">' +\r
-                                                                       '<legend style="display:block;width:0;height:0;overflow:hidden;' +\r
-                                                                       ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 'position:absolute' : '' ) +\r
-                                                                       '">' +\r
-                                                                               CKEDITOR.tools.htmlEncode( accTitle ) +\r
-                                                                       '</legend>' +\r
-                                                               '</fieldset>'\r
-                                                               , CKEDITOR.document );\r
-                                                       iframe.appendTo( fieldset );\r
-                                                       fieldset.appendTo( mainElement );\r
-                                               }\r
+                                                               var doc = iframe.getFrameDocument();\r
+                                                               doc.write( data );\r
+\r
+                                                               CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );\r
+                                                       });\r
 \r
-                                               if ( !CKEDITOR.env.ie )\r
-                                                       mainElement.append( iframe );\r
+                                               // Reset adjustment back to default (#5689)\r
+                                               if ( document.location.protocol == 'chrome:' )\r
+                                                       CKEDITOR.event.useCapture = false;\r
+\r
+                                               mainElement.append( iframe );\r
                                        };\r
 \r
-                                       // The script that is appended to the data being loaded. It\r
-                                       // enables editing, and makes some\r
+                                       // The script that launches the bootstrap logic on 'domReady', so the document\r
+                                       // is fully editable even before the editing iframe is fully loaded (#4455).\r
+                                       contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );\r
                                        var activationScript =\r
-                                               '<script id="cke_actscrpt" type="text/javascript">' +\r
-                                                       'window.onload = function()' +\r
-                                                       '{' +\r
-                                                               // Call the temporary function for the editing\r
-                                                               // boostrap.\r
-                                                               'window.parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +\r
-                                                       '}' +\r
+                                               '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +\r
+                                                       ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
+                                                       'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +\r
                                                '</script>';\r
 \r
                                        // Editing area bootstrap code.\r
-                                       var contentDomReady = function( domWindow )\r
+                                       function contentDomReady( domWindow )\r
                                        {\r
-                                               if ( frameLoaded )\r
+                                               if ( !frameLoaded )\r
                                                        return;\r
+                                               frameLoaded = 0;\r
 \r
-                                               frameLoaded = 1;\r
+                                               editor.fire( 'ariaWidget', iframe );\r
 \r
                                                var domDocument = domWindow.document,\r
                                                        body = domDocument.body;\r
 \r
                                                // Remove this script from the DOM.\r
                                                var script = domDocument.getElementById( "cke_actscrpt" );\r
-                                               script.parentNode.removeChild( script );\r
-\r
-                                               delete CKEDITOR._[ 'contentDomReady' + editor.name ];\r
+                                               script && script.parentNode.removeChild( script );\r
 \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
@@ -349,42 +599,53 @@ 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
-                                                       domDocument.designMode = 'on';\r
+                                               {\r
+                                                       // Avoid opening design mode in a frame window thread,\r
+                                                       // which will cause host page scrolling.(#4397)\r
+                                                       setTimeout( function()\r
+                                                       {\r
+                                                               // Prefer 'contentEditable' instead of 'designMode'. (#3593)\r
+                                                               if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900\r
+                                                                               || CKEDITOR.env.opera )\r
+                                                                       domDocument.$.body.contentEditable = editable;\r
+                                                               else if ( CKEDITOR.env.webkit )\r
+                                                                       domDocument.$.body.parentNode.contentEditable = editable;\r
+                                                               else\r
+                                                                       domDocument.$.designMode = editable? 'off' : 'on';\r
+                                                       }, 0 );\r
+                                               }\r
 \r
-                                               // IE, Opera and Safari may not support it and throw\r
-                                               // errors.\r
-                                               try { domDocument.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}\r
-                                               try { domDocument.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}\r
+                                               editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
 \r
-                                               domWindow       = editor.window         = new CKEDITOR.dom.window( domWindow );\r
+                                               domWindow       = editor.window = new CKEDITOR.dom.window( domWindow );\r
                                                domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
 \r
-                                               // Gecko need a key event to 'wake up' the editing\r
-                                               // ability when document is empty.(#3864)\r
-                                               var firstNode = domDocument.getBody().getFirst();\r
-                                               if ( CKEDITOR.env.gecko\r
-                                                       && firstNode && firstNode.is\r
-                                                       && firstNode.is( 'br' ) && firstNode.hasAttribute( '_moz_editor_bogus_node' ) )\r
+                                               editable && domDocument.on( 'dblclick', function( evt )\r
                                                {\r
-                                                       var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );\r
-                                                       keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,\r
-                                                               false, false, false, 0, 32 );\r
-                                                       domDocument.$.dispatchEvent( keyEventSimulate );\r
-                                                       var bogusText = domDocument.getBody().getFirst() ;\r
-                                                       // Compensate the line maintaining <br> if enterMode is not block.\r
-                                                       if ( editor.config.enterMode == CKEDITOR.ENTER_BR )\r
-                                                               domDocument.createElement( 'br', { attributes: { '_moz_dirty' : "" } } )\r
-                                                                       .replace( bogusText );\r
-                                                       else\r
-                                                               bogusText.remove();\r
-                                               }\r
+                                                       var element = evt.data.getTarget(),\r
+                                                               data = { element : element, dialog : '' };\r
+                                                       editor.fire( 'doubleclick', data );\r
+                                                       data.dialog && editor.openDialog( data.dialog );\r
+                                               });\r
+\r
+                                               // Prevent automatic submission in IE #6336\r
+                                               CKEDITOR.env.ie && domDocument.on( 'click', function( evt )\r
+                                               {\r
+                                                       var element = evt.data.getTarget();\r
+                                                       if ( element.is( 'input' ) )\r
+                                                       {\r
+                                                               var type = element.getAttribute( 'type' );\r
+                                                               if ( type == 'submit' || type == 'reset' )\r
+                                                                       evt.data.preventDefault();\r
+                                                       }\r
+                                               });\r
 \r
                                                // Gecko/Webkit need some help when selecting control type elements. (#3448)\r
-                                               if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )\r
+                                               if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )\r
                                                {\r
                                                        domDocument.on( 'mousedown', function( ev )\r
                                                        {\r
@@ -394,9 +655,39 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        } );\r
                                                }\r
 \r
+                                               if ( CKEDITOR.env.gecko )\r
+                                               {\r
+                                                       domDocument.on( 'mouseup', function( ev )\r
+                                                       {\r
+                                                               if ( ev.data.$.button == 2 )\r
+                                                               {\r
+                                                                       var target = ev.data.getTarget();\r
+\r
+                                                                       // Prevent right click from selecting an empty block even\r
+                                                                       // when selection is anchored inside it. (#5845)\r
+                                                                       if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )\r
+                                                                       {\r
+                                                                               var range = new CKEDITOR.dom.range( domDocument );\r
+                                                                               range.moveToElementEditStart( target );\r
+                                                                               range.select( true );\r
+                                                                       }\r
+                                                               }\r
+                                                       } );\r
+                                               }\r
+\r
+                                               // Prevent the browser opening links in read-only blocks. (#6032)\r
+                                               domDocument.on( 'click', function( ev )\r
+                                                       {\r
+                                                               ev = ev.data;\r
+                                                               if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )\r
+                                                                       ev.preventDefault();\r
+                                                       });\r
+\r
                                                // Webkit: avoid from editing form control elements content.\r
                                                if ( CKEDITOR.env.webkit )\r
                                                {\r
+                                                       // 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
@@ -412,26 +703,141 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        } );\r
                                                }\r
 \r
-                                               var focusTarget = ( CKEDITOR.env.ie || CKEDITOR.env.safari ) ?\r
-                                                               domWindow : domDocument;\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
                                                                editor.focusManager.blur();\r
                                                        });\r
 \r
+                                               var wasFocused;\r
+\r
                                                focusTarget.on( 'focus', function()\r
                                                        {\r
+                                                               var doc = editor.document;\r
+\r
+                                                               if ( editable && CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
+                                                                       blinkCursor();\r
+                                                               else if ( 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
+                                                               {\r
+                                                                       if ( !wasFocused )\r
+                                                                       {\r
+                                                                               editor.document.getDocumentElement().focus();\r
+                                                                               wasFocused = 1;\r
+                                                                       }\r
+                                                               }\r
+\r
                                                                editor.focusManager.focus();\r
                                                        });\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
+                                               {\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
+                                                       {\r
+                                                               var keyCode = evt.data.getKeystroke();\r
+\r
+                                                               // Backspace OR Delete.\r
+                                                               if ( keyCode in { 8 : 1, 46 : 1 } )\r
+                                                               {\r
+                                                                       var sel = editor.getSelection(),\r
+                                                                               control = sel.getSelectedElement();\r
+\r
+                                                                       if ( control )\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
+\r
+                                                                               editor.fire( 'saveSnapshot' );\r
+\r
+                                                                               evt.data.preventDefault();\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
+                                                       {\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
+                                                       editor.config.enterMode != CKEDITOR.ENTER_P\r
+                                                               && domDocument.on( 'selectionchange', function()\r
+                                                               {\r
+                                                                       var body = domDocument.getBody(),\r
+                                                                               range = editor.getSelection().getRanges()[ 0 ];\r
+\r
+                                                                       if ( 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
                                                if ( editor.contextMenu )\r
-                                                       editor.contextMenu.addTarget( domDocument );\r
+                                                       editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );\r
 \r
                                                setTimeout( function()\r
                                                        {\r
@@ -451,6 +857,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        editor.focus();\r
                                                                        isPendingFocus = false;\r
                                                                }\r
+                                                               setTimeout( function()\r
+                                                               {\r
+                                                                       editor.fire( 'dataReady' );\r
+                                                               }, 0 );\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
+                                                               {\r
+                                                                       try\r
+                                                                       {\r
+                                                                               editor.document.$.execCommand( 'enableObjectResizing', false, false );\r
+                                                                       }\r
+                                                                       catch(e)\r
+                                                                       {\r
+                                                                               // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)\r
+                                                                               editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )\r
+                                                                               {\r
+                                                                                       evt.data.preventDefault();\r
+                                                                               });\r
+                                                                       }\r
+                                                               }\r
 \r
                                                                /*\r
                                                                 * IE BUG: IE might have rendered the iframe with invisible contents.\r
@@ -474,7 +902,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                }\r
                                                        },\r
                                                        0 );\r
-                                       };\r
+                                       }\r
 \r
                                        editor.addMode( 'wysiwyg',\r
                                                {\r
@@ -500,52 +928,129 @@ 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
+                                                                       docType = config.docType;\r
+\r
+                                                               // Build the additional stuff to be included into <head>.\r
+                                                               var headExtra =\r
+                                                                       '<style type="text/css" data-cke-temp="1">' +\r
+                                                                               editor._.styles.join( '\n' ) +\r
+                                                                       '</style>';\r
+\r
+                                                               !fullPage && ( headExtra =\r
+                                                                       CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +\r
+                                                                       headExtra );\r
+\r
+                                                               var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';\r
+\r
+                                                               if ( fullPage )\r
+                                                               {\r
+                                                                       // Search and sweep out the doctype declaration.\r
+                                                                       data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )\r
+                                                                               {\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
                                                                // Get the HTML version of the data.\r
                                                                if ( editor.dataProcessor )\r
-                                                               {\r
                                                                        data = editor.dataProcessor.toHtml( data, fixForBody );\r
-                                                               }\r
 \r
-                                                               data =\r
-                                                                       editor.config.docType +\r
-                                                                       '<html dir="' + editor.config.contentsLangDirection + '">' +\r
-                                                                       '<head>' +\r
-                                                                               '<link href="' + editor.config.contentsCss + '" type="text/css" rel="stylesheet" _fcktemp="true"/>' +\r
-                                                                               '<style type="text/css" _fcktemp="true">' +\r
-                                                                                       editor._.styles.join( '\n' ) +\r
-                                                                               '</style>'+\r
-                                                                       '</head>' +\r
-                                                                       '<body>' +\r
-                                                                               data +\r
-                                                                       '</body>' +\r
-                                                                       '</html>' +\r
-                                                                       activationScript;\r
-\r
-                                                               window[ '_cke_htmlToLoad_' + editor.name ] = data;\r
-                                                               CKEDITOR._[ 'contentDomReady' + editor.name ] = contentDomReady;\r
-                                                               createIFrame();\r
-\r
-                                                               // Opera must use the old method for loading contents.\r
-                                                               if ( CKEDITOR.env.opera )\r
+                                                               if ( fullPage )\r
+                                                               {\r
+                                                                       // Check if the <body> tag is available.\r
+                                                                       if ( !(/<body[\s|>]/).test( data ) )\r
+                                                                               data = '<body>' + data;\r
+\r
+                                                                       // Check if the <html> tag is available.\r
+                                                                       if ( !(/<html[\s|>]/).test( data ) )\r
+                                                                               data = '<html>' + data + '</html>';\r
+\r
+                                                                       // Check if the <head> tag is available.\r
+                                                                       if ( !(/<head[\s|>]/).test( data ) )\r
+                                                                               data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;\r
+                                                                       else if ( !(/<title[\s|>]/).test( data ) )\r
+                                                                               data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;\r
+\r
+                                                                       // The base must be the first tag in the HEAD, e.g. to get relative\r
+                                                                       // links on styles.\r
+                                                                       baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );\r
+\r
+                                                                       // Inject the extra stuff into <head>.\r
+                                                                       // Attention: do not change it before testing it well. (V2)\r
+                                                                       // This is tricky... if the head ends with <meta ... content type>,\r
+                                                                       // Firefox will break. But, it works if we place our extra stuff as\r
+                                                                       // the last elements in the HEAD.\r
+                                                                       data = data.replace( /<\/head\s*>/, headExtra + '$&' );\r
+\r
+                                                                       // Add the DOCTYPE back to it.\r
+                                                                       data = docType + data;\r
+                                                               }\r
+                                                               else\r
                                                                {\r
-                                                                       var doc = iframe.$.contentWindow.document;\r
-                                                                       doc.open();\r
-                                                                       doc.write( data );\r
-                                                                       doc.close();\r
+                                                                       data =\r
+                                                                               config.docType +\r
+                                                                               '<html dir="' + config.contentsLangDirection + '"' +\r
+                                                                                       ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +\r
+                                                                               '<head>' +\r
+                                                                                       '<title>' + frameLabel + '</title>' +\r
+                                                                                       baseTag +\r
+                                                                                       headExtra +\r
+                                                                               '</head>' +\r
+                                                                               '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +\r
+                                                                                                 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +\r
+                                                                                                 '>' +\r
+                                                                                       data +\r
+                                                                               '</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
+                                                               // The iframe is recreated on each call of setData, so we need to clear DOM objects\r
+                                                               this.onDispose();\r
+                                                               createIFrame( data );\r
                                                        },\r
 \r
                                                        getData : function()\r
                                                        {\r
-                                                               var data = iframe.getFrameDocument().getBody().getHtml();\r
+                                                               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
-                                                               // Strip the last blank paragraph within document.\r
-                                                               if ( editor.config.ignoreEmptyParagraph )\r
-                                                                       data = data.replace( emptyParagraphRegexp, '' );\r
+                                                               // Reset empty if the document contains only one empty paragraph.\r
+                                                               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
                                                                return data;\r
                                                        },\r
@@ -560,8 +1065,31 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                iframe.getFrameDocument().getBody().setHtml( data );\r
                                                        },\r
 \r
+                                                       onDispose : function()\r
+                                                       {\r
+                                                               if ( !editor.document )\r
+                                                                       return;\r
+\r
+                                                               editor.document.getDocumentElement().clearCustomData();\r
+                                                               editor.document.getBody().clearCustomData();\r
+\r
+                                                               editor.window.clearCustomData();\r
+                                                               editor.document.clearCustomData();\r
+\r
+                                                               iframe.clearCustomData();\r
+\r
+                                                               /*\r
+                                                               * IE BUG: When destroying editor DOM with the selection remains inside\r
+                                                               * editing area would break IE7/8's selection system, we have to put the editing\r
+                                                               * iframe offline first. (#3812 and #5441)\r
+                                                               */\r
+                                                               iframe.remove();\r
+                                                       },\r
+\r
                                                        unload : function( holderElement )\r
                                                        {\r
+                                                               this.onDispose();\r
+\r
                                                                editor.window = editor.document = iframe = mainElement = isPendingFocus = null;\r
 \r
                                                                editor.fire( 'contentDomUnload' );\r
@@ -569,23 +1097,187 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                        focus : function()\r
                                                        {\r
+                                                               var win = editor.window;\r
+\r
                                                                if ( isLoadingData )\r
                                                                        isPendingFocus = true;\r
-                                                               else if ( editor.window )\r
+                                                               // Temporary solution caused by #6025, supposed be unified by #6154.\r
+                                                               else if ( CKEDITOR.env.opera && editor.document )\r
                                                                {\r
-                                                                       editor.window.focus();\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
+                                                               {\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
                                                                }\r
                                                        }\r
                                                });\r
 \r
-                                       editor.on( 'insertHtml', onInsertHtml, null, null, 20 );\r
-                                       editor.on( 'insertElement', onInsertElement, null, null, 20 );\r
+                                       editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );\r
+                                       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
                                });\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
+\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
+                       // IE>=8 stricts mode doesn't have 'contentEditable' in effect\r
+                       // on element unless it has layout. (#5562)\r
+                       if ( CKEDITOR.document.$.documentMode >= 8 )\r
+                       {\r
+                               editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );\r
+\r
+                               var selectors = [];\r
+                               for ( var tag in CKEDITOR.dtd.$removeEmpty )\r
+                                       selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );\r
+                               editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );\r
+                       }\r
+                       // Set the HTML style to 100% to have the text cursor in affect (#6341)\r
+                       else if ( CKEDITOR.env.gecko )\r
+                       {\r
+                               editor.addCss( 'html { height: 100% !important; }' );\r
+                               editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1;     width : 24px; height : 24px; }' );\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
+                       // Switch on design mode for a short while and close it after then.\r
+                       function blinkCursor( retry )\r
+                       {\r
+                               if ( editor.readOnly )\r
+                                       return;\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
+                       }\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
+                               var element = evt.data;\r
+                               if ( element.type == CKEDITOR.NODE_ELEMENT\r
+                                               && ( element.is( 'input' ) || element.is( 'textarea' ) ) )\r
+                               {\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
+                                               element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );\r
+                                       element.setAttribute( 'contentEditable', false );\r
+                               }\r
+                       });\r
+\r
                }\r
        });\r
+\r
+       // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)\r
+       if ( CKEDITOR.env.gecko )\r
+       {\r
+               (function()\r
+               {\r
+                       var body = document.body;\r
+\r
+                       if ( !body )\r
+                               window.addEventListener( 'load', arguments.callee, false );\r
+                       else\r
+                       {\r
+                               var currentHandler = body.getAttribute( 'onpageshow' );\r
+                               body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +\r
+                                                       'event.persisted && (function(){' +\r
+                                                               'var allInstances = CKEDITOR.instances, editor, doc;' +\r
+                                                               'for ( var i in allInstances )' +\r
+                                                               '{' +\r
+                                                               '       editor = allInstances[ i ];' +\r
+                                                               '       doc = editor.document;' +\r
+                                                               '       if ( doc )' +\r
+                                                               '       {' +\r
+                                                               '               doc.$.designMode = "off";' +\r
+                                                               '               doc.$.designMode = "on";' +\r
+                                                               '       }' +\r
+                                                               '}' +\r
+                                               '})();' );\r
+                       }\r
+               } )();\r
+\r
+       }\r
 })();\r
 \r
 /**\r
@@ -610,14 +1302,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
@@ -634,3 +1324,28 @@ CKEDITOR.config.disableNativeSpellChecker = true;
  * config.ignoreEmptyParagraph = false;\r
  */\r
 CKEDITOR.config.ignoreEmptyParagraph = true;\r
+\r
+/**\r
+ * Fired when data is loaded and ready for retrieval in an editor instance.\r
+ * @name CKEDITOR.editor#dataReady\r
+ * @event\r
+ */\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
+ * @param {Object} element The element being added\r
+ */\r