2 Copyright (c) 2003-2011, 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 // Matching an empty paragraph at the end of document.
\r
14 var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center|pre)[^>]*>\s*(?:<br[^>]*>| |\u00A0| )?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;
\r
16 var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );
\r
18 // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)
\r
19 function nonEditable( element )
\r
21 return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];
\r
25 function onInsert( insertFunc )
\r
27 return function( evt )
\r
29 if ( this.mode == 'wysiwyg' )
\r
33 this.fire( 'saveSnapshot' );
\r
35 insertFunc.call( this, evt.data );
\r
37 // Save snaps after the whole execution completed.
\r
38 // This's a workaround for make DOM modification's happened after
\r
39 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
41 CKEDITOR.tools.setTimeout( function()
\r
43 this.fire( 'saveSnapshot' );
\r
49 function doInsertHtml( data )
\r
51 if ( this.dataProcessor )
\r
52 data = this.dataProcessor.toHtml( data );
\r
54 // HTML insertion only considers the first range.
\r
55 var selection = this.getSelection(),
\r
56 range = selection.getRanges()[ 0 ];
\r
58 if ( range.checkReadOnly() )
\r
61 if ( CKEDITOR.env.ie )
\r
63 var selIsLocked = selection.isLocked;
\r
68 var $sel = selection.getNative();
\r
70 // Delete control selections to avoid IE bugs on pasteHTML.
\r
71 if ( $sel.type == 'Control' )
\r
73 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
75 // Due to IE bugs on handling contenteditable=false blocks
\r
76 // (#6005), we need to make some checks and eventually
\r
77 // delete the selection first.
\r
79 range = selection.getRanges()[ 0 ];
\r
80 var endContainer = range && range.endContainer;
\r
82 if ( endContainer &&
\r
83 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
84 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
85 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
87 range.setEndAfter( range.endContainer );
\r
88 range.deleteContents();
\r
94 $sel.createRange().pasteHTML( data );
\r
99 this.getSelection().lock();
\r
102 this.document.$.execCommand( 'inserthtml', false, data );
\r
104 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
105 if ( CKEDITOR.env.webkit )
\r
107 selection = this.getSelection();
\r
108 selection.scrollIntoView();
\r
112 function doInsertText( text )
\r
114 var selection = this.getSelection(),
\r
115 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
116 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
117 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
119 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
121 // Convert leading and trailing whitespaces into
\r
122 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
124 if ( match.length == 1 ) // one space, preserve it
\r
126 else if ( !offset ) // beginning of block
\r
127 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
128 else // end of block
\r
129 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
132 // Convert subsequent whitespaces into
\r
133 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
135 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
138 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
140 // Two line-breaks create one paragraph.
\r
141 if ( !isEnterBrMode )
\r
143 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
144 function( match, group1, text )
\r
146 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
150 // One <br> per line-break.
\r
151 html = html.replace( /\n/g, '<br>' );
\r
153 // Compensate padding <br> for non-IE.
\r
154 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
156 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
158 return CKEDITOR.tools.repeat( match, 2 );
\r
162 // Inline styles have to be inherited in Firefox.
\r
163 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
165 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
168 for ( var i = 0; i < path.elements.length; i++ )
\r
170 var tag = path.elements[ i ].getName();
\r
171 if ( tag in CKEDITOR.dtd.$inline )
\r
172 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
173 else if ( tag in CKEDITOR.dtd.$block )
\r
177 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
178 html = context.join( '' ) + html;
\r
181 doInsertHtml.call( this, html );
\r
184 function doInsertElement( element )
\r
186 var selection = this.getSelection(),
\r
187 ranges = selection.getRanges(),
\r
188 elementName = element.getName(),
\r
189 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
191 var selIsLocked = selection.isLocked;
\r
194 selection.unlock();
\r
196 var range, clone, lastElement, bookmark;
\r
198 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
200 range = ranges[ i ];
\r
202 if ( !range.checkReadOnly() )
\r
204 // Remove the original contents, merge splitted nodes.
\r
205 range.deleteContents( 1 );
\r
207 clone = !i && element || element.clone( 1 );
\r
209 // If we're inserting a block at dtd-violated position, split
\r
210 // the parent blocks until we reach blockLimit.
\r
214 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
215 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
216 && !( dtd && dtd [ elementName ] ) )
\r
218 // Split up inline elements.
\r
219 if ( current.getName() in CKEDITOR.dtd.span )
\r
220 range.splitElement( current );
\r
221 // If we're in an empty block which indicate a new paragraph,
\r
222 // simply replace it with the inserting block.(#3664)
\r
223 else if ( range.checkStartOfBlock()
\r
224 && range.checkEndOfBlock() )
\r
226 range.setStartBefore( current );
\r
227 range.collapse( true );
\r
231 range.splitBlock();
\r
235 // Insert the new node.
\r
236 range.insertNode( clone );
\r
238 // Save the last element reference so we can make the
\r
239 // selection later.
\r
240 if ( !lastElement )
\r
241 lastElement = clone;
\r
247 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
249 // If we're inserting a block element immediatelly followed by
\r
250 // another block element, the selection must move there. (#3100,#5436)
\r
253 var next = lastElement.getNext( notWhitespaceEval ),
\r
254 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
256 // Check if it's a block element that accepts text.
\r
257 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
\r
258 range.moveToElementEditStart( next );
\r
262 selection.selectRanges( [ range ] );
\r
265 this.getSelection().lock();
\r
268 // DOM modification here should not bother dirty flag.(#4385)
\r
269 function restoreDirty( editor )
\r
271 if ( !editor.checkDirty() )
\r
272 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
275 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
276 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
278 function isNotEmpty( node )
\r
280 return isNotWhitespace( node ) && isNotBookmark( node );
\r
283 function isNbsp( node )
\r
285 return node.type == CKEDITOR.NODE_TEXT
\r
286 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
289 function restoreSelection( selection )
\r
291 if ( selection.isLocked )
\r
293 selection.unlock();
\r
294 setTimeout( function() { selection.lock(); }, 0 );
\r
298 function isBlankParagraph( block )
\r
300 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
303 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
305 // Gecko need a key event to 'wake up' the editing
\r
306 // ability when document is empty.(#3864, #5781)
\r
307 function activateEditing( editor )
\r
309 var win = editor.window,
\r
310 doc = editor.document,
\r
311 body = editor.document.getBody(),
\r
312 bodyFirstChild = body.getFirst(),
\r
313 bodyChildsNum = body.getChildren().count();
\r
315 if ( !bodyChildsNum
\r
316 || bodyChildsNum == 1
\r
317 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
318 && bodyFirstChild.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 if ( CKEDITOR.env.gecko )
\r
362 activateEditing( editor );
\r
364 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
365 var pathBlock = path.block || path.blockLimit,
\r
366 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
368 // Check some specialities of the current path block:
\r
369 // 1. It is really displayed as block; (#7221)
\r
370 // 2. It doesn't end with one inner block; (#7467)
\r
371 // 3. It doesn't have bogus br yet.
\r
373 && pathBlock.isBlockBoundary()
\r
374 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
\r
375 && !pathBlock.is( 'pre' )
\r
376 && !pathBlock.getBogus() )
\r
378 editor.fire( 'updateSnapshot' );
\r
379 restoreDirty( editor );
\r
380 pathBlock.appendBogus();
\r
384 // When enterMode set to block, we'll establing new paragraph only if we're
\r
385 // selecting inline contents right under body. (#3657)
\r
386 if ( enterMode != CKEDITOR.ENTER_BR
\r
388 && blockLimit.getName() == 'body'
\r
391 editor.fire( 'updateSnapshot' );
\r
392 restoreDirty( editor );
\r
393 CKEDITOR.env.ie && restoreSelection( selection );
\r
395 var fixedBlock = range.fixBlock( true,
\r
396 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
398 // For IE, we should remove any filler node which was introduced before.
\r
399 if ( CKEDITOR.env.ie )
\r
401 var first = fixedBlock.getFirst( isNotEmpty );
\r
402 first && isNbsp( first ) && first.remove();
\r
405 // If the fixed block is actually blank and is already followed by an exitable blank
\r
406 // block, we should revert the fix and move into the existed one. (#3684)
\r
407 if ( isBlankParagraph( fixedBlock ) )
\r
409 var element = fixedBlock.getNext( isNotWhitespace );
\r
411 element.type == CKEDITOR.NODE_ELEMENT &&
\r
412 !nonEditable( element ) )
\r
414 range.moveToElementEditStart( element );
\r
415 fixedBlock.remove();
\r
419 element = fixedBlock.getPrevious( isNotWhitespace );
\r
421 element.type == CKEDITOR.NODE_ELEMENT &&
\r
422 !nonEditable( element ) )
\r
424 range.moveToElementEditEnd( element );
\r
425 fixedBlock.remove();
\r
431 // Cancel this selection change in favor of the next (correct). (#6811)
\r
435 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
\r
436 // at the end of document, makes it unable to continue adding content, we have to make this
\r
437 // easier by opening an new empty paragraph.
\r
438 var testRange = new CKEDITOR.dom.range( editor.document );
\r
439 testRange.moveToElementEditEnd( editor.document.getBody() );
\r
440 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
\r
441 if ( !testPath.blockLimit.is( 'body') )
\r
443 editor.fire( 'updateSnapshot' );
\r
444 restoreDirty( editor );
\r
445 CKEDITOR.env.ie && restoreSelection( selection );
\r
448 if ( enterMode != CKEDITOR.ENTER_BR )
\r
449 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
451 paddingBlock = body;
\r
453 if ( !CKEDITOR.env.ie )
\r
454 paddingBlock.appendBogus();
\r
458 CKEDITOR.plugins.add( 'wysiwygarea',
\r
460 requires : [ 'editingblock' ],
\r
462 init : function( editor )
\r
464 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )
\r
465 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
467 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
\r
469 var contentDomReadyHandler;
\r
470 editor.on( 'editingBlockReady', function()
\r
480 // Support for custom document.domain in IE.
\r
481 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
483 // Creates the iframe that holds the editable document.
\r
484 var createIFrame = function( data )
\r
490 'document.open();' +
\r
492 // The document domain must be set any time we
\r
493 // call document.open().
\r
494 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
496 'document.close();';
\r
498 // With IE, the custom domain has to be taken care at first,
\r
499 // for other browers, the 'src' attribute should be left empty to
\r
500 // trigger iframe's 'load' event.
\r
503 'javascript:void(0)' :
\r
505 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
509 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
510 ' style="width:100%;height:100%"' +
\r
511 ' frameBorder="0"' +
\r
512 ' title="' + frameLabel + '"' +
\r
513 ' src="' + src + '"' +
\r
514 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
515 ' allowTransparency="true"' +
\r
518 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
519 if ( document.location.protocol == 'chrome:' )
\r
520 CKEDITOR.event.useCapture = true;
\r
522 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
523 iframe.on( 'load', function( ev )
\r
526 ev.removeListener();
\r
528 var doc = iframe.getFrameDocument();
\r
531 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
534 // Reset adjustment back to default (#5689)
\r
535 if ( document.location.protocol == 'chrome:' )
\r
536 CKEDITOR.event.useCapture = false;
\r
538 // The container must be visible when creating the iframe in FF (#5956)
\r
539 var element = editor.element,
\r
540 isHidden = CKEDITOR.env.gecko && !element.isVisible(),
\r
541 previousStyles = {};
\r
546 position : element.getStyle( 'position' ),
\r
547 top : element.getStyle( 'top' )
\r
549 element.setStyles( { position : 'absolute', top : '-3000px' } );
\r
552 mainElement.append( iframe );
\r
556 setTimeout( function()
\r
559 element.setStyles( previousStyles );
\r
564 // The script that launches the bootstrap logic on 'domReady', so the document
\r
565 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
566 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
567 var activationScript =
\r
568 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
569 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
570 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
573 // Editing area bootstrap code.
\r
574 function contentDomReady( domWindow )
\r
576 if ( !frameLoaded )
\r
580 editor.fire( 'ariaWidget', iframe );
\r
582 var domDocument = domWindow.document,
\r
583 body = domDocument.body;
\r
585 // Remove this script from the DOM.
\r
586 var script = domDocument.getElementById( "cke_actscrpt" );
\r
587 script && script.parentNode.removeChild( script );
\r
589 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
591 if ( CKEDITOR.env.ie )
\r
593 // Don't display the focus border.
\r
594 body.hideFocus = true;
\r
596 // Disable and re-enable the body to avoid IE from
\r
597 // taking the editing focus at startup. (#141 / #523)
\r
598 body.disabled = true;
\r
599 body.contentEditable = true;
\r
600 body.removeAttribute( 'disabled' );
\r
604 // Avoid opening design mode in a frame window thread,
\r
605 // which will cause host page scrolling.(#4397)
\r
606 setTimeout( function()
\r
608 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
609 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
610 || CKEDITOR.env.opera )
\r
611 domDocument.$.body.contentEditable = true;
\r
612 else if ( CKEDITOR.env.webkit )
\r
613 domDocument.$.body.parentNode.contentEditable = true;
\r
615 domDocument.$.designMode = 'on';
\r
619 CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
621 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
622 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
624 domDocument.on( 'dblclick', function( evt )
\r
626 var element = evt.data.getTarget(),
\r
627 data = { element : element, dialog : '' };
\r
628 editor.fire( 'doubleclick', data );
\r
629 data.dialog && editor.openDialog( data.dialog );
\r
632 // Prevent automatic submission in IE #6336
\r
633 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
635 var element = evt.data.getTarget();
\r
636 if ( element.is( 'input' ) )
\r
638 var type = element.getAttribute( 'type' );
\r
639 if ( type == 'submit' || type == 'reset' )
\r
640 evt.data.preventDefault();
\r
644 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
645 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
647 domDocument.on( 'mousedown', function( ev )
\r
649 var control = ev.data.getTarget();
\r
650 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
651 editor.getSelection().selectElement( control );
\r
655 if ( CKEDITOR.env.gecko )
\r
657 domDocument.on( 'mouseup', function( ev )
\r
659 if ( ev.data.$.button == 2 )
\r
661 var target = ev.data.getTarget();
\r
663 // Prevent right click from selecting an empty block even
\r
664 // when selection is anchored inside it. (#5845)
\r
665 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
667 var range = new CKEDITOR.dom.range( domDocument );
\r
668 range.moveToElementEditStart( target );
\r
669 range.select( true );
\r
675 // Prevent the browser opening links in read-only blocks. (#6032)
\r
676 domDocument.on( 'click', function( ev )
\r
679 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
680 ev.preventDefault();
\r
683 // Webkit: avoid from editing form control elements content.
\r
684 if ( CKEDITOR.env.webkit )
\r
686 // Mark that cursor will right blinking (#7113).
\r
687 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
688 // Prevent from tick checkbox/radiobox/select
\r
689 domDocument.on( 'click', function( ev )
\r
691 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
692 ev.data.preventDefault();
\r
695 // Prevent from editig textfield/textarea value.
\r
696 domDocument.on( 'mouseup', function( ev )
\r
698 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
699 ev.data.preventDefault();
\r
703 // IE standard compliant in editing frame doesn't focus the editor when
\r
704 // clicking outside actual content, manually apply the focus. (#1659)
\r
705 if ( CKEDITOR.env.ie
\r
706 && domDocument.$.compatMode == 'CSS1Compat'
\r
707 || CKEDITOR.env.gecko
\r
708 || CKEDITOR.env.opera )
\r
710 var htmlElement = domDocument.getDocumentElement();
\r
711 htmlElement.on( 'mousedown', function( evt )
\r
713 // Setting focus directly on editor doesn't work, we
\r
714 // have to use here a temporary element to 'redirect'
\r
716 if ( evt.data.getTarget().equals( htmlElement ) )
\r
718 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
720 focusGrabber.focus();
\r
725 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
726 focusTarget.on( 'blur', function()
\r
728 editor.focusManager.blur();
\r
733 focusTarget.on( 'focus', function()
\r
735 var doc = editor.document;
\r
737 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
739 else if ( CKEDITOR.env.opera )
\r
740 doc.getBody().focus();
\r
741 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
742 else if ( CKEDITOR.env.webkit )
\r
746 editor.document.getDocumentElement().focus();
\r
751 editor.focusManager.focus();
\r
754 var keystrokeHandler = editor.keystrokeHandler;
\r
755 if ( keystrokeHandler )
\r
756 keystrokeHandler.attach( domDocument );
\r
758 if ( CKEDITOR.env.ie )
\r
760 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
761 // Override keystrokes which should have deletion behavior
\r
762 // on control types in IE . (#4047)
\r
763 domDocument.on( 'keydown', function( evt )
\r
765 var keyCode = evt.data.getKeystroke();
\r
767 // Backspace OR Delete.
\r
768 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
770 var sel = editor.getSelection(),
\r
771 control = sel.getSelectedElement();
\r
775 // Make undo snapshot.
\r
776 editor.fire( 'saveSnapshot' );
\r
778 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
779 // break up the selection, safely manage it here. (#4795)
\r
780 var bookmark = sel.getRanges()[ 0 ].createBookmark();
\r
781 // Remove the control manually.
\r
783 sel.selectBookmarks( [ bookmark ] );
\r
785 editor.fire( 'saveSnapshot' );
\r
787 evt.data.preventDefault();
\r
792 // PageUp/PageDown scrolling is broken in document
\r
793 // with standard doctype, manually fix it. (#4736)
\r
794 if ( domDocument.$.compatMode == 'CSS1Compat' )
\r
796 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
797 domDocument.on( 'keydown', function( evt )
\r
799 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
801 setTimeout( function ()
\r
803 editor.getSelection().scrollIntoView();
\r
809 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
810 editor.config.enterMode != CKEDITOR.ENTER_P
\r
811 && domDocument.on( 'selectionchange', function()
\r
813 var body = domDocument.getBody(),
\r
814 range = editor.getSelection().getRanges()[ 0 ];
\r
816 if ( body.getHtml().match( /^<p> <\/p>$/i )
\r
817 && range.startContainer.equals( body ) )
\r
819 // Avoid the ambiguity from a real user cursor position.
\r
820 setTimeout( function ()
\r
822 range = editor.getSelection().getRanges()[ 0 ];
\r
823 if ( !range.startContainer.equals ( 'body' ) )
\r
825 body.getFirst().remove( 1 );
\r
826 range.moveToElementEditEnd( body );
\r
834 // Adds the document body as a context menu target.
\r
835 if ( editor.contextMenu )
\r
836 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
838 setTimeout( function()
\r
840 editor.fire( 'contentDom' );
\r
844 editor.mode = 'wysiwyg';
\r
845 editor.fire( 'mode' );
\r
849 isLoadingData = false;
\r
851 if ( isPendingFocus )
\r
854 isPendingFocus = false;
\r
856 setTimeout( function()
\r
858 editor.fire( 'dataReady' );
\r
861 // IE, Opera and Safari may not support it and throw errors.
\r
862 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
863 if ( editor.config.disableObjectResizing )
\r
867 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
871 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
872 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
874 evt.data.preventDefault();
\r
880 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
881 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
884 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
885 * fix the problem. :(
\r
887 if ( CKEDITOR.env.ie )
\r
889 setTimeout( function()
\r
891 if ( editor.document )
\r
893 var $body = editor.document.$.body;
\r
894 $body.runtimeStyle.marginBottom = '0px';
\r
895 $body.runtimeStyle.marginBottom = '';
\r
903 editor.addMode( 'wysiwyg',
\r
905 load : function( holderElement, data, isSnapshot )
\r
907 mainElement = holderElement;
\r
909 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
910 holderElement.setStyle( 'position', 'relative' );
\r
912 // The editor data "may be dirty" after this
\r
914 editor.mayBeDirty = true;
\r
919 this.loadSnapshotData( data );
\r
921 this.loadData( data );
\r
924 loadData : function( data )
\r
926 isLoadingData = true;
\r
927 editor._.dataStore = { id : 1 };
\r
929 var config = editor.config,
\r
930 fullPage = config.fullPage,
\r
931 docType = config.docType;
\r
933 // Build the additional stuff to be included into <head>.
\r
935 '<style type="text/css" data-cke-temp="1">' +
\r
936 editor._.styles.join( '\n' ) +
\r
939 !fullPage && ( headExtra =
\r
940 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
943 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
947 // Search and sweep out the doctype declaration.
\r
948 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
950 editor.docType = docType = match;
\r
955 // Get the HTML version of the data.
\r
956 if ( editor.dataProcessor )
\r
957 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
961 // Check if the <body> tag is available.
\r
962 if ( !(/<body[\s|>]/).test( data ) )
\r
963 data = '<body>' + data;
\r
965 // Check if the <html> tag is available.
\r
966 if ( !(/<html[\s|>]/).test( data ) )
\r
967 data = '<html>' + data + '</html>';
\r
969 // Check if the <head> tag is available.
\r
970 if ( !(/<head[\s|>]/).test( data ) )
\r
971 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
972 else if ( !(/<title[\s|>]/).test( data ) )
\r
973 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
975 // The base must be the first tag in the HEAD, e.g. to get relative
\r
976 // links on styles.
\r
977 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
979 // Inject the extra stuff into <head>.
\r
980 // Attention: do not change it before testing it well. (V2)
\r
981 // This is tricky... if the head ends with <meta ... content type>,
\r
982 // Firefox will break. But, it works if we place our extra stuff as
\r
983 // the last elements in the HEAD.
\r
984 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
986 // Add the DOCTYPE back to it.
\r
987 data = docType + data;
\r
993 '<html dir="' + config.contentsLangDirection + '"' +
\r
994 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
996 '<title>' + frameLabel + '</title>' +
\r
1000 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
1001 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1007 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1008 if ( CKEDITOR.env.gecko )
\r
1009 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1011 data += activationScript;
\r
1014 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1016 createIFrame( data );
\r
1019 getData : function()
\r
1021 var config = editor.config,
\r
1022 fullPage = config.fullPage,
\r
1023 docType = fullPage && editor.docType,
\r
1024 doc = iframe.getFrameDocument();
\r
1026 var data = fullPage
\r
1027 ? doc.getDocumentElement().getOuterHtml()
\r
1028 : doc.getBody().getHtml();
\r
1030 // BR at the end of document is bogus node for Mozilla. (#5293).
\r
1031 if ( CKEDITOR.env.gecko )
\r
1032 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
\r
1034 if ( editor.dataProcessor )
\r
1035 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
1037 // Reset empty if the document contains only one empty paragraph.
\r
1038 if ( config.ignoreEmptyParagraph )
\r
1039 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
1042 data = docType + '\n' + data;
\r
1047 getSnapshotData : function()
\r
1049 return iframe.getFrameDocument().getBody().getHtml();
\r
1052 loadSnapshotData : function( data )
\r
1054 iframe.getFrameDocument().getBody().setHtml( data );
\r
1057 onDispose : function()
\r
1059 if ( !editor.document )
\r
1062 editor.document.getDocumentElement().clearCustomData();
\r
1063 editor.document.getBody().clearCustomData();
\r
1065 editor.window.clearCustomData();
\r
1066 editor.document.clearCustomData();
\r
1068 iframe.clearCustomData();
\r
1071 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1072 * editing area would break IE7/8's selection system, we have to put the editing
\r
1073 * iframe offline first. (#3812 and #5441)
\r
1078 unload : function( holderElement )
\r
1082 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1084 editor.fire( 'contentDomUnload' );
\r
1087 focus : function()
\r
1089 var win = editor.window;
\r
1091 if ( isLoadingData )
\r
1092 isPendingFocus = true;
\r
1093 // Temporary solution caused by #6025, supposed be unified by #6154.
\r
1094 else if ( CKEDITOR.env.opera && editor.document )
\r
1096 // Required for Opera when switching focus
\r
1097 // from another iframe, e.g. panels. (#6444)
\r
1098 var iframe = editor.window.$.frameElement;
\r
1099 iframe.blur(), iframe.focus();
\r
1100 editor.document.getBody().focus();
\r
1102 editor.selectionChange();
\r
1104 else if ( !CKEDITOR.env.opera && win )
\r
1106 // AIR needs a while to focus when moving from a link.
\r
1107 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1108 editor.selectionChange();
\r
1113 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1114 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1115 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1116 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1117 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
1121 // Setting voice label as window title, backup the original one
\r
1122 // and restore it before running into use.
\r
1123 editor.on( 'contentDom', function()
\r
1125 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1126 title.data( 'cke-title', editor.document.$.title );
\r
1127 editor.document.$.title = frameLabel;
\r
1130 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1131 // on element unless it has layout. (#5562)
\r
1132 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1134 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1136 var selectors = [];
\r
1137 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1138 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1139 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1141 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1142 else if ( CKEDITOR.env.gecko )
\r
1143 editor.addCss( 'html { height: 100% !important; }' );
\r
1145 // Switch on design mode for a short while and close it after then.
\r
1146 function blinkCursor( retry )
\r
1148 CKEDITOR.tools.tryThese(
\r
1151 editor.document.$.designMode = 'on';
\r
1152 setTimeout( function()
\r
1154 editor.document.$.designMode = 'off';
\r
1155 if ( CKEDITOR.currentInstance == editor )
\r
1156 editor.document.getBody().focus();
\r
1161 // The above call is known to fail when parent DOM
\r
1162 // tree layout changes may break design mode. (#5782)
\r
1163 // Refresh the 'contentEditable' is a cue to this.
\r
1164 editor.document.$.designMode = 'off';
\r
1165 var body = editor.document.getBody();
\r
1166 body.setAttribute( 'contentEditable', false );
\r
1167 body.setAttribute( 'contentEditable', true );
\r
1168 // Try it again once..
\r
1169 !retry && blinkCursor( 1 );
\r
1173 // Create an invisible element to grab focus.
\r
1174 if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1177 editor.on( 'uiReady', function()
\r
1179 focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
\r
1180 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)
\r
1181 '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );
\r
1183 focusGrabber.on( 'focus', function()
\r
1188 editor.focusGrabber = focusGrabber;
\r
1190 editor.on( 'destroy', function()
\r
1192 CKEDITOR.tools.removeFunction( contentDomReadyHandler );
\r
1193 focusGrabber.clearCustomData();
\r
1194 delete editor.focusGrabber;
\r
1198 // Disable form elements editing mode provided by some browers. (#5746)
\r
1199 editor.on( 'insertElement', function ( evt )
\r
1201 var element = evt.data;
\r
1202 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1203 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1205 // We should flag that the element was locked by our code so
\r
1206 // it'll be editable by the editor functions (#6046).
\r
1207 if ( !element.isReadOnly() )
\r
1208 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1209 element.setAttribute( 'contentEditable', false );
\r
1216 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1217 if ( CKEDITOR.env.gecko )
\r
1221 var body = document.body;
\r
1224 window.addEventListener( 'load', arguments.callee, false );
\r
1227 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1228 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1229 'event.persisted && (function(){' +
\r
1230 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1231 'for ( var i in allInstances )' +
\r
1233 ' editor = allInstances[ i ];' +
\r
1234 ' doc = editor.document;' +
\r
1237 ' doc.$.designMode = "off";' +
\r
1238 ' doc.$.designMode = "on";' +
\r
1249 * Disables the ability of resize objects (image and tables) in the editing
\r
1254 * config.disableObjectResizing = true;
\r
1256 CKEDITOR.config.disableObjectResizing = false;
\r
1259 * Disables the "table tools" offered natively by the browser (currently
\r
1260 * Firefox only) to make quick table editing operations, like adding or
\r
1261 * deleting rows and columns.
\r
1265 * config.disableNativeTableHandles = false;
\r
1267 CKEDITOR.config.disableNativeTableHandles = true;
\r
1270 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1272 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1273 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1274 * is enabled or you're simply not using the context menu plugin.
\r
1279 * config.disableNativeSpellChecker = false;
\r
1281 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1284 * Whether the editor must output an empty value ("") if it's contents is made
\r
1285 * by an empty paragraph only.
\r
1289 * config.ignoreEmptyParagraph = false;
\r
1291 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1294 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1295 * @name CKEDITOR.editor#dataReady
\r
1300 * Fired when some elements are added to the document
\r
1301 * @name CKEDITOR.editor#ariaWidget
\r
1303 * @param {Object} element The element being added
\r