JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index c71f040..6e4eacb 100644 (file)
@@ -10,19 +10,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
 (function()\r
 {\r
-       // List of elements in which has no way to move editing focus outside.\r
-       var nonExitableElementNames = { table:1,pre:1 };\r
-\r
        // Matching an empty paragraph at the end of document.\r
-       var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;\r
+       var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center|pre)[^>]*>\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
+       // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)\r
+       function nonEditable( element )\r
        {\r
-               return ( element.getName() in nonExitableElementNames )\r
-                               || element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];\r
+               return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];\r
        }\r
 \r
 \r
@@ -55,6 +51,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( this.dataProcessor )\r
                        data = this.dataProcessor.toHtml( data );\r
 \r
+               if ( !data )\r
+                       return;\r
+\r
                // HTML insertion only considers the first range.\r
                var selection = this.getSelection(),\r
                        range = selection.getRanges()[ 0 ];\r
@@ -62,6 +61,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                if ( range.checkReadOnly() )\r
                        return;\r
 \r
+               // Opera: force block splitting when pasted content contains block. (#7801)\r
+               if ( CKEDITOR.env.opera )\r
+               {\r
+                       var path = new CKEDITOR.dom.elementPath( range.startContainer );\r
+                       if ( path.block )\r
+                       {\r
+                               var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;\r
+                               for ( var i = 0, count = nodes.length; i < count; i++ )\r
+                               {\r
+                                       if ( nodes[ i ]._.isBlockLike )\r
+                                       {\r
+                                               range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
+                                               range.insertNode( range.document.createText( '' ) );\r
+                                               range.select();\r
+                                               break;\r
+                                       }\r
+                               }\r
+                       }\r
+               }\r
+\r
                if ( CKEDITOR.env.ie )\r
                {\r
                        var selIsLocked = selection.isLocked;\r
@@ -369,8 +388,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        var pathBlock = path.block || path.blockLimit,\r
                                lastNode = pathBlock && pathBlock.getLast( isNotEmpty );\r
 \r
-                       // In case it's not ended with block element and doesn't have bogus yet. (#7467)\r
+                       // Check some specialities of the current path block:\r
+                       // 1. It is really displayed as block; (#7221)\r
+                       // 2. It doesn't end with one inner block; (#7467)\r
+                       // 3. It doesn't have bogus br yet.\r
                        if ( pathBlock\r
+                                       && pathBlock.isBlockBoundary()\r
                                        && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )\r
                                        && !pathBlock.is( 'pre' )\r
                                        && !pathBlock.getBogus() )\r
@@ -381,12 +404,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                }\r
 \r
-               // When enterMode set to block, we'll establing new paragraph only if we're\r
-               // selecting inline contents right under body. (#3657)\r
-               if ( enterMode != CKEDITOR.ENTER_BR\r
-                    && range.collapsed\r
-                        && blockLimit.getName() == 'body'\r
-                        && !path.block )\r
+               // When we're in block enter mode, a new paragraph will be established\r
+               // to encapsulate inline contents right under body. (#3657)\r
+               if ( editor.config.autoParagraph !== false\r
+                               && enterMode != CKEDITOR.ENTER_BR\r
+                               && range.collapsed\r
+                               && blockLimit.getName() == 'body'\r
+                               && !path.block )\r
                {\r
                        editor.fire( 'updateSnapshot' );\r
                        restoreDirty( editor );\r
@@ -409,7 +433,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
-                                        !nonExitable( element ) )\r
+                                        !nonEditable( element ) )\r
                                {\r
                                        range.moveToElementEditStart( element );\r
                                        fixedBlock.remove();\r
@@ -419,7 +443,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
-                                                !nonExitable( element ) )\r
+                                                !nonEditable( element ) )\r
                                        {\r
                                                range.moveToElementEditEnd( element );\r
                                                fixedBlock.remove();\r
@@ -432,22 +456,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        evt.cancel();\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 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
+               // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)\r
+               // at the end of document, makes it unable to continue adding content, we have to make this\r
+               // easier by opening an new empty paragraph.\r
+               var testRange = new CKEDITOR.dom.range( editor.document );\r
+               testRange.moveToElementEditEnd( editor.document.getBody() );\r
+               var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );\r
+               if ( !testPath.blockLimit.is( 'body') )\r
                {\r
                        editor.fire( 'updateSnapshot' );\r
                        restoreDirty( editor );\r
@@ -455,7 +470,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        var paddingBlock;\r
                        if ( enterMode != CKEDITOR.ENTER_BR )\r
-                               paddingBlock = body.append( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
+                               paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
                        else\r
                                paddingBlock = body;\r
 \r
@@ -470,7 +485,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                init : function( editor )\r
                {\r
-                       var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )\r
+                       var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )\r
                                ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;\r
 \r
                        var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );\r
@@ -544,30 +559,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                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
@@ -597,6 +589,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                body.spellcheck = !editor.config.disableNativeSpellChecker;\r
 \r
+                                               var editable = !editor.readOnly;\r
+\r
                                                if ( CKEDITOR.env.ie )\r
                                                {\r
                                                        // Don't display the focus border.\r
@@ -605,7 +599,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // Disable and re-enable the body to avoid IE from\r
                                                        // taking the editing focus at startup. (#141 / #523)\r
                                                        body.disabled = true;\r
-                                                       body.contentEditable = true;\r
+                                                       body.contentEditable = editable;\r
                                                        body.removeAttribute( 'disabled' );\r
                                                }\r
                                                else\r
@@ -617,20 +611,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // Prefer 'contentEditable' instead of 'designMode'. (#3593)\r
                                                                if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900\r
                                                                                || CKEDITOR.env.opera )\r
-                                                                       domDocument.$.body.contentEditable = true;\r
+                                                                       domDocument.$.body.contentEditable = editable;\r
                                                                else if ( CKEDITOR.env.webkit )\r
-                                                                       domDocument.$.body.parentNode.contentEditable = true;\r
+                                                                       domDocument.$.body.parentNode.contentEditable = editable;\r
                                                                else\r
-                                                                       domDocument.$.designMode = 'on';\r
+                                                                       domDocument.$.designMode = editable? 'off' : 'on';\r
                                                        }, 0 );\r
                                                }\r
 \r
-                                               CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
+                                               editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
 \r
                                                domWindow       = editor.window = new CKEDITOR.dom.window( domWindow );\r
                                                domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
 \r
-                                               domDocument.on( 'dblclick', function( evt )\r
+                                               editable && domDocument.on( 'dblclick', function( evt )\r
                                                {\r
                                                        var element = evt.data.getTarget(),\r
                                                                data = { element : element, dialog : '' };\r
@@ -711,8 +705,8 @@ 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
-                                                       && domDocument.$.compatMode == 'CSS1Compat'\r
+                                               if ( editable &&\r
+                                                               CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat'\r
                                                                || CKEDITOR.env.gecko\r
                                                                || CKEDITOR.env.opera )\r
                                                {\r
@@ -743,7 +737,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                var doc = editor.document;\r
 \r
-                                                               if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
+                                                               if ( editable && CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
                                                                        blinkCursor();\r
                                                                else if ( CKEDITOR.env.opera )\r
                                                                        doc.getBody().focus();\r
@@ -761,15 +755,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        });\r
 \r
                                                var keystrokeHandler = editor.keystrokeHandler;\r
-                                               if ( keystrokeHandler )\r
-                                                       keystrokeHandler.attach( domDocument );\r
+                                               // Prevent backspace from navigating off the page.\r
+                                               keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;\r
+                                               keystrokeHandler.attach( domDocument );\r
 \r
                                                if ( CKEDITOR.env.ie )\r
                                                {\r
                                                        domDocument.getDocumentElement().addClass( domDocument.$.compatMode );\r
                                                        // Override keystrokes which should have deletion behavior\r
                                                        //  on control types in IE . (#4047)\r
-                                                       domDocument.on( 'keydown', function( evt )\r
+                                                       editable && domDocument.on( 'keydown', function( evt )\r
                                                        {\r
                                                                var keyCode = evt.data.getKeystroke();\r
 \r
@@ -958,6 +953,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                {\r
                                                                                        editor.docType = docType = match;\r
                                                                                        return '';\r
+                                                                               }).replace( /<\?xml\s[^\?]*\?>/i, function( match )\r
+                                                                               {\r
+                                                                                       editor.xmlDeclaration = match;\r
+                                                                                       return '';\r
                                                                                });\r
                                                                }\r
 \r
@@ -1030,6 +1029,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                var config = editor.config,\r
                                                                        fullPage = config.fullPage,\r
                                                                        docType = fullPage && editor.docType,\r
+                                                                       xmlDeclaration = fullPage && editor.xmlDeclaration,\r
                                                                        doc = iframe.getFrameDocument();\r
 \r
                                                                var data = fullPage\r
@@ -1047,6 +1047,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                if ( config.ignoreEmptyParagraph )\r
                                                                        data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );\r
 \r
+                                                               if ( xmlDeclaration )\r
+                                                                       data = xmlDeclaration + '\n' + data;\r
                                                                if ( docType )\r
                                                                        data = docType + '\n' + data;\r
 \r
@@ -1136,6 +1138,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        editor.document.$.title = frameLabel;\r
                                });\r
 \r
+                       editor.on( 'readOnly', function()\r
+                               {\r
+                                       if ( editor.mode == 'wysiwyg' )\r
+                                       {\r
+                                               // Symply reload the wysiwyg area. It'll take care of read-only.\r
+                                               var wysiwyg = editor.getMode();\r
+                                               wysiwyg.loadData( wysiwyg.getData() );\r
+                                       }\r
+                               });\r
+\r
                        // IE>=8 stricts mode doesn't have 'contentEditable' in effect\r
                        // on element unless it has layout. (#5562)\r
                        if ( CKEDITOR.document.$.documentMode >= 8 )\r
@@ -1149,11 +1161,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                        // Set the HTML style to 100% to have the text cursor in affect (#6341)\r
                        else if ( CKEDITOR.env.gecko )\r
+                       {\r
                                editor.addCss( 'html { height: 100% !important; }' );\r
+                               editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1;     width : 24px; height : 24px; }' );\r
+                       }\r
+\r
+                       /* #3658: [IE6] Editor document has horizontal scrollbar on long lines\r
+                       To prevent this misbehavior, we show the scrollbar always */\r
+                       /* #6341: The text cursor must be set on the editor area. */\r
+                       /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/\r
+                       editor.addCss( 'html {  _overflow-y: scroll; cursor: text;      *cursor:auto;}' );\r
+                       // Use correct cursor for these elements\r
+                       editor.addCss( 'img, input, textarea { cursor: default;}' );\r
 \r
                        // Switch on design mode for a short while and close it after then.\r
                        function blinkCursor( retry )\r
                        {\r
+                               if ( editor.readOnly )\r
+                                       return;\r
+\r
                                CKEDITOR.tools.tryThese(\r
                                        function()\r
                                        {\r
@@ -1306,6 +1332,18 @@ CKEDITOR.config.ignoreEmptyParagraph = true;
  */\r
 \r
 /**\r
+ * Whether automatically create wrapping blocks around inline contents inside document body,\r
+ * this helps to ensure the integrality of the block enter mode.\r
+ * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.\r
+ * @name CKEDITOR.config.autoParagraph\r
+ * @since 3.6\r
+ * @type Boolean\r
+ * @default true\r
+ * @example\r
+ * config.autoParagraph = false;\r
+ */\r
+\r
+/**\r
  * Fired when some elements are added to the document\r
  * @name CKEDITOR.editor#ariaWidget\r
  * @event\r