JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.1
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index c2a8b15..dc8329d 100644 (file)
@@ -89,7 +89,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                var current, dtd;\r
                                if ( isBlock )\r
                                {\r
-                                       while( ( current = range.getCommonAncestor( false, true ) )\r
+                                       while ( ( current = range.getCommonAncestor( false, true ) )\r
                                                        && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
                                                        && !( dtd && dtd [ elementName ] ) )\r
                                        {\r
@@ -143,7 +143,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        // DOM modification here should not bother dirty flag.(#4385)\r
        function restoreDirty( editor )\r
        {\r
-               if( !editor.checkDirty() )\r
+               if ( !editor.checkDirty() )\r
                        setTimeout( function(){ editor.resetDirty(); } );\r
        }\r
 \r
@@ -161,6 +161,15 @@ 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
        /**\r
         *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
         *  non-exitable-block by padding extra br.(#3189)\r
@@ -183,6 +192,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                         && !path.block )\r
                {\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
@@ -195,12 +206,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \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 ( fixedBlock.getOuterHtml().match( emptyParagraphRegexp ) )\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
@@ -214,21 +224,38 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        range.select();\r
                        // Notify non-IE that selection has changed.\r
-                       if( !CKEDITOR.env.ie )\r
+                       if ( !CKEDITOR.env.ie )\r
                                editor.selectionChange();\r
                }\r
 \r
                // All browsers are incapable to moving cursor out of certain non-exitable\r
                // blocks (e.g. table, list, pre) at the end of document, make this happen by\r
                // place a bogus node there, which would be later removed by dataprocessor.\r
-               var 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
                        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 +268,18 @@ 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
                        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,89 +288,58 @@ 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 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
+\r
+                                                       // The document domain must be set any time we\r
+                                                       // 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
-                                                       // Support for custom document.domain in IE.\r
-                                                       ( isCustomDomain ?\r
-                                                               ' src="javascript:void((function(){' +\r
-                                                                       'document.open();' +\r
-                                                                       'document.domain=\'' + document.domain + '\';' +\r
-                                                                       'document.close();' +\r
-                                                               '})())"' : '' ) +\r
-                                                       ' tabIndex="-1"' +\r
+                                                       ' title="' + frameLabel + '"' +\r
+                                                       // With FF, 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
+                                                       ' tabIndex="' + 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
-\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
-\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
+                                                       {\r
+                                                               ev.removeListener();\r
 \r
-                                               var accTitle = editor.lang.editorTitle.replace( '%1', editor.name );\r
+                                                               var doc = iframe.getFrameDocument().$;\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
+                                                               doc.open();\r
+                                                               doc.write( data );\r
+                                                               doc.close();\r
+                                                       });\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
                                        var activationScript =\r
-                                               '<script id="cke_actscrpt" type="text/javascript">' +\r
-                                                       'window.parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +\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
                                                '</script>';\r
 \r
                                        // Editing area bootstrap code.\r
@@ -349,9 +347,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        {\r
                                                if ( frameLoaded )\r
                                                        return;\r
-\r
                                                frameLoaded = 1;\r
 \r
+                                               editor.fire( 'ariaWidget', iframe );\r
+\r
                                                var domDocument = domWindow.document,\r
                                                        body = domDocument.body;\r
 \r
@@ -416,7 +415,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                // IE standard compliant in editing frame doesn't focus the editor when\r
                                                // clicking outside actual content, manually apply the focus. (#1659)\r
-                                               if( CKEDITOR.env.ie\r
+                                               if ( CKEDITOR.env.ie\r
                                                        && domDocument.$.compatMode == 'CSS1Compat' )\r
                                                {\r
                                                        var htmlElement = domDocument.getDocumentElement();\r
@@ -445,13 +444,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                if ( CKEDITOR.env.gecko )\r
                                                                {\r
                                                                        var first = body;\r
-                                                                       while( first.firstChild )\r
+                                                                       while ( first.firstChild )\r
                                                                                first = first.firstChild;\r
 \r
-                                                                       if( !first.nextSibling\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
@@ -475,31 +475,45 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                if ( CKEDITOR.env.ie )\r
                                                {\r
-                                                       // Cancel default action for backspace in IE on control types. (#4047)\r
+                                                       // Override keystrokes which should have deletion behavior\r
+                                                       //  on control types in IE . (#4047)\r
                                                        domDocument.on( 'keydown', function( evt )\r
                                                        {\r
-                                                               // Backspace.\r
-                                                               var control = evt.data.getKeystroke() == 8\r
-                                                                                         && editor.getSelection().getSelectedElement();\r
-                                                               if ( control )\r
+                                                               var keyCode = evt.data.getKeystroke();\r
+\r
+                                                               // Backspace OR Delete.\r
+                                                               if ( keyCode in { 8 : 1, 46 : 1 } )\r
                                                                {\r
-                                                                       // Make undo snapshot.\r
-                                                                       editor.fire( 'saveSnapshot' );\r
-                                                                       // Remove manually.\r
-                                                                       control.remove();\r
-                                                                       editor.fire( 'saveSnapshot' );\r
-                                                                       evt.cancel();\r
+                                                                       var sel = editor.getSelection(),\r
+                                                                               control = sel.getSelectedElement();\r
+\r
+                                                                       if ( control )\r
+                                                                       {\r
+                                                                               // Make undo snapshot.\r
+                                                                               editor.fire( 'saveSnapshot' );\r
+\r
+                                                                               // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will\r
+                                                                               // break up the selection, safely manage it here. (#4795)\r
+                                                                               var bookmark = sel.getRanges()[ 0 ].createBookmark();\r
+                                                                               // Remove the control manually.\r
+                                                                               control.remove();\r
+                                                                               sel.selectBookmarks( [ bookmark ] );\r
+\r
+                                                                               editor.fire( 'saveSnapshot' );\r
+\r
+                                                                               evt.data.preventDefault();\r
+                                                                       }\r
                                                                }\r
                                                        } );\r
 \r
                                                        // PageUp/PageDown scrolling is broken in document\r
                                                        // with standard doctype, manually fix it. (#4736)\r
-                                                       if( domDocument.$.compatMode == 'CSS1Compat' )\r
+                                                       if ( domDocument.$.compatMode == 'CSS1Compat' )\r
                                                        {\r
                                                                var pageUpDownKeys = { 33 : 1, 34 : 1 };\r
                                                                domDocument.on( 'keydown', function( evt )\r
                                                                {\r
-                                                                       if( evt.data.getKeystroke() in pageUpDownKeys )\r
+                                                                       if ( evt.data.getKeystroke() in pageUpDownKeys )\r
                                                                        {\r
                                                                                setTimeout( function ()\r
                                                                                {\r
@@ -649,6 +663,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        data =\r
                                                                                config.docType +\r
                                                                                '<html dir="' + config.contentsLangDirection + '">' +\r
+                                                                               '<title>' + frameLabel + '</title>' +\r
                                                                                '<head>' +\r
                                                                                        baseTag +\r
                                                                                        headExtra +\r
@@ -663,6 +678,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                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
@@ -700,8 +718,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
@@ -714,25 +748,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
@@ -744,49 +759,63 @@ 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
+\r
                        // Create an invisible element to grab focus.\r
-                       if( CKEDITOR.env.ie )\r
+                       if ( CKEDITOR.env.ie )\r
                        {\r
                                var ieFocusGrabber;\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
+                                               // 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
                                                {\r
                                                        editor.focus();\r
                                                } );\r
                                } );\r
+                               editor.on( 'destroy', function()\r
+                               {\r
+                                       ieFocusGrabber.clearCustomData();\r
+                               } );\r
                        }\r
                }\r
        });\r
 \r
        // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)\r
-       if( CKEDITOR.env.gecko )\r
+       if ( CKEDITOR.env.gecko )\r
        {\r
-               var topWin = window.top;\r
-\r
                ( function ()\r
                {\r
-                       var topBody = topWin.document.body;\r
+                       var body = document.body;\r
 \r
-                       if( !topBody )\r
-                               topWin.addEventListener( 'load', arguments.callee, false );\r
+                       if ( !body )\r
+                               window.addEventListener( 'load', arguments.callee, false );\r
                        else\r
                        {\r
-                               topBody.setAttribute( 'onpageshow', topBody.getAttribute( 'onpageshow' )\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
+                                                       for ( var i in allInstances )\r
                                                        {\r
                                                                editor = allInstances[ i ];\r
                                                                doc = editor.document;\r
-                                                               if( doc )\r
+                                                               if ( doc )\r
                                                                {\r
                                                                        doc.$.designMode = 'off';\r
                                                                        doc.$.designMode = 'on';\r
@@ -845,3 +874,9 @@ CKEDITOR.config.disableNativeSpellChecker = true;
  * config.ignoreEmptyParagraph = false;\r
  */\r
 CKEDITOR.config.ignoreEmptyParagraph = true;\r
+\r
+/**\r
+ * Fired when data is loaded and ready for retrieval in an editor instance.\r
+ * @name CKEDITOR.editor#dataReady\r
+ * @event\r
+ */\r