JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index c256646..a6b0639 100644 (file)
@@ -161,6 +161,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                           && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\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
        /**\r
         *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
         *  non-exitable-block by padding extra br.(#3189)\r
@@ -182,7 +198,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,20 +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
-\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
@@ -221,14 +241,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
@@ -243,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
@@ -262,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
@@ -277,30 +309,30 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // call document.open().\r
                                                        ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
 \r
-                                                       // With FF, it's better to load the data on\r
-                                                       // iframe.load. (#3894,#4058)\r
-                                                       // But in FF, we still need the open()-close() call\r
-                                                       // to avoid HTTPS warnings.\r
-                                                       ( CKEDITOR.env.gecko ? '' : ( '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
-                                                       ' src="javascript:void(function(){' + encodeURIComponent( srcScript ) + '}())"' +\r
-                                                       ' tabIndex="' + editor.tabIndex + '"' +\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="' + 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
@@ -310,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
@@ -332,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
@@ -348,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
@@ -413,32 +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
-                                                                               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
@@ -546,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
@@ -616,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
@@ -635,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
@@ -650,7 +683,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
@@ -688,8 +723,24 @@ 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
                                                        unload : function( holderElement )\r
                                                        {\r
+                                                               this.onDispose();\r
+\r
                                                                editor.window = editor.document = iframe = mainElement = isPendingFocus = null;\r
 \r
                                                                editor.fire( 'contentDomUnload' );\r
@@ -702,25 +753,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                else if ( editor.window )\r
                                                                {\r
                                                                        editor.window.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
-\r
                                                                        editor.selectionChange();\r
                                                                }\r
                                                        }\r
@@ -758,6 +790,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        editor.focus();\r
                                                } );\r
                                } );\r
+                               editor.on( 'destroy', function()\r
+                               {\r
+                                       CKEDITOR.tools.removeFunction( contentDomReadyHandler );\r
+                                       ieFocusGrabber.clearCustomData();\r
+                               } );\r
                        }\r
                }\r
        });\r
@@ -773,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