2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2010 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* Controls the [Enter] keystroke behavior in a document.
26
* @targetDocument : the target document.
27
* @enterMode : the behavior for the <Enter> keystroke.
28
* May be "p", "div", "br". Default is "p".
29
* @shiftEnterMode : the behavior for the <Shift>+<Enter> keystroke.
30
* May be "p", "div", "br". Defaults to "br".
32
var FCKEnterKey = function( targetWindow, enterMode, shiftEnterMode, tabSpaces )
34
this.Window = targetWindow ;
35
this.EnterMode = enterMode || 'p' ;
36
this.ShiftEnterMode = shiftEnterMode || 'br' ;
38
// Setup the Keystroke Handler.
39
var oKeystrokeHandler = new FCKKeystrokeHandler( false ) ;
40
oKeystrokeHandler._EnterKey = this ;
41
oKeystrokeHandler.OnKeystroke = FCKEnterKey_OnKeystroke ;
43
oKeystrokeHandler.SetKeystrokes( [
45
[ SHIFT + 13, 'ShiftEnter' ],
47
[ CTRL + 8 , 'CtrlBackspace' ],
53
// Safari by default inserts 4 spaces on TAB, while others make the editor
54
// loose focus. So, we need to handle it here to not include those spaces.
55
if ( tabSpaces > 0 || FCKBrowserInfo.IsSafari )
58
this.TabText += '\xa0' ;
60
oKeystrokeHandler.SetKeystrokes( [ 9, 'Tab' ] );
63
oKeystrokeHandler.AttachToElement( targetWindow.document ) ;
67
function FCKEnterKey_OnKeystroke( keyCombination, keystrokeValue )
69
var oEnterKey = this._EnterKey ;
73
switch ( keystrokeValue )
76
return oEnterKey.DoEnter() ;
79
return oEnterKey.DoShiftEnter() ;
82
return oEnterKey.DoBackspace() ;
85
return oEnterKey.DoDelete() ;
88
return oEnterKey.DoTab() ;
90
case 'CtrlBackspace' :
91
return oEnterKey.DoCtrlBackspace() ;
97
// If for any reason we are not able to handle it, go
98
// ahead with the browser default behavior.
105
* Executes the <Enter> key behavior.
107
FCKEnterKey.prototype.DoEnter = function( mode, hasShift )
109
// Save an undo snapshot before doing anything
110
FCKUndo.SaveUndoStep() ;
112
this._HasShift = ( hasShift === true ) ;
114
var parentElement = FCKSelection.GetParentElement() ;
115
var parentPath = new FCKElementPath( parentElement ) ;
116
var sMode = mode || this.EnterMode ;
118
if ( sMode == 'br' || parentPath.Block && parentPath.Block.tagName.toLowerCase() == 'pre' )
119
return this._ExecuteEnterBr() ;
121
return this._ExecuteEnterBlock( sMode ) ;
125
* Executes the <Shift>+<Enter> key behavior.
127
FCKEnterKey.prototype.DoShiftEnter = function()
129
return this.DoEnter( this.ShiftEnterMode, true ) ;
133
* Executes the <Backspace> key behavior.
135
FCKEnterKey.prototype.DoBackspace = function()
137
var bCustom = false ;
139
// Get the current selection.
140
var oRange = new FCKDomRange( this.Window ) ;
141
oRange.MoveToSelection() ;
144
if ( FCKBrowserInfo.IsIE && this._CheckIsAllContentsIncluded( oRange, this.Window.document.body ) )
146
this._FixIESelectAllBug( oRange ) ;
150
var isCollapsed = oRange.CheckIsCollapsed() ;
154
// Bug #327, Backspace with an img selection would activate the default action in IE.
155
// Let's override that with our logic here.
156
if ( FCKBrowserInfo.IsIE && this.Window.document.selection.type.toLowerCase() == "control" )
158
var controls = this.Window.document.selection.createRange() ;
159
for ( var i = controls.length - 1 ; i >= 0 ; i-- )
161
var el = controls.item( i ) ;
162
el.parentNode.removeChild( el ) ;
170
// On IE, it is better for us handle the deletion if the caret is preceeded
171
// by a <br> (#1383).
172
if ( FCKBrowserInfo.IsIE )
174
var previousElement = FCKDomTools.GetPreviousSourceElement( oRange.StartNode, true ) ;
176
if ( previousElement && previousElement.nodeName.toLowerCase() == 'br' )
178
// Create a range that starts after the <br> and ends at the
179
// current range position.
180
var testRange = oRange.Clone() ;
181
testRange.SetStart( previousElement, 4 ) ;
183
// If that range is empty, we can proceed cleaning that <br> manually.
184
if ( testRange.CheckIsEmpty() )
186
previousElement.parentNode.removeChild( previousElement ) ;
192
var oStartBlock = oRange.StartBlock ;
193
var oEndBlock = oRange.EndBlock ;
195
// The selection boundaries must be in the same "block limit" element
196
if ( oRange.StartBlockLimit == oRange.EndBlockLimit && oStartBlock && oEndBlock )
200
var bEndOfBlock = oRange.CheckEndOfBlock() ;
202
oRange.DeleteContents() ;
204
if ( oStartBlock != oEndBlock )
206
oRange.SetStart(oEndBlock,1) ;
207
oRange.SetEnd(oEndBlock,1) ;
209
// if ( bEndOfBlock )
210
// oEndBlock.parentNode.removeChild( oEndBlock ) ;
215
bCustom = ( oStartBlock == oEndBlock ) ;
218
if ( oRange.CheckStartOfBlock() )
220
var oCurrentBlock = oRange.StartBlock ;
222
var ePrevious = FCKDomTools.GetPreviousSourceElement( oCurrentBlock, true, [ 'BODY', oRange.StartBlockLimit.nodeName ], ['UL','OL'] ) ;
224
bCustom = this._ExecuteBackspace( oRange, ePrevious, oCurrentBlock ) ;
226
else if ( FCKBrowserInfo.IsGeckoLike )
228
// Firefox and Opera (#1095) loose the selection when executing
229
// CheckStartOfBlock, so we must reselect.
238
FCKEnterKey.prototype.DoCtrlBackspace = function()
240
FCKUndo.SaveUndoStep() ;
241
var oRange = new FCKDomRange( this.Window ) ;
242
oRange.MoveToSelection() ;
243
if ( FCKBrowserInfo.IsIE && this._CheckIsAllContentsIncluded( oRange, this.Window.document.body ) )
245
this._FixIESelectAllBug( oRange ) ;
251
FCKEnterKey.prototype._ExecuteBackspace = function( range, previous, currentBlock )
253
var bCustom = false ;
255
// We could be in a nested LI.
256
if ( !previous && currentBlock && currentBlock.nodeName.IEquals( 'LI' ) && currentBlock.parentNode.parentNode.nodeName.IEquals( 'LI' ) )
258
this._OutdentWithSelection( currentBlock, range ) ;
262
if ( previous && previous.nodeName.IEquals( 'LI' ) )
264
var oNestedList = FCKDomTools.GetLastChild( previous, ['UL','OL'] ) ;
266
while ( oNestedList )
268
previous = FCKDomTools.GetLastChild( oNestedList, 'LI' ) ;
269
oNestedList = FCKDomTools.GetLastChild( previous, ['UL','OL'] ) ;
273
if ( previous && currentBlock )
275
// If we are in a LI, and the previous block is not an LI, we must outdent it.
276
if ( currentBlock.nodeName.IEquals( 'LI' ) && !previous.nodeName.IEquals( 'LI' ) )
278
this._OutdentWithSelection( currentBlock, range ) ;
282
// Take a reference to the parent for post processing cleanup.
283
var oCurrentParent = currentBlock.parentNode ;
285
var sPreviousName = previous.nodeName.toLowerCase() ;
286
if ( FCKListsLib.EmptyElements[ sPreviousName ] != null || sPreviousName == 'table' )
288
FCKDomTools.RemoveNode( previous ) ;
293
// Remove the current block.
294
FCKDomTools.RemoveNode( currentBlock ) ;
296
// Remove any empty tag left by the block removal.
297
while ( oCurrentParent.innerHTML.Trim().length == 0 )
299
var oParent = oCurrentParent.parentNode ;
300
oParent.removeChild( oCurrentParent ) ;
301
oCurrentParent = oParent ;
304
// Cleanup the previous and the current elements.
305
FCKDomTools.LTrimNode( currentBlock ) ;
306
FCKDomTools.RTrimNode( previous ) ;
308
// Append a space to the previous.
309
// Maybe it is not always desirable...
310
// previous.appendChild( this.Window.document.createTextNode( ' ' ) ) ;
312
// Set the range to the end of the previous element and bookmark it.
313
range.SetStart( previous, 2, true ) ;
314
range.Collapse( true ) ;
315
var oBookmark = range.CreateBookmark( true ) ;
317
// Move the contents of the block to the previous element and delete it.
318
// But for some block types (e.g. table), moving the children to the previous block makes no sense.
319
// So a check is needed. (See #1081)
320
if ( ! currentBlock.tagName.IEquals( [ 'TABLE' ] ) )
321
FCKDomTools.MoveChildren( currentBlock, previous ) ;
323
// Place the selection at the bookmark.
324
range.SelectBookmark( oBookmark ) ;
334
* Executes the <Delete> key behavior.
336
FCKEnterKey.prototype.DoDelete = function()
338
// Save an undo snapshot before doing anything
339
// This is to conform with the behavior seen in MS Word
340
FCKUndo.SaveUndoStep() ;
342
// The <Delete> has the same effect as the <Backspace>, so we have the same
343
// results if we just move to the next block and apply the same <Backspace> logic.
345
var bCustom = false ;
347
// Get the current selection.
348
var oRange = new FCKDomRange( this.Window ) ;
349
oRange.MoveToSelection() ;
352
if ( FCKBrowserInfo.IsIE && this._CheckIsAllContentsIncluded( oRange, this.Window.document.body ) )
354
this._FixIESelectAllBug( oRange ) ;
358
// There is just one special case for collapsed selections at the end of a block.
359
if ( oRange.CheckIsCollapsed() && oRange.CheckEndOfBlock( FCKBrowserInfo.IsGeckoLike ) )
361
var oCurrentBlock = oRange.StartBlock ;
362
var eCurrentCell = FCKTools.GetElementAscensor( oCurrentBlock, 'td' );
364
var eNext = FCKDomTools.GetNextSourceElement( oCurrentBlock, true, [ oRange.StartBlockLimit.nodeName ],
365
['UL','OL','TR'], true ) ;
367
// Bug #1323 : if we're in a table cell, and the next node belongs to a different cell, then don't
371
var eNextCell = FCKTools.GetElementAscensor( eNext, 'td' );
372
if ( eNextCell != eCurrentCell )
376
bCustom = this._ExecuteBackspace( oRange, oCurrentBlock, eNext ) ;
384
* Executes the <Tab> key behavior.
386
FCKEnterKey.prototype.DoTab = function()
388
var oRange = new FCKDomRange( this.Window );
389
oRange.MoveToSelection() ;
391
// If the user pressed <tab> inside a table, we should give him the default behavior ( moving between cells )
392
// instead of giving him more non-breaking spaces. (Bug #973)
393
var node = oRange._Range.startContainer ;
396
if ( node.nodeType == 1 )
398
var tagName = node.tagName.toLowerCase() ;
399
if ( tagName == "tr" || tagName == "td" || tagName == "th" || tagName == "tbody" || tagName == "table" )
404
node = node.parentNode ;
409
oRange.DeleteContents() ;
410
oRange.InsertNode( this.Window.document.createTextNode( this.TabText ) ) ;
411
oRange.Collapse( false ) ;
417
FCKEnterKey.prototype._ExecuteEnterBlock = function( blockTag, range )
419
// Get the current selection.
420
var oRange = range || new FCKDomRange( this.Window ) ;
422
var oSplitInfo = oRange.SplitBlock( blockTag ) ;
426
// Get the current blocks.
427
var ePreviousBlock = oSplitInfo.PreviousBlock ;
428
var eNextBlock = oSplitInfo.NextBlock ;
430
var bIsStartOfBlock = oSplitInfo.WasStartOfBlock ;
431
var bIsEndOfBlock = oSplitInfo.WasEndOfBlock ;
433
// If there is one block under a list item, modify the split so that the list item gets split as well. (Bug #1647)
436
if ( eNextBlock.parentNode.nodeName.IEquals( 'li' ) )
438
FCKDomTools.BreakParent( eNextBlock, eNextBlock.parentNode ) ;
439
FCKDomTools.MoveNode( eNextBlock, eNextBlock.nextSibling, true ) ;
442
else if ( ePreviousBlock && ePreviousBlock.parentNode.nodeName.IEquals( 'li' ) )
444
FCKDomTools.BreakParent( ePreviousBlock, ePreviousBlock.parentNode ) ;
445
oRange.MoveToElementEditStart( ePreviousBlock.nextSibling );
446
FCKDomTools.MoveNode( ePreviousBlock, ePreviousBlock.previousSibling ) ;
449
// If we have both the previous and next blocks, it means that the
450
// boundaries were on separated blocks, or none of them where on the
451
// block limits (start/end).
452
if ( !bIsStartOfBlock && !bIsEndOfBlock )
454
// If the next block is an <li> with another list tree as the first child
455
// We'll need to append a placeholder or the list item wouldn't be editable. (Bug #1420)
456
if ( eNextBlock.nodeName.IEquals( 'li' ) && eNextBlock.firstChild
457
&& eNextBlock.firstChild.nodeName.IEquals( ['ul', 'ol'] ) )
458
eNextBlock.insertBefore( FCKTools.GetElementDocument( eNextBlock ).createTextNode( '\xa0' ), eNextBlock.firstChild ) ;
459
// Move the selection to the end block.
461
oRange.MoveToElementEditStart( eNextBlock ) ;
465
if ( bIsStartOfBlock && bIsEndOfBlock && ePreviousBlock.tagName.toUpperCase() == 'LI' )
467
oRange.MoveToElementStart( ePreviousBlock ) ;
468
this._OutdentWithSelection( ePreviousBlock, oRange ) ;
475
if ( ePreviousBlock )
477
var sPreviousBlockTag = ePreviousBlock.tagName.toUpperCase() ;
479
// If is a header tag, or we are in a Shift+Enter (#77),
480
// create a new block element (later in the code).
481
if ( !this._HasShift && !(/^H[1-6]$/).test( sPreviousBlockTag ) )
483
// Otherwise, duplicate the previous block.
484
eNewBlock = FCKDomTools.CloneElement( ePreviousBlock ) ;
487
else if ( eNextBlock )
488
eNewBlock = FCKDomTools.CloneElement( eNextBlock ) ;
491
eNewBlock = this.Window.document.createElement( blockTag ) ;
493
// Recreate the inline elements tree, which was available
494
// before the hitting enter, so the same styles will be
495
// available in the new block.
496
var elementPath = oSplitInfo.ElementPath ;
499
for ( var i = 0, len = elementPath.Elements.length ; i < len ; i++ )
501
var element = elementPath.Elements[i] ;
503
if ( element == elementPath.Block || element == elementPath.BlockLimit )
506
if ( FCKListsLib.InlineChildReqElements[ element.nodeName.toLowerCase() ] )
508
element = FCKDomTools.CloneElement( element ) ;
509
FCKDomTools.MoveChildren( eNewBlock, element ) ;
510
eNewBlock.appendChild( element ) ;
515
if ( FCKBrowserInfo.IsGeckoLike )
516
FCKTools.AppendBogusBr( eNewBlock ) ;
518
oRange.InsertNode( eNewBlock ) ;
520
// This is tricky, but to make the new block visible correctly
521
// we must select it.
522
if ( FCKBrowserInfo.IsIE )
524
// Move the selection to the new block.
525
oRange.MoveToElementEditStart( eNewBlock ) ;
529
// Move the selection to the new block.
530
oRange.MoveToElementEditStart( bIsStartOfBlock && !bIsEndOfBlock ? eNextBlock : eNewBlock ) ;
533
if ( FCKBrowserInfo.IsGeckoLike )
537
// If we have split the block, adds a temporary span at the
538
// range position and scroll relatively to it.
539
var tmpNode = this.Window.document.createElement( 'span' ) ;
541
// We need some content for Safari.
542
tmpNode.innerHTML = ' ';
544
oRange.InsertNode( tmpNode ) ;
545
FCKDomTools.ScrollIntoView( tmpNode, false ) ;
546
oRange.DeleteContents() ;
550
// We may use the above scroll logic for the new block case
551
// too, but it gives some weird result with Opera.
552
FCKDomTools.ScrollIntoView( eNextBlock || eNewBlock, false ) ;
559
// Release the resources used by the range.
565
FCKEnterKey.prototype._ExecuteEnterBr = function( blockTag )
567
// Get the current selection.
568
var oRange = new FCKDomRange( this.Window ) ;
569
oRange.MoveToSelection() ;
571
// The selection boundaries must be in the same "block limit" element.
572
if ( oRange.StartBlockLimit == oRange.EndBlockLimit )
574
oRange.DeleteContents() ;
576
// Get the new selection (it is collapsed at this point).
577
oRange.MoveToSelection() ;
579
var bIsStartOfBlock = oRange.CheckStartOfBlock() ;
580
var bIsEndOfBlock = oRange.CheckEndOfBlock() ;
582
var sStartBlockTag = oRange.StartBlock ? oRange.StartBlock.tagName.toUpperCase() : '' ;
584
var bHasShift = this._HasShift ;
587
if ( !bHasShift && sStartBlockTag == 'LI' )
588
return this._ExecuteEnterBlock( null, oRange ) ;
590
// If we are at the end of a header block.
591
if ( !bHasShift && bIsEndOfBlock && (/^H[1-6]$/).test( sStartBlockTag ) )
593
// Insert a BR after the current paragraph.
594
FCKDomTools.InsertAfterNode( oRange.StartBlock, this.Window.document.createElement( 'br' ) ) ;
596
// The space is required by Gecko only to make the cursor blink.
597
if ( FCKBrowserInfo.IsGecko )
598
FCKDomTools.InsertAfterNode( oRange.StartBlock, this.Window.document.createTextNode( '' ) ) ;
600
// IE and Gecko have different behaviors regarding the position.
601
oRange.SetStart( oRange.StartBlock.nextSibling, FCKBrowserInfo.IsIE ? 3 : 1 ) ;
606
bIsPre = sStartBlockTag.IEquals( 'pre' ) ;
608
eLineBreak = this.Window.document.createTextNode( FCKBrowserInfo.IsIE ? '\r' : '\n' ) ;
610
eLineBreak = this.Window.document.createElement( 'br' ) ;
612
oRange.InsertNode( eLineBreak ) ;
614
// The space is required by Gecko only to make the cursor blink.
615
if ( FCKBrowserInfo.IsGecko )
616
FCKDomTools.InsertAfterNode( eLineBreak, this.Window.document.createTextNode( '' ) ) ;
618
// If we are at the end of a block, we must be sure the bogus node is available in that block.
619
if ( bIsEndOfBlock && FCKBrowserInfo.IsGeckoLike )
620
FCKTools.AppendBogusBr( eLineBreak.parentNode ) ;
622
if ( FCKBrowserInfo.IsIE )
623
oRange.SetStart( eLineBreak, 4 ) ;
625
oRange.SetStart( eLineBreak.nextSibling, 1 ) ;
627
if ( ! FCKBrowserInfo.IsIE )
630
if ( FCKBrowserInfo.IsOpera )
631
dummy = this.Window.document.createElement( 'span' ) ;
633
dummy = this.Window.document.createElement( 'br' ) ;
635
eLineBreak.parentNode.insertBefore( dummy, eLineBreak.nextSibling ) ;
637
FCKDomTools.ScrollIntoView( dummy, false ) ;
639
dummy.parentNode.removeChild( dummy ) ;
643
// This collapse guarantees the cursor will be blinking.
644
oRange.Collapse( true ) ;
646
oRange.Select( bIsPre ) ;
649
// Release the resources used by the range.
655
// Outdents a LI, maintaining the selection defined on a range.
656
FCKEnterKey.prototype._OutdentWithSelection = function( li, range )
658
var oBookmark = range.CreateBookmark() ;
660
FCKListHandler.OutdentListItem( li ) ;
662
range.MoveToBookmark( oBookmark ) ;
666
// Is all the contents under a node included by a range?
667
FCKEnterKey.prototype._CheckIsAllContentsIncluded = function( range, node )
669
var startOk = false ;
673
FCKDebug.Output( 'sc='+range.StartContainer.nodeName+
674
',so='+range._Range.startOffset+
675
',ec='+range.EndContainer.nodeName+
676
',eo='+range._Range.endOffset ) ;
678
if ( range.StartContainer == node || range.StartContainer == node.firstChild )
679
startOk = ( range._Range.startOffset == 0 ) ;
681
if ( range.EndContainer == node || range.EndContainer == node.lastChild )
683
var nodeLength = range.EndContainer.nodeType == 3 ? range.EndContainer.length : range.EndContainer.childNodes.length ;
684
endOk = ( range._Range.endOffset == nodeLength ) ;
687
return startOk && endOk ;
691
FCKEnterKey.prototype._FixIESelectAllBug = function( range )
693
var doc = this.Window.document ;
694
doc.body.innerHTML = '' ;
696
if ( FCKConfig.EnterMode.IEquals( ['div', 'p'] ) )
698
editBlock = doc.createElement( FCKConfig.EnterMode ) ;
699
doc.body.appendChild( editBlock ) ;
702
editBlock = doc.body ;
704
range.MoveToNodeContents( editBlock ) ;
705
range.Collapse( true ) ;