JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 11fd76e..0a35305 100644 (file)
@@ -14,17 +14,30 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        var nonExitableElementNames = { table:1,pre:1 };\r
 \r
        // Matching an empty paragraph at the end of document.\r
-       var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
+       var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center|li)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
+\r
+       var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );\r
+\r
+       function checkReadOnly( selection )\r
+       {\r
+               if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT )\r
+                       return selection.getSelectedElement().isReadOnly();\r
+               else\r
+                       return selection.getCommonAncestor().isReadOnly();\r
+       }\r
 \r
        function onInsertHtml( evt )\r
        {\r
                if ( this.mode == 'wysiwyg' )\r
                {\r
                        this.focus();\r
-                       this.fire( 'saveSnapshot' );\r
 \r
-                       var selection = this.getSelection(),\r
-                               data = evt.data;\r
+                       var selection = this.getSelection();\r
+                       if ( checkReadOnly( selection ) )\r
+                               return;\r
+\r
+                       var data = evt.data;\r
+                       this.fire( 'saveSnapshot' );\r
 \r
                        if ( this.dataProcessor )\r
                                data = this.dataProcessor.toHtml( data );\r
@@ -37,8 +50,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        selection.unlock();\r
 \r
                                var $sel = selection.getNative();\r
+\r
+                               // Delete control selections to avoid IE bugs on pasteHTML.\r
                                if ( $sel.type == 'Control' )\r
                                        $sel.clear();\r
+                               else if  ( selection.getType() == CKEDITOR.SELECTION_TEXT )\r
+                               {\r
+                                       // Due to IE bugs on handling contenteditable=false blocks\r
+                                       // (#6005), we need to make some checks and eventually\r
+                                       // delete the selection first.\r
+\r
+                                       var range = selection.getRanges()[0],\r
+                                               endContainer = range && range.endContainer;\r
+\r
+                                       if ( endContainer &&\r
+                                                endContainer.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                endContainer.getAttribute( 'contenteditable' ) == 'false' &&\r
+                                                range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )\r
+                                       {\r
+                                               range.setEndAfter( range.endContainer );\r
+                                               range.deleteContents();\r
+                                       }\r
+                               }\r
+\r
                                $sel.createRange().pasteHTML( data );\r
 \r
                                if ( selIsLocked )\r
@@ -47,6 +81,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        else\r
                                this.document.$.execCommand( 'inserthtml', false, data );\r
 \r
+                       // Webkit does not scroll to the cursor position after pasting (#5558)\r
+                       if ( CKEDITOR.env.webkit )\r
+                       {\r
+                               this.document.$.execCommand( 'inserthtml', false, '<span id="cke_paste_marker" cke_temp="1"></span>' );\r
+                               var marker = this.document.getById( 'cke_paste_marker' );\r
+                               marker.scrollIntoView();\r
+                               marker.remove();\r
+                       }\r
+\r
                        CKEDITOR.tools.setTimeout( function()\r
                                {\r
                                        this.fire( 'saveSnapshot' );\r
@@ -59,15 +102,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( this.mode == 'wysiwyg' )\r
                {\r
                        this.focus();\r
+\r
+                       var selection = this.getSelection();\r
+                       if ( checkReadOnly( selection ) )\r
+                               return;\r
+\r
                        this.fire( 'saveSnapshot' );\r
 \r
-                       var element = evt.data,\r
+                       var ranges = selection.getRanges(),\r
+                               element = evt.data,\r
                                elementName = element.getName(),\r
                                isBlock = CKEDITOR.dtd.$block[ elementName ];\r
 \r
-                       var selection = this.getSelection(),\r
-                               ranges = selection.getRanges();\r
-\r
                        var selIsLocked = selection.isLocked;\r
 \r
                        if ( selIsLocked )\r
@@ -121,9 +167,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
 \r
-                       var next = lastElement.getNextSourceNode( true );\r
-                       if ( next && next.type == CKEDITOR.NODE_ELEMENT )\r
-                               range.moveToElementEditStart( next );\r
+                       // If we're inserting a block element immediatelly followed by\r
+                       // another block element, the selection must move there. (#3100,#5436)\r
+                       if ( isBlock )\r
+                       {\r
+                               var next = lastElement.getNext( notWhitespaceEval ),\r
+                                       nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();\r
+\r
+                               // Check if it's a block element that accepts text.\r
+                               if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )\r
+                                       range.moveToElementEditStart( next );\r
+                       }\r
 \r
                        selection.selectRanges( [ range ] );\r
 \r
@@ -161,6 +215,50 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                           && 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
+                       bodyChildsNum = body.getChildren().count();\r
+\r
+               if ( !bodyChildsNum || ( bodyChildsNum == 1&& body.getFirst().hasAttribute( '_moz_editor_bogus_node' ) ) )\r
+               {\r
+                       restoreDirty( editor );\r
+\r
+                       // Simulating keyboard character input by dispatching a keydown of white-space text.\r
+                       var keyEventSimulate = doc.$.createEvent( "KeyEvents" );\r
+                       keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,\r
+                               false, false, false, 0, 32 );\r
+                       doc.$.dispatchEvent( keyEventSimulate );\r
+\r
+                       // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
+                       bodyChildsNum && body.getFirst().remove();\r
+                       doc.getBody().appendBogus();\r
+                       var nativeRange = new CKEDITOR.dom.range( doc );\r
+                       nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );\r
+                       nativeRange.select();\r
+               }\r
+       }\r
+\r
        /**\r
         *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
         *  non-exitable-block by padding extra br.(#3189)\r
@@ -175,6 +273,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        body = editor.document.getBody(),\r
                        enterMode = editor.config.enterMode;\r
 \r
+               CKEDITOR.env.gecko && activateEditing( editor );\r
+\r
                // When enterMode set to block, we'll establing new paragraph only if we're\r
                // selecting inline contents right under body. (#3657)\r
                if ( enterMode != CKEDITOR.ENTER_BR\r
@@ -182,7 +282,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                         && blockLimit.getName() == 'body'\r
                         && !path.block )\r
                {\r
+                       editor.fire( 'updateSnapshot' );\r
                        restoreDirty( editor );\r
+                       CKEDITOR.env.ie && restoreSelection( selection );\r
+\r
                        var fixedBlock = range.fixBlock( true,\r
                                        editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'  );\r
 \r
@@ -193,23 +296,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                first && isNbsp( first ) && first.remove();\r
                        }\r
 \r
-                       // If the fixed block is blank and already followed by a exitable\r
-                       // block, we should revert the fix. (#3684)\r
-                       if ( fixedBlock.getOuterHtml().match( emptyParagraphRegexp ) )\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
-                               var previousElement = fixedBlock.getPrevious( isNotWhitespace ),\r
-                                       nextElement = fixedBlock.getNext( isNotWhitespace );\r
-\r
-\r
-                               if ( previousElement && previousElement.getName\r
-                                        && !( previousElement.getName() in nonExitableElementNames )\r
-                                        && range.moveToElementEditStart( previousElement )\r
-                                        || nextElement && nextElement.getName\r
-                                          && !( nextElement.getName() in nonExitableElementNames )\r
-                                          && range.moveToElementEditStart( nextElement ) )\r
+                               var element = fixedBlock.getNext( isNotWhitespace );\r
+                               if ( element &&\r
+                                        element.type == CKEDITOR.NODE_ELEMENT &&\r
+                                        !nonExitableElementNames[ element.getName() ] )\r
                                {\r
+                                       range.moveToElementEditStart( element );\r
                                        fixedBlock.remove();\r
                                }\r
+                               else\r
+                               {\r
+                                       element = fixedBlock.getPrevious( isNotWhitespace );\r
+                                       if ( element &&\r
+                                                element.type == CKEDITOR.NODE_ELEMENT &&\r
+                                                !nonExitableElementNames[ element.getName() ] )\r
+                                       {\r
+                                               range.moveToElementEditEnd( element );\r
+                                               fixedBlock.remove();\r
+                                       }\r
+                               }\r
                        }\r
 \r
                        range.select();\r
@@ -221,14 +330,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                // All browsers are incapable to moving cursor out of certain non-exitable\r
                // blocks (e.g. table, list, pre) at the end of document, make this happen by\r
                // place a bogus node there, which would be later removed by dataprocessor.\r
-               var lastNode = body.getLast( CKEDITOR.dom.walker.whitespaces( true ) );\r
-               if ( lastNode && lastNode.getName && ( lastNode.getName() in nonExitableElementNames ) )\r
+               var walkerRange = new CKEDITOR.dom.range( editor.document ),\r
+                       walker = new CKEDITOR.dom.walker( walkerRange );\r
+               walkerRange.selectNodeContents( body );\r
+               walker.evaluator = function( node )\r
                {\r
+                       return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in nonExitableElementNames );\r
+               };\r
+               walker.guard = function( node, isMoveout )\r
+               {\r
+                       return !( ( node.type == CKEDITOR.NODE_TEXT && isNotWhitespace( node ) ) || isMoveout );\r
+               };\r
+\r
+               if ( walker.previous() )\r
+               {\r
+                       editor.fire( 'updateSnapshot' );\r
                        restoreDirty( editor );\r
-                       if ( !CKEDITOR.env.ie )\r
-                               body.appendBogus();\r
+                       CKEDITOR.env.ie && restoreSelection( selection );\r
+\r
+                       var paddingBlock;\r
+                       if ( enterMode != CKEDITOR.ENTER_BR )\r
+                               paddingBlock = body.append( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
                        else\r
-                               body.append( editor.document.createText( '\xa0' ) );\r
+                               paddingBlock = body;\r
+\r
+                       if ( !CKEDITOR.env.ie )\r
+                               paddingBlock.appendBogus();\r
                }\r
        }\r
 \r
@@ -241,16 +368,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )\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
@@ -259,99 +389,71 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        {\r
                                                if ( iframe )\r
                                                        iframe.remove();\r
-                                               if ( fieldset )\r
-                                                       fieldset.remove();\r
 \r
-                                               frameLoaded = 0;\r
+\r
+                                               var srcScript =\r
+                                                       'document.open();' +\r
+\r
+                                                       // The document domain must be set any time we\r
+                                                       // call document.open().\r
+                                                       ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
+\r
+                                                       'document.close();';\r
 \r
                                                iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
                                                        ' style="width:100%;height:100%"' +\r
                                                        ' frameBorder="0"' +\r
-                                                       ( !CKEDITOR.env.webkit ?\r
-                                                               // Support for custom document.domain in IE.\r
-                                                               ' src="javascript:void((function(){' +\r
-                                                               'document.open();' +            // To avoid HTTPS warnings.\r
-                                                               ( isCustomDomain ?\r
-                                                                       'document.domain=\'' + document.domain + '\';' : '' ) +\r
-                                                               'document.close();' +\r
-                                                               '})())"' : '' ) +\r
-                                                       ' tabIndex="-1"' +\r
+                                                       ' title="' + frameLabel + '"' +\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="' + ( CKEDITOR.env.ie ? 'javascript:void(function(){' + encodeURIComponent( srcScript ) + '}())' : '' ) + '"' +\r
+                                                       ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +\r
                                                        ' allowTransparency="true"' +\r
                                                        '></iframe>' );\r
 \r
-                                               // Register onLoad event for iframe element, which\r
-                                               // will fill it with content and set custom domain.\r
-                                               iframe.on( 'load', function( e )\r
-                                               {\r
-                                                       e.removeListener();\r
-                                                       var doc = iframe.getFrameDocument().$;\r
+                                               // #5689 Running inside of Firefox chrome the load event doesn't bubble like in a normal page\r
+                                               if (document.location.protocol == 'chrome:')\r
+                                                       CKEDITOR.event.useCapture = true;\r
 \r
-                                                       // Custom domain handling is needed after each document.open().\r
-                                                       doc.open();\r
-                                                       if ( isCustomDomain )\r
-                                                               doc.domain = document.domain;\r
-                                                       doc.write( data );\r
-                                                       doc.close();\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
 \r
-                                               } );\r
+                                                               var doc = iframe.getFrameDocument().$;\r
 \r
-                                               var accTitle = editor.lang.editorTitle.replace( '%1', editor.name );\r
+                                                               // Don't leave any history log in IE. (#5657)\r
+                                                               doc.open( "text/html","replace" );\r
+                                                               doc.write( data );\r
+                                                               doc.close();\r
+                                                       });\r
 \r
-                                               if ( CKEDITOR.env.gecko )\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
+                                               // #5689 Reset adjustment back to default\r
+                                               if (document.location.protocol == 'chrome:')\r
+                                                       CKEDITOR.event.useCapture = false;\r
 \r
-                                               if ( !CKEDITOR.env.ie )\r
-                                                       mainElement.append( iframe );\r
+                                               mainElement.append( iframe );\r
                                        };\r
 \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" cke_temp="1">' +\r
-                                                       'window.parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +\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
@@ -360,8 +462,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                var script = domDocument.getElementById( "cke_actscrpt" );\r
                                                script.parentNode.removeChild( script );\r
 \r
-                                               delete CKEDITOR._[ 'contentDomReady' + editor.name ];\r
-\r
                                                body.spellcheck = !editor.config.disableNativeSpellChecker;\r
 \r
                                                if ( CKEDITOR.env.ie )\r
@@ -376,16 +476,35 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        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 = true;\r
+                                                               else if ( CKEDITOR.env.webkit )\r
+                                                                       domDocument.$.body.parentNode.contentEditable = true;\r
+                                                               else\r
+                                                                       domDocument.$.designMode = '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
+                                               CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
 \r
                                                domWindow       = editor.window         = new CKEDITOR.dom.window( domWindow );\r
                                                domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
 \r
+                                               domDocument.on( 'dblclick', function( evt )\r
+                                               {\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
                                                // Gecko/Webkit need some help when selecting control type elements. (#3448)\r
                                                if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )\r
                                                {\r
@@ -397,6 +516,34 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        } );\r
                                                }\r
 \r
+                                               if ( CKEDITOR.env.gecko )\r
+                                               {\r
+                                                       domDocument.on( 'mouseup', function( ev )\r
+                                                       {\r
+                                                               if ( ev.data.$.button == 2 )\r
+                                                               {\r
+                                                                       var target = ev.data.getTarget();\r
+\r
+                                                                       // Prevent right click from selecting an empty block even\r
+                                                                       // when selection is anchored inside it. (#5845)\r
+                                                                       if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )\r
+                                                                       {\r
+                                                                               var range = new CKEDITOR.dom.range( domDocument );\r
+                                                                               range.moveToElementEditStart( target );\r
+                                                                               range.select( true );\r
+                                                                       }\r
+                                                               }\r
+                                                       } );\r
+                                               }\r
+\r
+                                               // Prevent the browser opening links in read-only blocks. (#6032)\r
+                                               domDocument.on( 'click', function( ev )\r
+                                                       {\r
+                                                               ev = ev.data;\r
+                                                               if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )\r
+                                                                       ev.preventDefault();\r
+                                                       });\r
+\r
                                                // Webkit: avoid from editing form control elements content.\r
                                                if ( CKEDITOR.env.webkit )\r
                                                {\r
@@ -418,7 +565,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                // IE standard compliant in editing frame doesn't focus the editor when\r
                                                // clicking outside actual content, manually apply the focus. (#1659)\r
                                                if ( CKEDITOR.env.ie\r
-                                                       && domDocument.$.compatMode == 'CSS1Compat' )\r
+                                                       && domDocument.$.compatMode == 'CSS1Compat'\r
+                                                               || CKEDITOR.env.gecko\r
+                                                               || CKEDITOR.env.opera )\r
                                                {\r
                                                        var htmlElement = domDocument.getDocumentElement();\r
                                                        htmlElement.on( 'mousedown', function( evt )\r
@@ -427,45 +576,27 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // have to use here a temporary element to 'redirect'\r
                                                                // the focus.\r
                                                                if ( evt.data.getTarget().equals( htmlElement ) )\r
-                                                                       ieFocusGrabber.focus();\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 || CKEDITOR.env.webkit ) ?\r
-                                                               domWindow : domDocument;\r
-\r
-                                               focusTarget.on( 'blur', function()\r
+                                               domWindow.on( 'blur', function()\r
                                                        {\r
                                                                editor.focusManager.blur();\r
                                                        });\r
 \r
-                                               focusTarget.on( 'focus', function()\r
+                                               domWindow.on( 'focus', function()\r
                                                        {\r
-                                                               // Gecko need a key event to 'wake up' the editing\r
-                                                               // ability when document is empty.(#3864)\r
-                                                               if ( CKEDITOR.env.gecko )\r
-                                                               {\r
-                                                                       var first = body;\r
-                                                                       while ( first.firstChild )\r
-                                                                               first = first.firstChild;\r
+                                                               var doc = editor.document;\r
 \r
-                                                                       if ( !first.nextSibling\r
-                                                                               && ( 'BR' == first.tagName )\r
-                                                                               && first.hasAttribute( '_moz_editor_bogus_node' ) )\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
-                                                               }\r
+                                                               if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
+                                                                       blinkCursor();\r
+                                                               else if ( CKEDITOR.env.opera )\r
+                                                                       doc.getBody().focus();\r
 \r
                                                                editor.focusManager.focus();\r
                                                        });\r
@@ -476,6 +607,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \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
                                                        domDocument.on( 'keydown', function( evt )\r
@@ -552,6 +684,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        editor.fire( 'dataReady' );\r
                                                                }, 0 );\r
 \r
+                                                               // IE, Opera and Safari may not support it and throw errors.\r
+                                                               try { editor.document.$.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}\r
+                                                               try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}\r
+\r
                                                                /*\r
                                                                 * IE BUG: IE might have rendered the iframe with invisible contents.\r
                                                                 * (#3623). Push some inconsequential CSS style changes to force IE to\r
@@ -574,7 +710,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                }\r
                                                        },\r
                                                        0 );\r
-                                       };\r
+                                       }\r
 \r
                                        editor.addMode( 'wysiwyg',\r
                                                {\r
@@ -644,6 +780,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        // 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
@@ -663,8 +801,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                {\r
                                                                        data =\r
                                                                                config.docType +\r
-                                                                               '<html dir="' + config.contentsLangDirection + '">' +\r
+                                                                               '<html dir="' + config.contentsLangDirection + '"' +\r
+                                                                                       ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +\r
                                                                                '<head>' +\r
+                                                                                       '<title>' + frameLabel + '</title>' +\r
                                                                                        baseTag +\r
                                                                                        headExtra +\r
                                                                                '</head>' +\r
@@ -677,7 +817,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                data += activationScript;\r
 \r
-                                                               CKEDITOR._[ 'contentDomReady' + editor.name ] = contentDomReady;\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
@@ -715,8 +857,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
@@ -726,27 +891,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                if ( isLoadingData )\r
                                                                        isPendingFocus = true;\r
-                                                               else if ( editor.window )\r
+                                                               // Temporary solution caused by #6025, supposed be unified by #6154.\r
+                                                               else if ( CKEDITOR.env.opera && editor.document )\r
                                                                {\r
-                                                                       editor.window.focus();\r
+                                                                       editor.document.getBody().focus();\r
 \r
-                                                                       // Force the selection to happen, in this way\r
-                                                                       // we guarantee the focus will be there. (#4848)\r
-                                                                       if ( CKEDITOR.env.ie )\r
-                                                                       {\r
-                                                                               try\r
-                                                                               {\r
-                                                                                       var sel = editor.getSelection();\r
-                                                                                       sel = sel && sel.getNative();\r
-                                                                                       var range = sel && sel.type && sel.createRange();\r
-                                                                                       if ( range )\r
-                                                                                       {\r
-                                                                                                       sel.empty();\r
-                                                                                                       range.select();\r
-                                                                                       }\r
-                                                                               }\r
-                                                                               catch (e) {}\r
-                                                                       }\r
+                                                                       editor.selectionChange();\r
+                                                               }\r
+                                                               else if ( !CKEDITOR.env.opera && editor.window )\r
+                                                               {\r
+                                                                       editor.window.focus();\r
 \r
                                                                        editor.selectionChange();\r
                                                                }\r
@@ -759,21 +913,94 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        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.setAttribute( '_cke_title', editor.document.$.title );\r
+                                       editor.document.$.title = frameLabel;\r
+                               });\r
+\r
+                       // IE8 stricts mode doesn't have 'contentEditable' in effect\r
+                       // on element unless it has layout. (#5562)\r
+                       if ( CKEDITOR.env.ie8Compat )\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
+\r
+                       // Switch on design mode for a short while and close it after then.\r
+                       function blinkCursor( retry )\r
+                       {\r
+                               CKEDITOR.tools.tryThese(\r
+                                       function()\r
+                                       {\r
+                                               editor.document.$.designMode = 'on';\r
+                                               setTimeout( function ()\r
+                                               {\r
+                                                       editor.document.$.designMode = 'off';\r
+                                                       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.ie )\r
+                       if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )\r
                        {\r
-                               var ieFocusGrabber;\r
+                               var focusGrabber;\r
                                editor.on( 'uiReady', function()\r
                                {\r
-                                       ieFocusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(\r
-                                       '<input tabindex="-1" style="position:absolute; left:-10000">' ) );\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; left:-10000" role="presentation"></span>' ) );\r
 \r
-                                       ieFocusGrabber.on( 'focus', function()\r
+                                       focusGrabber.on( 'focus', function()\r
                                                {\r
                                                        editor.focus();\r
                                                } );\r
                                } );\r
+                               editor.on( 'destroy', function()\r
+                               {\r
+                                       CKEDITOR.tools.removeFunction( contentDomReadyHandler );\r
+                                       focusGrabber.clearCustomData();\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
+                                       if ( !element.isReadOnly() )\r
+                                       {\r
+                                               element.setAttribute( 'contentEditable', false );\r
+                                               // We should flag that the element was locked by our code so\r
+                                               // it'll be editable by the editor functions (#6046).\r
+                                               element.setCustomData( '_cke_notReadOnly', 1 );\r
+                                       }\r
+                               }\r
+                       });\r
+\r
                }\r
        });\r
 \r
@@ -788,24 +1015,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                window.addEventListener( 'load', arguments.callee, false );\r
                        else\r
                        {\r
-                               body.setAttribute( 'onpageshow', body.getAttribute( 'onpageshow' )\r
-                                               + ';event.persisted && CKEDITOR.tools.callFunction(' +\r
-                                               CKEDITOR.tools.addFunction( function()\r
-                                               {\r
-                                                       var allInstances = CKEDITOR.instances,\r
-                                                               editor,\r
-                                                               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
+                               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