source: trunk/admin/inc/ckeditor/_source/plugins/wysiwygarea/plugin.js@ 239

Last change on this file since 239 was 239, checked in by luc, 9 years ago

Admin: correzione visulaizzazione immissione dati spoglio per Chrome e Safari - Aggiornamento dell'editor da FCKeditor a CKeditor , accessibili anche a Chrome e Safari.

  • Property svn:executable set to *
File size: 42.4 KB
Line 
1/*
2Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6/**
7 * @fileOverview The "wysiwygarea" plugin. It registers the "wysiwyg" editing
8 * mode, which handles the main editing area space.
9 */
10
11(function()
12{
13 // Matching an empty paragraph at the end of document.
14 var emptyParagraphRegexp = /(^|<body\b[^>]*>)\s*<(p|div|address|h\d|center|pre)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\2>)?\s*(?=$|<\/body>)/gi;
15
16 var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );
17
18 // Elements that could blink the cursor anchoring beside it, like hr, page-break. (#6554)
19 function nonEditable( element )
20 {
21 return element.isBlockBoundary() && CKEDITOR.dtd.$empty[ element.getName() ];
22 }
23
24
25 function onInsert( insertFunc )
26 {
27 return function( evt )
28 {
29 if ( this.mode == 'wysiwyg' )
30 {
31 this.focus();
32
33 this.fire( 'saveSnapshot' );
34
35 insertFunc.call( this, evt.data );
36
37 // Save snaps after the whole execution completed.
38 // This's a workaround for make DOM modification's happened after
39 // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'
40 // call.
41 CKEDITOR.tools.setTimeout( function()
42 {
43 this.fire( 'saveSnapshot' );
44 }, 0, this );
45 }
46 };
47 }
48
49 function doInsertHtml( data )
50 {
51 if ( this.dataProcessor )
52 data = this.dataProcessor.toHtml( data );
53
54 if ( !data )
55 return;
56
57 // HTML insertion only considers the first range.
58 var selection = this.getSelection(),
59 range = selection.getRanges()[ 0 ];
60
61 if ( range.checkReadOnly() )
62 return;
63
64 // Opera: force block splitting when pasted content contains block. (#7801)
65 if ( CKEDITOR.env.opera )
66 {
67 var path = new CKEDITOR.dom.elementPath( range.startContainer );
68 if ( path.block )
69 {
70 var nodes = CKEDITOR.htmlParser.fragment.fromHtml( data, false ).children;
71 for ( var i = 0, count = nodes.length; i < count; i++ )
72 {
73 if ( nodes[ i ]._.isBlockLike )
74 {
75 range.splitBlock( this.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
76 range.insertNode( range.document.createText( '' ) );
77 range.select();
78 break;
79 }
80 }
81 }
82 }
83
84 if ( CKEDITOR.env.ie )
85 {
86 var selIsLocked = selection.isLocked;
87
88 if ( selIsLocked )
89 selection.unlock();
90
91 var $sel = selection.getNative();
92
93 // Delete control selections to avoid IE bugs on pasteHTML.
94 if ( $sel.type == 'Control' )
95 $sel.clear();
96 else if ( selection.getType() == CKEDITOR.SELECTION_TEXT )
97 {
98 // Due to IE bugs on handling contenteditable=false blocks
99 // (#6005), we need to make some checks and eventually
100 // delete the selection first.
101
102 range = selection.getRanges()[ 0 ];
103 var endContainer = range && range.endContainer;
104
105 if ( endContainer &&
106 endContainer.type == CKEDITOR.NODE_ELEMENT &&
107 endContainer.getAttribute( 'contenteditable' ) == 'false' &&
108 range.checkBoundaryOfElement( endContainer, CKEDITOR.END ) )
109 {
110 range.setEndAfter( range.endContainer );
111 range.deleteContents();
112 }
113 }
114
115 $sel.createRange().pasteHTML( data );
116
117 if ( selIsLocked )
118 this.getSelection().lock();
119 }
120 else
121 this.document.$.execCommand( 'inserthtml', false, data );
122
123 // Webkit does not scroll to the cursor position after pasting (#5558)
124 if ( CKEDITOR.env.webkit )
125 {
126 selection = this.getSelection();
127 selection.scrollIntoView();
128 }
129 }
130
131 function doInsertText( text )
132 {
133 var selection = this.getSelection(),
134 mode = selection.getStartElement().hasAscendant( 'pre', true ) ?
135 CKEDITOR.ENTER_BR : this.config.enterMode,
136 isEnterBrMode = mode == CKEDITOR.ENTER_BR;
137
138 var html = CKEDITOR.tools.htmlEncode( text.replace( /\r\n|\r/g, '\n' ) );
139
140 // Convert leading and trailing whitespaces into &nbsp;
141 html = html.replace( /^[ \t]+|[ \t]+$/g, function( match, offset, s )
142 {
143 if ( match.length == 1 ) // one space, preserve it
144 return '&nbsp;';
145 else if ( !offset ) // beginning of block
146 return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';
147 else // end of block
148 return ' ' + CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 );
149 } );
150
151 // Convert subsequent whitespaces into &nbsp;
152 html = html.replace( /[ \t]{2,}/g, function ( match )
153 {
154 return CKEDITOR.tools.repeat( '&nbsp;', match.length - 1 ) + ' ';
155 } );
156
157 var paragraphTag = mode == CKEDITOR.ENTER_P ? 'p' : 'div';
158
159 // Two line-breaks create one paragraph.
160 if ( !isEnterBrMode )
161 {
162 html = html.replace( /(\n{2})([\s\S]*?)(?:$|\1)/g,
163 function( match, group1, text )
164 {
165 return '<'+paragraphTag + '>' + text + '</' + paragraphTag + '>';
166 });
167 }
168
169 // One <br> per line-break.
170 html = html.replace( /\n/g, '<br>' );
171
172 // Compensate padding <br> for non-IE.
173 if ( !( isEnterBrMode || CKEDITOR.env.ie ) )
174 {
175 html = html.replace( new RegExp( '<br>(?=</' + paragraphTag + '>)' ), function( match )
176 {
177 return CKEDITOR.tools.repeat( match, 2 );
178 } );
179 }
180
181 // Inline styles have to be inherited in Firefox.
182 if ( CKEDITOR.env.gecko || CKEDITOR.env.webkit )
183 {
184 var path = new CKEDITOR.dom.elementPath( selection.getStartElement() ),
185 context = [];
186
187 for ( var i = 0; i < path.elements.length; i++ )
188 {
189 var tag = path.elements[ i ].getName();
190 if ( tag in CKEDITOR.dtd.$inline )
191 context.unshift( path.elements[ i ].getOuterHtml().match( /^<.*?>/) );
192 else if ( tag in CKEDITOR.dtd.$block )
193 break;
194 }
195
196 // Reproduce the context by preceding the pasted HTML with opening inline tags.
197 html = context.join( '' ) + html;
198 }
199
200 doInsertHtml.call( this, html );
201 }
202
203 function doInsertElement( element )
204 {
205 var selection = this.getSelection(),
206 ranges = selection.getRanges(),
207 elementName = element.getName(),
208 isBlock = CKEDITOR.dtd.$block[ elementName ];
209
210 var selIsLocked = selection.isLocked;
211
212 if ( selIsLocked )
213 selection.unlock();
214
215 var range, clone, lastElement, bookmark;
216
217 for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
218 {
219 range = ranges[ i ];
220
221 if ( !range.checkReadOnly() )
222 {
223 // Remove the original contents, merge splitted nodes.
224 range.deleteContents( 1 );
225
226 clone = !i && element || element.clone( 1 );
227
228 // If we're inserting a block at dtd-violated position, split
229 // the parent blocks until we reach blockLimit.
230 var current, dtd;
231 if ( isBlock )
232 {
233 while ( ( current = range.getCommonAncestor( 0, 1 ) )
234 && ( dtd = CKEDITOR.dtd[ current.getName() ] )
235 && !( dtd && dtd [ elementName ] ) )
236 {
237 // Split up inline elements.
238 if ( current.getName() in CKEDITOR.dtd.span )
239 range.splitElement( current );
240 // If we're in an empty block which indicate a new paragraph,
241 // simply replace it with the inserting block.(#3664)
242 else if ( range.checkStartOfBlock()
243 && range.checkEndOfBlock() )
244 {
245 range.setStartBefore( current );
246 range.collapse( true );
247 current.remove();
248 }
249 else
250 range.splitBlock();
251 }
252 }
253
254 // Insert the new node.
255 range.insertNode( clone );
256
257 // Save the last element reference so we can make the
258 // selection later.
259 if ( !lastElement )
260 lastElement = clone;
261 }
262 }
263
264 if ( lastElement )
265 {
266 range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );
267
268 // If we're inserting a block element immediatelly followed by
269 // another block element, the selection must move there. (#3100,#5436)
270 if ( isBlock )
271 {
272 var next = lastElement.getNext( notWhitespaceEval ),
273 nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();
274
275 // Check if it's a block element that accepts text.
276 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )
277 range.moveToElementEditStart( next );
278 }
279 }
280
281 selection.selectRanges( [ range ] );
282
283 if ( selIsLocked )
284 this.getSelection().lock();
285 }
286
287 // DOM modification here should not bother dirty flag.(#4385)
288 function restoreDirty( editor )
289 {
290 if ( !editor.checkDirty() )
291 setTimeout( function(){ editor.resetDirty(); }, 0 );
292 }
293
294 var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),
295 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );
296
297 function isNotEmpty( node )
298 {
299 return isNotWhitespace( node ) && isNotBookmark( node );
300 }
301
302 function isNbsp( node )
303 {
304 return node.type == CKEDITOR.NODE_TEXT
305 && CKEDITOR.tools.trim( node.getText() ).match( /^(?:&nbsp;|\xa0)$/ );
306 }
307
308 function restoreSelection( selection )
309 {
310 if ( selection.isLocked )
311 {
312 selection.unlock();
313 setTimeout( function() { selection.lock(); }, 0 );
314 }
315 }
316
317 function isBlankParagraph( block )
318 {
319 return block.getOuterHtml().match( emptyParagraphRegexp );
320 }
321
322 isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );
323
324 // Gecko need a key event to 'wake up' the editing
325 // ability when document is empty.(#3864, #5781)
326 function activateEditing( editor )
327 {
328 var win = editor.window,
329 doc = editor.document,
330 body = editor.document.getBody(),
331 bodyFirstChild = body.getFirst(),
332 bodyChildsNum = body.getChildren().count();
333
334 if ( !bodyChildsNum
335 || bodyChildsNum == 1
336 && bodyFirstChild.type == CKEDITOR.NODE_ELEMENT
337 && bodyFirstChild.hasAttribute( '_moz_editor_bogus_node' ) )
338 {
339 restoreDirty( editor );
340
341 // Memorize scroll position to restore it later (#4472).
342 var hostDocument = editor.element.getDocument();
343 var hostDocumentElement = hostDocument.getDocumentElement();
344 var scrollTop = hostDocumentElement.$.scrollTop;
345 var scrollLeft = hostDocumentElement.$.scrollLeft;
346
347 // Simulating keyboard character input by dispatching a keydown of white-space text.
348 var keyEventSimulate = doc.$.createEvent( "KeyEvents" );
349 keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,
350 false, false, false, 0, 32 );
351 doc.$.dispatchEvent( keyEventSimulate );
352
353 if ( scrollTop != hostDocumentElement.$.scrollTop || scrollLeft != hostDocumentElement.$.scrollLeft )
354 hostDocument.getWindow().$.scrollTo( scrollLeft, scrollTop );
355
356 // Restore the original document status by placing the cursor before a bogus br created (#5021).
357 bodyChildsNum && body.getFirst().remove();
358 doc.getBody().appendBogus();
359 var nativeRange = new CKEDITOR.dom.range( doc );
360 nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );
361 nativeRange.select();
362 }
363 }
364
365 /**
366 * Auto-fixing block-less content by wrapping paragraph (#3190), prevent
367 * non-exitable-block by padding extra br.(#3189)
368 */
369 function onSelectionChangeFixBody( evt )
370 {
371 var editor = evt.editor,
372 path = evt.data.path,
373 blockLimit = path.blockLimit,
374 selection = evt.data.selection,
375 range = selection.getRanges()[0],
376 body = editor.document.getBody(),
377 enterMode = editor.config.enterMode;
378
379 if ( CKEDITOR.env.gecko )
380 {
381 activateEditing( editor );
382
383 // Ensure bogus br could help to move cursor (out of styles) to the end of block. (#7041)
384 var pathBlock = path.block || path.blockLimit,
385 lastNode = pathBlock && pathBlock.getLast( isNotEmpty );
386
387 // Check some specialities of the current path block:
388 // 1. It is really displayed as block; (#7221)
389 // 2. It doesn't end with one inner block; (#7467)
390 // 3. It doesn't have bogus br yet.
391 if ( pathBlock
392 && pathBlock.isBlockBoundary()
393 && !( lastNode && lastNode.type == CKEDITOR.NODE_ELEMENT && lastNode.isBlockBoundary() )
394 && !pathBlock.is( 'pre' )
395 && !pathBlock.getBogus() )
396 {
397 pathBlock.appendBogus();
398 }
399 }
400
401 // When we're in block enter mode, a new paragraph will be established
402 // to encapsulate inline contents right under body. (#3657)
403 if ( editor.config.autoParagraph !== false
404 && enterMode != CKEDITOR.ENTER_BR
405 && range.collapsed
406 && blockLimit.getName() == 'body'
407 && !path.block )
408 {
409 var fixedBlock = range.fixBlock( true,
410 editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' );
411
412 // For IE, we should remove any filler node which was introduced before.
413 if ( CKEDITOR.env.ie )
414 {
415 var first = fixedBlock.getFirst( isNotEmpty );
416 first && isNbsp( first ) && first.remove();
417 }
418
419 // If the fixed block is actually blank and is already followed by an exitable blank
420 // block, we should revert the fix and move into the existed one. (#3684)
421 if ( isBlankParagraph( fixedBlock ) )
422 {
423 var element = fixedBlock.getNext( isNotWhitespace );
424 if ( element &&
425 element.type == CKEDITOR.NODE_ELEMENT &&
426 !nonEditable( element ) )
427 {
428 range.moveToElementEditStart( element );
429 fixedBlock.remove();
430 }
431 else
432 {
433 element = fixedBlock.getPrevious( isNotWhitespace );
434 if ( element &&
435 element.type == CKEDITOR.NODE_ELEMENT &&
436 !nonEditable( element ) )
437 {
438 range.moveToElementEditEnd( element );
439 fixedBlock.remove();
440 }
441 }
442 }
443
444 range.select();
445 // Cancel this selection change in favor of the next (correct). (#6811)
446 evt.cancel();
447 }
448
449 // Browsers are incapable of moving cursor out of certain block elements (e.g. table, div, pre)
450 // at the end of document, makes it unable to continue adding content, we have to make this
451 // easier by opening an new empty paragraph.
452 var testRange = new CKEDITOR.dom.range( editor.document );
453 testRange.moveToElementEditEnd( editor.document.getBody() );
454 var testPath = new CKEDITOR.dom.elementPath( testRange.startContainer );
455 if ( !testPath.blockLimit.is( 'body') )
456 {
457 var paddingBlock;
458 if ( enterMode != CKEDITOR.ENTER_BR )
459 paddingBlock = body.append( editor.document.createElement( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );
460 else
461 paddingBlock = body;
462
463 if ( !CKEDITOR.env.ie )
464 paddingBlock.appendBogus();
465 }
466 }
467
468 CKEDITOR.plugins.add( 'wysiwygarea',
469 {
470 requires : [ 'editingblock' ],
471
472 init : function( editor )
473 {
474 var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR && editor.config.autoParagraph !== false )
475 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;
476
477 var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );
478
479 var contentDomReadyHandler;
480 editor.on( 'editingBlockReady', function()
481 {
482 var mainElement,
483 iframe,
484 isLoadingData,
485 isPendingFocus,
486 frameLoaded,
487 fireMode;
488
489
490 // Support for custom document.domain in IE.
491 var isCustomDomain = CKEDITOR.env.isCustomDomain();
492
493 // Creates the iframe that holds the editable document.
494 var createIFrame = function( data )
495 {
496 if ( iframe )
497 iframe.remove();
498
499 var src =
500 'document.open();' +
501
502 // The document domain must be set any time we
503 // call document.open().
504 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
505
506 'document.close();';
507
508 // With IE, the custom domain has to be taken care at first,
509 // for other browers, the 'src' attribute should be left empty to
510 // trigger iframe's 'load' event.
511 src =
512 CKEDITOR.env.air ?
513 'javascript:void(0)' :
514 CKEDITOR.env.ie ?
515 'javascript:void(function(){' + encodeURIComponent( src ) + '}())'
516 :
517 '';
518
519 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +
520 ' style="width:100%;height:100%"' +
521 ' frameBorder="0"' +
522 ' title="' + frameLabel + '"' +
523 ' src="' + src + '"' +
524 ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +
525 ' allowTransparency="true"' +
526 '></iframe>' );
527
528 // Running inside of Firefox chrome the load event doesn't bubble like in a normal page (#5689)
529 if ( document.location.protocol == 'chrome:' )
530 CKEDITOR.event.useCapture = true;
531
532 // With FF, it's better to load the data on iframe.load. (#3894,#4058)
533 iframe.on( 'load', function( ev )
534 {
535 frameLoaded = 1;
536 ev.removeListener();
537
538 var doc = iframe.getFrameDocument();
539 doc.write( data );
540
541 CKEDITOR.env.air && contentDomReady( doc.getWindow().$ );
542 });
543
544 // Reset adjustment back to default (#5689)
545 if ( document.location.protocol == 'chrome:' )
546 CKEDITOR.event.useCapture = false;
547
548 mainElement.append( iframe );
549 };
550
551 // The script that launches the bootstrap logic on 'domReady', so the document
552 // is fully editable even before the editing iframe is fully loaded (#4455).
553 contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );
554 var activationScript =
555 '<script id="cke_actscrpt" type="text/javascript" data-cke-temp="1">' +
556 ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +
557 'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +
558 '</script>';
559
560 // Editing area bootstrap code.
561 function contentDomReady( domWindow )
562 {
563 if ( !frameLoaded )
564 return;
565 frameLoaded = 0;
566
567 editor.fire( 'ariaWidget', iframe );
568
569 var domDocument = domWindow.document,
570 body = domDocument.body;
571
572 // Remove this script from the DOM.
573 var script = domDocument.getElementById( "cke_actscrpt" );
574 script && script.parentNode.removeChild( script );
575
576 body.spellcheck = !editor.config.disableNativeSpellChecker;
577
578 var editable = !editor.readOnly;
579
580 if ( CKEDITOR.env.ie )
581 {
582 // Don't display the focus border.
583 body.hideFocus = true;
584
585 // Disable and re-enable the body to avoid IE from
586 // taking the editing focus at startup. (#141 / #523)
587 body.disabled = true;
588 body.contentEditable = editable;
589 body.removeAttribute( 'disabled' );
590 }
591 else
592 {
593 // Avoid opening design mode in a frame window thread,
594 // which will cause host page scrolling.(#4397)
595 setTimeout( function()
596 {
597 // Prefer 'contentEditable' instead of 'designMode'. (#3593)
598 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900
599 || CKEDITOR.env.opera )
600 domDocument.$.body.contentEditable = editable;
601 else if ( CKEDITOR.env.webkit )
602 domDocument.$.body.parentNode.contentEditable = editable;
603 else
604 domDocument.$.designMode = editable? 'off' : 'on';
605 }, 0 );
606 }
607
608 editable && CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );
609
610 domWindow = editor.window = new CKEDITOR.dom.window( domWindow );
611 domDocument = editor.document = new CKEDITOR.dom.document( domDocument );
612
613 editable && domDocument.on( 'dblclick', function( evt )
614 {
615 var element = evt.data.getTarget(),
616 data = { element : element, dialog : '' };
617 editor.fire( 'doubleclick', data );
618 data.dialog && editor.openDialog( data.dialog );
619 });
620
621 // Prevent automatic submission in IE #6336
622 CKEDITOR.env.ie && domDocument.on( 'click', function( evt )
623 {
624 var element = evt.data.getTarget();
625 if ( element.is( 'input' ) )
626 {
627 var type = element.getAttribute( 'type' );
628 if ( type == 'submit' || type == 'reset' )
629 evt.data.preventDefault();
630 }
631 });
632
633 // Gecko/Webkit need some help when selecting control type elements. (#3448)
634 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera ) )
635 {
636 domDocument.on( 'mousedown', function( ev )
637 {
638 var control = ev.data.getTarget();
639 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )
640 editor.getSelection().selectElement( control );
641 } );
642 }
643
644 if ( CKEDITOR.env.gecko )
645 {
646 domDocument.on( 'mouseup', function( ev )
647 {
648 if ( ev.data.$.button == 2 )
649 {
650 var target = ev.data.getTarget();
651
652 // Prevent right click from selecting an empty block even
653 // when selection is anchored inside it. (#5845)
654 if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )
655 {
656 var range = new CKEDITOR.dom.range( domDocument );
657 range.moveToElementEditStart( target );
658 range.select( true );
659 }
660 }
661 } );
662 }
663
664 // Prevent the browser opening links in read-only blocks. (#6032)
665 domDocument.on( 'click', function( ev )
666 {
667 ev = ev.data;
668 if ( ev.getTarget().is( 'a' ) && ev.$.button != 2 )
669 ev.preventDefault();
670 });
671
672 // Webkit: avoid from editing form control elements content.
673 if ( CKEDITOR.env.webkit )
674 {
675 // Mark that cursor will right blinking (#7113).
676 domDocument.on( 'mousedown', function() { wasFocused = 1; } );
677 // Prevent from tick checkbox/radiobox/select
678 domDocument.on( 'click', function( ev )
679 {
680 if ( ev.data.getTarget().is( 'input', 'select' ) )
681 ev.data.preventDefault();
682 } );
683
684 // Prevent from editig textfield/textarea value.
685 domDocument.on( 'mouseup', function( ev )
686 {
687 if ( ev.data.getTarget().is( 'input', 'textarea' ) )
688 ev.data.preventDefault();
689 } );
690 }
691
692 // IE standard compliant in editing frame doesn't focus the editor when
693 // clicking outside actual content, manually apply the focus. (#1659)
694 if ( editable &&
695 CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat'
696 || CKEDITOR.env.gecko
697 || CKEDITOR.env.opera )
698 {
699 var htmlElement = domDocument.getDocumentElement();
700 htmlElement.on( 'mousedown', function( evt )
701 {
702 // Setting focus directly on editor doesn't work, we
703 // have to use here a temporary element to 'redirect'
704 // the focus.
705 if ( evt.data.getTarget().equals( htmlElement ) )
706 {
707 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
708 blinkCursor();
709 focusGrabber.focus();
710 }
711 } );
712 }
713
714 var focusTarget = CKEDITOR.env.ie ? iframe : domWindow;
715 focusTarget.on( 'blur', function()
716 {
717 editor.focusManager.blur();
718 });
719
720 var wasFocused;
721
722 focusTarget.on( 'focus', function()
723 {
724 var doc = editor.document;
725
726 if ( editable && CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )
727 blinkCursor();
728 else if ( CKEDITOR.env.opera )
729 doc.getBody().focus();
730 // Webkit needs focus for the first time on the HTML element. (#6153)
731 else if ( CKEDITOR.env.webkit )
732 {
733 if ( !wasFocused )
734 {
735 editor.document.getDocumentElement().focus();
736 wasFocused = 1;
737 }
738 }
739
740 editor.focusManager.focus();
741 });
742
743 var keystrokeHandler = editor.keystrokeHandler;
744 // Prevent backspace from navigating off the page.
745 keystrokeHandler.blockedKeystrokes[ 8 ] = !editable;
746 keystrokeHandler.attach( domDocument );
747
748 domDocument.getDocumentElement().addClass( domDocument.$.compatMode );
749 // Override keystroke behaviors.
750 editable && domDocument.on( 'keydown', function( evt )
751 {
752 var keyCode = evt.data.getKeystroke();
753
754 // Backspace OR Delete.
755 if ( keyCode in { 8 : 1, 46 : 1 } )
756 {
757 var sel = editor.getSelection(),
758 selected = sel.getSelectedElement(),
759 range = sel.getRanges()[ 0 ];
760
761 // Override keystrokes which should have deletion behavior
762 // on fully selected element . (#4047) (#7645)
763 if ( selected )
764 {
765 // Make undo snapshot.
766 editor.fire( 'saveSnapshot' );
767
768 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will
769 // break up the selection, safely manage it here. (#4795)
770 range.moveToPosition( selected, CKEDITOR.POSITION_BEFORE_START );
771 // Remove the control manually.
772 selected.remove();
773 range.select();
774
775 editor.fire( 'saveSnapshot' );
776
777 evt.data.preventDefault();
778 return;
779 }
780 }
781 } );
782
783 // PageUp/PageDown scrolling is broken in document
784 // with standard doctype, manually fix it. (#4736)
785 if ( CKEDITOR.env.ie && domDocument.$.compatMode == 'CSS1Compat' )
786 {
787 var pageUpDownKeys = { 33 : 1, 34 : 1 };
788 domDocument.on( 'keydown', function( evt )
789 {
790 if ( evt.data.getKeystroke() in pageUpDownKeys )
791 {
792 setTimeout( function ()
793 {
794 editor.getSelection().scrollIntoView();
795 }, 0 );
796 }
797 } );
798 }
799
800 // Prevent IE from leaving new paragraph after deleting all contents in body. (#6966)
801 if ( CKEDITOR.env.ie && editor.config.enterMode != CKEDITOR.ENTER_P )
802 {
803 domDocument.on( 'selectionchange', function()
804 {
805 var body = domDocument.getBody(),
806 range = editor.getSelection().getRanges()[ 0 ];
807
808 if ( body.getHtml().match( /^<p>&nbsp;<\/p>$/i )
809 && range.startContainer.equals( body ) )
810 {
811 // Avoid the ambiguity from a real user cursor position.
812 setTimeout( function ()
813 {
814 range = editor.getSelection().getRanges()[ 0 ];
815 if ( !range.startContainer.equals ( 'body' ) )
816 {
817 body.getFirst().remove( 1 );
818 range.moveToElementEditEnd( body );
819 range.select( 1 );
820 }
821 }, 0 );
822 }
823 });
824 }
825
826 // Adds the document body as a context menu target.
827 if ( editor.contextMenu )
828 editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );
829
830 setTimeout( function()
831 {
832 editor.fire( 'contentDom' );
833
834 if ( fireMode )
835 {
836 editor.mode = 'wysiwyg';
837 editor.fire( 'mode', { previousMode : editor._.previousMode } );
838 fireMode = false;
839 }
840
841 isLoadingData = false;
842
843 if ( isPendingFocus )
844 {
845 editor.focus();
846 isPendingFocus = false;
847 }
848 setTimeout( function()
849 {
850 editor.fire( 'dataReady' );
851 }, 0 );
852
853 // IE, Opera and Safari may not support it and throw errors.
854 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ); } catch(e) {}
855 if ( editor.config.disableObjectResizing )
856 {
857 try
858 {
859 editor.document.$.execCommand( 'enableObjectResizing', false, false );
860 }
861 catch(e)
862 {
863 // For browsers in which the above method failed, we can cancel the resizing on the fly (#4208)
864 editor.document.getBody().on( CKEDITOR.env.ie ? 'resizestart' : 'resize', function( evt )
865 {
866 evt.data.preventDefault();
867 });
868 }
869 }
870
871 /*
872 * IE BUG: IE might have rendered the iframe with invisible contents.
873 * (#3623). Push some inconsequential CSS style changes to force IE to
874 * refresh it.
875 *
876 * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not
877 * fix the problem. :(
878 */
879 if ( CKEDITOR.env.ie )
880 {
881 setTimeout( function()
882 {
883 if ( editor.document )
884 {
885 var $body = editor.document.$.body;
886 $body.runtimeStyle.marginBottom = '0px';
887 $body.runtimeStyle.marginBottom = '';
888 }
889 }, 1000 );
890 }
891 },
892 0 );
893 }
894
895 editor.addMode( 'wysiwyg',
896 {
897 load : function( holderElement, data, isSnapshot )
898 {
899 mainElement = holderElement;
900
901 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )
902 holderElement.setStyle( 'position', 'relative' );
903
904 // The editor data "may be dirty" after this
905 // point.
906 editor.mayBeDirty = true;
907
908 fireMode = true;
909
910 if ( isSnapshot )
911 this.loadSnapshotData( data );
912 else
913 this.loadData( data );
914 },
915
916 loadData : function( data )
917 {
918 isLoadingData = true;
919 editor._.dataStore = { id : 1 };
920
921 var config = editor.config,
922 fullPage = config.fullPage,
923 docType = config.docType;
924
925 // Build the additional stuff to be included into <head>.
926 var headExtra =
927 '<style type="text/css" data-cke-temp="1">' +
928 editor._.styles.join( '\n' ) +
929 '</style>';
930
931 !fullPage && ( headExtra =
932 CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +
933 headExtra );
934
935 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" data-cke-temp="1" />' : '';
936
937 if ( fullPage )
938 {
939 // Search and sweep out the doctype declaration.
940 data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )
941 {
942 editor.docType = docType = match;
943 return '';
944 }).replace( /<\?xml\s[^\?]*\?>/i, function( match )
945 {
946 editor.xmlDeclaration = match;
947 return '';
948 });
949 }
950
951 // Get the HTML version of the data.
952 if ( editor.dataProcessor )
953 data = editor.dataProcessor.toHtml( data, fixForBody );
954
955 if ( fullPage )
956 {
957 // Check if the <body> tag is available.
958 if ( !(/<body[\s|>]/).test( data ) )
959 data = '<body>' + data;
960
961 // Check if the <html> tag is available.
962 if ( !(/<html[\s|>]/).test( data ) )
963 data = '<html>' + data + '</html>';
964
965 // Check if the <head> tag is available.
966 if ( !(/<head[\s|>]/).test( data ) )
967 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;
968 else if ( !(/<title[\s|>]/).test( data ) )
969 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;
970
971 // The base must be the first tag in the HEAD, e.g. to get relative
972 // links on styles.
973 baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );
974
975 // Inject the extra stuff into <head>.
976 // Attention: do not change it before testing it well. (V2)
977 // This is tricky... if the head ends with <meta ... content type>,
978 // Firefox will break. But, it works if we place our extra stuff as
979 // the last elements in the HEAD.
980 data = data.replace( /<\/head\s*>/, headExtra + '$&' );
981
982 // Add the DOCTYPE back to it.
983 data = docType + data;
984 }
985 else
986 {
987 data =
988 config.docType +
989 '<html dir="' + config.contentsLangDirection + '"' +
990 ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +
991 '<head>' +
992 '<title>' + frameLabel + '</title>' +
993 baseTag +
994 headExtra +
995 '</head>' +
996 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +
997 ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +
998 '>' +
999 data +
1000 '</html>';
1001 }
1002
1003 // Distinguish bogus to normal BR at the end of document for Mozilla. (#5293).
1004 if ( CKEDITOR.env.gecko )
1005 data = data.replace( /<br \/>(?=\s*<\/(:?html|body)>)/, '$&<br type="_moz" />' );
1006
1007 data += activationScript;
1008
1009
1010 // The iframe is recreated on each call of setData, so we need to clear DOM objects
1011 this.onDispose();
1012 createIFrame( data );
1013 },
1014
1015 getData : function()
1016 {
1017 var config = editor.config,
1018 fullPage = config.fullPage,
1019 docType = fullPage && editor.docType,
1020 xmlDeclaration = fullPage && editor.xmlDeclaration,
1021 doc = iframe.getFrameDocument();
1022
1023 var data = fullPage
1024 ? doc.getDocumentElement().getOuterHtml()
1025 : doc.getBody().getHtml();
1026
1027 // BR at the end of document is bogus node for Mozilla. (#5293).
1028 if ( CKEDITOR.env.gecko )
1029 data = data.replace( /<br>(?=\s*(:?$|<\/body>))/, '' );
1030
1031 if ( editor.dataProcessor )
1032 data = editor.dataProcessor.toDataFormat( data, fixForBody );
1033
1034 // Reset empty if the document contains only one empty paragraph.
1035 if ( config.ignoreEmptyParagraph )
1036 data = data.replace( emptyParagraphRegexp, function( match, lookback ) { return lookback; } );
1037
1038 if ( xmlDeclaration )
1039 data = xmlDeclaration + '\n' + data;
1040 if ( docType )
1041 data = docType + '\n' + data;
1042
1043 return data;
1044 },
1045
1046 getSnapshotData : function()
1047 {
1048 return iframe.getFrameDocument().getBody().getHtml();
1049 },
1050
1051 loadSnapshotData : function( data )
1052 {
1053 iframe.getFrameDocument().getBody().setHtml( data );
1054 },
1055
1056 onDispose : function()
1057 {
1058 if ( !editor.document )
1059 return;
1060
1061 editor.document.getDocumentElement().clearCustomData();
1062 editor.document.getBody().clearCustomData();
1063
1064 editor.window.clearCustomData();
1065 editor.document.clearCustomData();
1066
1067 iframe.clearCustomData();
1068
1069 /*
1070 * IE BUG: When destroying editor DOM with the selection remains inside
1071 * editing area would break IE7/8's selection system, we have to put the editing
1072 * iframe offline first. (#3812 and #5441)
1073 */
1074 iframe.remove();
1075 },
1076
1077 unload : function( holderElement )
1078 {
1079 this.onDispose();
1080
1081 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;
1082
1083 editor.fire( 'contentDomUnload' );
1084 },
1085
1086 focus : function()
1087 {
1088 var win = editor.window;
1089
1090 if ( isLoadingData )
1091 isPendingFocus = true;
1092 else if ( win )
1093 {
1094 // AIR needs a while to focus when moving from a link.
1095 CKEDITOR.env.air ? setTimeout( function () { win.focus(); }, 0 ) : win.focus();
1096 editor.selectionChange();
1097 }
1098 }
1099 });
1100
1101 editor.on( 'insertHtml', onInsert( doInsertHtml ) , null, null, 20 );
1102 editor.on( 'insertElement', onInsert( doInsertElement ), null, null, 20 );
1103 editor.on( 'insertText', onInsert( doInsertText ), null, null, 20 );
1104 // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)
1105 editor.on( 'selectionChange', function( evt )
1106 {
1107 if ( editor.readOnly )
1108 return;
1109
1110 var sel = editor.getSelection();
1111 // Do it only when selection is not locked. (#8222)
1112 if ( sel && !sel.isLocked )
1113 {
1114 var isDirty = editor.checkDirty();
1115 editor.fire( 'saveSnapshot', { contentOnly : 1 } );
1116 onSelectionChangeFixBody.call( this, evt );
1117 editor.fire( 'updateSnapshot' );
1118 !isDirty && editor.resetDirty();
1119 }
1120
1121 }, null, null, 1 );
1122 });
1123
1124 var titleBackup;
1125 // Setting voice label as window title, backup the original one
1126 // and restore it before running into use.
1127 editor.on( 'contentDom', function()
1128 {
1129 var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );
1130 title.data( 'cke-title', editor.document.$.title );
1131 editor.document.$.title = frameLabel;
1132 });
1133
1134 editor.on( 'readOnly', function()
1135 {
1136 if ( editor.mode == 'wysiwyg' )
1137 {
1138 // Symply reload the wysiwyg area. It'll take care of read-only.
1139 var wysiwyg = editor.getMode();
1140 wysiwyg.loadData( wysiwyg.getData() );
1141 }
1142 });
1143
1144 // IE>=8 stricts mode doesn't have 'contentEditable' in effect
1145 // on element unless it has layout. (#5562)
1146 if ( CKEDITOR.document.$.documentMode >= 8 )
1147 {
1148 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );
1149
1150 var selectors = [];
1151 for ( var tag in CKEDITOR.dtd.$removeEmpty )
1152 selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );
1153 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );
1154 }
1155 // Set the HTML style to 100% to have the text cursor in affect (#6341)
1156 else if ( CKEDITOR.env.gecko )
1157 {
1158 editor.addCss( 'html { height: 100% !important; }' );
1159 editor.addCss( 'img:-moz-broken { -moz-force-broken-image-icon : 1; width : 24px; height : 24px; }' );
1160 }
1161
1162 /* #3658: [IE6] Editor document has horizontal scrollbar on long lines
1163 To prevent this misbehavior, we show the scrollbar always */
1164 /* #6341: The text cursor must be set on the editor area. */
1165 /* #6632: Avoid having "text" shape of cursor in IE7 scrollbars.*/
1166 editor.addCss( 'html { _overflow-y: scroll; cursor: text; *cursor:auto;}' );
1167 // Use correct cursor for these elements
1168 editor.addCss( 'img, input, textarea { cursor: default;}' );
1169
1170 // Switch on design mode for a short while and close it after then.
1171 function blinkCursor( retry )
1172 {
1173 if ( editor.readOnly )
1174 return;
1175
1176 CKEDITOR.tools.tryThese(
1177 function()
1178 {
1179 editor.document.$.designMode = 'on';
1180 setTimeout( function()
1181 {
1182 editor.document.$.designMode = 'off';
1183 if ( CKEDITOR.currentInstance == editor )
1184 editor.document.getBody().focus();
1185 }, 50 );
1186 },
1187 function()
1188 {
1189 // The above call is known to fail when parent DOM
1190 // tree layout changes may break design mode. (#5782)
1191 // Refresh the 'contentEditable' is a cue to this.
1192 editor.document.$.designMode = 'off';
1193 var body = editor.document.getBody();
1194 body.setAttribute( 'contentEditable', false );
1195 body.setAttribute( 'contentEditable', true );
1196 // Try it again once..
1197 !retry && blinkCursor( 1 );
1198 });
1199 }
1200
1201 // Create an invisible element to grab focus.
1202 if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )
1203 {
1204 var focusGrabber;
1205 editor.on( 'uiReady', function()
1206 {
1207 focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(
1208 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)
1209 '<span tabindex="-1" style="position:absolute;" role="presentation"></span>' ) );
1210
1211 focusGrabber.on( 'focus', function()
1212 {
1213 editor.focus();
1214 } );
1215
1216 editor.focusGrabber = focusGrabber;
1217 } );
1218 editor.on( 'destroy', function()
1219 {
1220 CKEDITOR.tools.removeFunction( contentDomReadyHandler );
1221 focusGrabber.clearCustomData();
1222 delete editor.focusGrabber;
1223 } );
1224 }
1225
1226 // Disable form elements editing mode provided by some browers. (#5746)
1227 editor.on( 'insertElement', function ( evt )
1228 {
1229 var element = evt.data;
1230 if ( element.type == CKEDITOR.NODE_ELEMENT
1231 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )
1232 {
1233 // We should flag that the element was locked by our code so
1234 // it'll be editable by the editor functions (#6046).
1235 var readonly = element.getAttribute( 'contenteditable' ) == 'false';
1236 if ( !readonly )
1237 {
1238 element.data( 'cke-editable', element.hasAttribute( 'contenteditable' ) ? 'true' : '1' );
1239 element.setAttribute( 'contenteditable', false );
1240 }
1241 }
1242 });
1243
1244 }
1245 });
1246
1247 // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)
1248 if ( CKEDITOR.env.gecko )
1249 {
1250 (function()
1251 {
1252 var body = document.body;
1253
1254 if ( !body )
1255 window.addEventListener( 'load', arguments.callee, false );
1256 else
1257 {
1258 var currentHandler = body.getAttribute( 'onpageshow' );
1259 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +
1260 'event.persisted && (function(){' +
1261 'var allInstances = CKEDITOR.instances, editor, doc;' +
1262 'for ( var i in allInstances )' +
1263 '{' +
1264 ' editor = allInstances[ i ];' +
1265 ' doc = editor.document;' +
1266 ' if ( doc )' +
1267 ' {' +
1268 ' doc.$.designMode = "off";' +
1269 ' doc.$.designMode = "on";' +
1270 ' }' +
1271 '}' +
1272 '})();' );
1273 }
1274 } )();
1275
1276 }
1277})();
1278
1279/**
1280 * Disables the ability of resize objects (image and tables) in the editing
1281 * area.
1282 * @type Boolean
1283 * @default false
1284 * @example
1285 * config.disableObjectResizing = true;
1286 */
1287CKEDITOR.config.disableObjectResizing = false;
1288
1289/**
1290 * Disables the "table tools" offered natively by the browser (currently
1291 * Firefox only) to make quick table editing operations, like adding or
1292 * deleting rows and columns.
1293 * @type Boolean
1294 * @default true
1295 * @example
1296 * config.disableNativeTableHandles = false;
1297 */
1298CKEDITOR.config.disableNativeTableHandles = true;
1299
1300/**
1301 * Disables the built-in words spell checker if browser provides one.<br /><br />
1302 *
1303 * <strong>Note:</strong> Although word suggestions provided by browsers (natively) will not appear in CKEditor's default context menu,
1304 * users can always reach the native context menu by holding the <em>Ctrl</em> key when right-clicking if {@link CKEDITOR.config.browserContextMenuOnCtrl}
1305 * is enabled or you're simply not using the context menu plugin.
1306 *
1307 * @type Boolean
1308 * @default true
1309 * @example
1310 * config.disableNativeSpellChecker = false;
1311 */
1312CKEDITOR.config.disableNativeSpellChecker = true;
1313
1314/**
1315 * Whether the editor must output an empty value ("") if it's contents is made
1316 * by an empty paragraph only.
1317 * @type Boolean
1318 * @default true
1319 * @example
1320 * config.ignoreEmptyParagraph = false;
1321 */
1322CKEDITOR.config.ignoreEmptyParagraph = true;
1323
1324/**
1325 * Fired when data is loaded and ready for retrieval in an editor instance.
1326 * @name CKEDITOR.editor#dataReady
1327 * @event
1328 */
1329
1330/**
1331 * Whether automatically create wrapping blocks around inline contents inside document body,
1332 * this helps to ensure the integrality of the block enter mode.
1333 * <strong>Note:</strong> Changing the default value might introduce unpredictable usability issues.
1334 * @name CKEDITOR.config.autoParagraph
1335 * @since 3.6
1336 * @type Boolean
1337 * @default true
1338 * @example
1339 * config.autoParagraph = false;
1340 */
1341
1342/**
1343 * Fired when some elements are added to the document
1344 * @name CKEDITOR.editor#ariaWidget
1345 * @event
1346 * @param {Object} element The element being added
1347 */
Note: See TracBrowser for help on using the repository browser.