JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.3
[ckeditor.git] / _source / plugins / enterkey / plugin.js
index 0127ede..4114516 100644 (file)
@@ -24,8 +24,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // Get the range for the current selection.\r
                        range = range || getRange( editor );\r
 \r
+                       // We may not have valid ranges to work on, like when inside a\r
+                       // contenteditable=false element.\r
+                       if ( !range )\r
+                               return;\r
+\r
                        var doc = range.document;\r
 \r
+                       // Exit the list when we're inside an empty list item block. (#5376)\r
+                       if ( range.checkStartOfBlock() && range.checkEndOfBlock() )\r
+                       {\r
+                               var path = new CKEDITOR.dom.elementPath( range.startContainer ),\r
+                                               block = path.block;\r
+\r
+                               if ( block && ( block.is( 'li' ) || block.getParent().is( 'li' ) ) )\r
+                               {\r
+                                       editor.execCommand( 'outdent' );\r
+                                       return;\r
+                               }\r
+                       }\r
+\r
                        // Determine the block element to be used.\r
                        var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
 \r
@@ -51,7 +69,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                if ( node.is( 'li' ) )\r
                                {\r
                                        nextBlock.breakParent( node );\r
-                                       nextBlock.move( nextBlock.getNext(), true );\r
+                                       nextBlock.move( nextBlock.getNext(), 1 );\r
                                }\r
                        }\r
                        else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )\r
@@ -80,21 +98,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                        else\r
                        {\r
-\r
-                               if ( isStartOfBlock && isEndOfBlock && previousBlock.is( 'li' ) )\r
-                               {\r
-                                       editor.execCommand( 'outdent' );\r
-                                       return;\r
-                               }\r
-\r
-                               var newBlock;\r
+                               var newBlock,\r
+                                       newBlockDir;\r
 \r
                                if ( previousBlock )\r
                                {\r
                                        // Do not enter this block if it's a header tag, or we are in\r
                                        // a Shift+Enter (#77). Create a new block element instead\r
                                        // (later in the code).\r
-                                       if ( !forceMode && !headerTagRegex.test( previousBlock.getName() ) )\r
+                                       if ( previousBlock.is( 'li' ) || !headerTagRegex.test( previousBlock.getName() ) )\r
                                        {\r
                                                // Otherwise, duplicate the previous block.\r
                                                newBlock = previousBlock.clone();\r
@@ -104,7 +116,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        newBlock = nextBlock.clone();\r
 \r
                                if ( !newBlock )\r
+                               {\r
                                        newBlock = doc.createElement( blockTag );\r
+                                       if ( previousBlock && ( newBlockDir = previousBlock.getDirection() ) )\r
+                                               newBlock.setAttribute( 'dir', newBlockDir );\r
+                               }\r
+                               // Force the enter block unless we're talking of a list item.\r
+                               else if ( forceMode && !newBlock.is( 'li' ) )\r
+                                       newBlock.renameNode( blockTag );\r
 \r
                                // Recreate the inline elements tree, which was available\r
                                // before hitting enter, so the same styles will be available in\r
@@ -180,6 +199,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // Get the range for the current selection.\r
                        range = range || getRange( editor );\r
 \r
+                       // We may not have valid ranges to work on, like when inside a\r
+                       // contenteditable=false element.\r
+                       if ( !range )\r
+                               return;\r
+\r
                        var doc = range.document;\r
 \r
                        // Determine the block element to be used.\r
@@ -203,15 +227,28 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // If we are at the end of a header block.\r
                        if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )\r
                        {\r
-                               // Insert a <br> after the current paragraph.\r
-                               doc.createElement( 'br' ).insertAfter( startBlock );\r
+                               var newBlock,\r
+                                       newBlockDir;\r
 \r
-                               // A text node is required by Gecko only to make the cursor blink.\r
-                               if ( CKEDITOR.env.gecko )\r
-                                       doc.createText( '' ).insertAfter( startBlock );\r
+                               if ( newBlockDir = startBlock.getDirection() )\r
+                               {\r
+                                       newBlock = doc.createElement( 'div' );\r
+                                       newBlock.setAttribute( 'dir', newBlockDir );\r
+                                       newBlock.insertAfter( startBlock );\r
+                                       range.setStart( newBlock, 0 );\r
+                               }\r
+                               else\r
+                               {\r
+                                       // Insert a <br> after the current paragraph.\r
+                                       doc.createElement( 'br' ).insertAfter( startBlock );\r
+\r
+                                       // A text node is required by Gecko only to make the cursor blink.\r
+                                       if ( CKEDITOR.env.gecko )\r
+                                               doc.createText( '' ).insertAfter( startBlock );\r
 \r
-                               // IE has different behaviors regarding position.\r
-                               range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );\r
+                                       // IE has different behaviors regarding position.\r
+                                       range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );\r
+                               }\r
                        }\r
                        else\r
                        {\r
@@ -219,7 +256,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                isPre = ( startBlockTag == 'pre' );\r
 \r
-                               if ( isPre )\r
+                               // Gecko prefers <br> as line-break inside <pre> (#4711).\r
+                               if ( isPre && !CKEDITOR.env.gecko )\r
                                        lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );\r
                                else\r
                                        lineBreak = doc.createElement( 'br' );\r
@@ -283,13 +321,27 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        function shiftEnter( editor )\r
        {\r
-               // On SHIFT+ENTER we want to enforce the mode to be respected, instead\r
+               // Only effective within document.\r
+               if ( editor.mode != 'wysiwyg' )\r
+                       return false;\r
+\r
+               // On SHIFT+ENTER:\r
+               // 1. We want to enforce the mode to be respected, instead\r
                // of cloning the current block. (#77)\r
-               return enter( editor, editor.config.shiftEnterMode, true );\r
+               // 2. Always perform a block break when inside <pre> (#5402).\r
+               if ( editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )\r
+               {\r
+                       setTimeout( function() { enterBlock( editor, editor.config.enterMode, null, true ); }, 0 );\r
+                       return true;\r
+               }\r
+               else\r
+                       return enter( editor, editor.config.shiftEnterMode, 1 );\r
        }\r
 \r
        function enter( editor, mode, forceMode )\r
        {\r
+               forceMode = editor.config.forceEnterMode || forceMode;\r
+\r
                // Only effective within document.\r
                if ( editor.mode != 'wysiwyg' )\r
                        return false;\r
@@ -301,7 +353,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                setTimeout( function()\r
                        {\r
                                editor.fire( 'saveSnapshot' );  // Save undo step.\r
-                               if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )\r
+                               if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', 1 ) )\r
                                        enterBr( editor, mode, null, forceMode );\r
                                else\r
                                        enterBlock( editor, mode, null, forceMode );\r
@@ -315,7 +367,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        function getRange( editor )\r
        {\r
                // Get the selection ranges.\r
-               var ranges = editor.getSelection().getRanges();\r
+               var ranges = editor.getSelection().getRanges( true );\r
 \r
                // Delete the contents of all ranges except the first one.\r
                for ( var i = ranges.length - 1 ; i > 0 ; i-- )\r