JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index dc8329d..a6b0639 100644 (file)
@@ -170,6 +170,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        }\r
 \r
+       function isBlankParagraph( block )\r
+       {\r
+               return block.getOuterHtml().match( emptyParagraphRegexp );\r
+       }\r
+\r
+       isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );\r
+\r
        /**\r
         *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
         *  non-exitable-block by padding extra br.(#3189)\r
@@ -191,6 +198,7 @@ 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
@@ -204,19 +212,21 @@ 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
                                if ( previousElement && previousElement.getName\r
                                         && !( previousElement.getName() in nonExitableElementNames )\r
+                                        && isBlankParagraph( previousElement )\r
                                         && range.moveToElementEditStart( previousElement )\r
                                         || nextElement && nextElement.getName\r
-                                          && !( nextElement.getName() in nonExitableElementNames )\r
-                                          && range.moveToElementEditStart( nextElement ) )\r
+                                               && !( nextElement.getName() in nonExitableElementNames )\r
+                                               && isBlankParagraph( nextElement )\r
+                                               && range.moveToElementEditStart( nextElement ) )\r
                                {\r
                                        fixedBlock.remove();\r
                                }\r
@@ -245,6 +255,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                if ( walker.previous() )\r
                {\r
+                       editor.fire( 'updateSnapshot' );\r
                        restoreDirty( editor );\r
                        CKEDITOR.env.ie && restoreSelection( selection );\r
 \r
@@ -270,6 +281,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );\r
 \r
+                       var contentDomReadyHandler;\r
                        editor.on( 'editingBlockReady', function()\r
                                {\r
                                        var mainElement,\r
@@ -289,13 +301,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                if ( iframe )\r
                                                        iframe.remove();\r
 \r
-                                               frameLoaded = 0;\r
-\r
-                                               var setDataFn = !CKEDITOR.env.gecko && CKEDITOR.tools.addFunction( function( doc )\r
-                                                       {\r
-                                                               CKEDITOR.tools.removeFunction( setDataFn );\r
-                                                               doc.write( data );\r
-                                                       });\r
 \r
                                                var srcScript =\r
                                                        'document.open();' +\r
@@ -304,29 +309,30 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // call document.open().\r
                                                        ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
 \r
-                                                       ( ( 'parent.CKEDITOR.tools.callFunction(' + setDataFn + ',document);' ) ) +\r
-\r
                                                        'document.close();';\r
 \r
                                                iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
                                                        ' style="width:100%;height:100%"' +\r
                                                        ' frameBorder="0"' +\r
                                                        ' title="' + frameLabel + '"' +\r
-                                                       // With FF, the 'src' attribute should be left empty to\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.gecko ? '' : 'javascript:void(function(){' + encodeURIComponent( srcScript ) + '}())' ) + '"' +\r
+                                                       ' src="' + ( CKEDITOR.env.ie ? 'javascript:void(function(){' + encodeURIComponent( srcScript ) + '}())' : '' ) + '"' +\r
                                                        ' tabIndex="' + editor.tabIndex + '"' +\r
                                                        ' allowTransparency="true"' +\r
                                                        '></iframe>' );\r
 \r
                                                // With FF, it's better to load the data on iframe.load. (#3894,#4058)\r
-                                               CKEDITOR.env.gecko && iframe.on( 'load', function( ev )\r
+                                               iframe.on( 'load', function( ev )\r
                                                        {\r
+                                                               frameLoaded = 1;\r
                                                                ev.removeListener();\r
 \r
                                                                var doc = iframe.getFrameDocument().$;\r
 \r
-                                                               doc.open();\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
@@ -336,18 +342,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \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
                                                        ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
-                                                       'parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +\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 = 1;\r
+                                               frameLoaded = 0;\r
 \r
                                                editor.fire( 'ariaWidget', iframe );\r
 \r
@@ -358,8 +365,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
@@ -374,7 +379,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        body.removeAttribute( 'disabled' );\r
                                                }\r
                                                else\r
+                                               {\r
+                                                       // Gecko need a key event to 'wake up' the editing\r
+                                                       // ability when document is empty.(#3864)\r
+                                                       if ( CKEDITOR.env.gecko && !body.childNodes.length )\r
+                                                       {\r
+                                                               setTimeout( function()\r
+                                                               {\r
+                                                                       restoreDirty( editor );\r
+\r
+                                                                       // Simulating keyboard character input by dispatching a keydown of white-space text.\r
+                                                                       var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );\r
+                                                                       keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,\r
+                                                                               false, false, false, 0, 32 );\r
+                                                                       domDocument.$.dispatchEvent( keyEventSimulate );\r
+\r
+                                                                       // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
+                                                                       domDocument.createElement( 'br', { attributes: { '_moz_editor_bogus_node' : 'TRUE', '_moz_dirty' : "" } } )\r
+                                                                               .replace( domDocument.getBody().getFirst() );\r
+                                                                       var nativeRange = new CKEDITOR.dom.range( domDocument );\r
+                                                                       nativeRange.setStartAt( new CKEDITOR.dom.element( body ) , CKEDITOR.POSITION_AFTER_START );\r
+                                                                       nativeRange.select();\r
+                                                               }, 0 );\r
+                                                       }\r
+\r
                                                        domDocument.designMode = 'on';\r
+                                               }\r
 \r
                                                // IE, Opera and Safari may not support it and throw\r
                                                // errors.\r
@@ -439,33 +469,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                focusTarget.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
-\r
-                                                                       if ( !first.nextSibling\r
-                                                                               && ( 'BR' == first.tagName )\r
-                                                                               && first.hasAttribute( '_moz_editor_bogus_node' ) )\r
-                                                                       {\r
-                                                                               restoreDirty( editor );\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
-\r
                                                                editor.focusManager.focus();\r
                                                        });\r
 \r
@@ -573,7 +576,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                }\r
                                                        },\r
                                                        0 );\r
-                                       };\r
+                                       }\r
 \r
                                        editor.addMode( 'wysiwyg',\r
                                                {\r
@@ -643,6 +646,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
@@ -662,7 +667,8 @@ 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
                                                                                '<title>' + frameLabel + '</title>' +\r
                                                                                '<head>' +\r
                                                                                        baseTag +\r
@@ -677,7 +683,6 @@ 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
@@ -787,6 +792,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                } );\r
                                editor.on( 'destroy', function()\r
                                {\r
+                                       CKEDITOR.tools.removeFunction( contentDomReadyHandler );\r
                                        ieFocusGrabber.clearCustomData();\r
                                } );\r
                        }\r
@@ -804,24 +810,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