2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
8 CKEDITOR.plugins.add( 'enterkey',
10 requires : [ 'keystrokes', 'indent' ],
12 init : function( editor )
14 editor.addCommand( 'enter', {
15 modes : { wysiwyg:1 },
17 exec : function( editor ){ enter( editor ); }
20 editor.addCommand( 'shiftEnter', {
21 modes : { wysiwyg:1 },
23 exec : function( editor ){ shiftEnter( editor ); }
26 var keystrokes = editor.keystrokeHandler.keystrokes;
27 keystrokes[ 13 ] = 'enter';
28 keystrokes[ CKEDITOR.SHIFT + 13 ] = 'shiftEnter';
32 CKEDITOR.plugins.enterkey =
34 enterBlock : function( editor, mode, range, forceMode )
36 // Get the range for the current selection.
37 range = range || getRange( editor );
39 // We may not have valid ranges to work on, like when inside a
40 // contenteditable=false element.
44 var doc = range.document;
46 var atBlockStart = range.checkStartOfBlock(),
47 atBlockEnd = range.checkEndOfBlock(),
48 path = new CKEDITOR.dom.elementPath( range.startContainer ),
51 if ( atBlockStart && atBlockEnd )
53 // Exit the list when we're inside an empty list item block. (#5376)
54 if ( block && ( block.is( 'li' ) || block.getParent().is( 'li' ) ) )
56 editor.execCommand( 'outdent' );
60 if ( block && block.getParent().is( 'blockquote' ) )
62 block.breakParent( block.getParent() );
64 // If we were at the start of <blockquote>, there will be an empty element before it now.
65 if ( !block.getPrevious().getFirst( CKEDITOR.dom.walker.invisible(1) ) )
66 block.getPrevious().remove();
68 // If we were at the end of <blockquote>, there will be an empty element after it now.
69 if ( !block.getNext().getFirst( CKEDITOR.dom.walker.invisible(1) ) )
70 block.getNext().remove();
72 range.moveToElementEditStart( block );
77 // Don't split <pre> if we're in the middle of it, act as shift enter key.
78 else if ( block && block.is( 'pre' ) )
82 enterBr( editor, mode, range, forceMode );
86 // Don't split caption blocks. (#7944)
87 else if ( block && CKEDITOR.dtd.$captionBlock[ block.getName() ] )
89 enterBr( editor, mode, range, forceMode );
93 // Determine the block element to be used.
94 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
97 var splitInfo = range.splitBlock( blockTag );
102 // Get the current blocks.
103 var previousBlock = splitInfo.previousBlock,
104 nextBlock = splitInfo.nextBlock;
106 var isStartOfBlock = splitInfo.wasStartOfBlock,
107 isEndOfBlock = splitInfo.wasEndOfBlock;
111 // If this is a block under a list item, split it as well. (#1647)
114 node = nextBlock.getParent();
115 if ( node.is( 'li' ) )
117 nextBlock.breakParent( node );
118 nextBlock.move( nextBlock.getNext(), 1 );
121 else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )
123 previousBlock.breakParent( node );
124 node = previousBlock.getNext();
125 range.moveToElementEditStart( node );
126 previousBlock.move( previousBlock.getPrevious() );
129 // If we have both the previous and next blocks, it means that the
130 // boundaries were on separated blocks, or none of them where on the
131 // block limits (start/end).
132 if ( !isStartOfBlock && !isEndOfBlock )
134 // If the next block is an <li> with another list tree as the first
135 // child, we'll need to append a filler (<br>/NBSP) or the list item
136 // wouldn't be editable. (#1420)
137 if ( nextBlock.is( 'li' )
138 && ( node = nextBlock.getFirst( CKEDITOR.dom.walker.invisible( true ) ) )
139 && node.is && node.is( 'ul', 'ol' ) )
140 ( CKEDITOR.env.ie ? doc.createText( '\xa0' ) : doc.createElement( 'br' ) ).insertBefore( node );
142 // Move the selection to the end block.
144 range.moveToElementEditStart( nextBlock );
153 // Do not enter this block if it's a header tag, or we are in
154 // a Shift+Enter (#77). Create a new block element instead
155 // (later in the code).
156 if ( previousBlock.is( 'li' ) ||
157 ! ( headerTagRegex.test( previousBlock.getName() ) || previousBlock.is( 'pre' ) ) )
159 // Otherwise, duplicate the previous block.
160 newBlock = previousBlock.clone();
163 else if ( nextBlock )
164 newBlock = nextBlock.clone();
168 // We have already created a new list item. (#6849)
169 if ( node && node.is( 'li' ) )
173 newBlock = doc.createElement( blockTag );
174 if ( previousBlock && ( newBlockDir = previousBlock.getDirection() ) )
175 newBlock.setAttribute( 'dir', newBlockDir );
178 // Force the enter block unless we're talking of a list item.
179 else if ( forceMode && !newBlock.is( 'li' ) )
180 newBlock.renameNode( blockTag );
182 // Recreate the inline elements tree, which was available
183 // before hitting enter, so the same styles will be available in
185 var elementPath = splitInfo.elementPath;
188 for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )
190 var element = elementPath.elements[ i ];
192 if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )
195 if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )
197 element = element.clone();
198 newBlock.moveChildren( element );
199 newBlock.append( element );
204 if ( !CKEDITOR.env.ie )
205 newBlock.appendBogus();
207 if ( !newBlock.getParent() )
208 range.insertNode( newBlock );
210 // list item start number should not be duplicated (#7330), but we need
211 // to remove the attribute after it's onto the DOM tree because of old IEs (#7581).
212 newBlock.is( 'li' ) && newBlock.removeAttribute( 'value' );
214 // This is tricky, but to make the new block visible correctly
215 // we must select it.
216 // The previousBlock check has been included because it may be
217 // empty if we have fixed a block-less space (like ENTER into an
218 // empty table cell).
219 if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )
221 // Move the selection to the new block.
222 range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );
226 // Move the selection to the new block.
227 range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );
230 if ( !CKEDITOR.env.ie )
234 // If we have split the block, adds a temporary span at the
235 // range position and scroll relatively to it.
236 var tmpNode = doc.createElement( 'span' );
238 // We need some content for Safari.
239 tmpNode.setHtml( ' ' );
241 range.insertNode( tmpNode );
242 tmpNode.scrollIntoView();
243 range.deleteContents();
247 // We may use the above scroll logic for the new block case
248 // too, but it gives some weird result with Opera.
249 newBlock.scrollIntoView();
256 enterBr : function( editor, mode, range, forceMode )
258 // Get the range for the current selection.
259 range = range || getRange( editor );
261 // We may not have valid ranges to work on, like when inside a
262 // contenteditable=false element.
266 var doc = range.document;
268 // Determine the block element to be used.
269 var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
271 var isEndOfBlock = range.checkEndOfBlock();
273 var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );
275 var startBlock = elementPath.block,
276 startBlockTag = startBlock && elementPath.block.getName();
280 if ( !forceMode && startBlockTag == 'li' )
282 enterBlock( editor, mode, range, forceMode );
286 // If we are at the end of a header block.
287 if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )
292 if ( ( newBlockDir = startBlock.getDirection() ) )
294 newBlock = doc.createElement( 'div' );
295 newBlock.setAttribute( 'dir', newBlockDir );
296 newBlock.insertAfter( startBlock );
297 range.setStart( newBlock, 0 );
301 // Insert a <br> after the current paragraph.
302 doc.createElement( 'br' ).insertAfter( startBlock );
304 // A text node is required by Gecko only to make the cursor blink.
305 if ( CKEDITOR.env.gecko )
306 doc.createText( '' ).insertAfter( startBlock );
308 // IE has different behaviors regarding position.
309 range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );
316 isPre = ( startBlockTag == 'pre' );
318 // Gecko prefers <br> as line-break inside <pre> (#4711).
319 if ( isPre && !CKEDITOR.env.gecko )
320 lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );
322 lineBreak = doc.createElement( 'br' );
324 range.deleteContents();
325 range.insertNode( lineBreak );
327 // IE has different behavior regarding position.
328 if ( CKEDITOR.env.ie )
329 range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );
332 // A text node is required by Gecko only to make the cursor blink.
333 // We need some text inside of it, so the bogus <br> is properly
335 doc.createText( '\ufeff' ).insertAfter( lineBreak );
337 // If we are at the end of a block, we must be sure the bogus node is available in that block.
339 lineBreak.getParent().appendBogus();
341 // Now we can remove the text node contents, so the caret doesn't
343 lineBreak.getNext().$.nodeValue = '';
345 range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );
347 // Scroll into view, for non IE.
350 // BR is not positioned in Opera and Webkit.
351 if ( !CKEDITOR.env.gecko )
353 dummy = doc.createElement( 'span' );
354 // We need have some contents for Webkit to position it
355 // under parent node. ( #3681)
356 dummy.setHtml(' ');
359 dummy = doc.createElement( 'br' );
361 dummy.insertBefore( lineBreak.getNext() );
362 dummy.scrollIntoView();
367 // This collapse guarantees the cursor will be blinking.
368 range.collapse( true );
370 range.select( isPre );
374 var plugin = CKEDITOR.plugins.enterkey,
375 enterBr = plugin.enterBr,
376 enterBlock = plugin.enterBlock,
377 headerTagRegex = /^h[1-6]$/;
379 function shiftEnter( editor )
381 // Only effective within document.
382 if ( editor.mode != 'wysiwyg' )
386 // 1. We want to enforce the mode to be respected, instead
387 // of cloning the current block. (#77)
388 return enter( editor, editor.config.shiftEnterMode, 1 );
391 function enter( editor, mode, forceMode )
393 forceMode = editor.config.forceEnterMode || forceMode;
395 // Only effective within document.
396 if ( editor.mode != 'wysiwyg' )
400 mode = editor.config.enterMode;
402 // Use setTimout so the keys get cancelled immediatelly.
403 setTimeout( function()
405 editor.fire( 'saveSnapshot' ); // Save undo step.
407 if ( mode == CKEDITOR.ENTER_BR )
408 enterBr( editor, mode, null, forceMode );
410 enterBlock( editor, mode, null, forceMode );
412 editor.fire( 'saveSnapshot' );
419 function getRange( editor )
421 // Get the selection ranges.
422 var ranges = editor.getSelection().getRanges( true );
424 // Delete the contents of all ranges except the first one.
425 for ( var i = ranges.length - 1 ; i > 0 ; i-- )
427 ranges[ i ].deleteContents();
430 // Return the first range.