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
57 // HTML insertion only considers the first range.
\r
58 var selection = this.getSelection(),
\r
59 range = selection.getRanges()[ 0 ];
\r
61 if ( range.checkReadOnly() )
\r
64 // Opera: force block splitting when pasted content contains block. (#7801)
\r
65 if ( CKEDITOR.env.opera )
\r
67 var path = new CKEDITOR.dom.elementPath( range.startContainer );
\r
70 var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;
\r
71 for ( var i = 0, count = nodes.length; i < count; i++ )
\r
73 if ( nodes[ i ]._.isBlockLike )
\r
75 range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
76 range.insertNode( range.document.createText( '' ) );
\r
84 if ( CKEDITOR.env.ie )
\r
86 var selIsLocked = selection.isLocked;
\r
91 var $sel = selection.getNative();
\r
93 // Delete control selections to avoid IE bugs on pasteHTML.
\r
94 if ( $sel.type == 'Control' )
\r
96 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
98 // Due to IE bugs on handling contenteditable=false blocks
\r
99 // (#6005), we need to make some checks and eventually
\r
100 // delete the selection first.
\r
102 range = selection.getRanges()[ 0 ];
\r
103 var endContainer = range && range.endContainer;
\r
105 if ( endContainer &&
\r
106 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
107 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
108 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
110 range.setEndAfter( range.endContainer );
\r
111 range.deleteContents();
\r
117 $sel.createRange().pasteHTML( data );
\r
122 this.getSelection().lock();
\r
125 this.document.$.execCommand( 'inserthtml', false, data );
\r
127 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
128 if ( CKEDITOR.env.webkit )
\r
130 selection = this.getSelection();
\r
131 selection.scrollIntoView();
\r
135 function doInsertText( text )
\r
137 var selection = this.getSelection(),
\r
138 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
139 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
140 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
142 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
144 // Convert leading and trailing whitespaces into
\r
145 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
147 if ( match.length == 1 ) // one space, preserve it
\r
149 else if ( !offset ) // beginning of block
\r
150 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
151 else // end of block
\r
152 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
155 // Convert subsequent whitespaces into
\r
156 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
158 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
161 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
163 // Two line-breaks create one paragraph.
\r
164 if ( !isEnterBrMode )
\r
166 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
167 function( match, group1, text )
\r
169 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
173 // One <br> per line-break.
\r
174 html = html.replace( /\n/g, '<br>' );
\r
176 // Compensate padding <br> for non-IE.
\r
177 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
179 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
181 return CKEDITOR.tools.repeat( match, 2 );
\r
185 // Inline styles have to be inherited in Firefox.
\r
186 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
188 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
191 for ( var i = 0; i < path.elements.length; i++ )
\r
193 var tag = path.elements[ i ].getName();
\r
194 if ( tag in CKEDITOR.dtd.$inline )
\r
195 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
196 else if ( tag in CKEDITOR.dtd.$block )
\r
200 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
201 html = context.join( '' ) + html;
\r
204 doInsertHtml.call( this, html );
\r
207 function doInsertElement( element )
\r
209 var selection = this.getSelection(),
\r
210 ranges = selection.getRanges(),
\r
211 elementName = element.getName(),
\r
212 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
214 var selIsLocked = selection.isLocked;
\r
217 selection.unlock();
\r
219 var range, clone, lastElement, bookmark;
\r
221 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
223 range = ranges[ i ];
\r
225 if ( !range.checkReadOnly() )
\r
227 // Remove the original contents, merge splitted nodes.
\r
228 range.deleteContents( 1 );
\r
230 clone = !i && element || element.clone( 1 );
\r
232 // If we're inserting a block at dtd-violated position, split
\r
233 // the parent blocks until we reach blockLimit.
\r
237 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
238 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
239 && !( dtd && dtd [ elementName ] ) )
\r
241 // Split up inline elements.
\r
242 if ( current.getName() in CKEDITOR.dtd.span )
\r
243 range.splitElement( current );
\r
244 // If we're in an empty block which indicate a new paragraph,
\r
245 // simply replace it with the inserting block.(#3664)
\r
246 else if ( range.checkStartOfBlock()
\r
247 && range.checkEndOfBlock() )
\r
249 range.setStartBefore( current );
\r
250 range.collapse( true );
\r
254 range.splitBlock();
\r
258 // Insert the new node.
\r
259 range.insertNode( clone );
\r
261 // Save the last element reference so we can make the
\r
262 // selection later.
\r
263 if ( !lastElement )
\r
264 lastElement = clone;
\r
270 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
272 // If we're inserting a block element immediatelly followed by
\r
273 // another block element, the selection must move there. (#3100,#5436)
\r
276 var next = lastElement.getNext( notWhitespaceEval ),
\r
277 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
279 // Check if it's a block element that accepts text.
\r
280 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
\r
281 range.moveToElementEditStart( next );
\r
285 selection.selectRanges( [ range ] );
\r
288 this.getSelection().lock();
\r
291 // DOM modification here should not bother dirty flag.(#4385)
\r
292 function restoreDirty( editor )
\r
294 if ( !editor.checkDirty() )
\r
295 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
298 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
299 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
301 function isNotEmpty( node )
\r
303 return isNotWhitespace( node ) && isNotBookmark( node );
\r
306 function isNbsp( node )
\r
308 return node.type == CKEDITOR.NODE_TEXT
\r
309 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
312 function restoreSelection( selection )
\r
314 if ( selection.isLocked )
\r
316 selection.unlock();
\r
317 setTimeout( function() { selection.lock(); }, 0 );
\r
321 function isBlankParagraph( block )
\r
323 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
326 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
328 // Gecko need a key event to 'wake up' the editing
\r
329 // ability when document is empty.(#3864, #5781)
\r
330 function activateEditing( editor )
\r
332 var win = editor.window,
\r
333 doc = editor.document,
\r
334 body = editor.document.getBody(),
\r
335 bodyFirstChild = body.getFirst(),
\r
336 bodyChildsNum = body.getChildren().count();
\r
338 if ( !bodyChildsNum
\r
339 || bodyChildsNum == 1
\r
340 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
341 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
\r
343 restoreDirty( editor );
\r
345 // Memorize scroll position to restore it later (#4472).
\r
346 var hostDocument = editor.element.getDocument();
\r
347 var hostDocumentElement = hostDocument.getDocumentElement();
\r
348 var scrollTop = hostDocumentElement.$.scrollTop;
\r
349 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
351 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
352 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
353 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
354 false, false, false, 0, 32 );
\r
355 doc.$.dispatchEvent( keyEventSimulate );
\r
357 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
358 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
360 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
361 bodyChildsNum && body.getFirst().remove();
\r
362 doc.getBody().appendBogus();
\r
363 var nativeRange = new CKEDITOR.dom.range( doc );
\r
364 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
365 nativeRange.select();
\r
370 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
371 * non-exitable-block by padding extra br.(#3189)
\r
373 function onSelectionChangeFixBody( evt )
\r
375 var editor = evt.editor,
\r
376 path = evt.data.path,
\r
377 blockLimit = path.blockLimit,
\r
378 selection = evt.data.selection,
\r
379 range = selection.getRanges()[0],
\r
380 body = editor.document.getBody(),
\r
381 enterMode = editor.config.enterMode;
\r
383 if ( CKEDITOR.env.gecko )
\r
385 activateEditing( editor );
\r
387 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
388 var pathBlock = path.block || path.blockLimit,
\r
389 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
391 // Check some specialities of the current path block:
\r
392 // 1. It is really displayed as block; (#7221)
\r
393 // 2. It doesn't end with one inner block; (#7467)
\r
394 // 3. It doesn't have bogus br yet.
\r
396 && pathBlock.isBlockBoundary()
\r
397 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
\r
398 && !pathBlock.is( 'pre' )
\r
399 && !pathBlock.getBogus() )
\r
401 editor.fire( 'updateSnapshot' );
\r
402 restoreDirty( editor );
\r
403 pathBlock.appendBogus();
\r
407 // When we're in block enter mode, a new paragraph will be established
\r
408 // to encapsulate inline contents right under body. (#3657)
\r
409 if ( editor.config.autoParagraph !== false
\r
410 && enterMode != CKEDITOR.ENTER_BR
\r
412 && blockLimit.getName() == 'body'
\r
415 editor.fire( 'updateSnapshot' );
\r
416 restoreDirty( editor );
\r
417 CKEDITOR.env.ie && restoreSelection( selection );
\r
419 var fixedBlock = range.fixBlock( true,
\r
420 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
422 // For IE, we should remove any filler node which was introduced before.
\r
423 if ( CKEDITOR.env.ie )
\r
425 var first = fixedBlock.getFirst( isNotEmpty );
\r
426 first && isNbsp( first ) && first.remove();
\r
429 // If the fixed block is actually blank and is already followed by an exitable blank
\r
430 // block, we should revert the fix and move into the existed one. (#3684)
\r
431 if ( isBlankParagraph( fixedBlock ) )
\r
433 var element = fixedBlock.getNext( isNotWhitespace );
\r
435 element.type == CKEDITOR.NODE_ELEMENT &&
\r
436 !nonEditable( element ) )
\r
438 range.moveToElementEditStart( element );
\r
439 fixedBlock.remove();
\r
443 element = fixedBlock.getPrevious( isNotWhitespace );
\r
445 element.type == CKEDITOR.NODE_ELEMENT &&
\r
446 !nonEditable( element ) )
\r
448 range.moveToElementEditEnd( element );
\r
449 fixedBlock.remove();
\r
455 // Cancel this selection change in favor of the next (correct). (#6811)
\r
459 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
\r
460 // at the end of document, makes it unable to continue adding content, we have to make this
\r
461 // easier by opening an new empty paragraph.
\r
462 var testRange = new CKEDITOR.dom.range( editor.document );
\r
463 testRange.moveToElementEditEnd( editor.document.getBody() );
\r
464 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
\r
465 if ( !testPath.blockLimit.is( 'body') )
\r
467 editor.fire( 'updateSnapshot' );
\r
468 restoreDirty( editor );
\r
469 CKEDITOR.env.ie && restoreSelection( selection );
\r
472 if ( enterMode != CKEDITOR.ENTER_BR )
\r
473 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
475 paddingBlock = body;
\r
477 if ( !CKEDITOR.env.ie )
\r
478 paddingBlock.appendBogus();
\r
482 CKEDITOR.plugins.add( 'wysiwygarea',
\r
484 requires : [ 'editingblock' ],
\r
486 init : function( editor )
\r
488 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )
\r
489 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
491 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
\r
493 var contentDomReadyHandler;
\r
494 editor.on( 'editingBlockReady', function()
\r
504 // Support for custom document.domain in IE.
\r
505 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
507 // Creates the iframe that holds the editable document.
\r
508 var createIFrame = function( data )
\r
514 'document.open();' +
\r
516 // The document domain must be set any time we
\r
517 // call document.open().
\r
518 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
520 'document.close();';
\r
522 // With IE, the custom domain has to be taken care at first,
\r
523 // for other browers, the 'src' attribute should be left empty to
\r
524 // trigger iframe's 'load' event.
\r
527 'javascript:void(0)' :
\r
529 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
533 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
534 ' style="width:100%;height:100%"' +
\r
535 ' frameBorder="0"' +
\r
536 ' title="' + frameLabel + '"' +
\r
537 ' src="' + src + '"' +
\r
538 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
539 ' allowTransparency="true"' +
\r
542 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
543 if ( document.location.protocol == 'chrome:' )
\r
544 CKEDITOR.event.useCapture = true;
\r
546 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
547 iframe.on( 'load', function( ev )
\r
550 ev.removeListener();
\r
552 var doc = iframe.getFrameDocument();
\r
555 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
558 // Reset adjustment back to default (#5689)
\r
559 if ( document.location.protocol == 'chrome:' )
\r
560 CKEDITOR.event.useCapture = false;
\r
562 mainElement.append( iframe );
\r
565 // The script that launches the bootstrap logic on 'domReady', so the document
\r
566 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
567 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
568 var activationScript =
\r
569 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
570 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
571 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
574 // Editing area bootstrap code.
\r
575 function contentDomReady( domWindow )
\r
577 if ( !frameLoaded )
\r
581 editor.fire( 'ariaWidget', iframe );
\r
583 var domDocument = domWindow.document,
\r
584 body = domDocument.body;
\r
586 // Remove this script from the DOM.
\r
587 var script = domDocument.getElementById( "cke_actscrpt" );
\r
588 script && script.parentNode.removeChild( script );
\r
590 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
592 var editable = !editor.readOnly;
\r
594 if ( CKEDITOR.env.ie )
\r
596 // Don't display the focus border.
\r
597 body.hideFocus = true;
\r
599 // Disable and re-enable the body to avoid IE from
\r
600 // taking the editing focus at startup. (#141 / #523)
\r
601 body.disabled = true;
\r
602 body.contentEditable = editable;
\r
603 body.removeAttribute( 'disabled' );
\r
607 // Avoid opening design mode in a frame window thread,
\r
608 // which will cause host page scrolling.(#4397)
\r
609 setTimeout( function()
\r
611 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
612 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
613 || CKEDITOR.env.opera )
\r
614 domDocument.$.body.contentEditable = editable;
\r
615 else if ( CKEDITOR.env.webkit )
\r
616 domDocument.$.body.parentNode.contentEditable = editable;
\r
618 domDocument.$.designMode = editable? 'off' : 'on';
\r
622 editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
624 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
625 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
627 editable && domDocument.on( 'dblclick', function( evt )
\r
629 var element = evt.data.getTarget(),
\r
630 data = { element : element, dialog : '' };
\r
631 editor.fire( 'doubleclick', data );
\r
632 data.dialog && editor.openDialog( data.dialog );
\r
635 // Prevent automatic submission in IE #6336
\r
636 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
638 var element = evt.data.getTarget();
\r
639 if ( element.is( 'input' ) )
\r
641 var type = element.getAttribute( 'type' );
\r
642 if ( type == 'submit' || type == 'reset' )
\r
643 evt.data.preventDefault();
\r
647 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
648 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
650 domDocument.on( 'mousedown', function( ev )
\r
652 var control = ev.data.getTarget();
\r
653 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
654 editor.getSelection().selectElement( control );
\r
658 if ( CKEDITOR.env.gecko )
\r
660 domDocument.on( 'mouseup', function( ev )
\r
662 if ( ev.data.$.button == 2 )
\r
664 var target = ev.data.getTarget();
\r
666 // Prevent right click from selecting an empty block even
\r
667 // when selection is anchored inside it. (#5845)
\r
668 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
670 var range = new CKEDITOR.dom.range( domDocument );
\r
671 range.moveToElementEditStart( target );
\r
672 range.select( true );
\r
678 // Prevent the browser opening links in read-only blocks. (#6032)
\r
679 domDocument.on( 'click', function( ev )
\r
682 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
683 ev.preventDefault();
\r
686 // Webkit: avoid from editing form control elements content.
\r
687 if ( CKEDITOR.env.webkit )
\r
689 // Mark that cursor will right blinking (#7113).
\r
690 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
691 // Prevent from tick checkbox/radiobox/select
\r
692 domDocument.on( 'click', function( ev )
\r
694 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
695 ev.data.preventDefault();
\r
698 // Prevent from editig textfield/textarea value.
\r
699 domDocument.on( 'mouseup', function( ev )
\r
701 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
702 ev.data.preventDefault();
\r
706 // IE standard compliant in editing frame doesn't focus the editor when
\r
707 // clicking outside actual content, manually apply the focus. (#1659)
\r
709 CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat'
\r
710 || CKEDITOR.env.gecko
\r
711 || CKEDITOR.env.opera )
\r
713 var htmlElement = domDocument.getDocumentElement();
\r
714 htmlElement.on( 'mousedown', function( evt )
\r
716 // Setting focus directly on editor doesn't work, we
\r
717 // have to use here a temporary element to 'redirect'
\r
719 if ( evt.data.getTarget().equals( htmlElement ) )
\r
721 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
723 focusGrabber.focus();
\r
728 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
729 focusTarget.on( 'blur', function()
\r
731 editor.focusManager.blur();
\r
736 focusTarget.on( 'focus', function()
\r
738 var doc = editor.document;
\r
740 if ( editable && CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
742 else if ( CKEDITOR.env.opera )
\r
743 doc.getBody().focus();
\r
744 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
745 else if ( CKEDITOR.env.webkit )
\r
749 editor.document.getDocumentElement().focus();
\r
754 editor.focusManager.focus();
\r
757 var keystrokeHandler = editor.keystrokeHandler;
\r
758 // Prevent backspace from navigating off the page.
\r
759 keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;
\r
760 keystrokeHandler.attach( domDocument );
\r
762 if ( CKEDITOR.env.ie )
\r
764 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
765 // Override keystrokes which should have deletion behavior
\r
766 // on control types in IE . (#4047)
\r
767 editable && domDocument.on( 'keydown', function( evt )
\r
769 var keyCode = evt.data.getKeystroke();
\r
771 // Backspace OR Delete.
\r
772 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
774 var sel = editor.getSelection(),
\r
775 control = sel.getSelectedElement();
\r
779 // Make undo snapshot.
\r
780 editor.fire( 'saveSnapshot' );
\r
782 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
783 // break up the selection, safely manage it here. (#4795)
\r
784 var bookmark = sel.getRanges()[ 0 ].createBookmark();
\r
785 // Remove the control manually.
\r
787 sel.selectBookmarks( [ bookmark ] );
\r
789 editor.fire( 'saveSnapshot' );
\r
791 evt.data.preventDefault();
\r
796 // PageUp/PageDown scrolling is broken in document
\r
797 // with standard doctype, manually fix it. (#4736)
\r
798 if ( domDocument.$.compatMode == 'CSS1Compat' )
\r
800 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
801 domDocument.on( 'keydown', function( evt )
\r
803 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
805 setTimeout( function ()
\r
807 editor.getSelection().scrollIntoView();
\r
813 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
814 editor.config.enterMode != CKEDITOR.ENTER_P
\r
815 && domDocument.on( 'selectionchange', function()
\r
817 var body = domDocument.getBody(),
\r
818 range = editor.getSelection().getRanges()[ 0 ];
\r
820 if ( body.getHtml().match( /^<p> <\/p>$/i )
\r
821 && range.startContainer.equals( body ) )
\r
823 // Avoid the ambiguity from a real user cursor position.
\r
824 setTimeout( function ()
\r
826 range = editor.getSelection().getRanges()[ 0 ];
\r
827 if ( !range.startContainer.equals ( 'body' ) )
\r
829 body.getFirst().remove( 1 );
\r
830 range.moveToElementEditEnd( body );
\r
838 // Adds the document body as a context menu target.
\r
839 if ( editor.contextMenu )
\r
840 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
842 setTimeout( function()
\r
844 editor.fire( 'contentDom' );
\r
848 editor.mode = 'wysiwyg';
\r
849 editor.fire( 'mode' );
\r
853 isLoadingData = false;
\r
855 if ( isPendingFocus )
\r
858 isPendingFocus = false;
\r
860 setTimeout( function()
\r
862 editor.fire( 'dataReady' );
\r
865 // IE, Opera and Safari may not support it and throw errors.
\r
866 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
867 if ( editor.config.disableObjectResizing )
\r
871 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
875 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
876 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
878 evt.data.preventDefault();
\r
884 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
885 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
888 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
889 * fix the problem. :(
\r
891 if ( CKEDITOR.env.ie )
\r
893 setTimeout( function()
\r
895 if ( editor.document )
\r
897 var $body = editor.document.$.body;
\r
898 $body.runtimeStyle.marginBottom = '0px';
\r
899 $body.runtimeStyle.marginBottom = '';
\r
907 editor.addMode( 'wysiwyg',
\r
909 load : function( holderElement, data, isSnapshot )
\r
911 mainElement = holderElement;
\r
913 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
914 holderElement.setStyle( 'position', 'relative' );
\r
916 // The editor data "may be dirty" after this
\r
918 editor.mayBeDirty = true;
\r
923 this.loadSnapshotData( data );
\r
925 this.loadData( data );
\r
928 loadData : function( data )
\r
930 isLoadingData = true;
\r
931 editor._.dataStore = { id : 1 };
\r
933 var config = editor.config,
\r
934 fullPage = config.fullPage,
\r
935 docType = config.docType;
\r
937 // Build the additional stuff to be included into <head>.
\r
939 '<style type="text/css" data-cke-temp="1">' +
\r
940 editor._.styles.join( '\n' ) +
\r
943 !fullPage && ( headExtra =
\r
944 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
947 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
951 // Search and sweep out the doctype declaration.
\r
952 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
954 editor.docType = docType = match;
\r
956 }).replace( /<\?xml\s[^\?]*\?>/i, function( match )
\r
958 editor.xmlDeclaration = match;
\r
963 // Get the HTML version of the data.
\r
964 if ( editor.dataProcessor )
\r
965 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
969 // Check if the <body> tag is available.
\r
970 if ( !(/<body[\s|>]/).test( data ) )
\r
971 data = '<body>' + data;
\r
973 // Check if the <html> tag is available.
\r
974 if ( !(/<html[\s|>]/).test( data ) )
\r
975 data = '<html>' + data + '</html>';
\r
977 // Check if the <head> tag is available.
\r
978 if ( !(/<head[\s|>]/).test( data ) )
\r
979 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
980 else if ( !(/<title[\s|>]/).test( data ) )
\r
981 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
983 // The base must be the first tag in the HEAD, e.g. to get relative
\r
984 // links on styles.
\r
985 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
987 // Inject the extra stuff into <head>.
\r
988 // Attention: do not change it before testing it well. (V2)
\r
989 // This is tricky... if the head ends with <meta ... content type>,
\r
990 // Firefox will break. But, it works if we place our extra stuff as
\r
991 // the last elements in the HEAD.
\r
992 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
994 // Add the DOCTYPE back to it.
\r
995 data = docType + data;
\r
1001 '<html dir="' + config.contentsLangDirection + '"' +
\r
1002 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
1004 '<title>' + frameLabel + '</title>' +
\r
1008 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
1009 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1015 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1016 if ( CKEDITOR.env.gecko )
\r
1017 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1019 data += activationScript;
\r
1022 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1024 createIFrame( data );
\r
1027 getData : function()
\r
1029 var config = editor.config,
\r
1030 fullPage = config.fullPage,
\r
1031 docType = fullPage && editor.docType,
\r
1032 xmlDeclaration = fullPage && editor.xmlDeclaration,
\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
1050 if ( xmlDeclaration )
\r
1051 data = xmlDeclaration + '\n' + data;
\r
1053 data = docType + '\n' + data;
\r
1058 getSnapshotData : function()
\r
1060 return iframe.getFrameDocument().getBody().getHtml();
\r
1063 loadSnapshotData : function( data )
\r
1065 iframe.getFrameDocument().getBody().setHtml( data );
\r
1068 onDispose : function()
\r
1070 if ( !editor.document )
\r
1073 editor.document.getDocumentElement().clearCustomData();
\r
1074 editor.document.getBody().clearCustomData();
\r
1076 editor.window.clearCustomData();
\r
1077 editor.document.clearCustomData();
\r
1079 iframe.clearCustomData();
\r
1082 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1083 * editing area would break IE7/8's selection system, we have to put the editing
\r
1084 * iframe offline first. (#3812 and #5441)
\r
1089 unload : function( holderElement )
\r
1093 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1095 editor.fire( 'contentDomUnload' );
\r
1098 focus : function()
\r
1100 var win = editor.window;
\r
1102 if ( isLoadingData )
\r
1103 isPendingFocus = true;
\r
1104 // Temporary solution caused by #6025, supposed be unified by #6154.
\r
1105 else if ( CKEDITOR.env.opera && editor.document )
\r
1107 // Required for Opera when switching focus
\r
1108 // from another iframe, e.g. panels. (#6444)
\r
1109 var iframe = editor.window.$.frameElement;
\r
1110 iframe.blur(), iframe.focus();
\r
1111 editor.document.getBody().focus();
\r
1113 editor.selectionChange();
\r
1115 else if ( !CKEDITOR.env.opera && win )
\r
1117 // AIR needs a while to focus when moving from a link.
\r
1118 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1119 editor.selectionChange();
\r
1124 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1125 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1126 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1127 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1128 editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );
\r
1132 // Setting voice label as window title, backup the original one
\r
1133 // and restore it before running into use.
\r
1134 editor.on( 'contentDom', function()
\r
1136 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1137 title.data( 'cke-title', editor.document.$.title );
\r
1138 editor.document.$.title = frameLabel;
\r
1141 editor.on( 'readOnly', function()
\r
1143 if ( editor.mode == 'wysiwyg' )
\r
1145 // Symply reload the wysiwyg area. It'll take care of read-only.
\r
1146 var wysiwyg = editor.getMode();
\r
1147 wysiwyg.loadData( wysiwyg.getData() );
\r
1151 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1152 // on element unless it has layout. (#5562)
\r
1153 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1155 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1157 var selectors = [];
\r
1158 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1159 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1160 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1162 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1163 else if ( CKEDITOR.env.gecko )
\r
1165 editor.addCss( 'html { height: 100% !important; }' );
\r
1166 editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1; width : 24px; height : 24px; }' );
\r
1169 /* #3658: [IE6] Editor document has horizontal scrollbar on long lines
\r
1170 To prevent this misbehavior, we show the scrollbar always */
\r
1171 /* #6341: The text cursor must be set on the editor area. */
\r
1172 /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/
\r
1173 editor.addCss( 'html { _overflow-y: scroll; cursor: text; *cursor:auto;}' );
\r
1174 // Use correct cursor for these elements
\r
1175 editor.addCss( 'img, input, textarea { cursor: default;}' );
\r
1177 // Switch on design mode for a short while and close it after then.
\r
1178 function blinkCursor( retry )
\r
1180 if ( editor.readOnly )
\r
1183 CKEDITOR.tools.tryThese(
\r
1186 editor.document.$.designMode = 'on';
\r
1187 setTimeout( function()
\r
1189 editor.document.$.designMode = 'off';
\r
1190 if ( CKEDITOR.currentInstance == editor )
\r
1191 editor.document.getBody().focus();
\r
1196 // The above call is known to fail when parent DOM
\r
1197 // tree layout changes may break design mode. (#5782)
\r
1198 // Refresh the 'contentEditable' is a cue to this.
\r
1199 editor.document.$.designMode = 'off';
\r
1200 var body = editor.document.getBody();
\r
1201 body.setAttribute( 'contentEditable', false );
\r
1202 body.setAttribute( 'contentEditable', true );
\r
1203 // Try it again once..
\r
1204 !retry && blinkCursor( 1 );
\r
1208 // Create an invisible element to grab focus.
\r
1209 if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1212 editor.on( 'uiReady', function()
\r
1214 focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
\r
1215 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)
\r
1216 '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );
\r
1218 focusGrabber.on( 'focus', function()
\r
1223 editor.focusGrabber = focusGrabber;
\r
1225 editor.on( 'destroy', function()
\r
1227 CKEDITOR.tools.removeFunction( contentDomReadyHandler );
\r
1228 focusGrabber.clearCustomData();
\r
1229 delete editor.focusGrabber;
\r
1233 // Disable form elements editing mode provided by some browers. (#5746)
\r
1234 editor.on( 'insertElement', function ( evt )
\r
1236 var element = evt.data;
\r
1237 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1238 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1240 // We should flag that the element was locked by our code so
\r
1241 // it'll be editable by the editor functions (#6046).
\r
1242 if ( !element.isReadOnly() )
\r
1243 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1244 element.setAttribute( 'contentEditable', false );
\r
1251 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1252 if ( CKEDITOR.env.gecko )
\r
1256 var body = document.body;
\r
1259 window.addEventListener( 'load', arguments.callee, false );
\r
1262 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1263 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1264 'event.persisted && (function(){' +
\r
1265 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1266 'for ( var i in allInstances )' +
\r
1268 ' editor = allInstances[ i ];' +
\r
1269 ' doc = editor.document;' +
\r
1272 ' doc.$.designMode = "off";' +
\r
1273 ' doc.$.designMode = "on";' +
\r
1284 * Disables the ability of resize objects (image and tables) in the editing
\r
1289 * config.disableObjectResizing = true;
\r
1291 CKEDITOR.config.disableObjectResizing = false;
\r
1294 * Disables the "table tools" offered natively by the browser (currently
\r
1295 * Firefox only) to make quick table editing operations, like adding or
\r
1296 * deleting rows and columns.
\r
1300 * config.disableNativeTableHandles = false;
\r
1302 CKEDITOR.config.disableNativeTableHandles = true;
\r
1305 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1307 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1308 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1309 * is enabled or you're simply not using the context menu plugin.
\r
1314 * config.disableNativeSpellChecker = false;
\r
1316 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1319 * Whether the editor must output an empty value ("") if it's contents is made
\r
1320 * by an empty paragraph only.
\r
1324 * config.ignoreEmptyParagraph = false;
\r
1326 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1329 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1330 * @name CKEDITOR.editor#dataReady
\r
1335 * Whether automatically create wrapping blocks around inline contents inside document body,
\r
1336 * this helps to ensure the integrality of the block enter mode.
\r
1337 * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.
\r
1338 * @name CKEDITOR.config.autoParagraph
\r
1343 * config.autoParagraph = false;
\r
1347 * Fired when some elements are added to the document
\r
1348 * @name CKEDITOR.editor#ariaWidget
\r
1350 * @param {Object} element The element being added
\r