2 Copyright (c) 2003-2013, 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
50 // Save snaps after the whole execution completed.
\r
51 // This's a workaround for make DOM modification's happened after
\r
52 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
54 setTimeout( function()
\r
56 try { that.fire( 'saveSnapshot' ); }
\r
57 // IEs < 9 may requires a further delay to save snapshot, after pasting. (#9132)
\r
58 catch ( e ) { setTimeout( function(){ that.fire( 'saveSnapshot' ); }, 200 ); }
\r
64 function doInsertHtml( data )
\r
66 if ( this.dataProcessor )
\r
67 data = this.dataProcessor.toHtml( data );
\r
72 // HTML insertion only considers the first range.
\r
73 var selection = this.getSelection(),
\r
74 range = selection.getRanges()[ 0 ];
\r
76 if ( range.checkReadOnly() )
\r
79 // Opera: force block splitting when pasted content contains block. (#7801)
\r
80 if ( CKEDITOR.env.opera )
\r
82 var path = new CKEDITOR.dom.elementPath( range.startContainer );
\r
85 var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;
\r
86 for ( var i = 0, count = nodes.length; i < count; i++ )
\r
88 if ( nodes[ i ]._.isBlockLike )
\r
90 range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
91 range.insertNode( range.document.createText( '' ) );
\r
99 if ( CKEDITOR.env.ie )
\r
101 var $sel = selection.getNative();
\r
103 // Delete control selections to avoid IE bugs on pasteHTML.
\r
104 if ( $sel.type == 'Control' )
\r
106 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
108 // Due to IE bugs on handling contenteditable=false blocks
\r
109 // (#6005), we need to make some checks and eventually
\r
110 // delete the selection first.
\r
112 range = selection.getRanges()[ 0 ];
\r
113 var endContainer = range && range.endContainer;
\r
115 if ( endContainer &&
\r
116 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
117 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
118 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
120 range.setEndAfter( range.endContainer );
\r
121 range.deleteContents();
\r
125 $sel.createRange().pasteHTML( data );
\r
128 this.document.$.execCommand( 'inserthtml', false, data );
\r
130 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
131 if ( CKEDITOR.env.webkit )
\r
133 selection = this.getSelection();
\r
134 selection.scrollIntoView();
\r
138 function doInsertText( text )
\r
140 var selection = this.getSelection(),
\r
141 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
142 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
143 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
145 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
147 // Convert leading and trailing whitespaces into
\r
148 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
150 if ( match.length == 1 ) // one space, preserve it
\r
152 else if ( !offset ) // beginning of block
\r
153 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
154 else // end of block
\r
155 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
158 // Convert subsequent whitespaces into
\r
159 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
161 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
164 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
166 // Two line-breaks create one paragraph.
\r
167 if ( !isEnterBrMode )
\r
169 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
170 function( match, group1, text )
\r
172 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
176 // One <br> per line-break.
\r
177 html = html.replace( /\n/g, '<br>' );
\r
179 // Compensate padding <br> for non-IE.
\r
180 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
182 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
184 return CKEDITOR.tools.repeat( match, 2 );
\r
188 // Inline styles have to be inherited in Firefox.
\r
189 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
191 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
194 for ( var i = 0; i < path.elements.length; i++ )
\r
196 var tag = path.elements[ i ].getName();
\r
197 if ( tag in CKEDITOR.dtd.$inline )
\r
198 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
199 else if ( tag in CKEDITOR.dtd.$block )
\r
203 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
204 html = context.join( '' ) + html;
\r
207 doInsertHtml.call( this, html );
\r
210 function doInsertElement( element )
\r
212 var selection = this.getSelection(),
\r
213 ranges = selection.getRanges(),
\r
214 elementName = element.getName(),
\r
215 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
217 var selIsLocked = selection.isLocked;
\r
220 selection.unlock();
\r
222 var range, clone, lastElement, bookmark;
\r
224 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
226 range = ranges[ i ];
\r
228 if ( !range.checkReadOnly() )
\r
230 // Remove the original contents, merge splitted nodes.
\r
231 range.deleteContents( 1 );
\r
233 clone = !i && element || element.clone( 1 );
\r
235 // If we're inserting a block at dtd-violated position, split
\r
236 // the parent blocks until we reach blockLimit.
\r
240 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
241 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
242 && !( dtd && dtd [ elementName ] ) )
\r
244 // Split up inline elements.
\r
245 if ( current.getName() in CKEDITOR.dtd.span )
\r
246 range.splitElement( current );
\r
247 // If we're in an empty block which indicate a new paragraph,
\r
248 // simply replace it with the inserting block.(#3664)
\r
249 else if ( range.checkStartOfBlock()
\r
250 && range.checkEndOfBlock() )
\r
252 range.setStartBefore( current );
\r
253 range.collapse( true );
\r
257 range.splitBlock();
\r
261 // Insert the new node.
\r
262 range.insertNode( clone );
\r
264 // Save the last element reference so we can make the
\r
265 // selection later.
\r
266 if ( !lastElement )
\r
267 lastElement = clone;
\r
273 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
275 // If we're inserting a block element immediately followed by
\r
276 // another block element, the selection must be optimized. (#3100,#5436,#8950)
\r
279 var next = lastElement.getNext( notEmpty ),
\r
280 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
282 // If the next one is a text block, move cursor to the start of it's content.
\r
283 if ( nextName && CKEDITOR.dtd.$block[ nextName ] )
\r
285 if ( CKEDITOR.dtd[ nextName ][ '#' ] )
\r
286 range.moveToElementEditStart( next );
\r
287 // Otherwise move cursor to the before end of the last element.
\r
289 range.moveToElementEditEnd( lastElement );
\r
291 // Open a new line if the block is inserted at the end of parent.
\r
294 next = range.fixBlock( true, this.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
295 range.moveToElementEditStart( next );
\r
300 selection.selectRanges( [ range ] );
\r
303 this.getSelection().lock();
\r
306 // DOM modification here should not bother dirty flag.(#4385)
\r
307 function restoreDirty( editor )
\r
309 if ( !editor.checkDirty() )
\r
310 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
313 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
314 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
316 function isNotEmpty( node )
\r
318 return isNotWhitespace( node ) && isNotBookmark( node );
\r
321 function isNbsp( node )
\r
323 return node.type == CKEDITOR.NODE_TEXT
\r
324 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
327 function restoreSelection( selection )
\r
329 if ( selection.isLocked )
\r
331 selection.unlock();
\r
332 setTimeout( function() { selection.lock(); }, 0 );
\r
336 function isBlankParagraph( block )
\r
338 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
341 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
343 // Gecko need a key event to 'wake up' the editing
\r
344 // ability when document is empty.(#3864)
\r
345 function activateEditing( editor )
\r
347 var win = editor.window,
\r
348 doc = editor.document,
\r
349 body = editor.document.getBody(),
\r
350 bodyFirstChild = body.getFirst(),
\r
351 bodyChildsNum = body.getChildren().count();
\r
353 if ( !bodyChildsNum
\r
354 || bodyChildsNum == 1
\r
355 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
356 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
\r
358 restoreDirty( editor );
\r
360 // Memorize scroll position to restore it later (#4472).
\r
361 var hostDocument = editor.element.getDocument();
\r
362 var hostDocumentElement = hostDocument.getDocumentElement();
\r
363 var scrollTop = hostDocumentElement.$.scrollTop;
\r
364 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
366 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
367 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
368 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
369 false, false, false, 0, 32 );
\r
370 doc.$.dispatchEvent( keyEventSimulate );
\r
372 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
373 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
375 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
376 bodyChildsNum && body.getFirst().remove();
\r
377 doc.getBody().appendBogus();
\r
378 var nativeRange = new CKEDITOR.dom.range( doc );
\r
379 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
380 nativeRange.select();
\r
385 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
386 * non-exitable-block by padding extra br.(#3189)
\r
388 function onSelectionChangeFixBody( evt )
\r
390 var editor = evt.editor,
\r
391 path = evt.data.path,
\r
392 blockLimit = path.blockLimit,
\r
393 selection = evt.data.selection,
\r
394 range = selection.getRanges()[0],
\r
395 body = editor.document.getBody(),
\r
396 enterMode = editor.config.enterMode;
\r
398 if ( CKEDITOR.env.gecko )
\r
400 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
401 var pathBlock = path.block || path.blockLimit,
\r
402 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
404 // Check some specialities of the current path block:
\r
405 // 1. It is really displayed as block; (#7221)
\r
406 // 2. It doesn't end with one inner block; (#7467)
\r
407 // 3. It doesn't have bogus br yet.
\r
409 && pathBlock.isBlockBoundary()
\r
410 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
\r
411 && !pathBlock.is( 'pre' )
\r
412 && !pathBlock.getBogus() )
\r
414 pathBlock.appendBogus();
\r
418 // When we're in block enter mode, a new paragraph will be established
\r
419 // to encapsulate inline contents right under body. (#3657)
\r
420 if ( editor.config.autoParagraph !== false
\r
421 && enterMode != CKEDITOR.ENTER_BR
\r
423 && blockLimit.getName() == 'body'
\r
426 var fixedBlock = range.fixBlock( true,
\r
427 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
429 // For IE, we should remove any filler node which was introduced before.
\r
430 if ( CKEDITOR.env.ie )
\r
432 var first = fixedBlock.getFirst( isNotEmpty );
\r
433 first && isNbsp( first ) && first.remove();
\r
436 // If the fixed block is actually blank and is already followed by an exitable blank
\r
437 // block, we should revert the fix and move into the existed one. (#3684)
\r
438 if ( isBlankParagraph( fixedBlock ) )
\r
440 var element = fixedBlock.getNext( isNotWhitespace );
\r
442 element.type == CKEDITOR.NODE_ELEMENT &&
\r
443 !nonEditable( element ) )
\r
445 range.moveToElementEditStart( element );
\r
446 fixedBlock.remove();
\r
450 element = fixedBlock.getPrevious( isNotWhitespace );
\r
452 element.type == CKEDITOR.NODE_ELEMENT &&
\r
453 !nonEditable( element ) )
\r
455 range.moveToElementEditEnd( element );
\r
456 fixedBlock.remove();
\r
462 // Cancel this selection change in favor of the next (correct). (#6811)
\r
466 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
\r
467 // at the end of document, makes it unable to continue adding content, we have to make this
\r
468 // easier by opening an new empty paragraph.
\r
469 var testRange = new CKEDITOR.dom.range( editor.document );
\r
470 testRange.moveToElementEditEnd( editor.document.getBody() );
\r
471 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
\r
472 if ( !testPath.blockLimit.is( 'body') )
\r
475 if ( enterMode != CKEDITOR.ENTER_BR )
\r
476 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
478 paddingBlock = body;
\r
480 if ( !CKEDITOR.env.ie )
\r
481 paddingBlock.appendBogus();
\r
485 CKEDITOR.plugins.add( 'wysiwygarea',
\r
487 requires : [ 'editingblock' ],
\r
489 init : function( editor )
\r
491 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )
\r
492 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
494 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name ),
\r
495 frameDesc = editor.lang.editorHelp;
\r
497 if ( CKEDITOR.env.ie )
\r
498 frameLabel += ', ' + frameDesc;
\r
500 var win = CKEDITOR.document.getWindow();
\r
501 var contentDomReadyHandler;
\r
502 editor.on( 'editingBlockReady', function()
\r
513 // Support for custom document.domain in IE.
\r
514 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
516 // Creates the iframe that holds the editable document.
\r
517 var createIFrame = function( data )
\r
523 'document.open();' +
\r
525 // The document domain must be set any time we
\r
526 // call document.open().
\r
527 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
529 'document.close();';
\r
531 // With IE, the custom domain has to be taken care at first,
\r
532 // for other browers, the 'src' attribute should be left empty to
\r
533 // trigger iframe's 'load' event.
\r
536 'javascript:void(0)' :
\r
538 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
542 var labelId = CKEDITOR.tools.getNextId();
\r
543 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
544 ' style="width:100%;height:100%"' +
\r
545 ' frameBorder="0"' +
\r
546 ' aria-describedby="' + labelId + '"' +
\r
547 ' title="' + frameLabel + '"' +
\r
548 ' src="' + src + '"' +
\r
549 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
550 ' allowTransparency="true"' +
\r
553 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
554 if ( document.location.protocol == 'chrome:' )
\r
555 CKEDITOR.event.useCapture = true;
\r
557 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
558 iframe.on( 'load', function( ev )
\r
561 ev.removeListener();
\r
563 var doc = iframe.getFrameDocument();
\r
566 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
569 // Reset adjustment back to default (#5689)
\r
570 if ( document.location.protocol == 'chrome:' )
\r
571 CKEDITOR.event.useCapture = false;
\r
573 mainElement.append( CKEDITOR.dom.element.createFromHtml(
\r
574 '<span id="' + labelId + '" class="cke_voice_label">' + frameDesc + '</span>'));
\r
576 mainElement.append( iframe );
\r
578 // Webkit: iframe size doesn't auto fit well. (#7360)
\r
579 if ( CKEDITOR.env.webkit )
\r
581 onResize = function()
\r
583 // Hide the iframe to get real size of the holder. (#8941)
\r
584 mainElement.setStyle( 'width', '100%' );
\r
587 iframe.setSize( 'width', mainElement.getSize( 'width' ) );
\r
588 mainElement.removeStyle( 'width' );
\r
592 win.on( 'resize', onResize );
\r
596 // The script that launches the bootstrap logic on 'domReady', so the document
\r
597 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
598 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
599 var activationScript =
\r
600 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
601 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
602 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
605 // Editing area bootstrap code.
\r
606 function contentDomReady( domWindow )
\r
608 if ( !frameLoaded )
\r
612 editor.fire( 'ariaWidget', iframe );
\r
614 var domDocument = domWindow.document,
\r
615 body = domDocument.body;
\r
617 // Remove this script from the DOM.
\r
618 var script = domDocument.getElementById( "cke_actscrpt" );
\r
619 script && script.parentNode.removeChild( script );
\r
621 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
623 var editable = !editor.readOnly;
\r
625 if ( CKEDITOR.env.ie )
\r
627 // Don't display the focus border.
\r
628 body.hideFocus = true;
\r
630 // Disable and re-enable the body to avoid IE from
\r
631 // taking the editing focus at startup. (#141 / #523)
\r
632 body.disabled = true;
\r
633 body.contentEditable = editable;
\r
634 body.removeAttribute( 'disabled' );
\r
638 // Avoid opening design mode in a frame window thread,
\r
639 // which will cause host page scrolling.(#4397)
\r
640 setTimeout( function()
\r
642 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
643 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
644 || CKEDITOR.env.opera )
\r
645 domDocument.$.body.contentEditable = editable;
\r
646 else if ( CKEDITOR.env.webkit )
\r
647 domDocument.$.body.parentNode.contentEditable = editable;
\r
649 domDocument.$.designMode = editable? 'off' : 'on';
\r
653 editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
655 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
656 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
658 editable && domDocument.on( 'dblclick', function( evt )
\r
660 var element = evt.data.getTarget(),
\r
661 data = { element : element, dialog : '' };
\r
662 editor.fire( 'doubleclick', data );
\r
663 data.dialog && editor.openDialog( data.dialog );
\r
666 // Prevent automatic submission in IE #6336
\r
667 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
669 var element = evt.data.getTarget();
\r
670 if ( element.is( 'input' ) )
\r
672 var type = element.getAttribute( 'type' );
\r
673 if ( type == 'submit' || type == 'reset' )
\r
674 evt.data.preventDefault();
\r
678 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
679 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
681 domDocument.on( 'mousedown', function( ev )
\r
683 var control = ev.data.getTarget();
\r
684 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
685 editor.getSelection().selectElement( control );
\r
689 if ( CKEDITOR.env.gecko )
\r
691 domDocument.on( 'mouseup', function( ev )
\r
693 if ( ev.data.$.button == 2 )
\r
695 var target = ev.data.getTarget();
\r
697 // Prevent right click from selecting an empty block even
\r
698 // when selection is anchored inside it. (#5845)
\r
699 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
701 var range = new CKEDITOR.dom.range( domDocument );
\r
702 range.moveToElementEditStart( target );
\r
703 range.select( true );
\r
709 // Prevent the browser opening links in read-only blocks. (#6032)
\r
710 domDocument.on( 'click', function( ev )
\r
713 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
714 ev.preventDefault();
\r
717 // Webkit: avoid from editing form control elements content.
\r
718 if ( CKEDITOR.env.webkit )
\r
720 // Mark that cursor will right blinking (#7113).
\r
721 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
722 // Prevent from tick checkbox/radiobox/select
\r
723 domDocument.on( 'click', function( ev )
\r
725 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
726 ev.data.preventDefault();
\r
729 // Prevent from editig textfield/textarea value.
\r
730 domDocument.on( 'mouseup', function( ev )
\r
732 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
733 ev.data.preventDefault();
\r
737 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
738 focusTarget.on( 'blur', function()
\r
740 editor.focusManager.blur();
\r
745 focusTarget.on( 'focus', function()
\r
747 var doc = editor.document;
\r
749 if ( CKEDITOR.env.gecko || CKEDITOR.env.opera )
\r
750 doc.getBody().focus();
\r
751 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
752 else if ( CKEDITOR.env.webkit )
\r
756 editor.document.getDocumentElement().focus();
\r
761 editor.focusManager.focus();
\r
764 var keystrokeHandler = editor.keystrokeHandler;
\r
765 // Prevent backspace from navigating off the page.
\r
766 keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;
\r
767 keystrokeHandler.attach( domDocument );
\r
769 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
770 // Override keystroke behaviors.
\r
771 editor.on( 'key', function( evt )
\r
773 if ( editor.mode != 'wysiwyg' )
\r
776 var keyCode = evt.data.keyCode;
\r
778 // Backspace OR Delete.
\r
779 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
781 var sel = editor.getSelection(),
\r
782 selected = sel.getSelectedElement(),
\r
783 range = sel.getRanges()[ 0 ],
\r
784 path = new CKEDITOR.dom.elementPath( range.startContainer ),
\r
788 rtl = keyCode == 8;
\r
790 // Override keystrokes which should have deletion behavior
\r
791 // on fully selected element . (#4047) (#7645)
\r
794 // Make undo snapshot.
\r
795 editor.fire( 'saveSnapshot' );
\r
797 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
798 // break up the selection, safely manage it here. (#4795)
\r
799 range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );
\r
800 // Remove the control manually.
\r
804 editor.fire( 'saveSnapshot' );
\r
808 else if ( range.collapsed )
\r
810 // Handle the following special cases: (#6217)
\r
811 // 1. Del/Backspace key before/after table;
\r
812 // 2. Backspace Key after start of table.
\r
813 if ( ( block = path.block ) &&
\r
814 range[ rtl ? 'checkStartOfBlock' : 'checkEndOfBlock' ]() &&
\r
815 ( next = block[ rtl ? 'getPrevious' : 'getNext' ]( notWhitespaceEval ) ) &&
\r
816 next.is( 'table' ) )
\r
818 editor.fire( 'saveSnapshot' );
\r
820 // Remove the current empty block.
\r
821 if ( range[ rtl ? 'checkEndOfBlock' : 'checkStartOfBlock' ]() )
\r
824 // Move cursor to the beginning/end of table cell.
\r
825 range[ 'moveToElementEdit' + ( rtl ? 'End' : 'Start' ) ]( next );
\r
828 editor.fire( 'saveSnapshot' );
\r
832 else if ( path.blockLimit.is( 'td' ) &&
\r
833 ( parent = path.blockLimit.getAscendant( 'table' ) ) &&
\r
834 range.checkBoundaryOfElement( parent, rtl ? CKEDITOR.START : CKEDITOR.END ) &&
\r
835 ( next = parent[ rtl ? 'getPrevious' : 'getNext' ]( notWhitespaceEval ) ) )
\r
837 editor.fire( 'saveSnapshot' );
\r
839 // Move cursor to the end of previous block.
\r
840 range[ 'moveToElementEdit' + ( rtl ? 'End' : 'Start' ) ]( next );
\r
842 // Remove any previous empty block.
\r
843 if ( range.checkStartOfBlock() && range.checkEndOfBlock() )
\r
848 editor.fire( 'saveSnapshot' );
\r
856 // PageUp OR PageDown
\r
857 if ( keyCode == 33 || keyCode == 34 )
\r
859 if ( CKEDITOR.env.gecko )
\r
861 var body = domDocument.getBody();
\r
863 // Page up/down cause editor selection to leak
\r
864 // outside of editable thus we try to intercept
\r
865 // the behavior, while it affects only happen
\r
866 // when editor contents are not overflowed. (#7955)
\r
867 if ( domWindow.$.innerHeight > body.$.offsetHeight )
\r
869 range = new CKEDITOR.dom.range( domDocument );
\r
870 range[ keyCode == 33 ? 'moveToElementEditStart' : 'moveToElementEditEnd']( body );
\r
879 // PageUp/PageDown scrolling is broken in document
\r
880 // with standard doctype, manually fix it. (#4736)
\r
881 if ( CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat' )
\r
883 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
884 domDocument.on( 'keydown', function( evt )
\r
886 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
888 setTimeout( function ()
\r
890 editor.getSelection().scrollIntoView();
\r
896 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
897 if ( CKEDITOR.env.ie && editor.config.enterMode != CKEDITOR.ENTER_P )
\r
899 domDocument.on( 'selectionchange', function()
\r
901 var body = domDocument.getBody(),
\r
902 sel = editor.getSelection(),
\r
903 range = sel && sel.getRanges()[ 0 ];
\r
905 if ( range && body.getHtml().match( /^<p> <\/p>$/i )
\r
906 && range.startContainer.equals( body ) )
\r
908 // Avoid the ambiguity from a real user cursor position.
\r
909 setTimeout( function ()
\r
911 range = editor.getSelection().getRanges()[ 0 ];
\r
912 if ( !range.startContainer.equals ( 'body' ) )
\r
914 body.getFirst().remove( 1 );
\r
915 range.moveToElementEditEnd( body );
\r
923 // Adds the document body as a context menu target.
\r
924 if ( editor.contextMenu )
\r
925 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
927 setTimeout( function()
\r
929 editor.fire( 'contentDom' );
\r
933 editor.mode = 'wysiwyg';
\r
934 editor.fire( 'mode', { previousMode : editor._.previousMode } );
\r
938 isLoadingData = false;
\r
940 if ( isPendingFocus )
\r
943 isPendingFocus = false;
\r
945 setTimeout( function()
\r
947 editor.fire( 'dataReady' );
\r
950 // Enable dragging of position:absolute elements in IE.
\r
951 try { editor.document.$.execCommand ( '2D-position', false, true); } catch(e) {}
\r
953 // IE, Opera and Safari may not support it and throw errors.
\r
954 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
955 if ( editor.config.disableObjectResizing )
\r
959 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
963 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
964 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
966 evt.data.preventDefault();
\r
972 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
973 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
976 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
977 * fix the problem. :(
\r
979 if ( CKEDITOR.env.ie )
\r
981 setTimeout( function()
\r
983 if ( editor.document )
\r
985 var $body = editor.document.$.body;
\r
986 $body.runtimeStyle.marginBottom = '0px';
\r
987 $body.runtimeStyle.marginBottom = '';
\r
995 editor.addMode( 'wysiwyg',
\r
997 load : function( holderElement, data, isSnapshot )
\r
999 mainElement = holderElement;
\r
1001 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
1002 holderElement.setStyle( 'position', 'relative' );
\r
1004 // The editor data "may be dirty" after this
\r
1006 editor.mayBeDirty = true;
\r
1011 this.loadSnapshotData( data );
\r
1013 this.loadData( data );
\r
1016 loadData : function( data )
\r
1018 isLoadingData = true;
\r
1019 editor._.dataStore = { id : 1 };
\r
1021 var config = editor.config,
\r
1022 fullPage = config.fullPage,
\r
1023 docType = config.docType;
\r
1025 // Build the additional stuff to be included into <head>.
\r
1027 '<style type="text/css" data-cke-temp="1">' +
\r
1028 editor._.styles.join( '\n' ) +
\r
1031 !fullPage && ( headExtra =
\r
1032 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
1035 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
1039 // Search and sweep out the doctype declaration.
\r
1040 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
1042 editor.docType = docType = match;
\r
1044 }).replace( /<\?xml\s[^\?]*\?>/i, function( match )
\r
1046 editor.xmlDeclaration = match;
\r
1051 // Get the HTML version of the data.
\r
1052 if ( editor.dataProcessor )
\r
1053 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
1057 // Check if the <body> tag is available.
\r
1058 if ( !(/<body[\s|>]/).test( data ) )
\r
1059 data = '<body>' + data;
\r
1061 // Check if the <html> tag is available.
\r
1062 if ( !(/<html[\s|>]/).test( data ) )
\r
1063 data = '<html>' + data + '</html>';
\r
1065 // Check if the <head> tag is available.
\r
1066 if ( !(/<head[\s|>]/).test( data ) )
\r
1067 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
1068 else if ( !(/<title[\s|>]/).test( data ) )
\r
1069 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
1071 // The base must be the first tag in the HEAD, e.g. to get relative
\r
1072 // links on styles.
\r
1073 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
1075 // Inject the extra stuff into <head>.
\r
1076 // Attention: do not change it before testing it well. (V2)
\r
1077 // This is tricky... if the head ends with <meta ... content type>,
\r
1078 // Firefox will break. But, it works if we place our extra stuff as
\r
1079 // the last elements in the HEAD.
\r
1080 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
1082 // Add the DOCTYPE back to it.
\r
1083 data = docType + data;
\r
1089 '<html dir="' + config.contentsLangDirection + '"' +
\r
1090 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
1092 '<title>' + frameLabel + '</title>' +
\r
1096 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
1097 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1103 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1104 if ( CKEDITOR.env.gecko )
\r
1105 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1107 data += activationScript;
\r
1110 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1112 createIFrame( data );
\r
1115 getData : function()
\r
1117 var config = editor.config,
\r
1118 fullPage = config.fullPage,
\r
1119 docType = fullPage && editor.docType,
\r
1120 xmlDeclaration = fullPage && editor.xmlDeclaration,
\r
1121 doc = iframe.getFrameDocument();
\r
1123 var data = fullPage
\r
1124 ? doc.getDocumentElement().getOuterHtml()
\r
1125 : doc.getBody().getHtml();
\r
1127 // BR at the end of document is bogus node for Mozilla. (#5293).
\r
1128 if ( CKEDITOR.env.gecko )
\r
1129 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
\r
1131 if ( editor.dataProcessor )
\r
1132 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
1134 // Reset empty if the document contains only one empty paragraph.
\r
1135 if ( config.ignoreEmptyParagraph )
\r
1136 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
1138 if ( xmlDeclaration )
\r
1139 data = xmlDeclaration + '\n' + data;
\r
1141 data = docType + '\n' + data;
\r
1146 getSnapshotData : function()
\r
1148 return iframe.getFrameDocument().getBody().getHtml();
\r
1151 loadSnapshotData : function( data )
\r
1153 iframe.getFrameDocument().getBody().setHtml( data );
\r
1156 onDispose : function()
\r
1158 if ( !editor.document )
\r
1161 editor.document.getDocumentElement().clearCustomData();
\r
1162 editor.document.getBody().clearCustomData();
\r
1164 editor.window.clearCustomData();
\r
1165 editor.document.clearCustomData();
\r
1167 iframe.clearCustomData();
\r
1170 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1171 * editing area would break IE7/8's selection system, we have to put the editing
\r
1172 * iframe offline first. (#3812 and #5441)
\r
1177 unload : function( holderElement )
\r
1182 win.removeListener( 'resize', onResize );
\r
1184 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1186 editor.fire( 'contentDomUnload' );
\r
1189 focus : function()
\r
1191 var win = editor.window;
\r
1193 if ( isLoadingData )
\r
1194 isPendingFocus = true;
\r
1197 var sel = editor.getSelection(),
\r
1198 ieSel = sel && sel.getNative();
\r
1200 // IE considers control-type element as separate
\r
1201 // focus host when selected, avoid destroying the
\r
1202 // selection in such case. (#5812) (#8949)
\r
1203 if ( ieSel && ieSel.type == 'Control' )
\r
1206 // AIR needs a while to focus when moving from a link.
\r
1207 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1208 editor.selectionChange();
\r
1213 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1214 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1215 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1216 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1217 editor.on( 'selectionChange', function( evt )
\r
1219 if ( editor.readOnly )
\r
1222 var sel = editor.getSelection();
\r
1223 // Do it only when selection is not locked. (#8222)
\r
1224 if ( sel && !sel.isLocked )
\r
1226 var isDirty = editor.checkDirty();
\r
1227 editor.fire( 'saveSnapshot', { contentOnly : 1 } );
\r
1228 onSelectionChangeFixBody.call( this, evt );
\r
1229 editor.fire( 'updateSnapshot' );
\r
1230 !isDirty && editor.resetDirty();
\r
1233 }, null, null, 1 );
\r
1236 editor.on( 'contentDom', function()
\r
1238 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1239 title.data( 'cke-title', editor.document.$.title );
\r
1241 // [IE] JAWS will not recognize the aria label we used on the iframe
\r
1242 // unless the frame window title string is used as the voice label,
\r
1243 // backup the original one and restore it on output.
\r
1244 CKEDITOR.env.ie && ( editor.document.$.title = frameLabel );
\r
1247 editor.on( 'readOnly', function()
\r
1249 if ( editor.mode == 'wysiwyg' )
\r
1251 // Symply reload the wysiwyg area. It'll take care of read-only.
\r
1252 var wysiwyg = editor.getMode();
\r
1253 wysiwyg.loadData( wysiwyg.getData() );
\r
1257 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1258 // on element unless it has layout. (#5562)
\r
1259 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1261 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1263 var selectors = [];
\r
1264 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1265 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1266 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1268 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1269 else if ( CKEDITOR.env.gecko )
\r
1271 editor.addCss( 'html { height: 100% !important; }' );
\r
1272 editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1; min-width : 24px; min-height : 24px; }' );
\r
1275 /* #3658: [IE6] Editor document has horizontal scrollbar on long lines
\r
1276 To prevent this misbehavior, we show the scrollbar always */
\r
1277 /* #6341: The text cursor must be set on the editor area. */
\r
1278 /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/
\r
1279 editor.addCss( 'html { _overflow-y: scroll; cursor: text; *cursor:auto;}' );
\r
1280 // Use correct cursor for these elements
\r
1281 editor.addCss( 'img, input, textarea { cursor: default;}' );
\r
1283 // Disable form elements editing mode provided by some browers. (#5746)
\r
1284 editor.on( 'insertElement', function ( evt )
\r
1286 var element = evt.data;
\r
1287 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1288 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1290 // We should flag that the element was locked by our code so
\r
1291 // it'll be editable by the editor functions (#6046).
\r
1292 var readonly = element.getAttribute( 'contenteditable' ) == 'false';
\r
1295 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1296 element.setAttribute( 'contenteditable', false );
\r
1304 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1305 if ( CKEDITOR.env.gecko )
\r
1309 var body = document.body;
\r
1312 window.addEventListener( 'load', arguments.callee, false );
\r
1315 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1316 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1317 'event.persisted && (function(){' +
\r
1318 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1319 'for ( var i in allInstances )' +
\r
1321 ' editor = allInstances[ i ];' +
\r
1322 ' doc = editor.document;' +
\r
1325 ' doc.$.designMode = "off";' +
\r
1326 ' doc.$.designMode = "on";' +
\r
1337 * Disables the ability of resize objects (image and tables) in the editing
\r
1342 * config.disableObjectResizing = true;
\r
1344 CKEDITOR.config.disableObjectResizing = false;
\r
1347 * Disables the "table tools" offered natively by the browser (currently
\r
1348 * Firefox only) to make quick table editing operations, like adding or
\r
1349 * deleting rows and columns.
\r
1353 * config.disableNativeTableHandles = false;
\r
1355 CKEDITOR.config.disableNativeTableHandles = true;
\r
1358 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1360 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1361 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1362 * is enabled or you're simply not using the context menu plugin.
\r
1367 * config.disableNativeSpellChecker = false;
\r
1369 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1372 * Whether the editor must output an empty value ("") if it's contents is made
\r
1373 * by an empty paragraph only.
\r
1377 * config.ignoreEmptyParagraph = false;
\r
1379 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1382 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1383 * @name CKEDITOR.editor#dataReady
\r
1388 * Whether automatically create wrapping blocks around inline contents inside document body,
\r
1389 * this helps to ensure the integrality of the block enter mode.
\r
1390 * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.
\r
1391 * @name CKEDITOR.config.autoParagraph
\r
1396 * config.autoParagraph = false;
\r
1400 * Fired when some elements are added to the document
\r
1401 * @name CKEDITOR.editor#ariaWidget
\r
1403 * @param {Object} element The element being added
\r