JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
5046b1d0ddc9d1b1fb0145b7164f1d96876b7bfa
[ckeditor.git] / _source / plugins / enterkey / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 (function()\r
7 {\r
8         CKEDITOR.plugins.add( 'enterkey',\r
9         {\r
10                 requires : [ 'keystrokes', 'indent' ],\r
11 \r
12                 init : function( editor )\r
13                 {\r
14                         var specialKeys = editor.specialKeys;\r
15                         specialKeys[ 13 ] = enter;\r
16                         specialKeys[ CKEDITOR.SHIFT + 13 ] = shiftEnter;\r
17                 }\r
18         });\r
19 \r
20         CKEDITOR.plugins.enterkey =\r
21         {\r
22                 enterBlock : function( editor, mode, range, forceMode )\r
23                 {\r
24                         // Get the range for the current selection.\r
25                         range = range || getRange( editor );\r
26 \r
27                         // We may not have valid ranges to work on, like when inside a\r
28                         // contenteditable=false element.\r
29                         if ( !range )\r
30                                 return;\r
31 \r
32                         var doc = range.document;\r
33 \r
34                         // Exit the list when we're inside an empty list item block. (#5376)\r
35                         if ( range.checkStartOfBlock() && range.checkEndOfBlock() )\r
36                         {\r
37                                 var path = new CKEDITOR.dom.elementPath( range.startContainer ),\r
38                                                 block = path.block;\r
39 \r
40                                 if ( block && ( block.is( 'li' ) || block.getParent().is( 'li' ) ) )\r
41                                 {\r
42                                         editor.execCommand( 'outdent' );\r
43                                         return;\r
44                                 }\r
45                         }\r
46 \r
47                         // Determine the block element to be used.\r
48                         var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
49 \r
50                         // Split the range.\r
51                         var splitInfo = range.splitBlock( blockTag );\r
52 \r
53                         if ( !splitInfo )\r
54                                 return;\r
55 \r
56                         // Get the current blocks.\r
57                         var previousBlock       = splitInfo.previousBlock,\r
58                                 nextBlock               = splitInfo.nextBlock;\r
59 \r
60                         var isStartOfBlock      = splitInfo.wasStartOfBlock,\r
61                                 isEndOfBlock    = splitInfo.wasEndOfBlock;\r
62 \r
63                         var node;\r
64 \r
65                         // If this is a block under a list item, split it as well. (#1647)\r
66                         if ( nextBlock )\r
67                         {\r
68                                 node = nextBlock.getParent();\r
69                                 if ( node.is( 'li' ) )\r
70                                 {\r
71                                         nextBlock.breakParent( node );\r
72                                         nextBlock.move( nextBlock.getNext(), 1 );\r
73                                 }\r
74                         }\r
75                         else if ( previousBlock && ( node = previousBlock.getParent() ) && node.is( 'li' ) )\r
76                         {\r
77                                 previousBlock.breakParent( node );\r
78                                 range.moveToElementEditStart( previousBlock.getNext() );\r
79                                 previousBlock.move( previousBlock.getPrevious() );\r
80                         }\r
81 \r
82                         // If we have both the previous and next blocks, it means that the\r
83                         // boundaries were on separated blocks, or none of them where on the\r
84                         // block limits (start/end).\r
85                         if ( !isStartOfBlock && !isEndOfBlock )\r
86                         {\r
87                                 // If the next block is an <li> with another list tree as the first\r
88                                 // child, we'll need to append a filler (<br>/NBSP) or the list item\r
89                                 // wouldn't be editable. (#1420)\r
90                                 if ( nextBlock.is( 'li' )\r
91                                          && ( node = nextBlock.getFirst( CKEDITOR.dom.walker.invisible( true ) ) )\r
92                                          && node.is && node.is( 'ul', 'ol' ) )\r
93                                         ( CKEDITOR.env.ie ? doc.createText( '\xa0' ) : doc.createElement( 'br' ) ).insertBefore( node );\r
94 \r
95                                 // Move the selection to the end block.\r
96                                 if ( nextBlock )\r
97                                         range.moveToElementEditStart( nextBlock );\r
98                         }\r
99                         else\r
100                         {\r
101                                 var newBlock;\r
102 \r
103                                 if ( previousBlock )\r
104                                 {\r
105                                         // Do not enter this block if it's a header tag, or we are in\r
106                                         // a Shift+Enter (#77). Create a new block element instead\r
107                                         // (later in the code).\r
108                                         if ( previousBlock.is( 'li' ) || !headerTagRegex.test( previousBlock.getName() ) )\r
109                                         {\r
110                                                 // Otherwise, duplicate the previous block.\r
111                                                 newBlock = previousBlock.clone();\r
112                                         }\r
113                                 }\r
114                                 else if ( nextBlock )\r
115                                         newBlock = nextBlock.clone();\r
116 \r
117                                 if ( !newBlock )\r
118                                         newBlock = doc.createElement( blockTag );\r
119                                 // Force the enter block unless we're talking of a list item.\r
120                                 else if ( forceMode && !newBlock.is( 'li' ) )\r
121                                         newBlock.renameNode( blockTag );\r
122 \r
123                                 // Recreate the inline elements tree, which was available\r
124                                 // before hitting enter, so the same styles will be available in\r
125                                 // the new block.\r
126                                 var elementPath = splitInfo.elementPath;\r
127                                 if ( elementPath )\r
128                                 {\r
129                                         for ( var i = 0, len = elementPath.elements.length ; i < len ; i++ )\r
130                                         {\r
131                                                 var element = elementPath.elements[ i ];\r
132 \r
133                                                 if ( element.equals( elementPath.block ) || element.equals( elementPath.blockLimit ) )\r
134                                                         break;\r
135 \r
136                                                 if ( CKEDITOR.dtd.$removeEmpty[ element.getName() ] )\r
137                                                 {\r
138                                                         element = element.clone();\r
139                                                         newBlock.moveChildren( element );\r
140                                                         newBlock.append( element );\r
141                                                 }\r
142                                         }\r
143                                 }\r
144 \r
145                                 if ( !CKEDITOR.env.ie )\r
146                                         newBlock.appendBogus();\r
147 \r
148                                 range.insertNode( newBlock );\r
149 \r
150                                 // This is tricky, but to make the new block visible correctly\r
151                                 // we must select it.\r
152                                 // The previousBlock check has been included because it may be\r
153                                 // empty if we have fixed a block-less space (like ENTER into an\r
154                                 // empty table cell).\r
155                                 if ( CKEDITOR.env.ie && isStartOfBlock && ( !isEndOfBlock || !previousBlock.getChildCount() ) )\r
156                                 {\r
157                                         // Move the selection to the new block.\r
158                                         range.moveToElementEditStart( isEndOfBlock ? previousBlock : newBlock );\r
159                                         range.select();\r
160                                 }\r
161 \r
162                                 // Move the selection to the new block.\r
163                                 range.moveToElementEditStart( isStartOfBlock && !isEndOfBlock ? nextBlock : newBlock );\r
164                 }\r
165 \r
166                         if ( !CKEDITOR.env.ie )\r
167                         {\r
168                                 if ( nextBlock )\r
169                                 {\r
170                                         // If we have split the block, adds a temporary span at the\r
171                                         // range position and scroll relatively to it.\r
172                                         var tmpNode = doc.createElement( 'span' );\r
173 \r
174                                         // We need some content for Safari.\r
175                                         tmpNode.setHtml( '&nbsp;' );\r
176 \r
177                                         range.insertNode( tmpNode );\r
178                                         tmpNode.scrollIntoView();\r
179                                         range.deleteContents();\r
180                                 }\r
181                                 else\r
182                                 {\r
183                                         // We may use the above scroll logic for the new block case\r
184                                         // too, but it gives some weird result with Opera.\r
185                                         newBlock.scrollIntoView();\r
186                                 }\r
187                         }\r
188 \r
189                         range.select();\r
190                 },\r
191 \r
192                 enterBr : function( editor, mode, range, forceMode )\r
193                 {\r
194                         // Get the range for the current selection.\r
195                         range = range || getRange( editor );\r
196 \r
197                         // We may not have valid ranges to work on, like when inside a\r
198                         // contenteditable=false element.\r
199                         if ( !range )\r
200                                 return;\r
201 \r
202                         var doc = range.document;\r
203 \r
204                         // Determine the block element to be used.\r
205                         var blockTag = ( mode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );\r
206 \r
207                         var isEndOfBlock = range.checkEndOfBlock();\r
208 \r
209                         var elementPath = new CKEDITOR.dom.elementPath( editor.getSelection().getStartElement() );\r
210 \r
211                         var startBlock = elementPath.block,\r
212                                 startBlockTag = startBlock && elementPath.block.getName();\r
213 \r
214                         var isPre = false;\r
215 \r
216                         if ( !forceMode && startBlockTag == 'li' )\r
217                         {\r
218                                 enterBlock( editor, mode, range, forceMode );\r
219                                 return;\r
220                         }\r
221 \r
222                         // If we are at the end of a header block.\r
223                         if ( !forceMode && isEndOfBlock && headerTagRegex.test( startBlockTag ) )\r
224                         {\r
225                                 // Insert a <br> after the current paragraph.\r
226                                 doc.createElement( 'br' ).insertAfter( startBlock );\r
227 \r
228                                 // A text node is required by Gecko only to make the cursor blink.\r
229                                 if ( CKEDITOR.env.gecko )\r
230                                         doc.createText( '' ).insertAfter( startBlock );\r
231 \r
232                                 // IE has different behaviors regarding position.\r
233                                 range.setStartAt( startBlock.getNext(), CKEDITOR.env.ie ? CKEDITOR.POSITION_BEFORE_START : CKEDITOR.POSITION_AFTER_START );\r
234                         }\r
235                         else\r
236                         {\r
237                                 var lineBreak;\r
238 \r
239                                 isPre = ( startBlockTag == 'pre' );\r
240 \r
241                                 // Gecko prefers <br> as line-break inside <pre> (#4711).\r
242                                 if ( isPre && !CKEDITOR.env.gecko )\r
243                                         lineBreak = doc.createText( CKEDITOR.env.ie ? '\r' : '\n' );\r
244                                 else\r
245                                         lineBreak = doc.createElement( 'br' );\r
246 \r
247                                 range.deleteContents();\r
248                                 range.insertNode( lineBreak );\r
249 \r
250                                 // A text node is required by Gecko only to make the cursor blink.\r
251                                 // We need some text inside of it, so the bogus <br> is properly\r
252                                 // created.\r
253                                 if ( !CKEDITOR.env.ie )\r
254                                         doc.createText( '\ufeff' ).insertAfter( lineBreak );\r
255 \r
256                                 // If we are at the end of a block, we must be sure the bogus node is available in that block.\r
257                                 if ( isEndOfBlock && !CKEDITOR.env.ie )\r
258                                         lineBreak.getParent().appendBogus();\r
259 \r
260                                 // Now we can remove the text node contents, so the caret doesn't\r
261                                 // stop on it.\r
262                                 if ( !CKEDITOR.env.ie )\r
263                                         lineBreak.getNext().$.nodeValue = '';\r
264                                 // IE has different behavior regarding position.\r
265                                 if ( CKEDITOR.env.ie )\r
266                                         range.setStartAt( lineBreak, CKEDITOR.POSITION_AFTER_END );\r
267                                 else\r
268                                         range.setStartAt( lineBreak.getNext(), CKEDITOR.POSITION_AFTER_START );\r
269 \r
270                                 // Scroll into view, for non IE.\r
271                                 if ( !CKEDITOR.env.ie )\r
272                                 {\r
273                                         var dummy = null;\r
274 \r
275                                         // BR is not positioned in Opera and Webkit.\r
276                                         if ( !CKEDITOR.env.gecko )\r
277                                         {\r
278                                                 dummy = doc.createElement( 'span' );\r
279                                                 // We need have some contents for Webkit to position it\r
280                                                 // under parent node. ( #3681)\r
281                                                 dummy.setHtml('&nbsp;');\r
282                                         }\r
283                                         else\r
284                                                 dummy = doc.createElement( 'br' );\r
285 \r
286                                         dummy.insertBefore( lineBreak.getNext() );\r
287                                         dummy.scrollIntoView();\r
288                                         dummy.remove();\r
289                                 }\r
290                         }\r
291 \r
292                         // This collapse guarantees the cursor will be blinking.\r
293                         range.collapse( true );\r
294 \r
295                         range.select( isPre );\r
296                 }\r
297         };\r
298 \r
299         var plugin = CKEDITOR.plugins.enterkey,\r
300                 enterBr = plugin.enterBr,\r
301                 enterBlock = plugin.enterBlock,\r
302                 headerTagRegex = /^h[1-6]$/;\r
303 \r
304         function shiftEnter( editor )\r
305         {\r
306                 // Only effective within document.\r
307                 if ( editor.mode != 'wysiwyg' )\r
308                         return false;\r
309 \r
310                 // On SHIFT+ENTER:\r
311                 // 1. We want to enforce the mode to be respected, instead\r
312                 // of cloning the current block. (#77)\r
313                 // 2. Always perform a block break when inside <pre> (#5402).\r
314                 if ( editor.getSelection().getStartElement().hasAscendant( 'pre', true ) )\r
315                 {\r
316                         setTimeout( function() { enterBlock( editor, editor.config.enterMode, null, true ); }, 0 );\r
317                         return true;\r
318                 }\r
319                 else\r
320                         return enter( editor, editor.config.shiftEnterMode, 1 );\r
321         }\r
322 \r
323         function enter( editor, mode, forceMode )\r
324         {\r
325                 forceMode = editor.config.forceEnterMode || forceMode;\r
326 \r
327                 // Only effective within document.\r
328                 if ( editor.mode != 'wysiwyg' )\r
329                         return false;\r
330 \r
331                 if ( !mode )\r
332                         mode = editor.config.enterMode;\r
333 \r
334                 // Use setTimout so the keys get cancelled immediatelly.\r
335                 setTimeout( function()\r
336                         {\r
337                                 editor.fire( 'saveSnapshot' );  // Save undo step.\r
338                                 if ( mode == CKEDITOR.ENTER_BR || editor.getSelection().getStartElement().hasAscendant( 'pre', 1 ) )\r
339                                         enterBr( editor, mode, null, forceMode );\r
340                                 else\r
341                                         enterBlock( editor, mode, null, forceMode );\r
342 \r
343                         }, 0 );\r
344 \r
345                 return true;\r
346         }\r
347 \r
348 \r
349         function getRange( editor )\r
350         {\r
351                 // Get the selection ranges.\r
352                 var ranges = editor.getSelection().getRanges( true );\r
353 \r
354                 // Delete the contents of all ranges except the first one.\r
355                 for ( var i = ranges.length - 1 ; i > 0 ; i-- )\r
356                 {\r
357                         ranges[ i ].deleteContents();\r
358                 }\r
359 \r
360                 // Return the first range.\r
361                 return ranges[ 0 ];\r
362         }\r
363 })();\r