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
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 // Since the insertion might happen from within dialog or menu
\r
34 // where the editor selection might be locked at the moment,
\r
35 // update the locked selection.
\r
36 var selection = this.getSelection(),
\r
37 selIsLocked = selection.isLocked;
\r
39 selIsLocked && selection.unlock();
\r
41 this.fire( 'saveSnapshot' );
\r
43 insertFunc.call( this, evt.data );
\r
45 selIsLocked && this.getSelection().lock();
\r
47 // Save snaps after the whole execution completed.
\r
48 // This's a workaround for make DOM modification's happened after
\r
49 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
51 CKEDITOR.tools.setTimeout( function()
\r
53 this.fire( 'saveSnapshot' );
\r
59 function doInsertHtml( data )
\r
61 if ( this.dataProcessor )
\r
62 data = this.dataProcessor.toHtml( data );
\r
67 // HTML insertion only considers the first range.
\r
68 var selection = this.getSelection(),
\r
69 range = selection.getRanges()[ 0 ];
\r
71 if ( range.checkReadOnly() )
\r
74 // Opera: force block splitting when pasted content contains block. (#7801)
\r
75 if ( CKEDITOR.env.opera )
\r
77 var path = new CKEDITOR.dom.elementPath( range.startContainer );
\r
80 var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;
\r
81 for ( var i = 0, count = nodes.length; i < count; i++ )
\r
83 if ( nodes[ i ]._.isBlockLike )
\r
85 range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
86 range.insertNode( range.document.createText( '' ) );
\r
94 if ( CKEDITOR.env.ie )
\r
96 var $sel = selection.getNative();
\r
98 // Delete control selections to avoid IE bugs on pasteHTML.
\r
99 if ( $sel.type == 'Control' )
\r
101 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
103 // Due to IE bugs on handling contenteditable=false blocks
\r
104 // (#6005), we need to make some checks and eventually
\r
105 // delete the selection first.
\r
107 range = selection.getRanges()[ 0 ];
\r
108 var endContainer = range && range.endContainer;
\r
110 if ( endContainer &&
\r
111 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
112 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
113 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
115 range.setEndAfter( range.endContainer );
\r
116 range.deleteContents();
\r
120 $sel.createRange().pasteHTML( data );
\r
123 this.document.$.execCommand( 'inserthtml', false, data );
\r
125 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
126 if ( CKEDITOR.env.webkit )
\r
128 selection = this.getSelection();
\r
129 selection.scrollIntoView();
\r
133 function doInsertText( text )
\r
135 var selection = this.getSelection(),
\r
136 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
137 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
138 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
140 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
142 // Convert leading and trailing whitespaces into
\r
143 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
145 if ( match.length == 1 ) // one space, preserve it
\r
147 else if ( !offset ) // beginning of block
\r
148 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
149 else // end of block
\r
150 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
153 // Convert subsequent whitespaces into
\r
154 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
156 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
159 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
161 // Two line-breaks create one paragraph.
\r
162 if ( !isEnterBrMode )
\r
164 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
165 function( match, group1, text )
\r
167 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
171 // One <br> per line-break.
\r
172 html = html.replace( /\n/g, '<br>' );
\r
174 // Compensate padding <br> for non-IE.
\r
175 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
177 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
179 return CKEDITOR.tools.repeat( match, 2 );
\r
183 // Inline styles have to be inherited in Firefox.
\r
184 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
186 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
189 for ( var i = 0; i < path.elements.length; i++ )
\r
191 var tag = path.elements[ i ].getName();
\r
192 if ( tag in CKEDITOR.dtd.$inline )
\r
193 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
194 else if ( tag in CKEDITOR.dtd.$block )
\r
198 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
199 html = context.join( '' ) + html;
\r
202 doInsertHtml.call( this, html );
\r
205 function doInsertElement( element )
\r
207 var selection = this.getSelection(),
\r
208 ranges = selection.getRanges(),
\r
209 elementName = element.getName(),
\r
210 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
212 var selIsLocked = selection.isLocked;
\r
215 selection.unlock();
\r
217 var range, clone, lastElement, bookmark;
\r
219 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
221 range = ranges[ i ];
\r
223 if ( !range.checkReadOnly() )
\r
225 // Remove the original contents, merge splitted nodes.
\r
226 range.deleteContents( 1 );
\r
228 clone = !i && element || element.clone( 1 );
\r
230 // If we're inserting a block at dtd-violated position, split
\r
231 // the parent blocks until we reach blockLimit.
\r
235 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
236 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
237 && !( dtd && dtd [ elementName ] ) )
\r
239 // Split up inline elements.
\r
240 if ( current.getName() in CKEDITOR.dtd.span )
\r
241 range.splitElement( current );
\r
242 // If we're in an empty block which indicate a new paragraph,
\r
243 // simply replace it with the inserting block.(#3664)
\r
244 else if ( range.checkStartOfBlock()
\r
245 && range.checkEndOfBlock() )
\r
247 range.setStartBefore( current );
\r
248 range.collapse( true );
\r
252 range.splitBlock();
\r
256 // Insert the new node.
\r
257 range.insertNode( clone );
\r
259 // Save the last element reference so we can make the
\r
260 // selection later.
\r
261 if ( !lastElement )
\r
262 lastElement = clone;
\r
268 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
270 // If we're inserting a block element immediatelly followed by
\r
271 // another block element, the selection must move there. (#3100,#5436)
\r
274 var next = lastElement.getNext( notWhitespaceEval ),
\r
275 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
277 // Check if it's a block element that accepts text.
\r
278 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
\r
279 range.moveToElementEditStart( next );
\r
283 selection.selectRanges( [ range ] );
\r
286 this.getSelection().lock();
\r
289 // DOM modification here should not bother dirty flag.(#4385)
\r
290 function restoreDirty( editor )
\r
292 if ( !editor.checkDirty() )
\r
293 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
296 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
297 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
299 function isNotEmpty( node )
\r
301 return isNotWhitespace( node ) && isNotBookmark( node );
\r
304 function isNbsp( node )
\r
306 return node.type == CKEDITOR.NODE_TEXT
\r
307 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
310 function restoreSelection( selection )
\r
312 if ( selection.isLocked )
\r
314 selection.unlock();
\r
315 setTimeout( function() { selection.lock(); }, 0 );
\r
319 function isBlankParagraph( block )
\r
321 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
324 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
326 // Gecko need a key event to 'wake up' the editing
\r
327 // ability when document is empty.(#3864, #5781)
\r
328 function activateEditing( editor )
\r
330 var win = editor.window,
\r
331 doc = editor.document,
\r
332 body = editor.document.getBody(),
\r
333 bodyFirstChild = body.getFirst(),
\r
334 bodyChildsNum = body.getChildren().count();
\r
336 if ( !bodyChildsNum
\r
337 || bodyChildsNum == 1
\r
338 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
339 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
\r
341 restoreDirty( editor );
\r
343 // Memorize scroll position to restore it later (#4472).
\r
344 var hostDocument = editor.element.getDocument();
\r
345 var hostDocumentElement = hostDocument.getDocumentElement();
\r
346 var scrollTop = hostDocumentElement.$.scrollTop;
\r
347 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
349 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
350 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
351 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
352 false, false, false, 0, 32 );
\r
353 doc.$.dispatchEvent( keyEventSimulate );
\r
355 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
356 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
358 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
359 bodyChildsNum && body.getFirst().remove();
\r
360 doc.getBody().appendBogus();
\r
361 var nativeRange = new CKEDITOR.dom.range( doc );
\r
362 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
363 nativeRange.select();
\r
368 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
369 * non-exitable-block by padding extra br.(#3189)
\r
371 function onSelectionChangeFixBody( evt )
\r
373 var editor = evt.editor,
\r
374 path = evt.data.path,
\r
375 blockLimit = path.blockLimit,
\r
376 selection = evt.data.selection,
\r
377 range = selection.getRanges()[0],
\r
378 body = editor.document.getBody(),
\r
379 enterMode = editor.config.enterMode;
\r
381 if ( CKEDITOR.env.gecko )
\r
383 activateEditing( editor );
\r
385 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
386 var pathBlock = path.block || path.blockLimit,
\r
387 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
389 // Check some specialities of the current path block:
\r
390 // 1. It is really displayed as block; (#7221)
\r
391 // 2. It doesn't end with one inner block; (#7467)
\r
392 // 3. It doesn't have bogus br yet.
\r
394 && pathBlock.isBlockBoundary()
\r
395 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
\r
396 && !pathBlock.is( 'pre' )
\r
397 && !pathBlock.getBogus() )
\r
399 pathBlock.appendBogus();
\r
403 // When we're in block enter mode, a new paragraph will be established
\r
404 // to encapsulate inline contents right under body. (#3657)
\r
405 if ( editor.config.autoParagraph !== false
\r
406 && enterMode != CKEDITOR.ENTER_BR
\r
408 && blockLimit.getName() == 'body'
\r
411 var fixedBlock = range.fixBlock( true,
\r
412 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
414 // For IE, we should remove any filler node which was introduced before.
\r
415 if ( CKEDITOR.env.ie )
\r
417 var first = fixedBlock.getFirst( isNotEmpty );
\r
418 first && isNbsp( first ) && first.remove();
\r
421 // If the fixed block is actually blank and is already followed by an exitable blank
\r
422 // block, we should revert the fix and move into the existed one. (#3684)
\r
423 if ( isBlankParagraph( fixedBlock ) )
\r
425 var element = fixedBlock.getNext( isNotWhitespace );
\r
427 element.type == CKEDITOR.NODE_ELEMENT &&
\r
428 !nonEditable( element ) )
\r
430 range.moveToElementEditStart( element );
\r
431 fixedBlock.remove();
\r
435 element = fixedBlock.getPrevious( isNotWhitespace );
\r
437 element.type == CKEDITOR.NODE_ELEMENT &&
\r
438 !nonEditable( element ) )
\r
440 range.moveToElementEditEnd( element );
\r
441 fixedBlock.remove();
\r
447 // Cancel this selection change in favor of the next (correct). (#6811)
\r
451 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
\r
452 // at the end of document, makes it unable to continue adding content, we have to make this
\r
453 // easier by opening an new empty paragraph.
\r
454 var testRange = new CKEDITOR.dom.range( editor.document );
\r
455 testRange.moveToElementEditEnd( editor.document.getBody() );
\r
456 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
\r
457 if ( !testPath.blockLimit.is( 'body') )
\r
460 if ( enterMode != CKEDITOR.ENTER_BR )
\r
461 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
463 paddingBlock = body;
\r
465 if ( !CKEDITOR.env.ie )
\r
466 paddingBlock.appendBogus();
\r
470 CKEDITOR.plugins.add( 'wysiwygarea',
\r
472 requires : [ 'editingblock' ],
\r
474 init : function( editor )
\r
476 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )
\r
477 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
479 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
\r
481 var win = CKEDITOR.document.getWindow();
\r
482 var contentDomReadyHandler;
\r
483 editor.on( 'editingBlockReady', function()
\r
494 // Support for custom document.domain in IE.
\r
495 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
497 // Creates the iframe that holds the editable document.
\r
498 var createIFrame = function( data )
\r
504 'document.open();' +
\r
506 // The document domain must be set any time we
\r
507 // call document.open().
\r
508 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
510 'document.close();';
\r
512 // With IE, the custom domain has to be taken care at first,
\r
513 // for other browers, the 'src' attribute should be left empty to
\r
514 // trigger iframe's 'load' event.
\r
517 'javascript:void(0)' :
\r
519 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
523 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
524 ' style="width:100%;height:100%"' +
\r
525 ' frameBorder="0"' +
\r
526 ' title="' + frameLabel + '"' +
\r
527 ' src="' + src + '"' +
\r
528 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
529 ' allowTransparency="true"' +
\r
532 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
533 if ( document.location.protocol == 'chrome:' )
\r
534 CKEDITOR.event.useCapture = true;
\r
536 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
537 iframe.on( 'load', function( ev )
\r
540 ev.removeListener();
\r
542 var doc = iframe.getFrameDocument();
\r
545 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
548 // Reset adjustment back to default (#5689)
\r
549 if ( document.location.protocol == 'chrome:' )
\r
550 CKEDITOR.event.useCapture = false;
\r
552 mainElement.append( iframe );
\r
554 // Webkit: iframe size doesn't auto fit well. (#7360)
\r
555 if ( CKEDITOR.env.webkit )
\r
557 onResize = function()
\r
560 iframe.setSize( 'width', mainElement.getSize( 'width' ) );
\r
564 win.on( 'resize', onResize );
\r
568 // The script that launches the bootstrap logic on 'domReady', so the document
\r
569 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
570 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
571 var activationScript =
\r
572 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
573 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
574 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
577 // Editing area bootstrap code.
\r
578 function contentDomReady( domWindow )
\r
580 if ( !frameLoaded )
\r
584 editor.fire( 'ariaWidget', iframe );
\r
586 var domDocument = domWindow.document,
\r
587 body = domDocument.body;
\r
589 // Remove this script from the DOM.
\r
590 var script = domDocument.getElementById( "cke_actscrpt" );
\r
591 script && script.parentNode.removeChild( script );
\r
593 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
595 var editable = !editor.readOnly;
\r
597 if ( CKEDITOR.env.ie )
\r
599 // Don't display the focus border.
\r
600 body.hideFocus = true;
\r
602 // Disable and re-enable the body to avoid IE from
\r
603 // taking the editing focus at startup. (#141 / #523)
\r
604 body.disabled = true;
\r
605 body.contentEditable = editable;
\r
606 body.removeAttribute( 'disabled' );
\r
610 // Avoid opening design mode in a frame window thread,
\r
611 // which will cause host page scrolling.(#4397)
\r
612 setTimeout( function()
\r
614 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
615 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
616 || CKEDITOR.env.opera )
\r
617 domDocument.$.body.contentEditable = editable;
\r
618 else if ( CKEDITOR.env.webkit )
\r
619 domDocument.$.body.parentNode.contentEditable = editable;
\r
621 domDocument.$.designMode = editable? 'off' : 'on';
\r
625 editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
627 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
628 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
630 editable && domDocument.on( 'dblclick', function( evt )
\r
632 var element = evt.data.getTarget(),
\r
633 data = { element : element, dialog : '' };
\r
634 editor.fire( 'doubleclick', data );
\r
635 data.dialog && editor.openDialog( data.dialog );
\r
638 // Prevent automatic submission in IE #6336
\r
639 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
641 var element = evt.data.getTarget();
\r
642 if ( element.is( 'input' ) )
\r
644 var type = element.getAttribute( 'type' );
\r
645 if ( type == 'submit' || type == 'reset' )
\r
646 evt.data.preventDefault();
\r
650 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
651 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
653 domDocument.on( 'mousedown', function( ev )
\r
655 var control = ev.data.getTarget();
\r
656 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
657 editor.getSelection().selectElement( control );
\r
661 if ( CKEDITOR.env.gecko )
\r
663 domDocument.on( 'mouseup', function( ev )
\r
665 if ( ev.data.$.button == 2 )
\r
667 var target = ev.data.getTarget();
\r
669 // Prevent right click from selecting an empty block even
\r
670 // when selection is anchored inside it. (#5845)
\r
671 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
673 var range = new CKEDITOR.dom.range( domDocument );
\r
674 range.moveToElementEditStart( target );
\r
675 range.select( true );
\r
681 // Prevent the browser opening links in read-only blocks. (#6032)
\r
682 domDocument.on( 'click', function( ev )
\r
685 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
686 ev.preventDefault();
\r
689 // Webkit: avoid from editing form control elements content.
\r
690 if ( CKEDITOR.env.webkit )
\r
692 // Mark that cursor will right blinking (#7113).
\r
693 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
694 // Prevent from tick checkbox/radiobox/select
\r
695 domDocument.on( 'click', function( ev )
\r
697 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
698 ev.data.preventDefault();
\r
701 // Prevent from editig textfield/textarea value.
\r
702 domDocument.on( 'mouseup', function( ev )
\r
704 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
705 ev.data.preventDefault();
\r
709 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
710 focusTarget.on( 'blur', function()
\r
712 editor.focusManager.blur();
\r
717 focusTarget.on( 'focus', function()
\r
719 var doc = editor.document;
\r
721 if ( editable && CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
723 else if ( CKEDITOR.env.opera )
\r
724 doc.getBody().focus();
\r
725 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
726 else if ( CKEDITOR.env.webkit )
\r
730 editor.document.getDocumentElement().focus();
\r
735 editor.focusManager.focus();
\r
738 var keystrokeHandler = editor.keystrokeHandler;
\r
739 // Prevent backspace from navigating off the page.
\r
740 keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;
\r
741 keystrokeHandler.attach( domDocument );
\r
743 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
744 // Override keystroke behaviors.
\r
745 editable && domDocument.on( 'keydown', function( evt )
\r
747 var keyCode = evt.data.getKeystroke();
\r
749 // Backspace OR Delete.
\r
750 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
752 var sel = editor.getSelection(),
\r
753 selected = sel.getSelectedElement(),
\r
754 range = sel.getRanges()[ 0 ];
\r
756 // Override keystrokes which should have deletion behavior
\r
757 // on fully selected element . (#4047) (#7645)
\r
760 // Make undo snapshot.
\r
761 editor.fire( 'saveSnapshot' );
\r
763 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
764 // break up the selection, safely manage it here. (#4795)
\r
765 range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );
\r
766 // Remove the control manually.
\r
770 editor.fire( 'saveSnapshot' );
\r
772 evt.data.preventDefault();
\r
777 // PageUp OR PageDown
\r
778 if ( keyCode == 33 || keyCode == 34 )
\r
780 if ( CKEDITOR.env.gecko )
\r
782 var body = domDocument.getBody();
\r
784 // Page up/down cause editor selection to leak
\r
785 // outside of editable thus we try to intercept
\r
786 // the behavior, while it affects only happen
\r
787 // when editor contents are not overflowed. (#7955)
\r
788 if ( domWindow.$.innerHeight > body.$.offsetHeight )
\r
790 range = new CKEDITOR.dom.range( domDocument );
\r
791 range[ keyCode == 33 ? 'moveToElementEditStart' : 'moveToElementEditEnd']( body );
\r
793 evt.data.preventDefault();
\r
800 // PageUp/PageDown scrolling is broken in document
\r
801 // with standard doctype, manually fix it. (#4736)
\r
802 if ( CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat' )
\r
804 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
805 domDocument.on( 'keydown', function( evt )
\r
807 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
809 setTimeout( function ()
\r
811 editor.getSelection().scrollIntoView();
\r
817 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
818 if ( CKEDITOR.env.ie && editor.config.enterMode != CKEDITOR.ENTER_P )
\r
820 domDocument.on( 'selectionchange', function()
\r
822 var body = domDocument.getBody(),
\r
823 sel = editor.getSelection(),
\r
824 range = sel && sel.getRanges()[ 0 ];
\r
826 if ( range && body.getHtml().match( /^<p> <\/p>$/i )
\r
827 && range.startContainer.equals( body ) )
\r
829 // Avoid the ambiguity from a real user cursor position.
\r
830 setTimeout( function ()
\r
832 range = editor.getSelection().getRanges()[ 0 ];
\r
833 if ( !range.startContainer.equals ( 'body' ) )
\r
835 body.getFirst().remove( 1 );
\r
836 range.moveToElementEditEnd( body );
\r
844 // Adds the document body as a context menu target.
\r
845 if ( editor.contextMenu )
\r
846 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
848 setTimeout( function()
\r
850 editor.fire( 'contentDom' );
\r
854 editor.mode = 'wysiwyg';
\r
855 editor.fire( 'mode', { previousMode : editor._.previousMode } );
\r
859 isLoadingData = false;
\r
861 if ( isPendingFocus )
\r
864 isPendingFocus = false;
\r
866 setTimeout( function()
\r
868 editor.fire( 'dataReady' );
\r
871 // IE, Opera and Safari may not support it and throw errors.
\r
872 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
873 if ( editor.config.disableObjectResizing )
\r
877 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
881 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
882 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
884 evt.data.preventDefault();
\r
890 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
891 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
894 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
895 * fix the problem. :(
\r
897 if ( CKEDITOR.env.ie )
\r
899 setTimeout( function()
\r
901 if ( editor.document )
\r
903 var $body = editor.document.$.body;
\r
904 $body.runtimeStyle.marginBottom = '0px';
\r
905 $body.runtimeStyle.marginBottom = '';
\r
913 editor.addMode( 'wysiwyg',
\r
915 load : function( holderElement, data, isSnapshot )
\r
917 mainElement = holderElement;
\r
919 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
920 holderElement.setStyle( 'position', 'relative' );
\r
922 // The editor data "may be dirty" after this
\r
924 editor.mayBeDirty = true;
\r
929 this.loadSnapshotData( data );
\r
931 this.loadData( data );
\r
934 loadData : function( data )
\r
936 isLoadingData = true;
\r
937 editor._.dataStore = { id : 1 };
\r
939 var config = editor.config,
\r
940 fullPage = config.fullPage,
\r
941 docType = config.docType;
\r
943 // Build the additional stuff to be included into <head>.
\r
945 '<style type="text/css" data-cke-temp="1">' +
\r
946 editor._.styles.join( '\n' ) +
\r
949 !fullPage && ( headExtra =
\r
950 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
953 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
957 // Search and sweep out the doctype declaration.
\r
958 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
960 editor.docType = docType = match;
\r
962 }).replace( /<\?xml\s[^\?]*\?>/i, function( match )
\r
964 editor.xmlDeclaration = match;
\r
969 // Get the HTML version of the data.
\r
970 if ( editor.dataProcessor )
\r
971 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
975 // Check if the <body> tag is available.
\r
976 if ( !(/<body[\s|>]/).test( data ) )
\r
977 data = '<body>' + data;
\r
979 // Check if the <html> tag is available.
\r
980 if ( !(/<html[\s|>]/).test( data ) )
\r
981 data = '<html>' + data + '</html>';
\r
983 // Check if the <head> tag is available.
\r
984 if ( !(/<head[\s|>]/).test( data ) )
\r
985 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
986 else if ( !(/<title[\s|>]/).test( data ) )
\r
987 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
989 // The base must be the first tag in the HEAD, e.g. to get relative
\r
990 // links on styles.
\r
991 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
993 // Inject the extra stuff into <head>.
\r
994 // Attention: do not change it before testing it well. (V2)
\r
995 // This is tricky... if the head ends with <meta ... content type>,
\r
996 // Firefox will break. But, it works if we place our extra stuff as
\r
997 // the last elements in the HEAD.
\r
998 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
1000 // Add the DOCTYPE back to it.
\r
1001 data = docType + data;
\r
1007 '<html dir="' + config.contentsLangDirection + '"' +
\r
1008 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
1010 '<title>' + frameLabel + '</title>' +
\r
1014 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
1015 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1021 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1022 if ( CKEDITOR.env.gecko )
\r
1023 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1025 data += activationScript;
\r
1028 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1030 createIFrame( data );
\r
1033 getData : function()
\r
1035 var config = editor.config,
\r
1036 fullPage = config.fullPage,
\r
1037 docType = fullPage && editor.docType,
\r
1038 xmlDeclaration = fullPage && editor.xmlDeclaration,
\r
1039 doc = iframe.getFrameDocument();
\r
1041 var data = fullPage
\r
1042 ? doc.getDocumentElement().getOuterHtml()
\r
1043 : doc.getBody().getHtml();
\r
1045 // BR at the end of document is bogus node for Mozilla. (#5293).
\r
1046 if ( CKEDITOR.env.gecko )
\r
1047 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
\r
1049 if ( editor.dataProcessor )
\r
1050 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
1052 // Reset empty if the document contains only one empty paragraph.
\r
1053 if ( config.ignoreEmptyParagraph )
\r
1054 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
1056 if ( xmlDeclaration )
\r
1057 data = xmlDeclaration + '\n' + data;
\r
1059 data = docType + '\n' + data;
\r
1064 getSnapshotData : function()
\r
1066 return iframe.getFrameDocument().getBody().getHtml();
\r
1069 loadSnapshotData : function( data )
\r
1071 iframe.getFrameDocument().getBody().setHtml( data );
\r
1074 onDispose : function()
\r
1076 if ( !editor.document )
\r
1079 editor.document.getDocumentElement().clearCustomData();
\r
1080 editor.document.getBody().clearCustomData();
\r
1082 editor.window.clearCustomData();
\r
1083 editor.document.clearCustomData();
\r
1085 iframe.clearCustomData();
\r
1088 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1089 * editing area would break IE7/8's selection system, we have to put the editing
\r
1090 * iframe offline first. (#3812 and #5441)
\r
1095 unload : function( holderElement )
\r
1100 win.removeListener( 'resize', onResize );
\r
1102 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1104 editor.fire( 'contentDomUnload' );
\r
1107 focus : function()
\r
1109 var win = editor.window;
\r
1111 if ( isLoadingData )
\r
1112 isPendingFocus = true;
\r
1115 // AIR needs a while to focus when moving from a link.
\r
1116 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1117 editor.selectionChange();
\r
1122 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1123 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1124 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1125 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1126 editor.on( 'selectionChange', function( evt )
\r
1128 if ( editor.readOnly )
\r
1131 var sel = editor.getSelection();
\r
1132 // Do it only when selection is not locked. (#8222)
\r
1133 if ( sel && !sel.isLocked )
\r
1135 var isDirty = editor.checkDirty();
\r
1136 editor.fire( 'saveSnapshot', { contentOnly : 1 } );
\r
1137 onSelectionChangeFixBody.call( this, evt );
\r
1138 editor.fire( 'updateSnapshot' );
\r
1139 !isDirty && editor.resetDirty();
\r
1142 }, null, null, 1 );
\r
1146 // Setting voice label as window title, backup the original one
\r
1147 // and restore it before running into use.
\r
1148 editor.on( 'contentDom', function()
\r
1150 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1151 title.data( 'cke-title', editor.document.$.title );
\r
1152 editor.document.$.title = frameLabel;
\r
1155 editor.on( 'readOnly', function()
\r
1157 if ( editor.mode == 'wysiwyg' )
\r
1159 // Symply reload the wysiwyg area. It'll take care of read-only.
\r
1160 var wysiwyg = editor.getMode();
\r
1161 wysiwyg.loadData( wysiwyg.getData() );
\r
1165 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1166 // on element unless it has layout. (#5562)
\r
1167 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1169 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1171 var selectors = [];
\r
1172 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1173 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1174 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1176 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1177 else if ( CKEDITOR.env.gecko )
\r
1179 editor.addCss( 'html { height: 100% !important; }' );
\r
1180 editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1; width : 24px; height : 24px; }' );
\r
1182 // Remove the margin to avoid mouse confusion. (#8835)
\r
1183 else if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 && editor.config.contentsLangDirection == 'ltr' )
\r
1184 editor.addCss( 'body{margin-right:0;}' );
\r
1186 /* #3658: [IE6] Editor document has horizontal scrollbar on long lines
\r
1187 To prevent this misbehavior, we show the scrollbar always */
\r
1188 /* #6341: The text cursor must be set on the editor area. */
\r
1189 /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/
\r
1190 editor.addCss( 'html { _overflow-y: scroll; cursor: text; *cursor:auto;}' );
\r
1191 // Use correct cursor for these elements
\r
1192 editor.addCss( 'img, input, textarea { cursor: default;}' );
\r
1194 // Switch on design mode for a short while and close it after then.
\r
1195 function blinkCursor( retry )
\r
1197 if ( editor.readOnly )
\r
1200 CKEDITOR.tools.tryThese(
\r
1203 editor.document.$.designMode = 'on';
\r
1204 setTimeout( function()
\r
1206 editor.document.$.designMode = 'off';
\r
1207 if ( CKEDITOR.currentInstance == editor )
\r
1208 editor.document.getBody().focus();
\r
1213 // The above call is known to fail when parent DOM
\r
1214 // tree layout changes may break design mode. (#5782)
\r
1215 // Refresh the 'contentEditable' is a cue to this.
\r
1216 editor.document.$.designMode = 'off';
\r
1217 var body = editor.document.getBody();
\r
1218 body.setAttribute( 'contentEditable', false );
\r
1219 body.setAttribute( 'contentEditable', true );
\r
1220 // Try it again once..
\r
1221 !retry && blinkCursor( 1 );
\r
1225 // Disable form elements editing mode provided by some browers. (#5746)
\r
1226 editor.on( 'insertElement', function ( evt )
\r
1228 var element = evt.data;
\r
1229 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1230 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1232 // We should flag that the element was locked by our code so
\r
1233 // it'll be editable by the editor functions (#6046).
\r
1234 var readonly = element.getAttribute( 'contenteditable' ) == 'false';
\r
1237 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1238 element.setAttribute( 'contenteditable', false );
\r
1246 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1247 if ( CKEDITOR.env.gecko )
\r
1251 var body = document.body;
\r
1254 window.addEventListener( 'load', arguments.callee, false );
\r
1257 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1258 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1259 'event.persisted && (function(){' +
\r
1260 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1261 'for ( var i in allInstances )' +
\r
1263 ' editor = allInstances[ i ];' +
\r
1264 ' doc = editor.document;' +
\r
1267 ' doc.$.designMode = "off";' +
\r
1268 ' doc.$.designMode = "on";' +
\r
1279 * Disables the ability of resize objects (image and tables) in the editing
\r
1284 * config.disableObjectResizing = true;
\r
1286 CKEDITOR.config.disableObjectResizing = false;
\r
1289 * Disables the "table tools" offered natively by the browser (currently
\r
1290 * Firefox only) to make quick table editing operations, like adding or
\r
1291 * deleting rows and columns.
\r
1295 * config.disableNativeTableHandles = false;
\r
1297 CKEDITOR.config.disableNativeTableHandles = true;
\r
1300 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1302 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1303 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1304 * is enabled or you're simply not using the context menu plugin.
\r
1309 * config.disableNativeSpellChecker = false;
\r
1311 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1314 * Whether the editor must output an empty value ("") if it's contents is made
\r
1315 * by an empty paragraph only.
\r
1319 * config.ignoreEmptyParagraph = false;
\r
1321 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1324 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1325 * @name CKEDITOR.editor#dataReady
\r
1330 * Whether automatically create wrapping blocks around inline contents inside document body,
\r
1331 * this helps to ensure the integrality of the block enter mode.
\r
1332 * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.
\r
1333 * @name CKEDITOR.config.autoParagraph
\r
1338 * config.autoParagraph = false;
\r
1342 * Fired when some elements are added to the document
\r
1343 * @name CKEDITOR.editor#ariaWidget
\r
1345 * @param {Object} element The element being added
\r