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
85 if ( this.config.enterMode != CKEDITOR.ENTER_BR && isBlock )
\r
87 while( ( parent = range.getCommonAncestor( false, true ) )
\r
88 && ( dtd = CKEDITOR.dtd[ parent.getName() ] )
\r
89 && !( dtd && dtd [ elementName ] ) )
\r
95 // Insert the new node.
\r
96 range.insertNode( clone );
\r
98 // Save the last element reference so we can make the
\r
100 if ( !lastElement )
\r
101 lastElement = clone;
\r
104 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
106 var next = lastElement.getNextSourceNode( true );
\r
107 if ( next && next.type == CKEDITOR.NODE_ELEMENT )
\r
108 range.moveToElementEditStart( next );
\r
110 selection.selectRanges( [ range ] );
\r
113 this.getSelection().lock();
\r
115 // Save snaps after the whole execution completed.
\r
116 // This's a workaround for make DOM modification's happened after
\r
117 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
119 CKEDITOR.tools.setTimeout( function(){
\r
120 this.fire( 'saveSnapshot' );
\r
126 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
127 * non-exitable-block by padding extra br.(#3189)
\r
129 function onSelectionChangeFixBody( evt )
\r
131 var editor = evt.editor,
\r
132 path = evt.data.path,
\r
133 blockLimit = path.blockLimit,
\r
134 selection = evt.data.selection,
\r
135 range = selection.getRanges()[0],
\r
136 body = editor.document.getBody(),
\r
137 enterMode = editor.config.enterMode;
\r
139 // When enterMode set to block, we'll establing new paragraph only if we're
\r
140 // selecting inline contents right under body. (#3657)
\r
141 if ( enterMode != CKEDITOR.ENTER_BR
\r
143 && blockLimit.getName() == 'body'
\r
146 var bms = selection.createBookmarks(),
\r
147 fixedBlock = range.fixBlock( true,
\r
148 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
150 // For IE, we'll be removing any bogus br ( introduce by fixing body )
\r
151 // right now to prevent it introducing visual line break.
\r
152 if ( CKEDITOR.env.ie )
\r
154 var brNodeList = fixedBlock.getElementsByTag( 'br' ), brNode;
\r
155 for ( var i = 0 ; i < brNodeList.count() ; i++ )
\r
157 if( ( brNode = brNodeList.getItem( i ) ) && brNode.hasAttribute( '_cke_bogus' ) )
\r
162 selection.selectBookmarks( bms );
\r
164 // If the fixed block is blank and is already followed by a exitable
\r
165 // block, we should drop it and move to the exist block(#3684).
\r
166 var children = fixedBlock.getChildren(),
\r
167 count = children.count(),
\r
169 whitespaceGuard = CKEDITOR.dom.walker.whitespaces( true ),
\r
170 previousElement = fixedBlock.getPrevious( whitespaceGuard ),
\r
171 nextElement = fixedBlock.getNext( whitespaceGuard ),
\r
173 if ( previousElement && previousElement.getName
\r
174 && !( previousElement.getName() in nonExitableElementNames ) )
\r
175 enterBlock = previousElement;
\r
176 else if ( nextElement && nextElement.getName
\r
177 && !( nextElement.getName() in nonExitableElementNames ) )
\r
178 enterBlock = nextElement;
\r
180 // Not all blocks are editable, e.g. <hr />, further checking it.(#3994)
\r
182 || ( firstChild = children.getItem( 0 ) ) && firstChild.is && firstChild.is( 'br' ) )
\r
184 && range.moveToElementEditStart( enterBlock ) )
\r
186 fixedBlock.remove();
\r
191 // Inserting the padding-br before body if it's preceded by an
\r
192 // unexitable block.
\r
193 var lastNode = body.getLast( CKEDITOR.dom.walker.whitespaces( true ) );
\r
194 if ( lastNode && lastNode.getName && ( lastNode.getName() in nonExitableElementNames ) )
\r
196 var paddingBlock = editor.document.createElement(
\r
197 ( CKEDITOR.env.ie && enterMode != CKEDITOR.ENTER_BR ) ?
\r
198 '<br _cke_bogus="true" />' : 'br' );
\r
199 body.append( paddingBlock );
\r
203 CKEDITOR.plugins.add( 'wysiwygarea',
\r
205 requires : [ 'editingblock' ],
\r
207 init : function( editor )
\r
209 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )
\r
210 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
212 editor.on( 'editingBlockReady', function()
\r
222 // Support for custom document.domain in IE.
\r
223 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
225 // Creates the iframe that holds the editable document.
\r
226 var createIFrame = function()
\r
234 // The document domain must be set within the src
\r
236 // Defer the script execution until iframe
\r
237 // has been added to main window, this is needed for some
\r
238 // browsers which will begin to load the frame content
\r
239 // prior to it's presentation in DOM.(#3894)
\r
241 + ( CKEDITOR.env.gecko ? 'setTimeout' : '' ) + '( function(){' +
\r
242 'document.open();' +
\r
243 ( CKEDITOR.env.ie && isCustomDomain ? 'document.domain="' + document.domain + '";' : '' ) +
\r
244 'document.write( window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] );' +
\r
245 'document.close();' +
\r
246 'window.parent[ "_cke_htmlToLoad_' + editor.name + '" ] = null;' +
\r
248 + ( CKEDITOR.env.gecko ? ', 0 )' : ')()' )
\r
251 // Loading via src attribute does not work in Opera.
\r
252 if ( CKEDITOR.env.opera )
\r
255 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
256 ' style="width:100%;height:100%"' +
\r
257 ' frameBorder="0"' +
\r
259 ' allowTransparency="true"' +
\r
260 ' src="javascript:' + encodeURIComponent( src ) + '"' +
\r
263 var accTitle = editor.lang.editorTitle.replace( '%1', editor.name );
\r
265 if ( CKEDITOR.env.gecko )
\r
267 // Double checking the iframe will be loaded properly(#4058).
\r
268 iframe.on( 'load', function( ev )
\r
270 ev.removeListener();
\r
271 contentDomReady( iframe.$.contentWindow );
\r
274 // Accessibility attributes for Firefox.
\r
275 mainElement.setAttributes(
\r
280 iframe.setAttributes(
\r
286 else if ( CKEDITOR.env.webkit )
\r
288 iframe.setAttribute( 'title', accTitle ); // Safari 4
\r
289 iframe.setAttribute( 'name', accTitle ); // Safari 3
\r
291 else if ( CKEDITOR.env.ie )
\r
293 // Accessibility label for IE.
\r
294 fieldset = CKEDITOR.dom.element.createFromHtml(
\r
295 '<fieldset style="height:100%' +
\r
296 ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? ';position:relative' : '' ) +
\r
298 '<legend style="display:block;width:0;height:0;overflow:hidden;' +
\r
299 ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 'position:absolute' : '' ) +
\r
301 CKEDITOR.tools.htmlEncode( accTitle ) +
\r
304 , CKEDITOR.document );
\r
305 iframe.appendTo( fieldset );
\r
306 fieldset.appendTo( mainElement );
\r
309 if ( !CKEDITOR.env.ie )
\r
310 mainElement.append( iframe );
\r
313 // The script that is appended to the data being loaded. It
\r
314 // enables editing, and makes some
\r
315 var activationScript =
\r
316 '<script id="cke_actscrpt" type="text/javascript">' +
\r
317 'window.onload = function()' +
\r
319 // Call the temporary function for the editing
\r
321 'window.parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +
\r
325 // Editing area bootstrap code.
\r
326 var contentDomReady = function( domWindow )
\r
333 var domDocument = domWindow.document,
\r
334 body = domDocument.body;
\r
336 // Remove this script from the DOM.
\r
337 var script = domDocument.getElementById( "cke_actscrpt" );
\r
338 script.parentNode.removeChild( script );
\r
340 delete CKEDITOR._[ 'contentDomReady' + editor.name ];
\r
342 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
344 if ( CKEDITOR.env.ie )
\r
346 // Don't display the focus border.
\r
347 body.hideFocus = true;
\r
349 // Disable and re-enable the body to avoid IE from
\r
350 // taking the editing focus at startup. (#141 / #523)
\r
351 body.disabled = true;
\r
352 body.contentEditable = true;
\r
353 body.removeAttribute( 'disabled' );
\r
356 domDocument.designMode = 'on';
\r
358 // IE, Opera and Safari may not support it and throw
\r
360 try { domDocument.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}
\r
361 try { domDocument.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}
\r
363 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
364 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
366 // Gecko need a key event to 'wake up' the editing
\r
367 // ability when document is empty.(#3864)
\r
368 var firstNode = domDocument.getBody().getFirst();
\r
369 if ( CKEDITOR.env.gecko
\r
370 && firstNode && firstNode.is
\r
371 && firstNode.is( 'br' ) && firstNode.hasAttribute( '_moz_editor_bogus_node' ) )
\r
373 var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );
\r
374 keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,
\r
375 false, false, false, 0, 32 );
\r
376 domDocument.$.dispatchEvent( keyEventSimulate );
\r
377 var bogusText = domDocument.getBody().getFirst() ;
\r
378 // Compensate the line maintaining <br> if enterMode is not block.
\r
379 if ( editor.config.enterMode == CKEDITOR.ENTER_BR )
\r
380 domDocument.createElement( 'br', { attributes: { '_moz_dirty' : "" } } )
\r
381 .replace( bogusText );
\r
383 bogusText.remove();
\r
386 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
387 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )
\r
389 domDocument.on( 'mousedown', function( ev )
\r
391 var control = ev.data.getTarget();
\r
392 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
393 editor.getSelection().selectElement( control );
\r
397 // Webkit: avoid from editing form control elements content.
\r
398 if ( CKEDITOR.env.webkit )
\r
400 // Prevent from tick checkbox/radiobox/select
\r
401 domDocument.on( 'click', function( ev )
\r
403 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
404 ev.data.preventDefault();
\r
407 // Prevent from editig textfield/textarea value.
\r
408 domDocument.on( 'mouseup', function( ev )
\r
410 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
411 ev.data.preventDefault();
\r
415 var focusTarget = ( CKEDITOR.env.ie || CKEDITOR.env.safari ) ?
\r
416 domWindow : domDocument;
\r
418 focusTarget.on( 'blur', function()
\r
420 editor.focusManager.blur();
\r
423 focusTarget.on( 'focus', function()
\r
425 editor.focusManager.focus();
\r
428 var keystrokeHandler = editor.keystrokeHandler;
\r
429 if ( keystrokeHandler )
\r
430 keystrokeHandler.attach( domDocument );
\r
432 // Adds the document body as a context menu target.
\r
433 if ( editor.contextMenu )
\r
434 editor.contextMenu.addTarget( domDocument );
\r
436 setTimeout( function()
\r
438 editor.fire( 'contentDom' );
\r
442 editor.mode = 'wysiwyg';
\r
443 editor.fire( 'mode' );
\r
447 isLoadingData = false;
\r
449 if ( isPendingFocus )
\r
452 isPendingFocus = false;
\r
456 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
457 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
460 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
461 * fix the problem. :(
\r
463 if ( CKEDITOR.env.ie )
\r
465 setTimeout( function()
\r
467 if ( editor.document )
\r
469 var $body = editor.document.$.body;
\r
470 $body.runtimeStyle.marginBottom = '0px';
\r
471 $body.runtimeStyle.marginBottom = '';
\r
479 editor.addMode( 'wysiwyg',
\r
481 load : function( holderElement, data, isSnapshot )
\r
483 mainElement = holderElement;
\r
485 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
486 holderElement.setStyle( 'position', 'relative' );
\r
488 // The editor data "may be dirty" after this
\r
490 editor.mayBeDirty = true;
\r
495 this.loadSnapshotData( data );
\r
497 this.loadData( data );
\r
500 loadData : function( data )
\r
502 isLoadingData = true;
\r
504 // Get the HTML version of the data.
\r
505 if ( editor.dataProcessor )
\r
507 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
511 editor.config.docType +
\r
512 '<html dir="' + editor.config.contentsLangDirection + '">' +
\r
514 '<link href="' + editor.config.contentsCss + '" type="text/css" rel="stylesheet" _fcktemp="true"/>' +
\r
515 '<style type="text/css" _fcktemp="true">' +
\r
516 editor._.styles.join( '\n' ) +
\r
525 window[ '_cke_htmlToLoad_' + editor.name ] = data;
\r
526 CKEDITOR._[ 'contentDomReady' + editor.name ] = contentDomReady;
\r
529 // Opera must use the old method for loading contents.
\r
530 if ( CKEDITOR.env.opera )
\r
532 var doc = iframe.$.contentWindow.document;
\r
539 getData : function()
\r
541 var data = iframe.getFrameDocument().getBody().getHtml();
\r
543 if ( editor.dataProcessor )
\r
544 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
546 // Strip the last blank paragraph within document.
\r
547 if ( editor.config.ignoreEmptyParagraph )
\r
548 data = data.replace( emptyParagraphRegexp, '' );
\r
553 getSnapshotData : function()
\r
555 return iframe.getFrameDocument().getBody().getHtml();
\r
558 loadSnapshotData : function( data )
\r
560 iframe.getFrameDocument().getBody().setHtml( data );
\r
563 unload : function( holderElement )
\r
565 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
567 editor.fire( 'contentDomUnload' );
\r
572 if ( isLoadingData )
\r
573 isPendingFocus = true;
\r
574 else if ( editor.window )
\r
576 editor.window.focus();
\r
577 editor.selectionChange();
\r
582 editor.on( 'insertHtml', onInsertHtml, null, null, 20 );
\r
583 editor.on( 'insertElement', onInsertElement, null, null, 20 );
\r
584 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
585 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
592 * Disables the ability of resize objects (image and tables) in the editing
\r
597 * config.disableObjectResizing = true;
\r
599 CKEDITOR.config.disableObjectResizing = false;
\r
602 * Disables the "table tools" offered natively by the browser (currently
\r
603 * Firefox only) to make quick table editing operations, like adding or
\r
604 * deleting rows and columns.
\r
608 * config.disableNativeTableHandles = false;
\r
610 CKEDITOR.config.disableNativeTableHandles = true;
\r
613 * Disables the built-in spell checker while typing natively available in the
\r
614 * browser (currently Firefox and Safari only).<br /><br />
\r
616 * Even if word suggestions will not appear in the CKEditor context menu, this
\r
617 * feature is useful to help quickly identifying misspelled words.<br /><br />
\r
619 * This setting is currently compatible with Firefox only due to limitations in
\r
624 * config.disableNativeSpellChecker = false;
\r
626 CKEDITOR.config.disableNativeSpellChecker = true;
\r
629 * Whether the editor must output an empty value ("") if it's contents is made
\r
630 * by an empty paragraph only.
\r
634 * config.ignoreEmptyParagraph = false;
\r
636 CKEDITOR.config.ignoreEmptyParagraph = true;
\r