2 Copyright (c) 2003-2009, 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
14 * List of elements in which has no way to move editing focus outside.
\r
16 var nonExitableElementNames = { table:1,pre:1 };
\r
17 // Matching an empty paragraph at the end of document.
\r
18 var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>| |\u00A0| )?\s*(:?<\/\1>)?\s*$/gi;
\r
20 function onInsertHtml( evt )
\r
22 if ( this.mode == 'wysiwyg' )
\r
26 var selection = this.getSelection(),
\r
29 if ( this.dataProcessor )
\r
30 data = this.dataProcessor.toHtml( data );
\r
32 if ( CKEDITOR.env.ie )
\r
34 var selIsLocked = selection.isLocked;
\r
39 var $sel = selection.getNative();
\r
40 if ( $sel.type == 'Control' )
\r
42 $sel.createRange().pasteHTML( data );
\r
45 this.getSelection().lock();
\r
48 this.document.$.execCommand( 'inserthtml', false, data );
\r
52 function onInsertElement( evt )
\r
54 if ( this.mode == 'wysiwyg' )
\r
57 this.fire( 'saveSnapshot' );
\r
59 var element = evt.data,
\r
60 elementName = element.getName(),
\r
61 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
63 var selection = this.getSelection(),
\r
64 ranges = selection.getRanges();
\r
66 var selIsLocked = selection.isLocked;
\r
71 var range, clone, lastElement, bookmark;
\r
73 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
75 range = ranges[ i ];
\r
77 // Remove the original contents.
\r
78 range.deleteContents();
\r
80 clone = !i && element || element.clone( true );
\r
82 // If we're inserting a block at dtd-violated position, split
\r
83 // the parent blocks until we reach blockLimit.
\r
87 while( ( current = range.getCommonAncestor( false, true ) )
\r
88 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
89 && !( dtd && dtd [ elementName ] ) )
\r
91 // Split up inline elements.
\r
92 if ( current.getName() in CKEDITOR.dtd.span )
\r
93 range.splitElement( current );
\r
94 // If we're in an empty block which indicate a new paragraph,
\r
95 // simply replace it with the inserting block.(#3664)
\r
96 else if ( range.checkStartOfBlock()
\r
97 && range.checkEndOfBlock() )
\r
99 range.setStartBefore( current );
\r
100 range.collapse( true );
\r
104 range.splitBlock();
\r
108 // Insert the new node.
\r
109 range.insertNode( clone );
\r
111 // Save the last element reference so we can make the
\r
112 // selection later.
\r
113 if ( !lastElement )
\r
114 lastElement = clone;
\r
117 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
119 var next = lastElement.getNextSourceNode( true );
\r
120 if ( next && next.type == CKEDITOR.NODE_ELEMENT )
\r
121 range.moveToElementEditStart( next );
\r
123 selection.selectRanges( [ range ] );
\r
126 this.getSelection().lock();
\r
128 // Save snaps after the whole execution completed.
\r
129 // This's a workaround for make DOM modification's happened after
\r
130 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
132 CKEDITOR.tools.setTimeout( function(){
\r
133 this.fire( 'saveSnapshot' );
\r
138 // DOM modification here should not bother dirty flag.(#4385)
\r
139 function restoreDirty( editor )
\r
141 if( !editor.checkDirty() )
\r
142 setTimeout( function(){ editor.resetDirty(); } );
\r
145 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
146 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
148 function isNotEmpty( node )
\r
150 return isNotWhitespace( node ) && isNotBookmark( node );
\r
153 function isNbsp( node )
\r
155 return node.type == CKEDITOR.NODE_TEXT
\r
156 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
160 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
161 * non-exitable-block by padding extra br.(#3189)
\r
163 function onSelectionChangeFixBody( evt )
\r
165 var editor = evt.editor,
\r
166 path = evt.data.path,
\r
167 blockLimit = path.blockLimit,
\r
168 selection = evt.data.selection,
\r
169 range = selection.getRanges()[0],
\r
170 body = editor.document.getBody(),
\r
171 enterMode = editor.config.enterMode;
\r
173 // When enterMode set to block, we'll establing new paragraph only if we're
\r
174 // selecting inline contents right under body. (#3657)
\r
175 if ( enterMode != CKEDITOR.ENTER_BR
\r
177 && blockLimit.getName() == 'body'
\r
180 restoreDirty( editor );
\r
181 var fixedBlock = range.fixBlock( true,
\r
182 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
184 // For IE, we should remove any filler node which was introduced before.
\r
185 if ( CKEDITOR.env.ie )
\r
187 var first = fixedBlock.getFirst( isNotEmpty );
\r
188 first && isNbsp( first ) && first.remove();
\r
191 // If the fixed block is blank and already followed by a exitable
\r
192 // block, we should revert the fix. (#3684)
\r
193 if( fixedBlock.getOuterHtml().match( emptyParagraphRegexp ) )
\r
195 var previousElement = fixedBlock.getPrevious( isNotWhitespace ),
\r
196 nextElement = fixedBlock.getNext( isNotWhitespace );
\r
199 if ( previousElement && previousElement.getName
\r
200 && !( previousElement.getName() in nonExitableElementNames )
\r
201 && range.moveToElementEditStart( previousElement )
\r
202 || nextElement && nextElement.getName
\r
203 && !( nextElement.getName() in nonExitableElementNames )
\r
204 && range.moveToElementEditStart( nextElement ) )
\r
206 fixedBlock.remove();
\r
211 // Notify non-IE that selection has changed.
\r
212 if( !CKEDITOR.env.ie )
\r
213 editor.selectionChange();
\r
216 // All browsers are incapable to moving cursor out of certain non-exitable
\r
217 // blocks (e.g. table, list, pre) at the end of document, make this happen by
\r
218 // place a bogus node there, which would be later removed by dataprocessor.
\r
219 var lastNode = body.getLast( CKEDITOR.dom.walker.whitespaces( true ) );
\r
220 if ( lastNode && lastNode.getName && ( lastNode.getName() in nonExitableElementNames ) )
\r
222 restoreDirty( editor );
\r
223 if( !CKEDITOR.env.ie )
\r
224 body.appendBogus();
\r
226 body.append( editor.document.createText( '\xa0' ) );
\r
230 CKEDITOR.plugins.add( 'wysiwygarea',
\r
232 requires : [ 'editingblock' ],
\r
234 init : function( editor )
\r
236 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )
\r
237 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
239 editor.on( 'editingBlockReady', function()
\r
249 // Support for custom document.domain in IE.
\r
250 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
252 // Creates the iframe that holds the editable document.
\r
253 var createIFrame = function()
\r
261 // The document domain must be set within the src
\r
263 // Defer the script execution until iframe
\r
264 // has been added to main window, this is needed for some
\r
265 // browsers which will begin to load the frame content
\r
266 // prior to it's presentation in DOM.(#3894)
\r
268 + ( CKEDITOR.env.gecko ? 'setTimeout' : '' ) + '( function(){' +
\r
269 'document.open();' +
\r
270 ( CKEDITOR.env.ie && isCustomDomain ? 'document.domain="' + document.domain + '";' : '' ) +
\r
271 'document.write( window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] );' +
\r
272 'document.close();' +
\r
273 'window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] = null;' +
\r
275 + ( CKEDITOR.env.gecko ? ', 0 )' : ')()' )
\r
278 // Loading via src attribute does not work in Opera.
\r
279 if ( CKEDITOR.env.opera )
\r
282 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
283 ' style="width:100%;height:100%"' +
\r
284 ' frameBorder="0"' +
\r
286 ' allowTransparency="true"' +
\r
287 ' src="javascript:' + encodeURIComponent( src ) + '"' +
\r
290 var accTitle = editor.lang.editorTitle.replace( '%1', editor.name );
\r
292 if ( CKEDITOR.env.gecko )
\r
294 // Double checking the iframe will be loaded properly(#4058).
\r
295 iframe.on( 'load', function( ev )
\r
297 ev.removeListener();
\r
298 contentDomReady( iframe.$.contentWindow );
\r
301 // Accessibility attributes for Firefox.
\r
302 mainElement.setAttributes(
\r
307 iframe.setAttributes(
\r
313 else if ( CKEDITOR.env.webkit )
\r
315 iframe.setAttribute( 'title', accTitle ); // Safari 4
\r
316 iframe.setAttribute( 'name', accTitle ); // Safari 3
\r
318 else if ( CKEDITOR.env.ie )
\r
320 // Accessibility label for IE.
\r
321 fieldset = CKEDITOR.dom.element.createFromHtml(
\r
322 '<fieldset style="height:100%' +
\r
323 ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? ';position:relative' : '' ) +
\r
325 '<legend style="display:block;width:0;height:0;overflow:hidden;' +
\r
326 ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 'position:absolute' : '' ) +
\r
328 CKEDITOR.tools.htmlEncode( accTitle ) +
\r
331 , CKEDITOR.document );
\r
332 iframe.appendTo( fieldset );
\r
333 fieldset.appendTo( mainElement );
\r
336 if ( !CKEDITOR.env.ie )
\r
337 mainElement.append( iframe );
\r
340 // The script that launches the bootstrap logic on 'domReady', so the document
\r
341 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
342 var activationScript =
\r
343 '<script id="cke_actscrpt" type="text/javascript">' +
\r
344 'window.parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +
\r
347 // Editing area bootstrap code.
\r
348 var contentDomReady = function( domWindow )
\r
355 var domDocument = domWindow.document,
\r
356 body = domDocument.body;
\r
358 // Remove this script from the DOM.
\r
359 var script = domDocument.getElementById( "cke_actscrpt" );
\r
360 script.parentNode.removeChild( script );
\r
362 delete CKEDITOR._[ 'contentDomReady' + editor.name ];
\r
364 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
366 if ( CKEDITOR.env.ie )
\r
368 // Don't display the focus border.
\r
369 body.hideFocus = true;
\r
371 // Disable and re-enable the body to avoid IE from
\r
372 // taking the editing focus at startup. (#141 / #523)
\r
373 body.disabled = true;
\r
374 body.contentEditable = true;
\r
375 body.removeAttribute( 'disabled' );
\r
378 domDocument.designMode = 'on';
\r
380 // IE, Opera and Safari may not support it and throw
\r
382 try { domDocument.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}
\r
383 try { domDocument.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}
\r
385 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
386 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
388 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
389 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )
\r
391 domDocument.on( 'mousedown', function( ev )
\r
393 var control = ev.data.getTarget();
\r
394 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
395 editor.getSelection().selectElement( control );
\r
399 // Webkit: avoid from editing form control elements content.
\r
400 if ( CKEDITOR.env.webkit )
\r
402 // Prevent from tick checkbox/radiobox/select
\r
403 domDocument.on( 'click', function( ev )
\r
405 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
406 ev.data.preventDefault();
\r
409 // Prevent from editig textfield/textarea value.
\r
410 domDocument.on( 'mouseup', function( ev )
\r
412 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
413 ev.data.preventDefault();
\r
417 // IE standard compliant in editing frame doesn't focus the editor when
\r
418 // clicking outside actual content, manually apply the focus. (#1659)
\r
419 if( CKEDITOR.env.ie
\r
420 && domDocument.$.compatMode == 'CSS1Compat' )
\r
422 var htmlElement = domDocument.getDocumentElement();
\r
423 htmlElement.on( 'mousedown', function( evt )
\r
425 // Setting focus directly on editor doesn't work, we
\r
426 // have to use here a temporary element to 'redirect'
\r
428 if ( evt.data.getTarget().equals( htmlElement ) )
\r
429 ieFocusGrabber.focus();
\r
433 var focusTarget = ( CKEDITOR.env.ie || CKEDITOR.env.webkit ) ?
\r
434 domWindow : domDocument;
\r
436 focusTarget.on( 'blur', function()
\r
438 editor.focusManager.blur();
\r
441 focusTarget.on( 'focus', function()
\r
443 // Gecko need a key event to 'wake up' the editing
\r
444 // ability when document is empty.(#3864)
\r
445 if ( CKEDITOR.env.gecko )
\r
448 while( first.firstChild )
\r
449 first = first.firstChild;
\r
451 if( !first.nextSibling
\r
452 && ( 'BR' == first.tagName )
\r
453 && first.hasAttribute( '_moz_editor_bogus_node' ) )
\r
455 var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );
\r
456 keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,
\r
457 false, false, false, 0, 32 );
\r
458 domDocument.$.dispatchEvent( keyEventSimulate );
\r
459 var bogusText = domDocument.getBody().getFirst() ;
\r
460 // Compensate the line maintaining <br> if enterMode is not block.
\r
461 if ( editor.config.enterMode == CKEDITOR.ENTER_BR )
\r
462 domDocument.createElement( 'br', { attributes: { '_moz_dirty' : "" } } )
\r
463 .replace( bogusText );
\r
465 bogusText.remove();
\r
469 editor.focusManager.focus();
\r
472 var keystrokeHandler = editor.keystrokeHandler;
\r
473 if ( keystrokeHandler )
\r
474 keystrokeHandler.attach( domDocument );
\r
476 if ( CKEDITOR.env.ie )
\r
478 // Cancel default action for backspace in IE on control types. (#4047)
\r
479 domDocument.on( 'keydown', function( evt )
\r
482 var control = evt.data.getKeystroke() == 8
\r
483 && editor.getSelection().getSelectedElement();
\r
486 // Make undo snapshot.
\r
487 editor.fire( 'saveSnapshot' );
\r
488 // Remove manually.
\r
490 editor.fire( 'saveSnapshot' );
\r
495 // PageUp/PageDown scrolling is broken in document
\r
496 // with standard doctype, manually fix it. (#4736)
\r
497 if( domDocument.$.compatMode == 'CSS1Compat' )
\r
499 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
500 domDocument.on( 'keydown', function( evt )
\r
502 if( evt.data.getKeystroke() in pageUpDownKeys )
\r
504 setTimeout( function ()
\r
506 editor.getSelection().scrollIntoView();
\r
513 // Adds the document body as a context menu target.
\r
514 if ( editor.contextMenu )
\r
515 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
517 setTimeout( function()
\r
519 editor.fire( 'contentDom' );
\r
523 editor.mode = 'wysiwyg';
\r
524 editor.fire( 'mode' );
\r
528 isLoadingData = false;
\r
530 if ( isPendingFocus )
\r
533 isPendingFocus = false;
\r
535 setTimeout( function()
\r
537 editor.fire( 'dataReady' );
\r
541 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
542 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
545 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
546 * fix the problem. :(
\r
548 if ( CKEDITOR.env.ie )
\r
550 setTimeout( function()
\r
552 if ( editor.document )
\r
554 var $body = editor.document.$.body;
\r
555 $body.runtimeStyle.marginBottom = '0px';
\r
556 $body.runtimeStyle.marginBottom = '';
\r
564 editor.addMode( 'wysiwyg',
\r
566 load : function( holderElement, data, isSnapshot )
\r
568 mainElement = holderElement;
\r
570 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
571 holderElement.setStyle( 'position', 'relative' );
\r
573 // The editor data "may be dirty" after this
\r
575 editor.mayBeDirty = true;
\r
580 this.loadSnapshotData( data );
\r
582 this.loadData( data );
\r
585 loadData : function( data )
\r
587 isLoadingData = true;
\r
589 // Get the HTML version of the data.
\r
590 if ( editor.dataProcessor )
\r
592 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
596 editor.config.docType +
\r
597 '<html dir="' + editor.config.contentsLangDirection + '">' +
\r
599 '<link type="text/css" rel="stylesheet" href="' +
\r
600 [].concat( editor.config.contentsCss ).join( '"><link type="text/css" rel="stylesheet" href="' ) +
\r
602 '<style type="text/css" _fcktemp="true">' +
\r
603 editor._.styles.join( '\n' ) +
\r
612 window[ '_cke_htmlToLoad_' + editor.name ] = data;
\r
613 CKEDITOR._[ 'contentDomReady' + editor.name ] = contentDomReady;
\r
616 // Opera must use the old method for loading contents.
\r
617 if ( CKEDITOR.env.opera )
\r
619 var doc = iframe.$.contentWindow.document;
\r
626 getData : function()
\r
628 var data = iframe.getFrameDocument().getBody().getHtml();
\r
630 if ( editor.dataProcessor )
\r
631 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
633 // Strip the last blank paragraph within document.
\r
634 if ( editor.config.ignoreEmptyParagraph )
\r
635 data = data.replace( emptyParagraphRegexp, '' );
\r
640 getSnapshotData : function()
\r
642 return iframe.getFrameDocument().getBody().getHtml();
\r
645 loadSnapshotData : function( data )
\r
647 iframe.getFrameDocument().getBody().setHtml( data );
\r
650 unload : function( holderElement )
\r
652 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
654 editor.fire( 'contentDomUnload' );
\r
659 if ( isLoadingData )
\r
660 isPendingFocus = true;
\r
661 else if ( editor.window )
\r
663 editor.window.focus();
\r
665 // Force the selection to happen, in this way
\r
666 // we guarantee the focus will be there. (#4848)
\r
667 if ( CKEDITOR.env.ie )
\r
671 var sel = editor.getSelection();
\r
672 sel = sel && sel.getNative();
\r
673 var range = sel && sel.type && sel.createRange();
\r
683 editor.selectionChange();
\r
688 editor.on( 'insertHtml', onInsertHtml, null, null, 20 );
\r
689 editor.on( 'insertElement', onInsertElement, null, null, 20 );
\r
690 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
691 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
694 // Create an invisible element to grab focus.
\r
695 if( CKEDITOR.env.ie )
\r
697 var ieFocusGrabber;
\r
698 editor.on( 'uiReady', function()
\r
700 ieFocusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
\r
701 '<input tabindex="-1" style="position:absolute; left:-10000">' ) );
\r
703 ieFocusGrabber.on( 'focus', function()
\r
712 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
713 if( CKEDITOR.env.gecko )
\r
715 var topWin = window.top;
\r
719 var topBody = topWin.document.body;
\r
722 topWin.addEventListener( 'load', arguments.callee, false );
\r
725 topBody.setAttribute( 'onpageshow', topBody.getAttribute( 'onpageshow' )
\r
726 + ';event.persisted && CKEDITOR.tools.callFunction(' +
\r
727 CKEDITOR.tools.addFunction( function()
\r
729 var allInstances = CKEDITOR.instances,
\r
732 for( var i in allInstances )
\r
734 editor = allInstances[ i ];
\r
735 doc = editor.document;
\r
738 doc.$.designMode = 'off';
\r
739 doc.$.designMode = 'on';
\r
750 * Disables the ability of resize objects (image and tables) in the editing
\r
755 * config.disableObjectResizing = true;
\r
757 CKEDITOR.config.disableObjectResizing = false;
\r
760 * Disables the "table tools" offered natively by the browser (currently
\r
761 * Firefox only) to make quick table editing operations, like adding or
\r
762 * deleting rows and columns.
\r
766 * config.disableNativeTableHandles = false;
\r
768 CKEDITOR.config.disableNativeTableHandles = true;
\r
771 * Disables the built-in spell checker while typing natively available in the
\r
772 * browser (currently Firefox and Safari only).<br /><br />
\r
774 * Even if word suggestions will not appear in the CKEditor context menu, this
\r
775 * feature is useful to help quickly identifying misspelled words.<br /><br />
\r
777 * This setting is currently compatible with Firefox only due to limitations in
\r
782 * config.disableNativeSpellChecker = false;
\r
784 CKEDITOR.config.disableNativeSpellChecker = true;
\r
787 * Whether the editor must output an empty value ("") if it's contents is made
\r
788 * by an empty paragraph only.
\r
792 * config.ignoreEmptyParagraph = false;
\r
794 CKEDITOR.config.ignoreEmptyParagraph = true;
\r