2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview The "wysiwygarea" plugin. It registers the "wysiwyg" editing
\r
8 * mode, which handles the main editing area space.
\r
13 // List of elements in which has no way to move editing focus outside.
\r
14 var nonExitableElementNames = { table:1,pre:1 };
\r
16 // Matching an empty paragraph at the end of document.
\r
17 var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>| |\u00A0| )?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;
\r
19 var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );
\r
21 // Elements that could have empty new line around, including table, pre-formatted block, hr, page-break. (#6554)
\r
22 function nonExitable( element )
\r
24 return ( element.getName() in nonExitableElementNames )
\r
25 || element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];
\r
28 function checkReadOnly( selection )
\r
30 if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT )
\r
31 return selection.getSelectedElement().isReadOnly();
\r
33 return selection.getCommonAncestor().isReadOnly();
\r
37 function onInsert( insertFunc )
\r
39 return function( evt )
\r
41 if ( this.mode == 'wysiwyg' )
\r
45 var selection = this.getSelection();
\r
46 if ( checkReadOnly( selection ) )
\r
49 this.fire( 'saveSnapshot' );
\r
51 insertFunc.call( this, evt.data );
\r
53 // Save snaps after the whole execution completed.
\r
54 // This's a workaround for make DOM modification's happened after
\r
55 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
57 CKEDITOR.tools.setTimeout( function()
\r
59 this.fire( 'saveSnapshot' );
\r
65 function doInsertHtml( data )
\r
67 if ( this.dataProcessor )
\r
68 data = this.dataProcessor.toHtml( data );
\r
70 var selection = this.getSelection();
\r
71 if ( CKEDITOR.env.ie )
\r
73 var selIsLocked = selection.isLocked;
\r
78 var $sel = selection.getNative();
\r
80 // Delete control selections to avoid IE bugs on pasteHTML.
\r
81 if ( $sel.type == 'Control' )
\r
83 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
85 // Due to IE bugs on handling contenteditable=false blocks
\r
86 // (#6005), we need to make some checks and eventually
\r
87 // delete the selection first.
\r
89 var range = selection.getRanges()[0],
\r
90 endContainer = range && range.endContainer;
\r
92 if ( endContainer &&
\r
93 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
94 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
95 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
97 range.setEndAfter( range.endContainer );
\r
98 range.deleteContents();
\r
104 $sel.createRange().pasteHTML( data );
\r
109 this.getSelection().lock();
\r
112 this.document.$.execCommand( 'inserthtml', false, data );
\r
114 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
115 if ( CKEDITOR.env.webkit )
\r
117 selection = this.getSelection();
\r
118 selection.scrollIntoView();
\r
122 function doInsertText( text )
\r
124 var selection = this.getSelection(),
\r
125 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
126 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
127 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
129 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
131 // Convert leading and trailing whitespaces into
\r
132 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
134 if ( match.length == 1 ) // one space, preserve it
\r
136 else if ( !offset ) // beginning of block
\r
137 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
138 else // end of block
\r
139 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
142 // Convert subsequent whitespaces into
\r
143 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
145 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
148 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
150 // Two line-breaks create one paragraph.
\r
151 if ( !isEnterBrMode )
\r
153 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
154 function( match, group1, text )
\r
156 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
160 // One <br> per line-break.
\r
161 html = html.replace( /\n/g, '<br>' );
\r
163 // Compensate padding <br> for non-IE.
\r
164 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
166 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
168 return CKEDITOR.tools.repeat( match, 2 );
\r
172 // Inline styles have to be inherited in Firefox.
\r
173 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
175 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
178 for ( var i = 0; i < path.elements.length; i++ )
\r
180 var tag = path.elements[ i ].getName();
\r
181 if ( tag in CKEDITOR.dtd.$inline )
\r
182 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
183 else if ( tag in CKEDITOR.dtd.$block )
\r
187 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
188 html = context.join( '' ) + html;
\r
191 doInsertHtml.call( this, html );
\r
194 function doInsertElement( element )
\r
196 var selection = this.getSelection(),
\r
197 ranges = selection.getRanges(),
\r
198 elementName = element.getName(),
\r
199 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
201 var selIsLocked = selection.isLocked;
\r
204 selection.unlock();
\r
206 var range, clone, lastElement, bookmark;
\r
208 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
210 range = ranges[ i ];
\r
212 // Remove the original contents.
\r
213 range.deleteContents();
\r
215 clone = !i && element || element.clone( 1 );
\r
217 // If we're inserting a block at dtd-violated position, split
\r
218 // the parent blocks until we reach blockLimit.
\r
222 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
223 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
224 && !( dtd && dtd [ elementName ] ) )
\r
226 // Split up inline elements.
\r
227 if ( current.getName() in CKEDITOR.dtd.span )
\r
228 range.splitElement( current );
\r
229 // If we're in an empty block which indicate a new paragraph,
\r
230 // simply replace it with the inserting block.(#3664)
\r
231 else if ( range.checkStartOfBlock()
\r
232 && range.checkEndOfBlock() )
\r
234 range.setStartBefore( current );
\r
235 range.collapse( true );
\r
239 range.splitBlock();
\r
243 // Insert the new node.
\r
244 range.insertNode( clone );
\r
246 // Save the last element reference so we can make the
\r
247 // selection later.
\r
248 if ( !lastElement )
\r
249 lastElement = clone;
\r
252 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
254 // If we're inserting a block element immediatelly followed by
\r
255 // another block element, the selection must move there. (#3100,#5436)
\r
258 var next = lastElement.getNext( notWhitespaceEval ),
\r
259 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
261 // Check if it's a block element that accepts text.
\r
262 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
\r
263 range.moveToElementEditStart( next );
\r
266 selection.selectRanges( [ range ] );
\r
269 this.getSelection().lock();
\r
272 // DOM modification here should not bother dirty flag.(#4385)
\r
273 function restoreDirty( editor )
\r
275 if ( !editor.checkDirty() )
\r
276 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
279 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
280 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
282 function isNotEmpty( node )
\r
284 return isNotWhitespace( node ) && isNotBookmark( node );
\r
287 function isNbsp( node )
\r
289 return node.type == CKEDITOR.NODE_TEXT
\r
290 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
293 function restoreSelection( selection )
\r
295 if ( selection.isLocked )
\r
297 selection.unlock();
\r
298 setTimeout( function() { selection.lock(); }, 0 );
\r
302 function isBlankParagraph( block )
\r
304 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
307 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
309 // Gecko need a key event to 'wake up' the editing
\r
310 // ability when document is empty.(#3864, #5781)
\r
311 function activateEditing( editor )
\r
313 var win = editor.window,
\r
314 doc = editor.document,
\r
315 body = editor.document.getBody(),
\r
316 bodyChildsNum = body.getChildren().count();
\r
318 if ( !bodyChildsNum || ( bodyChildsNum == 1&& body.getFirst().hasAttribute( '_moz_editor_bogus_node' ) ) )
\r
320 restoreDirty( editor );
\r
322 // Memorize scroll position to restore it later (#4472).
\r
323 var hostDocument = editor.element.getDocument();
\r
324 var hostDocumentElement = hostDocument.getDocumentElement();
\r
325 var scrollTop = hostDocumentElement.$.scrollTop;
\r
326 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
328 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
329 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
330 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
331 false, false, false, 0, 32 );
\r
332 doc.$.dispatchEvent( keyEventSimulate );
\r
334 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
335 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
337 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
338 bodyChildsNum && body.getFirst().remove();
\r
339 doc.getBody().appendBogus();
\r
340 var nativeRange = new CKEDITOR.dom.range( doc );
\r
341 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
342 nativeRange.select();
\r
347 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
348 * non-exitable-block by padding extra br.(#3189)
\r
350 function onSelectionChangeFixBody( evt )
\r
352 var editor = evt.editor,
\r
353 path = evt.data.path,
\r
354 blockLimit = path.blockLimit,
\r
355 selection = evt.data.selection,
\r
356 range = selection.getRanges()[0],
\r
357 body = editor.document.getBody(),
\r
358 enterMode = editor.config.enterMode;
\r
360 CKEDITOR.env.gecko && activateEditing( editor );
\r
362 // When enterMode set to block, we'll establing new paragraph only if we're
\r
363 // selecting inline contents right under body. (#3657)
\r
364 if ( enterMode != CKEDITOR.ENTER_BR
\r
366 && blockLimit.getName() == 'body'
\r
369 editor.fire( 'updateSnapshot' );
\r
370 restoreDirty( editor );
\r
371 CKEDITOR.env.ie && restoreSelection( selection );
\r
373 var fixedBlock = range.fixBlock( true,
\r
374 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
376 // For IE, we should remove any filler node which was introduced before.
\r
377 if ( CKEDITOR.env.ie )
\r
379 var first = fixedBlock.getFirst( isNotEmpty );
\r
380 first && isNbsp( first ) && first.remove();
\r
383 // If the fixed block is actually blank and is already followed by an exitable blank
\r
384 // block, we should revert the fix and move into the existed one. (#3684)
\r
385 if ( isBlankParagraph( fixedBlock ) )
\r
387 var element = fixedBlock.getNext( isNotWhitespace );
\r
389 element.type == CKEDITOR.NODE_ELEMENT &&
\r
390 !nonExitable( element ) )
\r
392 range.moveToElementEditStart( element );
\r
393 fixedBlock.remove();
\r
397 element = fixedBlock.getPrevious( isNotWhitespace );
\r
399 element.type == CKEDITOR.NODE_ELEMENT &&
\r
400 !nonExitable( element ) )
\r
402 range.moveToElementEditEnd( element );
\r
403 fixedBlock.remove();
\r
409 // Notify non-IE that selection has changed.
\r
410 if ( !CKEDITOR.env.ie )
\r
411 editor.selectionChange();
\r
414 // All browsers are incapable to moving cursor out of certain non-exitable
\r
415 // blocks (e.g. table, list, pre) at the end of document, make this happen by
\r
416 // place a bogus node there, which would be later removed by dataprocessor.
\r
417 var walkerRange = new CKEDITOR.dom.range( editor.document ),
\r
418 walker = new CKEDITOR.dom.walker( walkerRange );
\r
419 walkerRange.selectNodeContents( body );
\r
420 walker.evaluator = function( node )
\r
422 return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in nonExitableElementNames );
\r
424 walker.guard = function( node, isMoveout )
\r
426 return !( ( node.type == CKEDITOR.NODE_TEXT && isNotWhitespace( node ) ) || isMoveout );
\r
429 if ( walker.previous() )
\r
431 editor.fire( 'updateSnapshot' );
\r
432 restoreDirty( editor );
\r
433 CKEDITOR.env.ie && restoreSelection( selection );
\r
436 if ( enterMode != CKEDITOR.ENTER_BR )
\r
437 paddingBlock = body.append( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
439 paddingBlock = body;
\r
441 if ( !CKEDITOR.env.ie )
\r
442 paddingBlock.appendBogus();
\r
446 CKEDITOR.plugins.add( 'wysiwygarea',
\r
448 requires : [ 'editingblock' ],
\r
450 init : function( editor )
\r
452 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )
\r
453 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
455 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
\r
457 var contentDomReadyHandler;
\r
458 editor.on( 'editingBlockReady', function()
\r
468 // Support for custom document.domain in IE.
\r
469 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
471 // Creates the iframe that holds the editable document.
\r
472 var createIFrame = function( data )
\r
478 'document.open();' +
\r
480 // The document domain must be set any time we
\r
481 // call document.open().
\r
482 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
484 'document.close();';
\r
486 // With IE, the custom domain has to be taken care at first,
\r
487 // for other browers, the 'src' attribute should be left empty to
\r
488 // trigger iframe's 'load' event.
\r
491 'javascript:void(0)' :
\r
493 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
497 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
498 ' style="width:100%;height:100%"' +
\r
499 ' frameBorder="0"' +
\r
500 ' title="' + frameLabel + '"' +
\r
501 ' src="' + src + '"' +
\r
502 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
503 ' allowTransparency="true"' +
\r
506 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
507 if ( document.location.protocol == 'chrome:' )
\r
508 CKEDITOR.event.useCapture = true;
\r
510 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
511 iframe.on( 'load', function( ev )
\r
514 ev.removeListener();
\r
516 var doc = iframe.getFrameDocument();
\r
519 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
522 // Reset adjustment back to default (#5689)
\r
523 if ( document.location.protocol == 'chrome:' )
\r
524 CKEDITOR.event.useCapture = false;
\r
526 // The container must be visible when creating the iframe in FF (#5956)
\r
527 var element = editor.element,
\r
528 isHidden = CKEDITOR.env.gecko && !element.isVisible(),
\r
529 previousStyles = {};
\r
534 position : element.getStyle( 'position' ),
\r
535 top : element.getStyle( 'top' )
\r
537 element.setStyles( { position : 'absolute', top : '-3000px' } );
\r
540 mainElement.append( iframe );
\r
544 setTimeout( function()
\r
547 element.setStyles( previousStyles );
\r
552 // The script that launches the bootstrap logic on 'domReady', so the document
\r
553 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
554 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
555 var activationScript =
\r
556 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
557 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
558 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
561 // Editing area bootstrap code.
\r
562 function contentDomReady( domWindow )
\r
564 if ( !frameLoaded )
\r
568 editor.fire( 'ariaWidget', iframe );
\r
570 var domDocument = domWindow.document,
\r
571 body = domDocument.body;
\r
573 // Remove this script from the DOM.
\r
574 var script = domDocument.getElementById( "cke_actscrpt" );
\r
575 script && script.parentNode.removeChild( script );
\r
577 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
579 if ( CKEDITOR.env.ie )
\r
581 // Don't display the focus border.
\r
582 body.hideFocus = true;
\r
584 // Disable and re-enable the body to avoid IE from
\r
585 // taking the editing focus at startup. (#141 / #523)
\r
586 body.disabled = true;
\r
587 body.contentEditable = true;
\r
588 body.removeAttribute( 'disabled' );
\r
592 // Avoid opening design mode in a frame window thread,
\r
593 // which will cause host page scrolling.(#4397)
\r
594 setTimeout( function()
\r
596 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
597 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
598 || CKEDITOR.env.opera )
\r
599 domDocument.$.body.contentEditable = true;
\r
600 else if ( CKEDITOR.env.webkit )
\r
601 domDocument.$.body.parentNode.contentEditable = true;
\r
603 domDocument.$.designMode = 'on';
\r
607 CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
609 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
610 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
612 domDocument.on( 'dblclick', function( evt )
\r
614 var element = evt.data.getTarget(),
\r
615 data = { element : element, dialog : '' };
\r
616 editor.fire( 'doubleclick', data );
\r
617 data.dialog && editor.openDialog( data.dialog );
\r
620 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
621 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
623 domDocument.on( 'mousedown', function( ev )
\r
625 var control = ev.data.getTarget();
\r
626 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
627 editor.getSelection().selectElement( control );
\r
631 if ( CKEDITOR.env.gecko )
\r
633 domDocument.on( 'mouseup', function( ev )
\r
635 if ( ev.data.$.button == 2 )
\r
637 var target = ev.data.getTarget();
\r
639 // Prevent right click from selecting an empty block even
\r
640 // when selection is anchored inside it. (#5845)
\r
641 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
643 var range = new CKEDITOR.dom.range( domDocument );
\r
644 range.moveToElementEditStart( target );
\r
645 range.select( true );
\r
651 // Prevent the browser opening links in read-only blocks. (#6032)
\r
652 domDocument.on( 'click', function( ev )
\r
655 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
656 ev.preventDefault();
\r
659 // Webkit: avoid from editing form control elements content.
\r
660 if ( CKEDITOR.env.webkit )
\r
662 // Prevent from tick checkbox/radiobox/select
\r
663 domDocument.on( 'click', function( ev )
\r
665 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
666 ev.data.preventDefault();
\r
669 // Prevent from editig textfield/textarea value.
\r
670 domDocument.on( 'mouseup', function( ev )
\r
672 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
673 ev.data.preventDefault();
\r
677 // IE standard compliant in editing frame doesn't focus the editor when
\r
678 // clicking outside actual content, manually apply the focus. (#1659)
\r
679 if ( CKEDITOR.env.ie
\r
680 && domDocument.$.compatMode == 'CSS1Compat'
\r
681 || CKEDITOR.env.gecko
\r
682 || CKEDITOR.env.opera )
\r
684 var htmlElement = domDocument.getDocumentElement();
\r
685 htmlElement.on( 'mousedown', function( evt )
\r
687 // Setting focus directly on editor doesn't work, we
\r
688 // have to use here a temporary element to 'redirect'
\r
690 if ( evt.data.getTarget().equals( htmlElement ) )
\r
692 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
694 focusGrabber.focus();
\r
699 domWindow.on( 'blur', function()
\r
701 editor.focusManager.blur();
\r
706 domWindow.on( 'focus', function()
\r
708 var doc = editor.document;
\r
710 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
712 else if ( CKEDITOR.env.opera )
\r
713 doc.getBody().focus();
\r
714 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
715 else if ( CKEDITOR.env.webkit )
\r
719 editor.document.getDocumentElement().focus();
\r
724 editor.focusManager.focus();
\r
727 var keystrokeHandler = editor.keystrokeHandler;
\r
728 if ( keystrokeHandler )
\r
729 keystrokeHandler.attach( domDocument );
\r
731 if ( CKEDITOR.env.ie )
\r
733 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
734 // Override keystrokes which should have deletion behavior
\r
735 // on control types in IE . (#4047)
\r
736 domDocument.on( 'keydown', function( evt )
\r
738 var keyCode = evt.data.getKeystroke();
\r
740 // Backspace OR Delete.
\r
741 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
743 var sel = editor.getSelection(),
\r
744 control = sel.getSelectedElement();
\r
748 // Make undo snapshot.
\r
749 editor.fire( 'saveSnapshot' );
\r
751 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
752 // break up the selection, safely manage it here. (#4795)
\r
753 var bookmark = sel.getRanges()[ 0 ].createBookmark();
\r
754 // Remove the control manually.
\r
756 sel.selectBookmarks( [ bookmark ] );
\r
758 editor.fire( 'saveSnapshot' );
\r
760 evt.data.preventDefault();
\r
765 // PageUp/PageDown scrolling is broken in document
\r
766 // with standard doctype, manually fix it. (#4736)
\r
767 if ( domDocument.$.compatMode == 'CSS1Compat' )
\r
769 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
770 domDocument.on( 'keydown', function( evt )
\r
772 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
774 setTimeout( function ()
\r
776 editor.getSelection().scrollIntoView();
\r
783 // Adds the document body as a context menu target.
\r
784 if ( editor.contextMenu )
\r
785 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
787 setTimeout( function()
\r
789 editor.fire( 'contentDom' );
\r
793 editor.mode = 'wysiwyg';
\r
794 editor.fire( 'mode' );
\r
798 isLoadingData = false;
\r
800 if ( isPendingFocus )
\r
803 isPendingFocus = false;
\r
805 setTimeout( function()
\r
807 editor.fire( 'dataReady' );
\r
810 // IE, Opera and Safari may not support it and throw errors.
\r
811 try { editor.document.$.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}
\r
812 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}
\r
815 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
816 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
819 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
820 * fix the problem. :(
\r
822 if ( CKEDITOR.env.ie )
\r
824 setTimeout( function()
\r
826 if ( editor.document )
\r
828 var $body = editor.document.$.body;
\r
829 $body.runtimeStyle.marginBottom = '0px';
\r
830 $body.runtimeStyle.marginBottom = '';
\r
838 editor.addMode( 'wysiwyg',
\r
840 load : function( holderElement, data, isSnapshot )
\r
842 mainElement = holderElement;
\r
844 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
845 holderElement.setStyle( 'position', 'relative' );
\r
847 // The editor data "may be dirty" after this
\r
849 editor.mayBeDirty = true;
\r
854 this.loadSnapshotData( data );
\r
856 this.loadData( data );
\r
859 loadData : function( data )
\r
861 isLoadingData = true;
\r
863 var config = editor.config,
\r
864 fullPage = config.fullPage,
\r
865 docType = config.docType;
\r
867 // Build the additional stuff to be included into <head>.
\r
869 '<style type="text/css" data-cke-temp="1">' +
\r
870 editor._.styles.join( '\n' ) +
\r
873 !fullPage && ( headExtra =
\r
874 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
877 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
881 // Search and sweep out the doctype declaration.
\r
882 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
884 editor.docType = docType = match;
\r
889 // Get the HTML version of the data.
\r
890 if ( editor.dataProcessor )
\r
891 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
895 // Check if the <body> tag is available.
\r
896 if ( !(/<body[\s|>]/).test( data ) )
\r
897 data = '<body>' + data;
\r
899 // Check if the <html> tag is available.
\r
900 if ( !(/<html[\s|>]/).test( data ) )
\r
901 data = '<html>' + data + '</html>';
\r
903 // Check if the <head> tag is available.
\r
904 if ( !(/<head[\s|>]/).test( data ) )
\r
905 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
906 else if ( !(/<title[\s|>]/).test( data ) )
\r
907 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
909 // The base must be the first tag in the HEAD, e.g. to get relative
\r
910 // links on styles.
\r
911 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
913 // Inject the extra stuff into <head>.
\r
914 // Attention: do not change it before testing it well. (V2)
\r
915 // This is tricky... if the head ends with <meta ... content type>,
\r
916 // Firefox will break. But, it works if we place our extra stuff as
\r
917 // the last elements in the HEAD.
\r
918 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
920 // Add the DOCTYPE back to it.
\r
921 data = docType + data;
\r
927 '<html dir="' + config.contentsLangDirection + '"' +
\r
928 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
930 '<title>' + frameLabel + '</title>' +
\r
934 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
935 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
941 data += activationScript;
\r
944 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
946 createIFrame( data );
\r
949 getData : function()
\r
951 var config = editor.config,
\r
952 fullPage = config.fullPage,
\r
953 docType = fullPage && editor.docType,
\r
954 doc = iframe.getFrameDocument();
\r
956 var data = fullPage
\r
957 ? doc.getDocumentElement().getOuterHtml()
\r
958 : doc.getBody().getHtml();
\r
960 if ( editor.dataProcessor )
\r
961 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
963 // Reset empty if the document contains only one empty paragraph.
\r
964 if ( config.ignoreEmptyParagraph )
\r
965 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
968 data = docType + '\n' + data;
\r
973 getSnapshotData : function()
\r
975 return iframe.getFrameDocument().getBody().getHtml();
\r
978 loadSnapshotData : function( data )
\r
980 iframe.getFrameDocument().getBody().setHtml( data );
\r
983 onDispose : function()
\r
985 if ( !editor.document )
\r
988 editor.document.getDocumentElement().clearCustomData();
\r
989 editor.document.getBody().clearCustomData();
\r
991 editor.window.clearCustomData();
\r
992 editor.document.clearCustomData();
\r
994 iframe.clearCustomData();
\r
997 * IE BUG: When destroying editor DOM with the selection remains inside
\r
998 * editing area would break IE7/8's selection system, we have to put the editing
\r
999 * iframe offline first. (#3812 and #5441)
\r
1004 unload : function( holderElement )
\r
1008 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1010 editor.fire( 'contentDomUnload' );
\r
1013 focus : function()
\r
1015 var win = editor.window;
\r
1017 if ( isLoadingData )
\r
1018 isPendingFocus = true;
\r
1019 // Temporary solution caused by #6025, supposed be unified by #6154.
\r
1020 else if ( CKEDITOR.env.opera && editor.document )
\r
1022 // Required for Opera when switching focus
\r
1023 // from another iframe, e.g. panels. (#6444)
\r
1024 var iframe = editor.window.$.frameElement;
\r
1025 iframe.blur(), iframe.focus();
\r
1026 editor.document.getBody().focus();
\r
1028 editor.selectionChange();
\r
1030 else if ( !CKEDITOR.env.opera && win )
\r
1032 // AIR needs a while to focus when moving from a link.
\r
1033 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1034 editor.selectionChange();
\r
1039 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1040 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1041 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1042 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1043 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
1047 // Setting voice label as window title, backup the original one
\r
1048 // and restore it before running into use.
\r
1049 editor.on( 'contentDom', function()
\r
1051 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1052 title.data( 'cke-title', editor.document.$.title );
\r
1053 editor.document.$.title = frameLabel;
\r
1056 // IE8 stricts mode doesn't have 'contentEditable' in effect
\r
1057 // on element unless it has layout. (#5562)
\r
1058 if ( CKEDITOR.env.ie8Compat )
\r
1060 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1062 var selectors = [];
\r
1063 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1064 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1065 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1067 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1068 else if ( CKEDITOR.env.gecko )
\r
1069 editor.addCss( 'html { height: 100% !important; }' );
\r
1071 // Switch on design mode for a short while and close it after then.
\r
1072 function blinkCursor( retry )
\r
1074 CKEDITOR.tools.tryThese(
\r
1077 editor.document.$.designMode = 'on';
\r
1078 setTimeout( function()
\r
1080 editor.document.$.designMode = 'off';
\r
1081 if ( CKEDITOR.currentInstance == editor )
\r
1082 editor.document.getBody().focus();
\r
1087 // The above call is known to fail when parent DOM
\r
1088 // tree layout changes may break design mode. (#5782)
\r
1089 // Refresh the 'contentEditable' is a cue to this.
\r
1090 editor.document.$.designMode = 'off';
\r
1091 var body = editor.document.getBody();
\r
1092 body.setAttribute( 'contentEditable', false );
\r
1093 body.setAttribute( 'contentEditable', true );
\r
1094 // Try it again once..
\r
1095 !retry && blinkCursor( 1 );
\r
1099 // Create an invisible element to grab focus.
\r
1100 if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1103 editor.on( 'uiReady', function()
\r
1105 focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
\r
1106 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)
\r
1107 '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );
\r
1109 focusGrabber.on( 'focus', function()
\r
1114 editor.on( 'destroy', function()
\r
1116 CKEDITOR.tools.removeFunction( contentDomReadyHandler );
\r
1117 focusGrabber.clearCustomData();
\r
1121 // Disable form elements editing mode provided by some browers. (#5746)
\r
1122 editor.on( 'insertElement', function ( evt )
\r
1124 var element = evt.data;
\r
1125 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1126 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1128 if ( !element.isReadOnly() )
\r
1130 element.setAttribute( 'contentEditable', false );
\r
1131 // We should flag that the element was locked by our code so
\r
1132 // it'll be editable by the editor functions (#6046).
\r
1133 element.setCustomData( '_cke_notReadOnly', 1 );
\r
1141 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1142 if ( CKEDITOR.env.gecko )
\r
1146 var body = document.body;
\r
1149 window.addEventListener( 'load', arguments.callee, false );
\r
1152 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1153 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1154 'event.persisted && (function(){' +
\r
1155 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1156 'for ( var i in allInstances )' +
\r
1158 ' editor = allInstances[ i ];' +
\r
1159 ' doc = editor.document;' +
\r
1162 ' doc.$.designMode = "off";' +
\r
1163 ' doc.$.designMode = "on";' +
\r
1174 * Disables the ability of resize objects (image and tables) in the editing
\r
1179 * config.disableObjectResizing = true;
\r
1181 CKEDITOR.config.disableObjectResizing = false;
\r
1184 * Disables the "table tools" offered natively by the browser (currently
\r
1185 * Firefox only) to make quick table editing operations, like adding or
\r
1186 * deleting rows and columns.
\r
1190 * config.disableNativeTableHandles = false;
\r
1192 CKEDITOR.config.disableNativeTableHandles = true;
\r
1195 * Disables the built-in spell checker while typing natively available in the
\r
1196 * browser (currently Firefox and Safari only).<br /><br />
\r
1198 * Even if word suggestions will not appear in the CKEditor context menu, this
\r
1199 * feature is useful to help quickly identifying misspelled words.<br /><br />
\r
1201 * This setting is currently compatible with Firefox only due to limitations in
\r
1206 * config.disableNativeSpellChecker = false;
\r
1208 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1211 * Whether the editor must output an empty value ("") if it's contents is made
\r
1212 * by an empty paragraph only.
\r
1216 * config.ignoreEmptyParagraph = false;
\r
1218 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1221 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1222 * @name CKEDITOR.editor#dataReady
\r