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