JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index 7e7c192..df2fd07 100644 (file)
@@ -14,10 +14,17 @@ 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|li)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
+       var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;\r
 \r
        var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );\r
 \r
+       // Elements that could have empty new line around, including table, pre-formatted block, hr, page-break. (#6554)\r
+       function nonExitable( element )\r
+       {\r
+               return ( element.getName() in nonExitableElementNames )\r
+                               || element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];\r
+       }\r
+\r
        function checkReadOnly( selection )\r
        {\r
                if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT )\r
@@ -73,7 +80,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        }\r
                                }\r
 \r
-                               $sel.createRange().pasteHTML( data );\r
+                               try\r
+                               {\r
+                                       $sel.createRange().pasteHTML( data );\r
+                               }\r
+                               catch (e) {}\r
 \r
                                if ( selIsLocked )\r
                                        this.getSelection().lock();\r
@@ -88,6 +99,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                var marker = this.document.getById( 'cke_paste_marker' );\r
                                marker.scrollIntoView();\r
                                marker.remove();\r
+                               marker = null;\r
                        }\r
 \r
                        CKEDITOR.tools.setTimeout( function()\r
@@ -128,14 +140,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                // Remove the original contents.\r
                                range.deleteContents();\r
 \r
-                               clone = !i && element || element.clone( true );\r
+                               clone = !i && element || element.clone( 1 );\r
 \r
                                // If we're inserting a block at dtd-violated position, split\r
                                // the parent blocks until we reach blockLimit.\r
                                var current, dtd;\r
                                if ( isBlock )\r
                                {\r
-                                       while ( ( current = range.getCommonAncestor( false, true ) )\r
+                                       while ( ( current = range.getCommonAncestor( 0, 1 ) )\r
                                                        && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
                                                        && !( dtd && dtd [ elementName ] ) )\r
                                        {\r
@@ -198,7 +210,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function restoreDirty( editor )\r
        {\r
                if ( !editor.checkDirty() )\r
-                       setTimeout( function(){ editor.resetDirty(); } );\r
+                       setTimeout( function(){ editor.resetDirty(); }, 0 );\r
        }\r
 \r
        var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),\r
@@ -244,12 +256,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        restoreDirty( editor );\r
 \r
+                       // Memorize scroll position to restore it later (#4472).\r
+                       var hostDocument = editor.element.getDocument();\r
+                       var hostDocumentElement = hostDocument.getDocumentElement();\r
+                       var scrollTop = hostDocumentElement.$.scrollTop;\r
+                       var scrollLeft = hostDocumentElement.$.scrollLeft;\r
+\r
                        // Simulating keyboard character input by dispatching a keydown of white-space text.\r
                        var keyEventSimulate = doc.$.createEvent( "KeyEvents" );\r
                        keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,\r
                                false, false, false, 0, 32 );\r
                        doc.$.dispatchEvent( keyEventSimulate );\r
 \r
+                       if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )\r
+                               hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );\r
+\r
                        // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
                        bodyChildsNum && body.getFirst().remove();\r
                        doc.getBody().appendBogus();\r
@@ -303,7 +324,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                var element = fixedBlock.getNext( isNotWhitespace );\r
                                if ( element &&\r
                                         element.type == CKEDITOR.NODE_ELEMENT &&\r
-                                        !nonExitableElementNames[ element.getName() ] )\r
+                                        !nonExitable( element ) )\r
                                {\r
                                        range.moveToElementEditStart( element );\r
                                        fixedBlock.remove();\r
@@ -313,7 +334,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        element = fixedBlock.getPrevious( isNotWhitespace );\r
                                        if ( element &&\r
                                                 element.type == CKEDITOR.NODE_ELEMENT &&\r
-                                                !nonExitableElementNames[ element.getName() ] )\r
+                                                !nonExitable( element ) )\r
                                        {\r
                                                range.moveToElementEditEnd( element );\r
                                                fixedBlock.remove();\r
@@ -412,8 +433,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        ' allowTransparency="true"' +\r
                                                        '></iframe>' );\r
 \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
+                                               // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)\r
+                                               if ( document.location.protocol == 'chrome:' )\r
                                                        CKEDITOR.event.useCapture = true;\r
 \r
                                                // With FF, it's better to load the data on iframe.load. (#3894,#4058)\r
@@ -430,11 +451,34 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                doc.close();\r
                                                        });\r
 \r
-                                               // #5689 Reset adjustment back to default\r
-                                               if (document.location.protocol == 'chrome:')\r
+                                               // Reset adjustment back to default (#5689)\r
+                                               if ( document.location.protocol == 'chrome:' )\r
                                                        CKEDITOR.event.useCapture = false;\r
 \r
+                                               // The container must be visible when creating the iframe in FF (#5956)\r
+                                               var element = editor.element,\r
+                                                       isHidden = CKEDITOR.env.gecko && !element.isVisible(),\r
+                                                       previousStyles = {};\r
+                                               if ( isHidden )\r
+                                               {\r
+                                                       element.show();\r
+                                                       previousStyles = {\r
+                                                               position : element.getStyle( 'position' ),\r
+                                                               top : element.getStyle( 'top' )\r
+                                                       };\r
+                                                       element.setStyles( { position : 'absolute', top : '-3000px' } );\r
+                                               }\r
+\r
                                                mainElement.append( iframe );\r
+\r
+                                               if ( isHidden )\r
+                                               {\r
+                                                       setTimeout( function()\r
+                                                       {\r
+                                                               element.hide();\r
+                                                               element.setStyles( previousStyles );\r
+                                                       }, 1000 );\r
+                                               }\r
                                        };\r
 \r
                                        // The script that launches the bootstrap logic on 'domReady', so the document\r
@@ -494,7 +538,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
 \r
-                                               domWindow       = editor.window         = new CKEDITOR.dom.window( domWindow );\r
+                                               domWindow       = editor.window = new CKEDITOR.dom.window( domWindow );\r
                                                domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
 \r
                                                domDocument.on( 'dblclick', function( evt )\r
@@ -506,7 +550,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                });\r
 \r
                                                // Gecko/Webkit need some help when selecting control type elements. (#3448)\r
-                                               if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )\r
+                                               if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )\r
                                                {\r
                                                        domDocument.on( 'mousedown', function( ev )\r
                                                        {\r
@@ -589,6 +633,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                editor.focusManager.blur();\r
                                                        });\r
 \r
+                                               var wasFocused;\r
+\r
                                                domWindow.on( 'focus', function()\r
                                                        {\r
                                                                var doc = editor.document;\r
@@ -597,6 +643,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        blinkCursor();\r
                                                                else if ( CKEDITOR.env.opera )\r
                                                                        doc.getBody().focus();\r
+                                                               // Webkit needs focus for the first time on the HTML element. (#6153)\r
+                                                               else if ( CKEDITOR.env.webkit )\r
+                                                               {\r
+                                                                       if ( !wasFocused )\r
+                                                                       {\r
+                                                                               editor.document.getDocumentElement().focus();\r
+                                                                               wasFocused = 1;\r
+                                                                       }\r
+                                                               }\r
 \r
                                                                editor.focusManager.focus();\r
                                                        });\r
@@ -837,9 +892,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                if ( editor.dataProcessor )\r
                                                                        data = editor.dataProcessor.toDataFormat( data, fixForBody );\r
 \r
-                                                               // Strip the last blank paragraph within document.\r
+                                                               // Reset empty if the document contains only one empty paragraph.\r
                                                                if ( config.ignoreEmptyParagraph )\r
-                                                                       data = data.replace( emptyParagraphRegexp, '' );\r
+                                                                       data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );\r
 \r
                                                                if ( docType )\r
                                                                        data = docType + '\n' + data;\r
@@ -891,7 +946,18 @@ 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
+                                                                       // Required for Opera when switching focus\r
+                                                                       // from another iframe, e.g. panels. (#6444)\r
+                                                                       var iframe = editor.window.$.frameElement;\r
+                                                                       iframe.blur(), iframe.focus();\r
+                                                                       editor.document.getBody().focus();\r
+\r
+                                                                       editor.selectionChange();\r
+                                                               }\r
+                                                               else if ( !CKEDITOR.env.opera && editor.window )\r
                                                                {\r
                                                                        editor.window.focus();\r
 \r
@@ -909,7 +975,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        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
+                       editor.on( 'contentDom', function()\r
                                {\r
                                        var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );\r
                                        title.setAttribute( '_cke_title', editor.document.$.title );\r
@@ -927,6 +993,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );\r
                                editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );\r
                        }\r
+                       // Set the HTML style to 100% to have the text cursor in affect (#6341)\r
+                       else if ( CKEDITOR.env.gecko )\r
+                               editor.addCss( 'html { height: 100% !important; }' );\r
 \r
                        // Switch on design mode for a short while and close it after then.\r
                        function blinkCursor( retry )\r
@@ -935,10 +1004,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        function()\r
                                        {\r
                                                editor.document.$.designMode = 'on';\r
-                                               setTimeout( function ()\r
+                                               setTimeout( function()\r
                                                {\r
                                                        editor.document.$.designMode = 'off';\r
-                                                       editor.document.getBody().focus();\r
+                                                       if ( CKEDITOR.currentInstance == editor )\r
+                                                               editor.document.getBody().focus();\r
                                                }, 50 );\r
                                        },\r
                                        function()\r
@@ -963,7 +1033,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                {\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
+                                               '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );\r
 \r
                                        focusGrabber.on( 'focus', function()\r
                                                {\r
@@ -1000,7 +1070,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)\r
        if ( CKEDITOR.env.gecko )\r
        {\r
-               ( function ()\r
+               (function()\r
                {\r
                        var body = document.body;\r
 \r