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[^>]*>| | )\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 // If we're in an empty block which indicate a new paragraph,
\r
92 // simply replace it with the inserting block.(#3664)
\r
93 if ( range.checkStartOfBlock()
\r
94 && range.checkEndOfBlock() )
\r
96 range.setStartBefore( current );
\r
97 range.collapse( true );
\r
101 range.splitBlock();
\r
105 // Insert the new node.
\r
106 range.insertNode( clone );
\r
108 // Save the last element reference so we can make the
\r
109 // selection later.
\r
110 if ( !lastElement )
\r
111 lastElement = clone;
\r
114 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
116 var next = lastElement.getNextSourceNode( true );
\r
117 if ( next && next.type == CKEDITOR.NODE_ELEMENT )
\r
118 range.moveToElementEditStart( next );
\r
120 selection.selectRanges( [ range ] );
\r
123 this.getSelection().lock();
\r
125 // Save snaps after the whole execution completed.
\r
126 // This's a workaround for make DOM modification's happened after
\r
127 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
129 CKEDITOR.tools.setTimeout( function(){
\r
130 this.fire( 'saveSnapshot' );
\r
135 // DOM modification here should not bother dirty flag.(#4385)
\r
136 function restoreDirty( editor )
\r
138 if( !editor.checkDirty() )
\r
139 setTimeout( function(){ editor.resetDirty(); } );
\r
143 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
144 * non-exitable-block by padding extra br.(#3189)
\r
146 function onSelectionChangeFixBody( evt )
\r
148 var editor = evt.editor,
\r
149 path = evt.data.path,
\r
150 blockLimit = path.blockLimit,
\r
151 selection = evt.data.selection,
\r
152 range = selection.getRanges()[0],
\r
153 body = editor.document.getBody(),
\r
154 enterMode = editor.config.enterMode;
\r
156 // When enterMode set to block, we'll establing new paragraph only if we're
\r
157 // selecting inline contents right under body. (#3657)
\r
158 if ( enterMode != CKEDITOR.ENTER_BR
\r
160 && blockLimit.getName() == 'body'
\r
163 restoreDirty( editor );
\r
164 var bms = selection.createBookmarks(),
\r
165 fixedBlock = range.fixBlock( true,
\r
166 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
168 // For IE, we'll be removing any bogus br ( introduce by fixing body )
\r
169 // right now to prevent it introducing visual line break.
\r
170 if ( CKEDITOR.env.ie )
\r
172 var brNodeList = fixedBlock.getElementsByTag( 'br' ), brNode;
\r
173 for ( var i = 0 ; i < brNodeList.count() ; i++ )
\r
175 if( ( brNode = brNodeList.getItem( i ) ) && brNode.hasAttribute( '_cke_bogus' ) )
\r
180 selection.selectBookmarks( bms );
\r
182 // If the fixed block is blank and is already followed by a exitable
\r
183 // block, we should drop it and move to the exist block(#3684).
\r
184 var children = fixedBlock.getChildren(),
\r
185 count = children.count(),
\r
187 whitespaceGuard = CKEDITOR.dom.walker.whitespaces( true ),
\r
188 previousElement = fixedBlock.getPrevious( whitespaceGuard ),
\r
189 nextElement = fixedBlock.getNext( whitespaceGuard ),
\r
191 if ( previousElement && previousElement.getName
\r
192 && !( previousElement.getName() in nonExitableElementNames ) )
\r
193 enterBlock = previousElement;
\r
194 else if ( nextElement && nextElement.getName
\r
195 && !( nextElement.getName() in nonExitableElementNames ) )
\r
196 enterBlock = nextElement;
\r
198 // Not all blocks are editable, e.g. <hr />, further checking it.(#3994)
\r
200 || ( firstChild = children.getItem( 0 ) ) && firstChild.is && firstChild.is( 'br' ) )
\r
202 && range.moveToElementEditStart( enterBlock ) )
\r
204 fixedBlock.remove();
\r
209 // Inserting the padding-br before body if it's preceded by an
\r
210 // unexitable block.
\r
211 var lastNode = body.getLast( CKEDITOR.dom.walker.whitespaces( true ) );
\r
212 if ( lastNode && lastNode.getName && ( lastNode.getName() in nonExitableElementNames ) )
\r
214 restoreDirty( editor );
\r
215 var paddingBlock = editor.document.createElement(
\r
216 ( CKEDITOR.env.ie && enterMode != CKEDITOR.ENTER_BR ) ?
\r
217 '<br _cke_bogus="true" />' : 'br' );
\r
218 body.append( paddingBlock );
\r
222 CKEDITOR.plugins.add( 'wysiwygarea',
\r
224 requires : [ 'editingblock' ],
\r
226 init : function( editor )
\r
228 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )
\r
229 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
231 editor.on( 'editingBlockReady', function()
\r
241 // Support for custom document.domain in IE.
\r
242 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
244 // Creates the iframe that holds the editable document.
\r
245 var createIFrame = function()
\r
253 // The document domain must be set within the src
\r
255 // Defer the script execution until iframe
\r
256 // has been added to main window, this is needed for some
\r
257 // browsers which will begin to load the frame content
\r
258 // prior to it's presentation in DOM.(#3894)
\r
260 + ( CKEDITOR.env.gecko ? 'setTimeout' : '' ) + '( function(){' +
\r
261 'document.open();' +
\r
262 ( CKEDITOR.env.ie && isCustomDomain ? 'document.domain="' + document.domain + '";' : '' ) +
\r
263 'document.write( window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] );' +
\r
264 'document.close();' +
\r
265 'window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] = null;' +
\r
267 + ( CKEDITOR.env.gecko ? ', 0 )' : ')()' )
\r
270 // Loading via src attribute does not work in Opera.
\r
271 if ( CKEDITOR.env.opera )
\r
274 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
275 ' style="width:100%;height:100%"' +
\r
276 ' frameBorder="0"' +
\r
278 ' allowTransparency="true"' +
\r
279 ' src="javascript:' + encodeURIComponent( src ) + '"' +
\r
282 var accTitle = editor.lang.editorTitle.replace( '%1', editor.name );
\r
284 if ( CKEDITOR.env.gecko )
\r
286 // Double checking the iframe will be loaded properly(#4058).
\r
287 iframe.on( 'load', function( ev )
\r
289 ev.removeListener();
\r
290 contentDomReady( iframe.$.contentWindow );
\r
293 // Accessibility attributes for Firefox.
\r
294 mainElement.setAttributes(
\r
299 iframe.setAttributes(
\r
305 else if ( CKEDITOR.env.webkit )
\r
307 iframe.setAttribute( 'title', accTitle ); // Safari 4
\r
308 iframe.setAttribute( 'name', accTitle ); // Safari 3
\r
310 else if ( CKEDITOR.env.ie )
\r
312 // Accessibility label for IE.
\r
313 fieldset = CKEDITOR.dom.element.createFromHtml(
\r
314 '<fieldset style="height:100%' +
\r
315 ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? ';position:relative' : '' ) +
\r
317 '<legend style="display:block;width:0;height:0;overflow:hidden;' +
\r
318 ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 'position:absolute' : '' ) +
\r
320 CKEDITOR.tools.htmlEncode( accTitle ) +
\r
323 , CKEDITOR.document );
\r
324 iframe.appendTo( fieldset );
\r
325 fieldset.appendTo( mainElement );
\r
328 if ( !CKEDITOR.env.ie )
\r
329 mainElement.append( iframe );
\r
332 // The script that is appended to the data being loaded. It
\r
333 // enables editing, and makes some
\r
334 var activationScript =
\r
335 '<script id="cke_actscrpt" type="text/javascript">' +
\r
336 'window.onload = function()' +
\r
338 // Call the temporary function for the editing
\r
340 'window.parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +
\r
344 // Editing area bootstrap code.
\r
345 var contentDomReady = function( domWindow )
\r
352 var domDocument = domWindow.document,
\r
353 body = domDocument.body;
\r
355 // Remove this script from the DOM.
\r
356 var script = domDocument.getElementById( "cke_actscrpt" );
\r
357 script.parentNode.removeChild( script );
\r
359 delete CKEDITOR._[ 'contentDomReady' + editor.name ];
\r
361 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
363 if ( CKEDITOR.env.ie )
\r
365 // Don't display the focus border.
\r
366 body.hideFocus = true;
\r
368 // Disable and re-enable the body to avoid IE from
\r
369 // taking the editing focus at startup. (#141 / #523)
\r
370 body.disabled = true;
\r
371 body.contentEditable = true;
\r
372 body.removeAttribute( 'disabled' );
\r
375 domDocument.designMode = 'on';
\r
377 // IE, Opera and Safari may not support it and throw
\r
379 try { domDocument.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}
\r
380 try { domDocument.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}
\r
382 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
383 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
385 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
386 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )
\r
388 domDocument.on( 'mousedown', function( ev )
\r
390 var control = ev.data.getTarget();
\r
391 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
392 editor.getSelection().selectElement( control );
\r
396 // Webkit: avoid from editing form control elements content.
\r
397 if ( CKEDITOR.env.webkit )
\r
399 // Prevent from tick checkbox/radiobox/select
\r
400 domDocument.on( 'click', function( ev )
\r
402 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
403 ev.data.preventDefault();
\r
406 // Prevent from editig textfield/textarea value.
\r
407 domDocument.on( 'mouseup', function( ev )
\r
409 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
410 ev.data.preventDefault();
\r
414 var focusTarget = ( CKEDITOR.env.ie || CKEDITOR.env.webkit ) ?
\r
415 domWindow : domDocument;
\r
417 focusTarget.on( 'blur', function()
\r
419 editor.focusManager.blur();
\r
422 focusTarget.on( 'focus', function()
\r
424 // Gecko need a key event to 'wake up' the editing
\r
425 // ability when document is empty.(#3864)
\r
426 if ( CKEDITOR.env.gecko )
\r
429 while( first.firstChild )
\r
430 first = first.firstChild;
\r
432 if( !first.nextSibling
\r
433 && ( 'BR' == first.tagName )
\r
434 && first.hasAttribute( '_moz_editor_bogus_node' ) )
\r
436 var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );
\r
437 keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,
\r
438 false, false, false, 0, 32 );
\r
439 domDocument.$.dispatchEvent( keyEventSimulate );
\r
440 var bogusText = domDocument.getBody().getFirst() ;
\r
441 // Compensate the line maintaining <br> if enterMode is not block.
\r
442 if ( editor.config.enterMode == CKEDITOR.ENTER_BR )
\r
443 domDocument.createElement( 'br', { attributes: { '_moz_dirty' : "" } } )
\r
444 .replace( bogusText );
\r
446 bogusText.remove();
\r
450 editor.focusManager.focus();
\r
453 var keystrokeHandler = editor.keystrokeHandler;
\r
454 if ( keystrokeHandler )
\r
455 keystrokeHandler.attach( domDocument );
\r
457 // Cancel default action for backspace in IE on control types. (#4047)
\r
458 if ( CKEDITOR.env.ie )
\r
460 editor.on( 'key', function( event )
\r
463 var control = event.data.keyCode == 8
\r
464 && editor.getSelection().getSelectedElement();
\r
467 // Make undo snapshot.
\r
468 editor.fire( 'saveSnapshot' );
\r
469 // Remove manually.
\r
471 editor.fire( 'saveSnapshot' );
\r
477 // Adds the document body as a context menu target.
\r
478 if ( editor.contextMenu )
\r
479 editor.contextMenu.addTarget( domDocument );
\r
481 setTimeout( function()
\r
483 editor.fire( 'contentDom' );
\r
487 editor.mode = 'wysiwyg';
\r
488 editor.fire( 'mode' );
\r
492 isLoadingData = false;
\r
494 if ( isPendingFocus )
\r
497 isPendingFocus = false;
\r
499 setTimeout( function()
\r
501 editor.fire( 'dataReady' );
\r
505 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
506 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
509 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
510 * fix the problem. :(
\r
512 if ( CKEDITOR.env.ie )
\r
514 setTimeout( function()
\r
516 if ( editor.document )
\r
518 var $body = editor.document.$.body;
\r
519 $body.runtimeStyle.marginBottom = '0px';
\r
520 $body.runtimeStyle.marginBottom = '';
\r
528 editor.addMode( 'wysiwyg',
\r
530 load : function( holderElement, data, isSnapshot )
\r
532 mainElement = holderElement;
\r
534 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
535 holderElement.setStyle( 'position', 'relative' );
\r
537 // The editor data "may be dirty" after this
\r
539 editor.mayBeDirty = true;
\r
544 this.loadSnapshotData( data );
\r
546 this.loadData( data );
\r
549 loadData : function( data )
\r
551 isLoadingData = true;
\r
553 // Get the HTML version of the data.
\r
554 if ( editor.dataProcessor )
\r
556 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
560 editor.config.docType +
\r
561 '<html dir="' + editor.config.contentsLangDirection + '">' +
\r
563 '<link type="text/css" rel="stylesheet" href="' +
\r
564 [].concat( editor.config.contentsCss ).join( '"><link type="text/css" rel="stylesheet" href="' ) +
\r
566 '<style type="text/css" _fcktemp="true">' +
\r
567 editor._.styles.join( '\n' ) +
\r
576 window[ '_cke_htmlToLoad_' + editor.name ] = data;
\r
577 CKEDITOR._[ 'contentDomReady' + editor.name ] = contentDomReady;
\r
580 // Opera must use the old method for loading contents.
\r
581 if ( CKEDITOR.env.opera )
\r
583 var doc = iframe.$.contentWindow.document;
\r
590 getData : function()
\r
592 var data = iframe.getFrameDocument().getBody().getHtml();
\r
594 if ( editor.dataProcessor )
\r
595 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
597 // Strip the last blank paragraph within document.
\r
598 if ( editor.config.ignoreEmptyParagraph )
\r
599 data = data.replace( emptyParagraphRegexp, '' );
\r
604 getSnapshotData : function()
\r
606 return iframe.getFrameDocument().getBody().getHtml();
\r
609 loadSnapshotData : function( data )
\r
611 iframe.getFrameDocument().getBody().setHtml( data );
\r
614 unload : function( holderElement )
\r
616 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
618 editor.fire( 'contentDomUnload' );
\r
623 if ( isLoadingData )
\r
624 isPendingFocus = true;
\r
625 else if ( editor.window )
\r
627 editor.window.focus();
\r
628 editor.selectionChange();
\r
633 editor.on( 'insertHtml', onInsertHtml, null, null, 20 );
\r
634 editor.on( 'insertElement', onInsertElement, null, null, 20 );
\r
635 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
636 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
643 * Disables the ability of resize objects (image and tables) in the editing
\r
648 * config.disableObjectResizing = true;
\r
650 CKEDITOR.config.disableObjectResizing = false;
\r
653 * Disables the "table tools" offered natively by the browser (currently
\r
654 * Firefox only) to make quick table editing operations, like adding or
\r
655 * deleting rows and columns.
\r
659 * config.disableNativeTableHandles = false;
\r
661 CKEDITOR.config.disableNativeTableHandles = true;
\r
664 * Disables the built-in spell checker while typing natively available in the
\r
665 * browser (currently Firefox and Safari only).<br /><br />
\r
667 * Even if word suggestions will not appear in the CKEditor context menu, this
\r
668 * feature is useful to help quickly identifying misspelled words.<br /><br />
\r
670 * This setting is currently compatible with Firefox only due to limitations in
\r
675 * config.disableNativeSpellChecker = false;
\r
677 CKEDITOR.config.disableNativeSpellChecker = true;
\r
680 * Whether the editor must output an empty value ("") if it's contents is made
\r
681 * by an empty paragraph only.
\r
685 * config.ignoreEmptyParagraph = false;
\r
687 CKEDITOR.config.ignoreEmptyParagraph = true;
\r