2 Copyright (c) 2003-2012, 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
17 notBogus = CKEDITOR.dom.walker.bogus( true ),
\r
18 notEmpty = function( node ) { return notWhitespaceEval( node ) && notBogus( node ); };
\r
20 // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)
\r
21 function nonEditable( element )
\r
23 return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];
\r
27 function onInsert( insertFunc )
\r
29 return function( evt )
\r
31 if ( this.mode == 'wysiwyg' )
\r
35 // Since the insertion might happen from within dialog or menu
\r
36 // where the editor selection might be locked at the moment,
\r
37 // update the locked selection.
\r
38 var selection = this.getSelection(),
\r
39 selIsLocked = selection.isLocked;
\r
41 selIsLocked && selection.unlock();
\r
43 this.fire( 'saveSnapshot' );
\r
45 insertFunc.call( this, evt.data );
\r
47 selIsLocked && this.getSelection().lock();
\r
49 // Save snaps after the whole execution completed.
\r
50 // This's a workaround for make DOM modification's happened after
\r
51 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
53 CKEDITOR.tools.setTimeout( function()
\r
55 this.fire( 'saveSnapshot' );
\r
61 function doInsertHtml( data )
\r
63 if ( this.dataProcessor )
\r
64 data = this.dataProcessor.toHtml( data );
\r
69 // HTML insertion only considers the first range.
\r
70 var selection = this.getSelection(),
\r
71 range = selection.getRanges()[ 0 ];
\r
73 if ( range.checkReadOnly() )
\r
76 // Opera: force block splitting when pasted content contains block. (#7801)
\r
77 if ( CKEDITOR.env.opera )
\r
79 var path = new CKEDITOR.dom.elementPath( range.startContainer );
\r
82 var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;
\r
83 for ( var i = 0, count = nodes.length; i < count; i++ )
\r
85 if ( nodes[ i ]._.isBlockLike )
\r
87 range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
88 range.insertNode( range.document.createText( '' ) );
\r
96 if ( CKEDITOR.env.ie )
\r
98 var $sel = selection.getNative();
\r
100 // Delete control selections to avoid IE bugs on pasteHTML.
\r
101 if ( $sel.type == 'Control' )
\r
103 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
105 // Due to IE bugs on handling contenteditable=false blocks
\r
106 // (#6005), we need to make some checks and eventually
\r
107 // delete the selection first.
\r
109 range = selection.getRanges()[ 0 ];
\r
110 var endContainer = range && range.endContainer;
\r
112 if ( endContainer &&
\r
113 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
114 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
115 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
117 range.setEndAfter( range.endContainer );
\r
118 range.deleteContents();
\r
122 $sel.createRange().pasteHTML( data );
\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 immediately followed by
\r
273 // another block element, the selection must be optimized. (#3100,#5436,#8950)
\r
276 var next = lastElement.getNext( notEmpty ),
\r
277 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
279 // If the next one is a text block, move cursor to the start of it's content.
\r
280 if ( nextName && CKEDITOR.dtd.$block[ nextName ] )
\r
282 if ( CKEDITOR.dtd[ nextName ][ '#' ] )
\r
283 range.moveToElementEditStart( next );
\r
284 // Otherwise move cursor to the before end of the last element.
\r
286 range.moveToElementEditEnd( lastElement );
\r
288 // Open a new line if the block is inserted at the end of parent.
\r
291 next = range.fixBlock( true, this.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
292 range.moveToElementEditStart( next );
\r
297 selection.selectRanges( [ range ] );
\r
300 this.getSelection().lock();
\r
303 // DOM modification here should not bother dirty flag.(#4385)
\r
304 function restoreDirty( editor )
\r
306 if ( !editor.checkDirty() )
\r
307 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
310 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
311 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
313 function isNotEmpty( node )
\r
315 return isNotWhitespace( node ) && isNotBookmark( node );
\r
318 function isNbsp( node )
\r
320 return node.type == CKEDITOR.NODE_TEXT
\r
321 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
324 function restoreSelection( selection )
\r
326 if ( selection.isLocked )
\r
328 selection.unlock();
\r
329 setTimeout( function() { selection.lock(); }, 0 );
\r
333 function isBlankParagraph( block )
\r
335 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
338 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
340 // Gecko need a key event to 'wake up' the editing
\r
341 // ability when document is empty.(#3864, #5781)
\r
342 function activateEditing( editor )
\r
344 var win = editor.window,
\r
345 doc = editor.document,
\r
346 body = editor.document.getBody(),
\r
347 bodyFirstChild = body.getFirst(),
\r
348 bodyChildsNum = body.getChildren().count();
\r
350 if ( !bodyChildsNum
\r
351 || bodyChildsNum == 1
\r
352 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
353 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
\r
355 restoreDirty( editor );
\r
357 // Memorize scroll position to restore it later (#4472).
\r
358 var hostDocument = editor.element.getDocument();
\r
359 var hostDocumentElement = hostDocument.getDocumentElement();
\r
360 var scrollTop = hostDocumentElement.$.scrollTop;
\r
361 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
363 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
364 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
365 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
366 false, false, false, 0, 32 );
\r
367 doc.$.dispatchEvent( keyEventSimulate );
\r
369 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
370 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
372 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
373 bodyChildsNum && body.getFirst().remove();
\r
374 doc.getBody().appendBogus();
\r
375 var nativeRange = new CKEDITOR.dom.range( doc );
\r
376 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
377 nativeRange.select();
\r
382 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
383 * non-exitable-block by padding extra br.(#3189)
\r
385 function onSelectionChangeFixBody( evt )
\r
387 var editor = evt.editor,
\r
388 path = evt.data.path,
\r
389 blockLimit = path.blockLimit,
\r
390 selection = evt.data.selection,
\r
391 range = selection.getRanges()[0],
\r
392 body = editor.document.getBody(),
\r
393 enterMode = editor.config.enterMode;
\r
395 if ( CKEDITOR.env.gecko )
\r
397 activateEditing( editor );
\r
399 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
400 var pathBlock = path.block || path.blockLimit,
\r
401 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
403 // Check some specialities of the current path block:
\r
404 // 1. It is really displayed as block; (#7221)
\r
405 // 2. It doesn't end with one inner block; (#7467)
\r
406 // 3. It doesn't have bogus br yet.
\r
408 && pathBlock.isBlockBoundary()
\r
409 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
\r
410 && !pathBlock.is( 'pre' )
\r
411 && !pathBlock.getBogus() )
\r
413 pathBlock.appendBogus();
\r
417 // When we're in block enter mode, a new paragraph will be established
\r
418 // to encapsulate inline contents right under body. (#3657)
\r
419 if ( editor.config.autoParagraph !== false
\r
420 && enterMode != CKEDITOR.ENTER_BR
\r
422 && blockLimit.getName() == 'body'
\r
425 var fixedBlock = range.fixBlock( true,
\r
426 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
428 // For IE, we should remove any filler node which was introduced before.
\r
429 if ( CKEDITOR.env.ie )
\r
431 var first = fixedBlock.getFirst( isNotEmpty );
\r
432 first && isNbsp( first ) && first.remove();
\r
435 // If the fixed block is actually blank and is already followed by an exitable blank
\r
436 // block, we should revert the fix and move into the existed one. (#3684)
\r
437 if ( isBlankParagraph( fixedBlock ) )
\r
439 var element = fixedBlock.getNext( isNotWhitespace );
\r
441 element.type == CKEDITOR.NODE_ELEMENT &&
\r
442 !nonEditable( element ) )
\r
444 range.moveToElementEditStart( element );
\r
445 fixedBlock.remove();
\r
449 element = fixedBlock.getPrevious( isNotWhitespace );
\r
451 element.type == CKEDITOR.NODE_ELEMENT &&
\r
452 !nonEditable( element ) )
\r
454 range.moveToElementEditEnd( element );
\r
455 fixedBlock.remove();
\r
461 // Cancel this selection change in favor of the next (correct). (#6811)
\r
465 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
\r
466 // at the end of document, makes it unable to continue adding content, we have to make this
\r
467 // easier by opening an new empty paragraph.
\r
468 var testRange = new CKEDITOR.dom.range( editor.document );
\r
469 testRange.moveToElementEditEnd( editor.document.getBody() );
\r
470 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
\r
471 if ( !testPath.blockLimit.is( 'body') )
\r
474 if ( enterMode != CKEDITOR.ENTER_BR )
\r
475 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
477 paddingBlock = body;
\r
479 if ( !CKEDITOR.env.ie )
\r
480 paddingBlock.appendBogus();
\r
484 CKEDITOR.plugins.add( 'wysiwygarea',
\r
486 requires : [ 'editingblock' ],
\r
488 init : function( editor )
\r
490 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )
\r
491 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
493 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name ),
\r
494 frameDesc = editor.lang.editorHelp;
\r
496 if ( CKEDITOR.env.ie )
\r
497 frameLabel += ', ' + frameDesc;
\r
499 var win = CKEDITOR.document.getWindow();
\r
500 var contentDomReadyHandler;
\r
501 editor.on( 'editingBlockReady', function()
\r
512 // Support for custom document.domain in IE.
\r
513 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
515 // Creates the iframe that holds the editable document.
\r
516 var createIFrame = function( data )
\r
522 'document.open();' +
\r
524 // The document domain must be set any time we
\r
525 // call document.open().
\r
526 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
528 'document.close();';
\r
530 // With IE, the custom domain has to be taken care at first,
\r
531 // for other browers, the 'src' attribute should be left empty to
\r
532 // trigger iframe's 'load' event.
\r
535 'javascript:void(0)' :
\r
537 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
541 var labelId = CKEDITOR.tools.getNextId();
\r
542 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
543 ' style="width:100%;height:100%"' +
\r
544 ' frameBorder="0"' +
\r
545 ' aria-describedby="' + labelId + '"' +
\r
546 ' title="' + frameLabel + '"' +
\r
547 ' src="' + src + '"' +
\r
548 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
549 ' allowTransparency="true"' +
\r
552 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
553 if ( document.location.protocol == 'chrome:' )
\r
554 CKEDITOR.event.useCapture = true;
\r
556 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
557 iframe.on( 'load', function( ev )
\r
560 ev.removeListener();
\r
562 var doc = iframe.getFrameDocument();
\r
565 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
568 // Reset adjustment back to default (#5689)
\r
569 if ( document.location.protocol == 'chrome:' )
\r
570 CKEDITOR.event.useCapture = false;
\r
572 mainElement.append( CKEDITOR.dom.element.createFromHtml(
\r
573 '<span id="' + labelId + '" class="cke_voice_label">' + frameDesc + '</span>'));
\r
575 mainElement.append( iframe );
\r
577 // Webkit: iframe size doesn't auto fit well. (#7360)
\r
578 if ( CKEDITOR.env.webkit )
\r
580 onResize = function()
\r
582 // Hide the iframe to get real size of the holder. (#8941)
\r
583 mainElement.setStyle( 'width', '100%' );
\r
586 iframe.setSize( 'width', mainElement.getSize( 'width' ) );
\r
587 mainElement.removeStyle( 'width' );
\r
591 win.on( 'resize', onResize );
\r
595 // The script that launches the bootstrap logic on 'domReady', so the document
\r
596 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
597 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
598 var activationScript =
\r
599 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
600 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
601 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
604 // Editing area bootstrap code.
\r
605 function contentDomReady( domWindow )
\r
607 if ( !frameLoaded )
\r
611 editor.fire( 'ariaWidget', iframe );
\r
613 var domDocument = domWindow.document,
\r
614 body = domDocument.body;
\r
616 // Remove this script from the DOM.
\r
617 var script = domDocument.getElementById( "cke_actscrpt" );
\r
618 script && script.parentNode.removeChild( script );
\r
620 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
622 var editable = !editor.readOnly;
\r
624 if ( CKEDITOR.env.ie )
\r
626 // Don't display the focus border.
\r
627 body.hideFocus = true;
\r
629 // Disable and re-enable the body to avoid IE from
\r
630 // taking the editing focus at startup. (#141 / #523)
\r
631 body.disabled = true;
\r
632 body.contentEditable = editable;
\r
633 body.removeAttribute( 'disabled' );
\r
637 // Avoid opening design mode in a frame window thread,
\r
638 // which will cause host page scrolling.(#4397)
\r
639 setTimeout( function()
\r
641 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
642 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
643 || CKEDITOR.env.opera )
\r
644 domDocument.$.body.contentEditable = editable;
\r
645 else if ( CKEDITOR.env.webkit )
\r
646 domDocument.$.body.parentNode.contentEditable = editable;
\r
648 domDocument.$.designMode = editable? 'off' : 'on';
\r
652 editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
654 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
655 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
657 editable && domDocument.on( 'dblclick', function( evt )
\r
659 var element = evt.data.getTarget(),
\r
660 data = { element : element, dialog : '' };
\r
661 editor.fire( 'doubleclick', data );
\r
662 data.dialog && editor.openDialog( data.dialog );
\r
665 // Prevent automatic submission in IE #6336
\r
666 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
668 var element = evt.data.getTarget();
\r
669 if ( element.is( 'input' ) )
\r
671 var type = element.getAttribute( 'type' );
\r
672 if ( type == 'submit' || type == 'reset' )
\r
673 evt.data.preventDefault();
\r
677 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
678 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
680 domDocument.on( 'mousedown', function( ev )
\r
682 var control = ev.data.getTarget();
\r
683 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
684 editor.getSelection().selectElement( control );
\r
688 if ( CKEDITOR.env.gecko )
\r
690 domDocument.on( 'mouseup', function( ev )
\r
692 if ( ev.data.$.button == 2 )
\r
694 var target = ev.data.getTarget();
\r
696 // Prevent right click from selecting an empty block even
\r
697 // when selection is anchored inside it. (#5845)
\r
698 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
700 var range = new CKEDITOR.dom.range( domDocument );
\r
701 range.moveToElementEditStart( target );
\r
702 range.select( true );
\r
708 // Prevent the browser opening links in read-only blocks. (#6032)
\r
709 domDocument.on( 'click', function( ev )
\r
712 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
713 ev.preventDefault();
\r
716 // Webkit: avoid from editing form control elements content.
\r
717 if ( CKEDITOR.env.webkit )
\r
719 // Mark that cursor will right blinking (#7113).
\r
720 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
721 // Prevent from tick checkbox/radiobox/select
\r
722 domDocument.on( 'click', function( ev )
\r
724 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
725 ev.data.preventDefault();
\r
728 // Prevent from editig textfield/textarea value.
\r
729 domDocument.on( 'mouseup', function( ev )
\r
731 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
732 ev.data.preventDefault();
\r
736 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
737 focusTarget.on( 'blur', function()
\r
739 editor.focusManager.blur();
\r
744 focusTarget.on( 'focus', function()
\r
746 var doc = editor.document;
\r
748 if ( CKEDITOR.env.gecko || 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 // Prevent backspace from navigating off the page.
\r
765 keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;
\r
766 keystrokeHandler.attach( domDocument );
\r
768 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
769 // Override keystroke behaviors.
\r
770 editable && domDocument.on( 'keydown', function( evt )
\r
772 var keyCode = evt.data.getKeystroke();
\r
774 // Backspace OR Delete.
\r
775 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
777 var sel = editor.getSelection(),
\r
778 selected = sel.getSelectedElement(),
\r
779 range = sel.getRanges()[ 0 ],
\r
780 path = new CKEDITOR.dom.elementPath( range.startContainer ),
\r
784 rtl = keyCode == 8;
\r
786 // Override keystrokes which should have deletion behavior
\r
787 // on fully selected element . (#4047) (#7645)
\r
790 // Make undo snapshot.
\r
791 editor.fire( 'saveSnapshot' );
\r
793 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
794 // break up the selection, safely manage it here. (#4795)
\r
795 range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );
\r
796 // Remove the control manually.
\r
800 editor.fire( 'saveSnapshot' );
\r
802 evt.data.preventDefault();
\r
806 // Handle the following special cases: (#6217)
\r
807 // 1. Del/Backspace key before/after table;
\r
808 // 2. Backspace Key after start of table.
\r
809 if ( ( block = path.block ) &&
\r
810 range[ rtl ? 'checkStartOfBlock' : 'checkEndOfBlock' ]() &&
\r
811 ( next = block[ rtl ? 'getPrevious' : 'getNext' ]( notWhitespaceEval ) ) &&
\r
812 next.is( 'table' ) )
\r
814 editor.fire( 'saveSnapshot' );
\r
816 // Remove the current empty block.
\r
817 if ( range[ rtl ? 'checkEndOfBlock' : 'checkStartOfBlock' ]() )
\r
820 // Move cursor to the beginning/end of table cell.
\r
821 range[ 'moveToElementEdit' + ( rtl ? 'End' : 'Start' ) ]( next );
\r
824 editor.fire( 'saveSnapshot' );
\r
826 evt.data.preventDefault();
\r
828 else if ( path.blockLimit.is( 'td' ) &&
\r
829 ( parent = path.blockLimit.getAscendant( 'table' ) ) &&
\r
830 range.checkBoundaryOfElement( parent, rtl ? CKEDITOR.START : CKEDITOR.END ) &&
\r
831 ( next = parent[ rtl ? 'getPrevious' : 'getNext' ]( notWhitespaceEval ) ) )
\r
833 editor.fire( 'saveSnapshot' );
\r
835 // Move cursor to the end of previous block.
\r
836 range[ 'moveToElementEdit' + ( rtl ? 'End' : 'Start' ) ]( next );
\r
838 // Remove any previous empty block.
\r
839 if ( range.checkStartOfBlock() && range.checkEndOfBlock() )
\r
844 editor.fire( 'saveSnapshot' );
\r
846 evt.data.preventDefault();
\r
852 // PageUp OR PageDown
\r
853 if ( keyCode == 33 || keyCode == 34 )
\r
855 if ( CKEDITOR.env.gecko )
\r
857 var body = domDocument.getBody();
\r
859 // Page up/down cause editor selection to leak
\r
860 // outside of editable thus we try to intercept
\r
861 // the behavior, while it affects only happen
\r
862 // when editor contents are not overflowed. (#7955)
\r
863 if ( domWindow.$.innerHeight > body.$.offsetHeight )
\r
865 range = new CKEDITOR.dom.range( domDocument );
\r
866 range[ keyCode == 33 ? 'moveToElementEditStart' : 'moveToElementEditEnd']( body );
\r
868 evt.data.preventDefault();
\r
875 // PageUp/PageDown scrolling is broken in document
\r
876 // with standard doctype, manually fix it. (#4736)
\r
877 if ( CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat' )
\r
879 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
880 domDocument.on( 'keydown', function( evt )
\r
882 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
884 setTimeout( function ()
\r
886 editor.getSelection().scrollIntoView();
\r
892 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
893 if ( CKEDITOR.env.ie && editor.config.enterMode != CKEDITOR.ENTER_P )
\r
895 domDocument.on( 'selectionchange', function()
\r
897 var body = domDocument.getBody(),
\r
898 sel = editor.getSelection(),
\r
899 range = sel && sel.getRanges()[ 0 ];
\r
901 if ( range && body.getHtml().match( /^<p> <\/p>$/i )
\r
902 && range.startContainer.equals( body ) )
\r
904 // Avoid the ambiguity from a real user cursor position.
\r
905 setTimeout( function ()
\r
907 range = editor.getSelection().getRanges()[ 0 ];
\r
908 if ( !range.startContainer.equals ( 'body' ) )
\r
910 body.getFirst().remove( 1 );
\r
911 range.moveToElementEditEnd( body );
\r
919 // Adds the document body as a context menu target.
\r
920 if ( editor.contextMenu )
\r
921 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
923 setTimeout( function()
\r
925 editor.fire( 'contentDom' );
\r
929 editor.mode = 'wysiwyg';
\r
930 editor.fire( 'mode', { previousMode : editor._.previousMode } );
\r
934 isLoadingData = false;
\r
936 if ( isPendingFocus )
\r
939 isPendingFocus = false;
\r
941 setTimeout( function()
\r
943 editor.fire( 'dataReady' );
\r
946 // Enable dragging of position:absolute elements in IE.
\r
947 try { editor.document.$.execCommand ( '2D-position', false, true); } catch(e) {}
\r
949 // IE, Opera and Safari may not support it and throw errors.
\r
950 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
951 if ( editor.config.disableObjectResizing )
\r
955 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
959 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
960 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
962 evt.data.preventDefault();
\r
968 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
969 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
972 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
973 * fix the problem. :(
\r
975 if ( CKEDITOR.env.ie )
\r
977 setTimeout( function()
\r
979 if ( editor.document )
\r
981 var $body = editor.document.$.body;
\r
982 $body.runtimeStyle.marginBottom = '0px';
\r
983 $body.runtimeStyle.marginBottom = '';
\r
991 editor.addMode( 'wysiwyg',
\r
993 load : function( holderElement, data, isSnapshot )
\r
995 mainElement = holderElement;
\r
997 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
998 holderElement.setStyle( 'position', 'relative' );
\r
1000 // The editor data "may be dirty" after this
\r
1002 editor.mayBeDirty = true;
\r
1007 this.loadSnapshotData( data );
\r
1009 this.loadData( data );
\r
1012 loadData : function( data )
\r
1014 isLoadingData = true;
\r
1015 editor._.dataStore = { id : 1 };
\r
1017 var config = editor.config,
\r
1018 fullPage = config.fullPage,
\r
1019 docType = config.docType;
\r
1021 // Build the additional stuff to be included into <head>.
\r
1023 '<style type="text/css" data-cke-temp="1">' +
\r
1024 editor._.styles.join( '\n' ) +
\r
1027 !fullPage && ( headExtra =
\r
1028 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
1031 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
1035 // Search and sweep out the doctype declaration.
\r
1036 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
1038 editor.docType = docType = match;
\r
1040 }).replace( /<\?xml\s[^\?]*\?>/i, function( match )
\r
1042 editor.xmlDeclaration = match;
\r
1047 // Get the HTML version of the data.
\r
1048 if ( editor.dataProcessor )
\r
1049 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
1053 // Check if the <body> tag is available.
\r
1054 if ( !(/<body[\s|>]/).test( data ) )
\r
1055 data = '<body>' + data;
\r
1057 // Check if the <html> tag is available.
\r
1058 if ( !(/<html[\s|>]/).test( data ) )
\r
1059 data = '<html>' + data + '</html>';
\r
1061 // Check if the <head> tag is available.
\r
1062 if ( !(/<head[\s|>]/).test( data ) )
\r
1063 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
1064 else if ( !(/<title[\s|>]/).test( data ) )
\r
1065 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
1067 // The base must be the first tag in the HEAD, e.g. to get relative
\r
1068 // links on styles.
\r
1069 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
1071 // Inject the extra stuff into <head>.
\r
1072 // Attention: do not change it before testing it well. (V2)
\r
1073 // This is tricky... if the head ends with <meta ... content type>,
\r
1074 // Firefox will break. But, it works if we place our extra stuff as
\r
1075 // the last elements in the HEAD.
\r
1076 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
1078 // Add the DOCTYPE back to it.
\r
1079 data = docType + data;
\r
1085 '<html dir="' + config.contentsLangDirection + '"' +
\r
1086 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
1088 '<title>' + frameLabel + '</title>' +
\r
1092 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
1093 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1099 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1100 if ( CKEDITOR.env.gecko )
\r
1101 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1103 data += activationScript;
\r
1106 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1108 createIFrame( data );
\r
1111 getData : function()
\r
1113 var config = editor.config,
\r
1114 fullPage = config.fullPage,
\r
1115 docType = fullPage && editor.docType,
\r
1116 xmlDeclaration = fullPage && editor.xmlDeclaration,
\r
1117 doc = iframe.getFrameDocument();
\r
1119 var data = fullPage
\r
1120 ? doc.getDocumentElement().getOuterHtml()
\r
1121 : doc.getBody().getHtml();
\r
1123 // BR at the end of document is bogus node for Mozilla. (#5293).
\r
1124 if ( CKEDITOR.env.gecko )
\r
1125 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
\r
1127 if ( editor.dataProcessor )
\r
1128 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
1130 // Reset empty if the document contains only one empty paragraph.
\r
1131 if ( config.ignoreEmptyParagraph )
\r
1132 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
1134 if ( xmlDeclaration )
\r
1135 data = xmlDeclaration + '\n' + data;
\r
1137 data = docType + '\n' + data;
\r
1142 getSnapshotData : function()
\r
1144 return iframe.getFrameDocument().getBody().getHtml();
\r
1147 loadSnapshotData : function( data )
\r
1149 iframe.getFrameDocument().getBody().setHtml( data );
\r
1152 onDispose : function()
\r
1154 if ( !editor.document )
\r
1157 editor.document.getDocumentElement().clearCustomData();
\r
1158 editor.document.getBody().clearCustomData();
\r
1160 editor.window.clearCustomData();
\r
1161 editor.document.clearCustomData();
\r
1163 iframe.clearCustomData();
\r
1166 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1167 * editing area would break IE7/8's selection system, we have to put the editing
\r
1168 * iframe offline first. (#3812 and #5441)
\r
1173 unload : function( holderElement )
\r
1178 win.removeListener( 'resize', onResize );
\r
1180 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1182 editor.fire( 'contentDomUnload' );
\r
1185 focus : function()
\r
1187 var win = editor.window;
\r
1189 if ( isLoadingData )
\r
1190 isPendingFocus = true;
\r
1193 var sel = editor.getSelection(),
\r
1194 ieSel = sel && sel.getNative();
\r
1196 // IE considers control-type element as separate
\r
1197 // focus host when selected, avoid destroying the
\r
1198 // selection in such case. (#5812) (#8949)
\r
1199 if ( ieSel && ieSel.type == 'Control' )
\r
1202 // AIR needs a while to focus when moving from a link.
\r
1203 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1204 editor.selectionChange();
\r
1209 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1210 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1211 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1212 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1213 editor.on( 'selectionChange', function( evt )
\r
1215 if ( editor.readOnly )
\r
1218 var sel = editor.getSelection();
\r
1219 // Do it only when selection is not locked. (#8222)
\r
1220 if ( sel && !sel.isLocked )
\r
1222 var isDirty = editor.checkDirty();
\r
1223 editor.fire( 'saveSnapshot', { contentOnly : 1 } );
\r
1224 onSelectionChangeFixBody.call( this, evt );
\r
1225 editor.fire( 'updateSnapshot' );
\r
1226 !isDirty && editor.resetDirty();
\r
1229 }, null, null, 1 );
\r
1232 editor.on( 'contentDom', function()
\r
1234 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1235 title.data( 'cke-title', editor.document.$.title );
\r
1237 // [IE] JAWS will not recognize the aria label we used on the iframe
\r
1238 // unless the frame window title string is used as the voice label,
\r
1239 // backup the original one and restore it on output.
\r
1240 CKEDITOR.env.ie && ( editor.document.$.title = frameLabel );
\r
1243 editor.on( 'readOnly', function()
\r
1245 if ( editor.mode == 'wysiwyg' )
\r
1247 // Symply reload the wysiwyg area. It'll take care of read-only.
\r
1248 var wysiwyg = editor.getMode();
\r
1249 wysiwyg.loadData( wysiwyg.getData() );
\r
1253 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1254 // on element unless it has layout. (#5562)
\r
1255 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1257 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1259 var selectors = [];
\r
1260 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1261 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1262 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1264 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1265 else if ( CKEDITOR.env.gecko )
\r
1267 editor.addCss( 'html { height: 100% !important; }' );
\r
1268 editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1; min-width : 24px; min-height : 24px; }' );
\r
1270 // Remove the margin to avoid mouse confusion. (#8835)
\r
1271 else if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 && editor.config.contentsLangDirection == 'ltr' )
\r
1272 editor.addCss( 'body{margin-right:0;}' );
\r
1274 /* #3658: [IE6] Editor document has horizontal scrollbar on long lines
\r
1275 To prevent this misbehavior, we show the scrollbar always */
\r
1276 /* #6341: The text cursor must be set on the editor area. */
\r
1277 /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/
\r
1278 editor.addCss( 'html { _overflow-y: scroll; cursor: text; *cursor:auto;}' );
\r
1279 // Use correct cursor for these elements
\r
1280 editor.addCss( 'img, input, textarea { cursor: default;}' );
\r
1282 // Disable form elements editing mode provided by some browers. (#5746)
\r
1283 editor.on( 'insertElement', function ( evt )
\r
1285 var element = evt.data;
\r
1286 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1287 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1289 // We should flag that the element was locked by our code so
\r
1290 // it'll be editable by the editor functions (#6046).
\r
1291 var readonly = element.getAttribute( 'contenteditable' ) == 'false';
\r
1294 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1295 element.setAttribute( 'contenteditable', false );
\r
1303 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1304 if ( CKEDITOR.env.gecko )
\r
1308 var body = document.body;
\r
1311 window.addEventListener( 'load', arguments.callee, false );
\r
1314 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1315 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1316 'event.persisted && (function(){' +
\r
1317 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1318 'for ( var i in allInstances )' +
\r
1320 ' editor = allInstances[ i ];' +
\r
1321 ' doc = editor.document;' +
\r
1324 ' doc.$.designMode = "off";' +
\r
1325 ' doc.$.designMode = "on";' +
\r
1336 * Disables the ability of resize objects (image and tables) in the editing
\r
1341 * config.disableObjectResizing = true;
\r
1343 CKEDITOR.config.disableObjectResizing = false;
\r
1346 * Disables the "table tools" offered natively by the browser (currently
\r
1347 * Firefox only) to make quick table editing operations, like adding or
\r
1348 * deleting rows and columns.
\r
1352 * config.disableNativeTableHandles = false;
\r
1354 CKEDITOR.config.disableNativeTableHandles = true;
\r
1357 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1359 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1360 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1361 * is enabled or you're simply not using the context menu plugin.
\r
1366 * config.disableNativeSpellChecker = false;
\r
1368 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1371 * Whether the editor must output an empty value ("") if it's contents is made
\r
1372 * by an empty paragraph only.
\r
1376 * config.ignoreEmptyParagraph = false;
\r
1378 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1381 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1382 * @name CKEDITOR.editor#dataReady
\r
1387 * Whether automatically create wrapping blocks around inline contents inside document body,
\r
1388 * this helps to ensure the integrality of the block enter mode.
\r
1389 * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.
\r
1390 * @name CKEDITOR.config.autoParagraph
\r
1395 * config.autoParagraph = false;
\r
1399 * Fired when some elements are added to the document
\r
1400 * @name CKEDITOR.editor#ariaWidget
\r
1402 * @param {Object} element The element being added
\r