JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
index df2fd07..4816cdc 100644 (file)
@@ -33,177 +33,240 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return selection.getCommonAncestor().isReadOnly();\r
        }\r
 \r
-       function onInsertHtml( evt )\r
+\r
+       function onInsert( insertFunc )\r
        {\r
-               if ( this.mode == 'wysiwyg' )\r
+               return function( evt )\r
                {\r
-                       this.focus();\r
+                       if ( this.mode == 'wysiwyg' )\r
+                       {\r
+                               this.focus();\r
 \r
-                       var selection = this.getSelection();\r
-                       if ( checkReadOnly( selection ) )\r
-                               return;\r
+                               var selection = this.getSelection();\r
+                               if ( checkReadOnly( selection ) )\r
+                                       return;\r
 \r
-                       var data = evt.data;\r
-                       this.fire( 'saveSnapshot' );\r
+                               this.fire( 'saveSnapshot' );\r
 \r
-                       if ( this.dataProcessor )\r
-                               data = this.dataProcessor.toHtml( data );\r
+                               insertFunc.call( this, evt.data );\r
 \r
-                       if ( CKEDITOR.env.ie )\r
-                       {\r
-                               var selIsLocked = selection.isLocked;\r
+                               // Save snaps after the whole execution completed.\r
+                               // This's a workaround for make DOM modification's happened after\r
+                               // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'\r
+                               // call.\r
+                               CKEDITOR.tools.setTimeout( function()\r
+                                  {\r
+                                          this.fire( 'saveSnapshot' );\r
+                                  }, 0, this );\r
+                       }\r
+               };\r
+       }\r
 \r
-                               if ( selIsLocked )\r
-                                       selection.unlock();\r
+       function doInsertHtml( data )\r
+       {\r
+               if ( this.dataProcessor )\r
+                       data = this.dataProcessor.toHtml( data );\r
 \r
-                               var $sel = selection.getNative();\r
+               var selection = this.getSelection();\r
+               if ( CKEDITOR.env.ie )\r
+               {\r
+                       var selIsLocked = selection.isLocked;\r
 \r
-                               // Delete control selections to avoid IE bugs on pasteHTML.\r
-                               if ( $sel.type == 'Control' )\r
-                                       $sel.clear();\r
+                       if ( selIsLocked )\r
+                               selection.unlock();\r
+\r
+                       var $sel = selection.getNative();\r
+\r
+                       // Delete control selections to avoid IE bugs on pasteHTML.\r
+                       if ( $sel.type == 'Control' )\r
+                               $sel.clear();\r
                                else if  ( selection.getType() == CKEDITOR.SELECTION_TEXT )\r
-                               {\r
-                                       // Due to IE bugs on handling contenteditable=false blocks\r
-                                       // (#6005), we need to make some checks and eventually\r
-                                       // delete the selection first.\r
+                       {\r
+                               // Due to IE bugs on handling contenteditable=false blocks\r
+                               // (#6005), we need to make some checks and eventually\r
+                               // delete the selection first.\r
 \r
-                                       var range = selection.getRanges()[0],\r
+                               var range = selection.getRanges()[0],\r
                                                endContainer = range && range.endContainer;\r
 \r
-                                       if ( endContainer &&\r
-                                                endContainer.type == CKEDITOR.NODE_ELEMENT &&\r
-                                                endContainer.getAttribute( 'contenteditable' ) == 'false' &&\r
-                                                range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )\r
-                                       {\r
-                                               range.setEndAfter( range.endContainer );\r
-                                               range.deleteContents();\r
-                                       }\r
-                               }\r
-\r
-                               try\r
+                               if ( endContainer &&\r
+                                               endContainer.type == CKEDITOR.NODE_ELEMENT &&\r
+                                               endContainer.getAttribute( 'contenteditable' ) == 'false' &&\r
+                                               range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )\r
                                {\r
-                                       $sel.createRange().pasteHTML( data );\r
+                                       range.setEndAfter( range.endContainer );\r
+                                       range.deleteContents();\r
                                }\r
-                               catch (e) {}\r
-\r
-                               if ( selIsLocked )\r
-                                       this.getSelection().lock();\r
                        }\r
-                       else\r
-                               this.document.$.execCommand( 'inserthtml', false, data );\r
 \r
-                       // Webkit does not scroll to the cursor position after pasting (#5558)\r
-                       if ( CKEDITOR.env.webkit )\r
+                       try\r
                        {\r
-                               this.document.$.execCommand( 'inserthtml', false, '<span id="cke_paste_marker" cke_temp="1"></span>' );\r
-                               var marker = this.document.getById( 'cke_paste_marker' );\r
-                               marker.scrollIntoView();\r
-                               marker.remove();\r
-                               marker = null;\r
+                               $sel.createRange().pasteHTML( data );\r
                        }\r
+                               catch (e) {}\r
 \r
-                       CKEDITOR.tools.setTimeout( function()\r
-                               {\r
-                                       this.fire( 'saveSnapshot' );\r
-                               }, 0, this );\r
+                       if ( selIsLocked )\r
+                               this.getSelection().lock();\r
+                       }\r
+               else\r
+                       this.document.$.execCommand( 'inserthtml', false, data );\r
+\r
+               // Webkit does not scroll to the cursor position after pasting (#5558)\r
+               if ( CKEDITOR.env.webkit )\r
+               {\r
+                       selection = this.getSelection();\r
+                       selection.scrollIntoView();\r
                }\r
        }\r
 \r
-       function onInsertElement( evt )\r
+       function doInsertText( text )\r
        {\r
-               if ( this.mode == 'wysiwyg' )\r
+               var selection = this.getSelection(),\r
+                       mode = selection.getStartElement().hasAscendant( 'pre', true ) ?\r
+                                  CKEDITOR.ENTER_BR : this.config.enterMode,\r
+                       isEnterBrMode = mode == CKEDITOR.ENTER_BR;\r
+\r
+               var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );\r
+\r
+               // Convert leading and trailing whitespaces into &nbsp;\r
+               html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )\r
+                       {\r
+                               if ( match.length == 1 )        // one space, preserve it\r
+                                       return '&nbsp;';\r
+                               else if ( !offset )             // beginning of block\r
+                                       return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';\r
+                               else                            // end of block\r
+                                       return ' ' + CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 );\r
+                       } );\r
+\r
+               // Convert subsequent whitespaces into &nbsp;\r
+               html = html.replace( /[ \t]{2,}/g, function ( match )\r
+                  {\r
+                          return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';\r
+                  } );\r
+\r
+               var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';\r
+\r
+               // Two line-breaks create one paragraph.\r
+               if ( !isEnterBrMode )\r
                {\r
-                       this.focus();\r
+                       html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,\r
+                               function( match, group1, text )\r
+                               {\r
+                                       return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';\r
+                               });\r
+               }\r
 \r
-                       var selection = this.getSelection();\r
-                       if ( checkReadOnly( selection ) )\r
-                               return;\r
+               // One <br> per line-break.\r
+               html = html.replace( /\n/g, '<br>' );\r
 \r
-                       this.fire( 'saveSnapshot' );\r
+               // Compensate padding <br> for non-IE.\r
+               if ( !( isEnterBrMode || CKEDITOR.env.ie ) )\r
+               {\r
+                       html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )\r
+                       {\r
+                               return CKEDITOR.tools.repeat( match, 2 );\r
+                       } );\r
+               }\r
 \r
-                       var ranges = selection.getRanges(),\r
-                               element = evt.data,\r
+               // Inline styles have to be inherited in Firefox.\r
+               if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )\r
+               {\r
+                       var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),\r
+                               context = [];\r
+\r
+                       for ( var i = 0; i < path.elements.length; i++ )\r
+                       {\r
+                               var tag = path.elements[ i ].getName();\r
+                               if ( tag in CKEDITOR.dtd.$inline )\r
+                                       context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );\r
+                               else if ( tag in CKEDITOR.dtd.$block )\r
+                                       break;\r
+                       }\r
+\r
+                       // Reproduce the context  by preceding the pasted HTML with opening inline tags.\r
+                       html = context.join( '' ) + html;\r
+               }\r
+\r
+               doInsertHtml.call( this, html );\r
+       }\r
+\r
+       function doInsertElement( element )\r
+       {\r
+               var selection = this.getSelection(),\r
+                               ranges = selection.getRanges(),\r
                                elementName = element.getName(),\r
                                isBlock = CKEDITOR.dtd.$block[ elementName ];\r
 \r
-                       var selIsLocked = selection.isLocked;\r
+               var selIsLocked = selection.isLocked;\r
 \r
-                       if ( selIsLocked )\r
-                               selection.unlock();\r
+               if ( selIsLocked )\r
+                       selection.unlock();\r
 \r
-                       var range, clone, lastElement, bookmark;\r
+               var range, clone, lastElement, bookmark;\r
 \r
-                       for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
-                       {\r
-                               range = ranges[ i ];\r
+               for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
+               {\r
+                       range = ranges[ i ];\r
 \r
-                               // Remove the original contents.\r
-                               range.deleteContents();\r
+                       // Remove the original contents.\r
+                       range.deleteContents();\r
 \r
-                               clone = !i && element || element.clone( 1 );\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
+                       // 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( 0, 1 ) )\r
+                                               && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
+                                               && !( dtd && dtd [ elementName ] ) )\r
                                {\r
-                                       while ( ( current = range.getCommonAncestor( 0, 1 ) )\r
-                                                       && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
-                                                       && !( dtd && dtd [ elementName ] ) )\r
+                                       // Split up inline elements.\r
+                                       if ( current.getName() in CKEDITOR.dtd.span )\r
+                                               range.splitElement( current );\r
+                                       // If we're in an empty block which indicate a new paragraph,\r
+                                       // simply replace it with the inserting block.(#3664)\r
+                                       else if ( range.checkStartOfBlock()\r
+                                                       && range.checkEndOfBlock() )\r
                                        {\r
-                                               // Split up inline elements.\r
-                                               if ( current.getName() in CKEDITOR.dtd.span )\r
-                                                       range.splitElement( current );\r
-                                               // If we're in an empty block which indicate a new paragraph,\r
-                                               // simply replace it with the inserting block.(#3664)\r
-                                               else if ( range.checkStartOfBlock()\r
-                                                        && range.checkEndOfBlock() )\r
-                                               {\r
-                                                       range.setStartBefore( current );\r
-                                                       range.collapse( true );\r
-                                                       current.remove();\r
-                                               }\r
-                                               else\r
-                                                       range.splitBlock();\r
+                                               range.setStartBefore( current );\r
+                                               range.collapse( true );\r
+                                               current.remove();\r
                                        }\r
+                                       else\r
+                                               range.splitBlock();\r
                                }\r
+                       }\r
 \r
-                               // Insert the new node.\r
-                               range.insertNode( clone );\r
+                       // Insert the new node.\r
+                       range.insertNode( clone );\r
 \r
-                               // Save the last element reference so we can make the\r
-                               // selection later.\r
-                               if ( !lastElement )\r
-                                       lastElement = clone;\r
-                       }\r
+                       // Save the last element reference so we can make the\r
+                       // selection later.\r
+                       if ( !lastElement )\r
+                               lastElement = clone;\r
+               }\r
 \r
-                       range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
+               range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
 \r
-                       // If we're inserting a block element immediatelly followed by\r
-                       // another block element, the selection must move there. (#3100,#5436)\r
-                       if ( isBlock )\r
-                       {\r
-                               var next = lastElement.getNext( notWhitespaceEval ),\r
+               // If we're inserting a block element immediatelly followed by\r
+               // another block element, the selection must move there. (#3100,#5436)\r
+               if ( isBlock )\r
+               {\r
+                       var next = lastElement.getNext( notWhitespaceEval ),\r
                                        nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();\r
 \r
-                               // Check if it's a block element that accepts text.\r
-                               if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )\r
-                                       range.moveToElementEditStart( next );\r
-                       }\r
-\r
-                       selection.selectRanges( [ range ] );\r
+                       // Check if it's a block element that accepts text.\r
+                       if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )\r
+                               range.moveToElementEditStart( next );\r
+               }\r
 \r
-                       if ( selIsLocked )\r
-                               this.getSelection().lock();\r
+               selection.selectRanges( [ range ] );\r
 \r
-                       // Save snaps after the whole execution completed.\r
-                       // This's a workaround for make DOM modification's happened after\r
-                       // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'\r
-                       // call.\r
-                       CKEDITOR.tools.setTimeout( function(){\r
-                               this.fire( 'saveSnapshot' );\r
-                       }, 0, this );\r
-               }\r
+               if ( selIsLocked )\r
+                       this.getSelection().lock();\r
        }\r
 \r
        // DOM modification here should not bother dirty flag.(#4385)\r
@@ -411,8 +474,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                if ( iframe )\r
                                                        iframe.remove();\r
 \r
-\r
-                                               var srcScript =\r
+                                               var src =\r
                                                        'document.open();' +\r
 \r
                                                        // The document domain must be set any time we\r
@@ -421,14 +483,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                        'document.close();';\r
 \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 =\r
+                                                       CKEDITOR.env.air ?\r
+                                                               'javascript:void(0)' :\r
+                                                       CKEDITOR.env.ie ?\r
+                                                               'javascript:void(function(){' + encodeURIComponent( src ) + '}())'\r
+                                                       :\r
+                                                               '';\r
+\r
                                                iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
                                                        ' style="width:100%;height:100%"' +\r
                                                        ' frameBorder="0"' +\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
+                                                       ' src="' + src + '"' +\r
                                                        ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +\r
                                                        ' allowTransparency="true"' +\r
                                                        '></iframe>' );\r
@@ -443,12 +513,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                frameLoaded = 1;\r
                                                                ev.removeListener();\r
 \r
-                                                               var doc = iframe.getFrameDocument().$;\r
-\r
-                                                               // Don't leave any history log in IE. (#5657)\r
-                                                               doc.open( "text/html","replace" );\r
+                                                               var doc = iframe.getFrameDocument();\r
                                                                doc.write( data );\r
-                                                               doc.close();\r
+\r
+                                                               CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );\r
                                                        });\r
 \r
                                                // Reset adjustment back to default (#5689)\r
@@ -485,7 +553,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        // 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
+                                               '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +\r
                                                        ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
                                                        'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +\r
                                                '</script>';\r
@@ -504,7 +572,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                // Remove this script from the DOM.\r
                                                var script = domDocument.getElementById( "cke_actscrpt" );\r
-                                               script.parentNode.removeChild( script );\r
+                                               script && script.parentNode.removeChild( script );\r
 \r
                                                body.spellcheck = !editor.config.disableNativeSpellChecker;\r
 \r
@@ -798,7 +866,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                // Build the additional stuff to be included into <head>.\r
                                                                var headExtra =\r
-                                                                       '<style type="text/css" cke_temp="1">' +\r
+                                                                       '<style type="text/css" data-cke-temp="1">' +\r
                                                                                editor._.styles.join( '\n' ) +\r
                                                                        '</style>';\r
 \r
@@ -806,7 +874,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +\r
                                                                        headExtra );\r
 \r
-                                                               var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" cke_temp="1" />' : '';\r
+                                                               var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';\r
 \r
                                                                if ( fullPage )\r
                                                                {\r
@@ -944,6 +1012,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                        focus : function()\r
                                                        {\r
+                                                               var win = editor.window;\r
+\r
                                                                if ( isLoadingData )\r
                                                                        isPendingFocus = true;\r
                                                                // Temporary solution caused by #6025, supposed be unified by #6154.\r
@@ -957,17 +1027,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                        editor.selectionChange();\r
                                                                }\r
-                                                               else if ( !CKEDITOR.env.opera && editor.window )\r
+                                                               else if ( !CKEDITOR.env.opera && win )\r
                                                                {\r
-                                                                       editor.window.focus();\r
-\r
+                                                                       // AIR needs a while to focus when moving from a link.\r
+                                                                       CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();\r
                                                                        editor.selectionChange();\r
                                                                }\r
                                                        }\r
                                                });\r
 \r
-                                       editor.on( 'insertHtml', onInsertHtml, null, null, 20 );\r
-                                       editor.on( 'insertElement', onInsertElement, null, null, 20 );\r
+                                       editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );\r
+                                       editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );\r
+                                       editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );\r
                                        // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)\r
                                        editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );\r
                                });\r
@@ -978,7 +1049,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        editor.on( 'contentDom', function()\r
                                {\r
                                        var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );\r
-                                       title.setAttribute( '_cke_title', editor.document.$.title );\r
+                                       title.data( 'cke-title', editor.document.$.title );\r
                                        editor.document.$.title = frameLabel;\r
                                });\r
 \r