2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview The "wysiwygarea" plugin. It registers the "wysiwyg" editing
\r
8 * mode, which handles the main editing area space.
\r
13 // Matching an empty paragraph at the end of document.
\r
14 var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center|pre)[^>]*>\s*(?:<br[^>]*>| |\u00A0| )?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;
\r
16 var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );
\r
18 // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)
\r
19 function nonEditable( element )
\r
21 return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];
\r
25 function onInsert( insertFunc )
\r
27 return function( evt )
\r
29 if ( this.mode == 'wysiwyg' )
\r
33 this.fire( 'saveSnapshot' );
\r
35 insertFunc.call( this, evt.data );
\r
37 // Save snaps after the whole execution completed.
\r
38 // This's a workaround for make DOM modification's happened after
\r
39 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
\r
41 CKEDITOR.tools.setTimeout( function()
\r
43 this.fire( 'saveSnapshot' );
\r
49 function doInsertHtml( data )
\r
51 if ( this.dataProcessor )
\r
52 data = this.dataProcessor.toHtml( data );
\r
57 // HTML insertion only considers the first range.
\r
58 var selection = this.getSelection(),
\r
59 range = selection.getRanges()[ 0 ];
\r
61 if ( range.checkReadOnly() )
\r
64 // Opera: force block splitting when pasted content contains block. (#7801)
\r
65 if ( CKEDITOR.env.opera )
\r
67 var path = new CKEDITOR.dom.elementPath( range.startContainer );
\r
70 var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;
\r
71 for ( var i = 0, count = nodes.length; i < count; i++ )
\r
73 if ( nodes[ i ]._.isBlockLike )
\r
75 range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
76 range.insertNode( range.document.createText( '' ) );
\r
84 if ( CKEDITOR.env.ie )
\r
86 var selIsLocked = selection.isLocked;
\r
91 var $sel = selection.getNative();
\r
93 // Delete control selections to avoid IE bugs on pasteHTML.
\r
94 if ( $sel.type == 'Control' )
\r
96 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
\r
98 // Due to IE bugs on handling contenteditable=false blocks
\r
99 // (#6005), we need to make some checks and eventually
\r
100 // delete the selection first.
\r
102 range = selection.getRanges()[ 0 ];
\r
103 var endContainer = range && range.endContainer;
\r
105 if ( endContainer &&
\r
106 endContainer.type == CKEDITOR.NODE_ELEMENT &&
\r
107 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
\r
108 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
\r
110 range.setEndAfter( range.endContainer );
\r
111 range.deleteContents();
\r
115 $sel.createRange().pasteHTML( data );
\r
118 this.getSelection().lock();
\r
121 this.document.$.execCommand( 'inserthtml', false, data );
\r
123 // Webkit does not scroll to the cursor position after pasting (#5558)
\r
124 if ( CKEDITOR.env.webkit )
\r
126 selection = this.getSelection();
\r
127 selection.scrollIntoView();
\r
131 function doInsertText( text )
\r
133 var selection = this.getSelection(),
\r
134 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
\r
135 CKEDITOR.ENTER_BR : this.config.enterMode,
\r
136 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
\r
138 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
\r
140 // Convert leading and trailing whitespaces into
\r
141 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
\r
143 if ( match.length == 1 ) // one space, preserve it
\r
145 else if ( !offset ) // beginning of block
\r
146 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
147 else // end of block
\r
148 return ' ' + CKEDITOR.tools.repeat( ' ', match.length - 1 );
\r
151 // Convert subsequent whitespaces into
\r
152 html = html.replace( /[ \t]{2,}/g, function ( match )
\r
154 return CKEDITOR.tools.repeat( ' ', match.length - 1 ) + ' ';
\r
157 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
\r
159 // Two line-breaks create one paragraph.
\r
160 if ( !isEnterBrMode )
\r
162 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
\r
163 function( match, group1, text )
\r
165 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
\r
169 // One <br> per line-break.
\r
170 html = html.replace( /\n/g, '<br>' );
\r
172 // Compensate padding <br> for non-IE.
\r
173 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
\r
175 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
\r
177 return CKEDITOR.tools.repeat( match, 2 );
\r
181 // Inline styles have to be inherited in Firefox.
\r
182 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
\r
184 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
\r
187 for ( var i = 0; i < path.elements.length; i++ )
\r
189 var tag = path.elements[ i ].getName();
\r
190 if ( tag in CKEDITOR.dtd.$inline )
\r
191 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
\r
192 else if ( tag in CKEDITOR.dtd.$block )
\r
196 // Reproduce the context by preceding the pasted HTML with opening inline tags.
\r
197 html = context.join( '' ) + html;
\r
200 doInsertHtml.call( this, html );
\r
203 function doInsertElement( element )
\r
205 var selection = this.getSelection(),
\r
206 ranges = selection.getRanges(),
\r
207 elementName = element.getName(),
\r
208 isBlock = CKEDITOR.dtd.$block[ elementName ];
\r
210 var selIsLocked = selection.isLocked;
\r
213 selection.unlock();
\r
215 var range, clone, lastElement, bookmark;
\r
217 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
\r
219 range = ranges[ i ];
\r
221 if ( !range.checkReadOnly() )
\r
223 // Remove the original contents, merge splitted nodes.
\r
224 range.deleteContents( 1 );
\r
226 clone = !i && element || element.clone( 1 );
\r
228 // If we're inserting a block at dtd-violated position, split
\r
229 // the parent blocks until we reach blockLimit.
\r
233 while ( ( current = range.getCommonAncestor( 0, 1 ) )
\r
234 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
\r
235 && !( dtd && dtd [ elementName ] ) )
\r
237 // Split up inline elements.
\r
238 if ( current.getName() in CKEDITOR.dtd.span )
\r
239 range.splitElement( current );
\r
240 // If we're in an empty block which indicate a new paragraph,
\r
241 // simply replace it with the inserting block.(#3664)
\r
242 else if ( range.checkStartOfBlock()
\r
243 && range.checkEndOfBlock() )
\r
245 range.setStartBefore( current );
\r
246 range.collapse( true );
\r
250 range.splitBlock();
\r
254 // Insert the new node.
\r
255 range.insertNode( clone );
\r
257 // Save the last element reference so we can make the
\r
258 // selection later.
\r
259 if ( !lastElement )
\r
260 lastElement = clone;
\r
266 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
\r
268 // If we're inserting a block element immediatelly followed by
\r
269 // another block element, the selection must move there. (#3100,#5436)
\r
272 var next = lastElement.getNext( notWhitespaceEval ),
\r
273 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
\r
275 // Check if it's a block element that accepts text.
\r
276 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
\r
277 range.moveToElementEditStart( next );
\r
281 selection.selectRanges( [ range ] );
\r
284 this.getSelection().lock();
\r
287 // DOM modification here should not bother dirty flag.(#4385)
\r
288 function restoreDirty( editor )
\r
290 if ( !editor.checkDirty() )
\r
291 setTimeout( function(){ editor.resetDirty(); }, 0 );
\r
294 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
\r
295 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
\r
297 function isNotEmpty( node )
\r
299 return isNotWhitespace( node ) && isNotBookmark( node );
\r
302 function isNbsp( node )
\r
304 return node.type == CKEDITOR.NODE_TEXT
\r
305 && CKEDITOR.tools.trim( node.getText() ).match( /^(?: |\xa0)$/ );
\r
308 function restoreSelection( selection )
\r
310 if ( selection.isLocked )
\r
312 selection.unlock();
\r
313 setTimeout( function() { selection.lock(); }, 0 );
\r
317 function isBlankParagraph( block )
\r
319 return block.getOuterHtml().match( emptyParagraphRegexp );
\r
322 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
\r
324 // Gecko need a key event to 'wake up' the editing
\r
325 // ability when document is empty.(#3864, #5781)
\r
326 function activateEditing( editor )
\r
328 var win = editor.window,
\r
329 doc = editor.document,
\r
330 body = editor.document.getBody(),
\r
331 bodyFirstChild = body.getFirst(),
\r
332 bodyChildsNum = body.getChildren().count();
\r
334 if ( !bodyChildsNum
\r
335 || bodyChildsNum == 1
\r
336 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
\r
337 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
\r
339 restoreDirty( editor );
\r
341 // Memorize scroll position to restore it later (#4472).
\r
342 var hostDocument = editor.element.getDocument();
\r
343 var hostDocumentElement = hostDocument.getDocumentElement();
\r
344 var scrollTop = hostDocumentElement.$.scrollTop;
\r
345 var scrollLeft = hostDocumentElement.$.scrollLeft;
\r
347 // Simulating keyboard character input by dispatching a keydown of white-space text.
\r
348 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
\r
349 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
\r
350 false, false, false, 0, 32 );
\r
351 doc.$.dispatchEvent( keyEventSimulate );
\r
353 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
\r
354 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
\r
356 // Restore the original document status by placing the cursor before a bogus br created (#5021).
\r
357 bodyChildsNum && body.getFirst().remove();
\r
358 doc.getBody().appendBogus();
\r
359 var nativeRange = new CKEDITOR.dom.range( doc );
\r
360 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
\r
361 nativeRange.select();
\r
366 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
\r
367 * non-exitable-block by padding extra br.(#3189)
\r
369 function onSelectionChangeFixBody( evt )
\r
371 var editor = evt.editor,
\r
372 path = evt.data.path,
\r
373 blockLimit = path.blockLimit,
\r
374 selection = evt.data.selection,
\r
375 range = selection.getRanges()[0],
\r
376 body = editor.document.getBody(),
\r
377 enterMode = editor.config.enterMode;
\r
379 if ( CKEDITOR.env.gecko )
\r
381 activateEditing( editor );
\r
383 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
\r
384 var pathBlock = path.block || path.blockLimit,
\r
385 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
\r
387 // Check some specialities of the current path block:
\r
388 // 1. It is really displayed as block; (#7221)
\r
389 // 2. It doesn't end with one inner block; (#7467)
\r
390 // 3. It doesn't have bogus br yet.
\r
392 && pathBlock.isBlockBoundary()
\r
393 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
\r
394 && !pathBlock.is( 'pre' )
\r
395 && !pathBlock.getBogus() )
\r
397 pathBlock.appendBogus();
\r
401 // When we're in block enter mode, a new paragraph will be established
\r
402 // to encapsulate inline contents right under body. (#3657)
\r
403 if ( editor.config.autoParagraph !== false
\r
404 && enterMode != CKEDITOR.ENTER_BR
\r
406 && blockLimit.getName() == 'body'
\r
409 var fixedBlock = range.fixBlock( true,
\r
410 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
\r
412 // For IE, we should remove any filler node which was introduced before.
\r
413 if ( CKEDITOR.env.ie )
\r
415 var first = fixedBlock.getFirst( isNotEmpty );
\r
416 first && isNbsp( first ) && first.remove();
\r
419 // If the fixed block is actually blank and is already followed by an exitable blank
\r
420 // block, we should revert the fix and move into the existed one. (#3684)
\r
421 if ( isBlankParagraph( fixedBlock ) )
\r
423 var element = fixedBlock.getNext( isNotWhitespace );
\r
425 element.type == CKEDITOR.NODE_ELEMENT &&
\r
426 !nonEditable( element ) )
\r
428 range.moveToElementEditStart( element );
\r
429 fixedBlock.remove();
\r
433 element = fixedBlock.getPrevious( isNotWhitespace );
\r
435 element.type == CKEDITOR.NODE_ELEMENT &&
\r
436 !nonEditable( element ) )
\r
438 range.moveToElementEditEnd( element );
\r
439 fixedBlock.remove();
\r
445 // Cancel this selection change in favor of the next (correct). (#6811)
\r
449 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
\r
450 // at the end of document, makes it unable to continue adding content, we have to make this
\r
451 // easier by opening an new empty paragraph.
\r
452 var testRange = new CKEDITOR.dom.range( editor.document );
\r
453 testRange.moveToElementEditEnd( editor.document.getBody() );
\r
454 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
\r
455 if ( !testPath.blockLimit.is( 'body') )
\r
458 if ( enterMode != CKEDITOR.ENTER_BR )
\r
459 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
\r
461 paddingBlock = body;
\r
463 if ( !CKEDITOR.env.ie )
\r
464 paddingBlock.appendBogus();
\r
468 CKEDITOR.plugins.add( 'wysiwygarea',
\r
470 requires : [ 'editingblock' ],
\r
472 init : function( editor )
\r
474 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )
\r
475 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
\r
477 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
\r
479 var contentDomReadyHandler;
\r
480 editor.on( 'editingBlockReady', function()
\r
490 // Support for custom document.domain in IE.
\r
491 var isCustomDomain = CKEDITOR.env.isCustomDomain();
\r
493 // Creates the iframe that holds the editable document.
\r
494 var createIFrame = function( data )
\r
500 'document.open();' +
\r
502 // The document domain must be set any time we
\r
503 // call document.open().
\r
504 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
506 'document.close();';
\r
508 // With IE, the custom domain has to be taken care at first,
\r
509 // for other browers, the 'src' attribute should be left empty to
\r
510 // trigger iframe's 'load' event.
\r
513 'javascript:void(0)' :
\r
515 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
\r
519 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
\r
520 ' style="width:100%;height:100%"' +
\r
521 ' frameBorder="0"' +
\r
522 ' title="' + frameLabel + '"' +
\r
523 ' src="' + src + '"' +
\r
524 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
\r
525 ' allowTransparency="true"' +
\r
528 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
\r
529 if ( document.location.protocol == 'chrome:' )
\r
530 CKEDITOR.event.useCapture = true;
\r
532 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
\r
533 iframe.on( 'load', function( ev )
\r
536 ev.removeListener();
\r
538 var doc = iframe.getFrameDocument();
\r
541 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
\r
544 // Reset adjustment back to default (#5689)
\r
545 if ( document.location.protocol == 'chrome:' )
\r
546 CKEDITOR.event.useCapture = false;
\r
548 mainElement.append( iframe );
\r
551 // The script that launches the bootstrap logic on 'domReady', so the document
\r
552 // is fully editable even before the editing iframe is fully loaded (#4455).
\r
553 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
\r
554 var activationScript =
\r
555 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
\r
556 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
\r
557 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
\r
560 // Editing area bootstrap code.
\r
561 function contentDomReady( domWindow )
\r
563 if ( !frameLoaded )
\r
567 editor.fire( 'ariaWidget', iframe );
\r
569 var domDocument = domWindow.document,
\r
570 body = domDocument.body;
\r
572 // Remove this script from the DOM.
\r
573 var script = domDocument.getElementById( "cke_actscrpt" );
\r
574 script && script.parentNode.removeChild( script );
\r
576 body.spellcheck = !editor.config.disableNativeSpellChecker;
\r
578 var editable = !editor.readOnly;
\r
580 if ( CKEDITOR.env.ie )
\r
582 // Don't display the focus border.
\r
583 body.hideFocus = true;
\r
585 // Disable and re-enable the body to avoid IE from
\r
586 // taking the editing focus at startup. (#141 / #523)
\r
587 body.disabled = true;
\r
588 body.contentEditable = editable;
\r
589 body.removeAttribute( 'disabled' );
\r
593 // Avoid opening design mode in a frame window thread,
\r
594 // which will cause host page scrolling.(#4397)
\r
595 setTimeout( function()
\r
597 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
\r
598 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
\r
599 || CKEDITOR.env.opera )
\r
600 domDocument.$.body.contentEditable = editable;
\r
601 else if ( CKEDITOR.env.webkit )
\r
602 domDocument.$.body.parentNode.contentEditable = editable;
\r
604 domDocument.$.designMode = editable? 'off' : 'on';
\r
608 editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
\r
610 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
\r
611 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
\r
613 editable && domDocument.on( 'dblclick', function( evt )
\r
615 var element = evt.data.getTarget(),
\r
616 data = { element : element, dialog : '' };
\r
617 editor.fire( 'doubleclick', data );
\r
618 data.dialog && editor.openDialog( data.dialog );
\r
621 // Prevent automatic submission in IE #6336
\r
622 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
\r
624 var element = evt.data.getTarget();
\r
625 if ( element.is( 'input' ) )
\r
627 var type = element.getAttribute( 'type' );
\r
628 if ( type == 'submit' || type == 'reset' )
\r
629 evt.data.preventDefault();
\r
633 // Gecko/Webkit need some help when selecting control type elements. (#3448)
\r
634 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
\r
636 domDocument.on( 'mousedown', function( ev )
\r
638 var control = ev.data.getTarget();
\r
639 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
\r
640 editor.getSelection().selectElement( control );
\r
644 if ( CKEDITOR.env.gecko )
\r
646 domDocument.on( 'mouseup', function( ev )
\r
648 if ( ev.data.$.button == 2 )
\r
650 var target = ev.data.getTarget();
\r
652 // Prevent right click from selecting an empty block even
\r
653 // when selection is anchored inside it. (#5845)
\r
654 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
\r
656 var range = new CKEDITOR.dom.range( domDocument );
\r
657 range.moveToElementEditStart( target );
\r
658 range.select( true );
\r
664 // Prevent the browser opening links in read-only blocks. (#6032)
\r
665 domDocument.on( 'click', function( ev )
\r
668 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
\r
669 ev.preventDefault();
\r
672 // Webkit: avoid from editing form control elements content.
\r
673 if ( CKEDITOR.env.webkit )
\r
675 // Mark that cursor will right blinking (#7113).
\r
676 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
\r
677 // Prevent from tick checkbox/radiobox/select
\r
678 domDocument.on( 'click', function( ev )
\r
680 if ( ev.data.getTarget().is( 'input', 'select' ) )
\r
681 ev.data.preventDefault();
\r
684 // Prevent from editig textfield/textarea value.
\r
685 domDocument.on( 'mouseup', function( ev )
\r
687 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
\r
688 ev.data.preventDefault();
\r
692 // IE standard compliant in editing frame doesn't focus the editor when
\r
693 // clicking outside actual content, manually apply the focus. (#1659)
\r
695 CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat'
\r
696 || CKEDITOR.env.gecko
\r
697 || CKEDITOR.env.opera )
\r
699 var htmlElement = domDocument.getDocumentElement();
\r
700 htmlElement.on( 'mousedown', function( evt )
\r
702 // Setting focus directly on editor doesn't work, we
\r
703 // have to use here a temporary element to 'redirect'
\r
705 if ( evt.data.getTarget().equals( htmlElement ) )
\r
707 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
709 focusGrabber.focus();
\r
714 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
\r
715 focusTarget.on( 'blur', function()
\r
717 editor.focusManager.blur();
\r
722 focusTarget.on( 'focus', function()
\r
724 var doc = editor.document;
\r
726 if ( editable && CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
\r
728 else if ( CKEDITOR.env.opera )
\r
729 doc.getBody().focus();
\r
730 // Webkit needs focus for the first time on the HTML element. (#6153)
\r
731 else if ( CKEDITOR.env.webkit )
\r
735 editor.document.getDocumentElement().focus();
\r
740 editor.focusManager.focus();
\r
743 var keystrokeHandler = editor.keystrokeHandler;
\r
744 // Prevent backspace from navigating off the page.
\r
745 keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;
\r
746 keystrokeHandler.attach( domDocument );
\r
748 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
\r
749 // Override keystroke behaviors.
\r
750 editable && domDocument.on( 'keydown', function( evt )
\r
752 var keyCode = evt.data.getKeystroke();
\r
754 // Backspace OR Delete.
\r
755 if ( keyCode in { 8 : 1, 46 : 1 } )
\r
757 var sel = editor.getSelection(),
\r
758 selected = sel.getSelectedElement(),
\r
759 range = sel.getRanges()[ 0 ];
\r
761 // Override keystrokes which should have deletion behavior
\r
762 // on fully selected element . (#4047) (#7645)
\r
765 // Make undo snapshot.
\r
766 editor.fire( 'saveSnapshot' );
\r
768 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
\r
769 // break up the selection, safely manage it here. (#4795)
\r
770 range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );
\r
771 // Remove the control manually.
\r
775 editor.fire( 'saveSnapshot' );
\r
777 evt.data.preventDefault();
\r
783 // PageUp/PageDown scrolling is broken in document
\r
784 // with standard doctype, manually fix it. (#4736)
\r
785 if ( CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat' )
\r
787 var pageUpDownKeys = { 33 : 1, 34 : 1 };
\r
788 domDocument.on( 'keydown', function( evt )
\r
790 if ( evt.data.getKeystroke() in pageUpDownKeys )
\r
792 setTimeout( function ()
\r
794 editor.getSelection().scrollIntoView();
\r
800 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
\r
801 if ( CKEDITOR.env.ie && editor.config.enterMode != CKEDITOR.ENTER_P )
\r
803 domDocument.on( 'selectionchange', function()
\r
805 var body = domDocument.getBody(),
\r
806 range = editor.getSelection().getRanges()[ 0 ];
\r
808 if ( body.getHtml().match( /^<p> <\/p>$/i )
\r
809 && range.startContainer.equals( body ) )
\r
811 // Avoid the ambiguity from a real user cursor position.
\r
812 setTimeout( function ()
\r
814 range = editor.getSelection().getRanges()[ 0 ];
\r
815 if ( !range.startContainer.equals ( 'body' ) )
\r
817 body.getFirst().remove( 1 );
\r
818 range.moveToElementEditEnd( body );
\r
826 // Adds the document body as a context menu target.
\r
827 if ( editor.contextMenu )
\r
828 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
\r
830 setTimeout( function()
\r
832 editor.fire( 'contentDom' );
\r
836 editor.mode = 'wysiwyg';
\r
837 editor.fire( 'mode', { previousMode : editor._.previousMode } );
\r
841 isLoadingData = false;
\r
843 if ( isPendingFocus )
\r
846 isPendingFocus = false;
\r
848 setTimeout( function()
\r
850 editor.fire( 'dataReady' );
\r
853 // IE, Opera and Safari may not support it and throw errors.
\r
854 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
\r
855 if ( editor.config.disableObjectResizing )
\r
859 editor.document.$.execCommand( 'enableObjectResizing', false, false );
\r
863 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
\r
864 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
\r
866 evt.data.preventDefault();
\r
872 * IE BUG: IE might have rendered the iframe with invisible contents.
\r
873 * (#3623). Push some inconsequential CSS style changes to force IE to
\r
876 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
\r
877 * fix the problem. :(
\r
879 if ( CKEDITOR.env.ie )
\r
881 setTimeout( function()
\r
883 if ( editor.document )
\r
885 var $body = editor.document.$.body;
\r
886 $body.runtimeStyle.marginBottom = '0px';
\r
887 $body.runtimeStyle.marginBottom = '';
\r
895 editor.addMode( 'wysiwyg',
\r
897 load : function( holderElement, data, isSnapshot )
\r
899 mainElement = holderElement;
\r
901 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
\r
902 holderElement.setStyle( 'position', 'relative' );
\r
904 // The editor data "may be dirty" after this
\r
906 editor.mayBeDirty = true;
\r
911 this.loadSnapshotData( data );
\r
913 this.loadData( data );
\r
916 loadData : function( data )
\r
918 isLoadingData = true;
\r
919 editor._.dataStore = { id : 1 };
\r
921 var config = editor.config,
\r
922 fullPage = config.fullPage,
\r
923 docType = config.docType;
\r
925 // Build the additional stuff to be included into <head>.
\r
927 '<style type="text/css" data-cke-temp="1">' +
\r
928 editor._.styles.join( '\n' ) +
\r
931 !fullPage && ( headExtra =
\r
932 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
\r
935 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
\r
939 // Search and sweep out the doctype declaration.
\r
940 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
\r
942 editor.docType = docType = match;
\r
944 }).replace( /<\?xml\s[^\?]*\?>/i, function( match )
\r
946 editor.xmlDeclaration = match;
\r
951 // Get the HTML version of the data.
\r
952 if ( editor.dataProcessor )
\r
953 data = editor.dataProcessor.toHtml( data, fixForBody );
\r
957 // Check if the <body> tag is available.
\r
958 if ( !(/<body[\s|>]/).test( data ) )
\r
959 data = '<body>' + data;
\r
961 // Check if the <html> tag is available.
\r
962 if ( !(/<html[\s|>]/).test( data ) )
\r
963 data = '<html>' + data + '</html>';
\r
965 // Check if the <head> tag is available.
\r
966 if ( !(/<head[\s|>]/).test( data ) )
\r
967 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
\r
968 else if ( !(/<title[\s|>]/).test( data ) )
\r
969 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
\r
971 // The base must be the first tag in the HEAD, e.g. to get relative
\r
972 // links on styles.
\r
973 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
\r
975 // Inject the extra stuff into <head>.
\r
976 // Attention: do not change it before testing it well. (V2)
\r
977 // This is tricky... if the head ends with <meta ... content type>,
\r
978 // Firefox will break. But, it works if we place our extra stuff as
\r
979 // the last elements in the HEAD.
\r
980 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
\r
982 // Add the DOCTYPE back to it.
\r
983 data = docType + data;
\r
989 '<html dir="' + config.contentsLangDirection + '"' +
\r
990 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
\r
992 '<title>' + frameLabel + '</title>' +
\r
996 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
\r
997 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
\r
1003 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
\r
1004 if ( CKEDITOR.env.gecko )
\r
1005 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
\r
1007 data += activationScript;
\r
1010 // The iframe is recreated on each call of setData, so we need to clear DOM objects
\r
1012 createIFrame( data );
\r
1015 getData : function()
\r
1017 var config = editor.config,
\r
1018 fullPage = config.fullPage,
\r
1019 docType = fullPage && editor.docType,
\r
1020 xmlDeclaration = fullPage && editor.xmlDeclaration,
\r
1021 doc = iframe.getFrameDocument();
\r
1023 var data = fullPage
\r
1024 ? doc.getDocumentElement().getOuterHtml()
\r
1025 : doc.getBody().getHtml();
\r
1027 // BR at the end of document is bogus node for Mozilla. (#5293).
\r
1028 if ( CKEDITOR.env.gecko )
\r
1029 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
\r
1031 if ( editor.dataProcessor )
\r
1032 data = editor.dataProcessor.toDataFormat( data, fixForBody );
\r
1034 // Reset empty if the document contains only one empty paragraph.
\r
1035 if ( config.ignoreEmptyParagraph )
\r
1036 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
\r
1038 if ( xmlDeclaration )
\r
1039 data = xmlDeclaration + '\n' + data;
\r
1041 data = docType + '\n' + data;
\r
1046 getSnapshotData : function()
\r
1048 return iframe.getFrameDocument().getBody().getHtml();
\r
1051 loadSnapshotData : function( data )
\r
1053 iframe.getFrameDocument().getBody().setHtml( data );
\r
1056 onDispose : function()
\r
1058 if ( !editor.document )
\r
1061 editor.document.getDocumentElement().clearCustomData();
\r
1062 editor.document.getBody().clearCustomData();
\r
1064 editor.window.clearCustomData();
\r
1065 editor.document.clearCustomData();
\r
1067 iframe.clearCustomData();
\r
1070 * IE BUG: When destroying editor DOM with the selection remains inside
\r
1071 * editing area would break IE7/8's selection system, we have to put the editing
\r
1072 * iframe offline first. (#3812 and #5441)
\r
1077 unload : function( holderElement )
\r
1081 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
\r
1083 editor.fire( 'contentDomUnload' );
\r
1086 focus : function()
\r
1088 var win = editor.window;
\r
1090 if ( isLoadingData )
\r
1091 isPendingFocus = true;
\r
1094 // AIR needs a while to focus when moving from a link.
\r
1095 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
\r
1096 editor.selectionChange();
\r
1101 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
\r
1102 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
\r
1103 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
\r
1104 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
\r
1105 editor.on( 'selectionChange', function( evt )
\r
1107 if ( editor.readOnly )
\r
1110 var sel = editor.getSelection();
\r
1111 // Do it only when selection is not locked. (#8222)
\r
1112 if ( sel && !sel.isLocked )
\r
1114 var isDirty = editor.checkDirty();
\r
1115 editor.fire( 'saveSnapshot', { contentOnly : 1 } );
\r
1116 onSelectionChangeFixBody.call( this, evt );
\r
1117 editor.fire( 'updateSnapshot' );
\r
1118 !isDirty && editor.resetDirty();
\r
1121 }, null, null, 1 );
\r
1125 // Setting voice label as window title, backup the original one
\r
1126 // and restore it before running into use.
\r
1127 editor.on( 'contentDom', function()
\r
1129 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
\r
1130 title.data( 'cke-title', editor.document.$.title );
\r
1131 editor.document.$.title = frameLabel;
\r
1134 editor.on( 'readOnly', function()
\r
1136 if ( editor.mode == 'wysiwyg' )
\r
1138 // Symply reload the wysiwyg area. It'll take care of read-only.
\r
1139 var wysiwyg = editor.getMode();
\r
1140 wysiwyg.loadData( wysiwyg.getData() );
\r
1144 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
\r
1145 // on element unless it has layout. (#5562)
\r
1146 if ( CKEDITOR.document.$.documentMode >= 8 )
\r
1148 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
\r
1150 var selectors = [];
\r
1151 for ( var tag in CKEDITOR.dtd.$removeEmpty )
\r
1152 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
\r
1153 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
\r
1155 // Set the HTML style to 100% to have the text cursor in affect (#6341)
\r
1156 else if ( CKEDITOR.env.gecko )
\r
1158 editor.addCss( 'html { height: 100% !important; }' );
\r
1159 editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1; width : 24px; height : 24px; }' );
\r
1162 /* #3658: [IE6] Editor document has horizontal scrollbar on long lines
\r
1163 To prevent this misbehavior, we show the scrollbar always */
\r
1164 /* #6341: The text cursor must be set on the editor area. */
\r
1165 /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/
\r
1166 editor.addCss( 'html { _overflow-y: scroll; cursor: text; *cursor:auto;}' );
\r
1167 // Use correct cursor for these elements
\r
1168 editor.addCss( 'img, input, textarea { cursor: default;}' );
\r
1170 // Switch on design mode for a short while and close it after then.
\r
1171 function blinkCursor( retry )
\r
1173 if ( editor.readOnly )
\r
1176 CKEDITOR.tools.tryThese(
\r
1179 editor.document.$.designMode = 'on';
\r
1180 setTimeout( function()
\r
1182 editor.document.$.designMode = 'off';
\r
1183 if ( CKEDITOR.currentInstance == editor )
\r
1184 editor.document.getBody().focus();
\r
1189 // The above call is known to fail when parent DOM
\r
1190 // tree layout changes may break design mode. (#5782)
\r
1191 // Refresh the 'contentEditable' is a cue to this.
\r
1192 editor.document.$.designMode = 'off';
\r
1193 var body = editor.document.getBody();
\r
1194 body.setAttribute( 'contentEditable', false );
\r
1195 body.setAttribute( 'contentEditable', true );
\r
1196 // Try it again once..
\r
1197 !retry && blinkCursor( 1 );
\r
1201 // Create an invisible element to grab focus.
\r
1202 if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )
\r
1205 editor.on( 'uiReady', function()
\r
1207 focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
\r
1208 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)
\r
1209 '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );
\r
1211 focusGrabber.on( 'focus', function()
\r
1216 editor.focusGrabber = focusGrabber;
\r
1218 editor.on( 'destroy', function()
\r
1220 CKEDITOR.tools.removeFunction( contentDomReadyHandler );
\r
1221 focusGrabber.clearCustomData();
\r
1222 delete editor.focusGrabber;
\r
1226 // Disable form elements editing mode provided by some browers. (#5746)
\r
1227 editor.on( 'insertElement', function ( evt )
\r
1229 var element = evt.data;
\r
1230 if ( element.type == CKEDITOR.NODE_ELEMENT
\r
1231 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
\r
1233 // We should flag that the element was locked by our code so
\r
1234 // it'll be editable by the editor functions (#6046).
\r
1235 var readonly = element.getAttribute( 'contenteditable' ) == 'false';
\r
1238 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
\r
1239 element.setAttribute( 'contenteditable', false );
\r
1247 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
\r
1248 if ( CKEDITOR.env.gecko )
\r
1252 var body = document.body;
\r
1255 window.addEventListener( 'load', arguments.callee, false );
\r
1258 var currentHandler = body.getAttribute( 'onpageshow' );
\r
1259 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
\r
1260 'event.persisted && (function(){' +
\r
1261 'var allInstances = CKEDITOR.instances, editor, doc;' +
\r
1262 'for ( var i in allInstances )' +
\r
1264 ' editor = allInstances[ i ];' +
\r
1265 ' doc = editor.document;' +
\r
1268 ' doc.$.designMode = "off";' +
\r
1269 ' doc.$.designMode = "on";' +
\r
1280 * Disables the ability of resize objects (image and tables) in the editing
\r
1285 * config.disableObjectResizing = true;
\r
1287 CKEDITOR.config.disableObjectResizing = false;
\r
1290 * Disables the "table tools" offered natively by the browser (currently
\r
1291 * Firefox only) to make quick table editing operations, like adding or
\r
1292 * deleting rows and columns.
\r
1296 * config.disableNativeTableHandles = false;
\r
1298 CKEDITOR.config.disableNativeTableHandles = true;
\r
1301 * Disables the built-in words spell checker if browser provides one.<br /><br />
\r
1303 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
\r
1304 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
\r
1305 * is enabled or you're simply not using the context menu plugin.
\r
1310 * config.disableNativeSpellChecker = false;
\r
1312 CKEDITOR.config.disableNativeSpellChecker = true;
\r
1315 * Whether the editor must output an empty value ("") if it's contents is made
\r
1316 * by an empty paragraph only.
\r
1320 * config.ignoreEmptyParagraph = false;
\r
1322 CKEDITOR.config.ignoreEmptyParagraph = true;
\r
1325 * Fired when data is loaded and ready for retrieval in an editor instance.
\r
1326 * @name CKEDITOR.editor#dataReady
\r
1331 * Whether automatically create wrapping blocks around inline contents inside document body,
\r
1332 * this helps to ensure the integrality of the block enter mode.
\r
1333 * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.
\r
1334 * @name CKEDITOR.config.autoParagraph
\r
1339 * config.autoParagraph = false;
\r
1343 * Fired when some elements are added to the document
\r
1344 * @name CKEDITOR.editor#ariaWidget
\r
1346 * @param {Object} element The element being added
\r