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 // 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
29 function onInsert( insertFunc )
\r
31 return function( evt )
\r
33 if ( this.mode == 'wysiwyg' )
\r
37 this.fire( 'saveSnapshot' );
\r
39 insertFunc.call( this, evt.data );
\r
41 // Save snaps after the whole execution completed.
\r
42 // This's a workaround for make DOM modification's happened after
\r
43 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
45 CKEDITOR.tools.setTimeout( function()
\r
47 this.fire( 'saveSnapshot' );
\r
53 function doInsertHtml( data )
\r
55 if ( this.dataProcessor )
\r
56 data = this.dataProcessor.toHtml( data );
\r
58 // HTML insertion only considers the first range.
\r
59 var selection = this.getSelection(),
\r
60 range = selection.getRanges()[ 0 ];
\r
62 if ( range.checkReadOnly() )
\r
65 if ( CKEDITOR.env.ie )
\r
67 var selIsLocked = selection.isLocked;
\r
72 var $sel = selection.getNative();
\r
74 // Delete control selections to avoid IE bugs on pasteHTML.
\r
75 if ( $sel.type == 'Control' )
\r
77 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
79 // Due to IE bugs on handling contenteditable=false blocks
\r
80 // (#6005), we need to make some checks and eventually
\r
81 // delete the selection first.
\r
83 range = selection.getRanges()[ 0 ];
\r
84 var endContainer = range && range.endContainer;
\r
86 if ( endContainer &&
\r
87 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
88 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
89 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
91 range.setEndAfter( range.endContainer );
\r
92 range.deleteContents();
\r
98 $sel.createRange().pasteHTML( data );
\r
103 this.getSelection().lock();
\r
106 this.document.$.execCommand( 'inserthtml', false, data );
\r
108 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
109 if ( CKEDITOR.env.webkit )
\r
111 selection = this.getSelection();
\r
112 selection.scrollIntoView();
\r
116 function doInsertText( text )
\r
118 var selection = this.getSelection(),
\r
119 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
120 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
121 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
123 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
125 // Convert leading and trailing whitespaces into
\r
126 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
128 if ( match.length == 1 ) // one space, preserve it
\r
130 else if ( !offset ) // beginning of block
\r
131 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
132 else // end of block
\r
133 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
136 // Convert subsequent whitespaces into
\r
137 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
139 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
142 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
144 // Two line-breaks create one paragraph.
\r
145 if ( !isEnterBrMode )
\r
147 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
148 function( match, group1, text )
\r
150 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
154 // One <br> per line-break.
\r
155 html = html.replace( /\n/g, '<br>' );
\r
157 // Compensate padding <br> for non-IE.
\r
158 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
160 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
162 return CKEDITOR.tools.repeat( match, 2 );
\r
166 // Inline styles have to be inherited in Firefox.
\r
167 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
169 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
172 for ( var i = 0; i < path.elements.length; i++ )
\r
174 var tag = path.elements[ i ].getName();
\r
175 if ( tag in CKEDITOR.dtd.$inline )
\r
176 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
177 else if ( tag in CKEDITOR.dtd.$block )
\r
181 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
182 html = context.join( '' ) + html;
\r
185 doInsertHtml.call( this, html );
\r
188 function doInsertElement( element )
\r
190 var selection = this.getSelection(),
\r
191 ranges = selection.getRanges(),
\r
192 elementName = element.getName(),
\r
193 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
195 var selIsLocked = selection.isLocked;
\r
198 selection.unlock();
\r
200 var range, clone, lastElement, bookmark;
\r
202 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
204 range = ranges[ i ];
\r
206 if ( !range.checkReadOnly() )
\r
208 // Remove the original contents, merge splitted nodes.
\r
209 range.deleteContents( 1 );
\r
211 clone = !i && element || element.clone( 1 );
\r
213 // If we're inserting a block at dtd-violated position, split
\r
214 // the parent blocks until we reach blockLimit.
\r
218 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
219 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
220 && !( dtd && dtd [ elementName ] ) )
\r
222 // Split up inline elements.
\r
223 if ( current.getName() in CKEDITOR.dtd.span )
\r
224 range.splitElement( current );
\r
225 // If we're in an empty block which indicate a new paragraph,
\r
226 // simply replace it with the inserting block.(#3664)
\r
227 else if ( range.checkStartOfBlock()
\r
228 && range.checkEndOfBlock() )
\r
230 range.setStartBefore( current );
\r
231 range.collapse( true );
\r
235 range.splitBlock();
\r
239 // Insert the new node.
\r
240 range.insertNode( clone );
\r
242 // Save the last element reference so we can make the
\r
243 // selection later.
\r
244 if ( !lastElement )
\r
245 lastElement = clone;
\r
251 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
253 // If we're inserting a block element immediatelly followed by
\r
254 // another block element, the selection must move there. (#3100,#5436)
\r
257 var next = lastElement.getNext( notWhitespaceEval ),
\r
258 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
260 // Check if it's a block element that accepts text.
\r
261 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
\r
262 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 bodyFirstChild = body.getFirst(),
\r
317 bodyChildsNum = body.getChildren().count();
\r
319 if ( !bodyChildsNum
\r
320 || bodyChildsNum == 1
\r
321 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
322 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
\r
324 restoreDirty( editor );
\r
326 // Memorize scroll position to restore it later (#4472).
\r
327 var hostDocument = editor.element.getDocument();
\r
328 var hostDocumentElement = hostDocument.getDocumentElement();
\r
329 var scrollTop = hostDocumentElement.$.scrollTop;
\r
330 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
332 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
333 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
334 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
335 false, false, false, 0, 32 );
\r
336 doc.$.dispatchEvent( keyEventSimulate );
\r
338 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
339 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
341 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
342 bodyChildsNum && body.getFirst().remove();
\r
343 doc.getBody().appendBogus();
\r
344 var nativeRange = new CKEDITOR.dom.range( doc );
\r
345 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
346 nativeRange.select();
\r
351 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
352 * non-exitable-block by padding extra br.(#3189)
\r
354 function onSelectionChangeFixBody( evt )
\r
356 var editor = evt.editor,
\r
357 path = evt.data.path,
\r
358 blockLimit = path.blockLimit,
\r
359 selection = evt.data.selection,
\r
360 range = selection.getRanges()[0],
\r
361 body = editor.document.getBody(),
\r
362 enterMode = editor.config.enterMode;
\r
364 if ( CKEDITOR.env.gecko )
\r
366 activateEditing( editor );
\r
368 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
369 var pathBlock = path.block || path.blockLimit,
\r
370 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
372 // In case it's not ended with block element and doesn't have bogus yet. (#7467)
\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 !nonExitable( 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 !nonExitable( 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 // All browsers are incapable to moving cursor out of certain non-exitable
\r
436 // blocks (e.g. table, list, pre) at the end of document, make this happen by
\r
437 // place a bogus node there, which would be later removed by dataprocessor.
\r
438 var walkerRange = new CKEDITOR.dom.range( editor.document ),
\r
439 walker = new CKEDITOR.dom.walker( walkerRange );
\r
440 walkerRange.selectNodeContents( body );
\r
441 walker.evaluator = function( node )
\r
443 return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in nonExitableElementNames );
\r
445 walker.guard = function( node, isMoveout )
\r
447 return !( ( node.type == CKEDITOR.NODE_TEXT && isNotWhitespace( node ) ) || isMoveout );
\r
450 if ( walker.previous() )
\r
452 editor.fire( 'updateSnapshot' );
\r
453 restoreDirty( editor );
\r
454 CKEDITOR.env.ie && restoreSelection( selection );
\r
457 if ( enterMode != CKEDITOR.ENTER_BR )
\r
458 paddingBlock = body.append( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
460 paddingBlock = body;
\r
462 if ( !CKEDITOR.env.ie )
\r
463 paddingBlock.appendBogus();
\r
467 CKEDITOR.plugins.add( 'wysiwygarea',
\r
469 requires : [ 'editingblock' ],
\r
471 init : function( editor )
\r
473 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )
\r
474 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
476 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
\r
478 var contentDomReadyHandler;
\r
479 editor.on( 'editingBlockReady', function()
\r
489 // Support for custom document.domain in IE.
\r
490 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
492 // Creates the iframe that holds the editable document.
\r
493 var createIFrame = function( data )
\r
499 'document.open();' +
\r
501 // The document domain must be set any time we
\r
502 // call document.open().
\r
503 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
505 'document.close();';
\r
507 // With IE, the custom domain has to be taken care at first,
\r
508 // for other browers, the 'src' attribute should be left empty to
\r
509 // trigger iframe's 'load' event.
\r
512 'javascript:void(0)' :
\r
514 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
518 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
519 ' style="width:100%;height:100%"' +
\r
520 ' frameBorder="0"' +
\r
521 ' title="' + frameLabel + '"' +
\r
522 ' src="' + src + '"' +
\r
523 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
524 ' allowTransparency="true"' +
\r
527 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
528 if ( document.location.protocol == 'chrome:' )
\r
529 CKEDITOR.event.useCapture = true;
\r
531 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
532 iframe.on( 'load', function( ev )
\r
535 ev.removeListener();
\r
537 var doc = iframe.getFrameDocument();
\r
540 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
543 // Reset adjustment back to default (#5689)
\r
544 if ( document.location.protocol == 'chrome:' )
\r
545 CKEDITOR.event.useCapture = false;
\r
547 // The container must be visible when creating the iframe in FF (#5956)
\r
548 var element = editor.element,
\r
549 isHidden = CKEDITOR.env.gecko && !element.isVisible(),
\r
550 previousStyles = {};
\r
555 position : element.getStyle( 'position' ),
\r
556 top : element.getStyle( 'top' )
\r
558 element.setStyles( { position : 'absolute', top : '-3000px' } );
\r
561 mainElement.append( iframe );
\r
565 setTimeout( function()
\r
568 element.setStyles( previousStyles );
\r
573 // The script that launches the bootstrap logic on 'domReady', so the document
\r
574 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
575 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
576 var activationScript =
\r
577 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
578 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
579 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
582 // Editing area bootstrap code.
\r
583 function contentDomReady( domWindow )
\r
585 if ( !frameLoaded )
\r
589 editor.fire( 'ariaWidget', iframe );
\r
591 var domDocument = domWindow.document,
\r
592 body = domDocument.body;
\r
594 // Remove this script from the DOM.
\r
595 var script = domDocument.getElementById( "cke_actscrpt" );
\r
596 script && script.parentNode.removeChild( script );
\r
598 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
600 if ( CKEDITOR.env.ie )
\r
602 // Don't display the focus border.
\r
603 body.hideFocus = true;
\r
605 // Disable and re-enable the body to avoid IE from
\r
606 // taking the editing focus at startup. (#141 / #523)
\r
607 body.disabled = true;
\r
608 body.contentEditable = true;
\r
609 body.removeAttribute( 'disabled' );
\r
613 // Avoid opening design mode in a frame window thread,
\r
614 // which will cause host page scrolling.(#4397)
\r
615 setTimeout( function()
\r
617 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
618 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
619 || CKEDITOR.env.opera )
\r
620 domDocument.$.body.contentEditable = true;
\r
621 else if ( CKEDITOR.env.webkit )
\r
622 domDocument.$.body.parentNode.contentEditable = true;
\r
624 domDocument.$.designMode = 'on';
\r
628 CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
630 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
631 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
633 domDocument.on( 'dblclick', function( evt )
\r
635 var element = evt.data.getTarget(),
\r
636 data = { element : element, dialog : '' };
\r
637 editor.fire( 'doubleclick', data );
\r
638 data.dialog && editor.openDialog( data.dialog );
\r
641 // Prevent automatic submission in IE #6336
\r
642 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
644 var element = evt.data.getTarget();
\r
645 if ( element.is( 'input' ) )
\r
647 var type = element.getAttribute( 'type' );
\r
648 if ( type == 'submit' || type == 'reset' )
\r
649 evt.data.preventDefault();
\r
653 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
654 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
656 domDocument.on( 'mousedown', function( ev )
\r
658 var control = ev.data.getTarget();
\r
659 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
660 editor.getSelection().selectElement( control );
\r
664 if ( CKEDITOR.env.gecko )
\r
666 domDocument.on( 'mouseup', function( ev )
\r
668 if ( ev.data.$.button == 2 )
\r
670 var target = ev.data.getTarget();
\r
672 // Prevent right click from selecting an empty block even
\r
673 // when selection is anchored inside it. (#5845)
\r
674 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
676 var range = new CKEDITOR.dom.range( domDocument );
\r
677 range.moveToElementEditStart( target );
\r
678 range.select( true );
\r
684 // Prevent the browser opening links in read-only blocks. (#6032)
\r
685 domDocument.on( 'click', function( ev )
\r
688 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
689 ev.preventDefault();
\r
692 // Webkit: avoid from editing form control elements content.
\r
693 if ( CKEDITOR.env.webkit )
\r
695 // Mark that cursor will right blinking (#7113).
\r
696 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
697 // Prevent from tick checkbox/radiobox/select
\r
698 domDocument.on( 'click', function( ev )
\r
700 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
701 ev.data.preventDefault();
\r
704 // Prevent from editig textfield/textarea value.
\r
705 domDocument.on( 'mouseup', function( ev )
\r
707 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
708 ev.data.preventDefault();
\r
712 // IE standard compliant in editing frame doesn't focus the editor when
\r
713 // clicking outside actual content, manually apply the focus. (#1659)
\r
714 if ( CKEDITOR.env.ie
\r
715 && domDocument.$.compatMode == 'CSS1Compat'
\r
716 || CKEDITOR.env.gecko
\r
717 || CKEDITOR.env.opera )
\r
719 var htmlElement = domDocument.getDocumentElement();
\r
720 htmlElement.on( 'mousedown', function( evt )
\r
722 // Setting focus directly on editor doesn't work, we
\r
723 // have to use here a temporary element to 'redirect'
\r
725 if ( evt.data.getTarget().equals( htmlElement ) )
\r
727 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
729 focusGrabber.focus();
\r
734 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
735 focusTarget.on( 'blur', function()
\r
737 editor.focusManager.blur();
\r
742 focusTarget.on( 'focus', function()
\r
744 var doc = editor.document;
\r
746 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
748 else if ( CKEDITOR.env.opera )
\r
749 doc.getBody().focus();
\r
750 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
751 else if ( CKEDITOR.env.webkit )
\r
755 editor.document.getDocumentElement().focus();
\r
760 editor.focusManager.focus();
\r
763 var keystrokeHandler = editor.keystrokeHandler;
\r
764 if ( keystrokeHandler )
\r
765 keystrokeHandler.attach( domDocument );
\r
767 if ( CKEDITOR.env.ie )
\r
769 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
770 // Override keystrokes which should have deletion behavior
\r
771 // on control types in IE . (#4047)
\r
772 domDocument.on( 'keydown', function( evt )
\r
774 var keyCode = evt.data.getKeystroke();
\r
776 // Backspace OR Delete.
\r
777 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
779 var sel = editor.getSelection(),
\r
780 control = sel.getSelectedElement();
\r
784 // Make undo snapshot.
\r
785 editor.fire( 'saveSnapshot' );
\r
787 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
788 // break up the selection, safely manage it here. (#4795)
\r
789 var bookmark = sel.getRanges()[ 0 ].createBookmark();
\r
790 // Remove the control manually.
\r
792 sel.selectBookmarks( [ bookmark ] );
\r
794 editor.fire( 'saveSnapshot' );
\r
796 evt.data.preventDefault();
\r
801 // PageUp/PageDown scrolling is broken in document
\r
802 // with standard doctype, manually fix it. (#4736)
\r
803 if ( domDocument.$.compatMode == 'CSS1Compat' )
\r
805 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
806 domDocument.on( 'keydown', function( evt )
\r
808 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
810 setTimeout( function ()
\r
812 editor.getSelection().scrollIntoView();
\r
818 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
819 editor.config.enterMode != CKEDITOR.ENTER_P
\r
820 && domDocument.on( 'selectionchange', function()
\r
822 var body = domDocument.getBody(),
\r
823 range = editor.getSelection().getRanges()[ 0 ];
\r
825 if ( body.getHtml().match( /^<p> <\/p>$/i )
\r
826 && range.startContainer.equals( body ) )
\r
828 // Avoid the ambiguity from a real user cursor position.
\r
829 setTimeout( function ()
\r
831 range = editor.getSelection().getRanges()[ 0 ];
\r
832 if ( !range.startContainer.equals ( 'body' ) )
\r
834 body.getFirst().remove( 1 );
\r
835 range.moveToElementEditEnd( body );
\r
843 // Adds the document body as a context menu target.
\r
844 if ( editor.contextMenu )
\r
845 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
847 setTimeout( function()
\r
849 editor.fire( 'contentDom' );
\r
853 editor.mode = 'wysiwyg';
\r
854 editor.fire( 'mode' );
\r
858 isLoadingData = false;
\r
860 if ( isPendingFocus )
\r
863 isPendingFocus = false;
\r
865 setTimeout( function()
\r
867 editor.fire( 'dataReady' );
\r
870 // IE, Opera and Safari may not support it and throw errors.
\r
871 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
872 if ( editor.config.disableObjectResizing )
\r
876 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
880 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
881 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
883 evt.data.preventDefault();
\r
889 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
890 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
893 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
894 * fix the problem. :(
\r
896 if ( CKEDITOR.env.ie )
\r
898 setTimeout( function()
\r
900 if ( editor.document )
\r
902 var $body = editor.document.$.body;
\r
903 $body.runtimeStyle.marginBottom = '0px';
\r
904 $body.runtimeStyle.marginBottom = '';
\r
912 editor.addMode( 'wysiwyg',
\r
914 load : function( holderElement, data, isSnapshot )
\r
916 mainElement = holderElement;
\r
918 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
919 holderElement.setStyle( 'position', 'relative' );
\r
921 // The editor data "may be dirty" after this
\r
923 editor.mayBeDirty = true;
\r
928 this.loadSnapshotData( data );
\r
930 this.loadData( data );
\r
933 loadData : function( data )
\r
935 isLoadingData = true;
\r
936 editor._.dataStore = { id : 1 };
\r
938 var config = editor.config,
\r
939 fullPage = config.fullPage,
\r
940 docType = config.docType;
\r
942 // Build the additional stuff to be included into <head>.
\r
944 '<style type="text/css" data-cke-temp="1">' +
\r
945 editor._.styles.join( '\n' ) +
\r
948 !fullPage && ( headExtra =
\r
949 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
952 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
956 // Search and sweep out the doctype declaration.
\r
957 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
959 editor.docType = docType = match;
\r
964 // Get the HTML version of the data.
\r
965 if ( editor.dataProcessor )
\r
966 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
970 // Check if the <body> tag is available.
\r
971 if ( !(/<body[\s|>]/).test( data ) )
\r
972 data = '<body>' + data;
\r
974 // Check if the <html> tag is available.
\r
975 if ( !(/<html[\s|>]/).test( data ) )
\r
976 data = '<html>' + data + '</html>';
\r
978 // Check if the <head> tag is available.
\r
979 if ( !(/<head[\s|>]/).test( data ) )
\r
980 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
981 else if ( !(/<title[\s|>]/).test( data ) )
\r
982 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
984 // The base must be the first tag in the HEAD, e.g. to get relative
\r
985 // links on styles.
\r
986 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
988 // Inject the extra stuff into <head>.
\r
989 // Attention: do not change it before testing it well. (V2)
\r
990 // This is tricky... if the head ends with <meta ... content type>,
\r
991 // Firefox will break. But, it works if we place our extra stuff as
\r
992 // the last elements in the HEAD.
\r
993 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
995 // Add the DOCTYPE back to it.
\r
996 data = docType + data;
\r
1002 '<html dir="' + config.contentsLangDirection + '"' +
\r
1003 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
1005 '<title>' + frameLabel + '</title>' +
\r
1009 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
1010 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1016 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1017 if ( CKEDITOR.env.gecko )
\r
1018 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1020 data += activationScript;
\r
1023 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1025 createIFrame( data );
\r
1028 getData : function()
\r
1030 var config = editor.config,
\r
1031 fullPage = config.fullPage,
\r
1032 docType = fullPage && editor.docType,
\r
1033 doc = iframe.getFrameDocument();
\r
1035 var data = fullPage
\r
1036 ? doc.getDocumentElement().getOuterHtml()
\r
1037 : doc.getBody().getHtml();
\r
1039 // BR at the end of document is bogus node for Mozilla. (#5293).
\r
1040 if ( CKEDITOR.env.gecko )
\r
1041 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
\r
1043 if ( editor.dataProcessor )
\r
1044 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
1046 // Reset empty if the document contains only one empty paragraph.
\r
1047 if ( config.ignoreEmptyParagraph )
\r
1048 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
1051 data = docType + '\n' + data;
\r
1056 getSnapshotData : function()
\r
1058 return iframe.getFrameDocument().getBody().getHtml();
\r
1061 loadSnapshotData : function( data )
\r
1063 iframe.getFrameDocument().getBody().setHtml( data );
\r
1066 onDispose : function()
\r
1068 if ( !editor.document )
\r
1071 editor.document.getDocumentElement().clearCustomData();
\r
1072 editor.document.getBody().clearCustomData();
\r
1074 editor.window.clearCustomData();
\r
1075 editor.document.clearCustomData();
\r
1077 iframe.clearCustomData();
\r
1080 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1081 * editing area would break IE7/8's selection system, we have to put the editing
\r
1082 * iframe offline first. (#3812 and #5441)
\r
1087 unload : function( holderElement )
\r
1091 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1093 editor.fire( 'contentDomUnload' );
\r
1096 focus : function()
\r
1098 var win = editor.window;
\r
1100 if ( isLoadingData )
\r
1101 isPendingFocus = true;
\r
1102 // Temporary solution caused by #6025, supposed be unified by #6154.
\r
1103 else if ( CKEDITOR.env.opera && editor.document )
\r
1105 // Required for Opera when switching focus
\r
1106 // from another iframe, e.g. panels. (#6444)
\r
1107 var iframe = editor.window.$.frameElement;
\r
1108 iframe.blur(), iframe.focus();
\r
1109 editor.document.getBody().focus();
\r
1111 editor.selectionChange();
\r
1113 else if ( !CKEDITOR.env.opera && win )
\r
1115 // AIR needs a while to focus when moving from a link.
\r
1116 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1117 editor.selectionChange();
\r
1122 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1123 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1124 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1125 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1126 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
1130 // Setting voice label as window title, backup the original one
\r
1131 // and restore it before running into use.
\r
1132 editor.on( 'contentDom', function()
\r
1134 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1135 title.data( 'cke-title', editor.document.$.title );
\r
1136 editor.document.$.title = frameLabel;
\r
1139 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1140 // on element unless it has layout. (#5562)
\r
1141 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1143 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1145 var selectors = [];
\r
1146 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1147 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1148 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1150 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1151 else if ( CKEDITOR.env.gecko )
\r
1152 editor.addCss( 'html { height: 100% !important; }' );
\r
1154 // Switch on design mode for a short while and close it after then.
\r
1155 function blinkCursor( retry )
\r
1157 CKEDITOR.tools.tryThese(
\r
1160 editor.document.$.designMode = 'on';
\r
1161 setTimeout( function()
\r
1163 editor.document.$.designMode = 'off';
\r
1164 if ( CKEDITOR.currentInstance == editor )
\r
1165 editor.document.getBody().focus();
\r
1170 // The above call is known to fail when parent DOM
\r
1171 // tree layout changes may break design mode. (#5782)
\r
1172 // Refresh the 'contentEditable' is a cue to this.
\r
1173 editor.document.$.designMode = 'off';
\r
1174 var body = editor.document.getBody();
\r
1175 body.setAttribute( 'contentEditable', false );
\r
1176 body.setAttribute( 'contentEditable', true );
\r
1177 // Try it again once..
\r
1178 !retry && blinkCursor( 1 );
\r
1182 // Create an invisible element to grab focus.
\r
1183 if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1186 editor.on( 'uiReady', function()
\r
1188 focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
\r
1189 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)
\r
1190 '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );
\r
1192 focusGrabber.on( 'focus', function()
\r
1197 editor.focusGrabber = focusGrabber;
\r
1199 editor.on( 'destroy', function()
\r
1201 CKEDITOR.tools.removeFunction( contentDomReadyHandler );
\r
1202 focusGrabber.clearCustomData();
\r
1203 delete editor.focusGrabber;
\r
1207 // Disable form elements editing mode provided by some browers. (#5746)
\r
1208 editor.on( 'insertElement', function ( evt )
\r
1210 var element = evt.data;
\r
1211 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1212 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1214 // We should flag that the element was locked by our code so
\r
1215 // it'll be editable by the editor functions (#6046).
\r
1216 if ( !element.isReadOnly() )
\r
1217 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1218 element.setAttribute( 'contentEditable', false );
\r
1225 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1226 if ( CKEDITOR.env.gecko )
\r
1230 var body = document.body;
\r
1233 window.addEventListener( 'load', arguments.callee, false );
\r
1236 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1237 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1238 'event.persisted && (function(){' +
\r
1239 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1240 'for ( var i in allInstances )' +
\r
1242 ' editor = allInstances[ i ];' +
\r
1243 ' doc = editor.document;' +
\r
1246 ' doc.$.designMode = "off";' +
\r
1247 ' doc.$.designMode = "on";' +
\r
1258 * Disables the ability of resize objects (image and tables) in the editing
\r
1263 * config.disableObjectResizing = true;
\r
1265 CKEDITOR.config.disableObjectResizing = false;
\r
1268 * Disables the "table tools" offered natively by the browser (currently
\r
1269 * Firefox only) to make quick table editing operations, like adding or
\r
1270 * deleting rows and columns.
\r
1274 * config.disableNativeTableHandles = false;
\r
1276 CKEDITOR.config.disableNativeTableHandles = true;
\r
1279 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1281 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1282 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1283 * is enabled or you're simply not using the context menu plugin.
\r
1288 * config.disableNativeSpellChecker = false;
\r
1290 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1293 * Whether the editor must output an empty value ("") if it's contents is made
\r
1294 * by an empty paragraph only.
\r
1298 * config.ignoreEmptyParagraph = false;
\r
1300 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1303 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1304 * @name CKEDITOR.editor#dataReady
\r
1309 * Fired when some elements are added to the document
\r
1310 * @name CKEDITOR.editor#ariaWidget
\r
1312 * @param {Object} element The element being added
\r