JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
dc8329d9afee1c507bbc829cbac20fca74d90e2e
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview The "wysiwygarea" plugin. It registers the "wysiwyg" editing\r
8  *              mode, which handles the main editing area space.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         // List of elements in which has no way to move editing focus outside.\r
14         var nonExitableElementNames = { table:1,pre:1 };\r
15 \r
16         // Matching an empty paragraph at the end of document.\r
17         var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
18 \r
19         function onInsertHtml( evt )\r
20         {\r
21                 if ( this.mode == 'wysiwyg' )\r
22                 {\r
23                         this.focus();\r
24                         this.fire( 'saveSnapshot' );\r
25 \r
26                         var selection = this.getSelection(),\r
27                                 data = evt.data;\r
28 \r
29                         if ( this.dataProcessor )\r
30                                 data = this.dataProcessor.toHtml( data );\r
31 \r
32                         if ( CKEDITOR.env.ie )\r
33                         {\r
34                                 var selIsLocked = selection.isLocked;\r
35 \r
36                                 if ( selIsLocked )\r
37                                         selection.unlock();\r
38 \r
39                                 var $sel = selection.getNative();\r
40                                 if ( $sel.type == 'Control' )\r
41                                         $sel.clear();\r
42                                 $sel.createRange().pasteHTML( data );\r
43 \r
44                                 if ( selIsLocked )\r
45                                         this.getSelection().lock();\r
46                         }\r
47                         else\r
48                                 this.document.$.execCommand( 'inserthtml', false, data );\r
49 \r
50                         CKEDITOR.tools.setTimeout( function()\r
51                                 {\r
52                                         this.fire( 'saveSnapshot' );\r
53                                 }, 0, this );\r
54                 }\r
55         }\r
56 \r
57         function onInsertElement( evt )\r
58         {\r
59                 if ( this.mode == 'wysiwyg' )\r
60                 {\r
61                         this.focus();\r
62                         this.fire( 'saveSnapshot' );\r
63 \r
64                         var element = evt.data,\r
65                                 elementName = element.getName(),\r
66                                 isBlock = CKEDITOR.dtd.$block[ elementName ];\r
67 \r
68                         var selection = this.getSelection(),\r
69                                 ranges = selection.getRanges();\r
70 \r
71                         var selIsLocked = selection.isLocked;\r
72 \r
73                         if ( selIsLocked )\r
74                                 selection.unlock();\r
75 \r
76                         var range, clone, lastElement, bookmark;\r
77 \r
78                         for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
79                         {\r
80                                 range = ranges[ i ];\r
81 \r
82                                 // Remove the original contents.\r
83                                 range.deleteContents();\r
84 \r
85                                 clone = !i && element || element.clone( true );\r
86 \r
87                                 // If we're inserting a block at dtd-violated position, split\r
88                                 // the parent blocks until we reach blockLimit.\r
89                                 var current, dtd;\r
90                                 if ( isBlock )\r
91                                 {\r
92                                         while ( ( current = range.getCommonAncestor( false, true ) )\r
93                                                         && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
94                                                         && !( dtd && dtd [ elementName ] ) )\r
95                                         {\r
96                                                 // Split up inline elements.\r
97                                                 if ( current.getName() in CKEDITOR.dtd.span )\r
98                                                         range.splitElement( current );\r
99                                                 // If we're in an empty block which indicate a new paragraph,\r
100                                                 // simply replace it with the inserting block.(#3664)\r
101                                                 else if ( range.checkStartOfBlock()\r
102                                                          && range.checkEndOfBlock() )\r
103                                                 {\r
104                                                         range.setStartBefore( current );\r
105                                                         range.collapse( true );\r
106                                                         current.remove();\r
107                                                 }\r
108                                                 else\r
109                                                         range.splitBlock();\r
110                                         }\r
111                                 }\r
112 \r
113                                 // Insert the new node.\r
114                                 range.insertNode( clone );\r
115 \r
116                                 // Save the last element reference so we can make the\r
117                                 // selection later.\r
118                                 if ( !lastElement )\r
119                                         lastElement = clone;\r
120                         }\r
121 \r
122                         range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
123 \r
124                         var next = lastElement.getNextSourceNode( true );\r
125                         if ( next && next.type == CKEDITOR.NODE_ELEMENT )\r
126                                 range.moveToElementEditStart( next );\r
127 \r
128                         selection.selectRanges( [ range ] );\r
129 \r
130                         if ( selIsLocked )\r
131                                 this.getSelection().lock();\r
132 \r
133                         // Save snaps after the whole execution completed.\r
134                         // This's a workaround for make DOM modification's happened after\r
135                         // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'\r
136                         // call.\r
137                         CKEDITOR.tools.setTimeout( function(){\r
138                                 this.fire( 'saveSnapshot' );\r
139                         }, 0, this );\r
140                 }\r
141         }\r
142 \r
143         // DOM modification here should not bother dirty flag.(#4385)\r
144         function restoreDirty( editor )\r
145         {\r
146                 if ( !editor.checkDirty() )\r
147                         setTimeout( function(){ editor.resetDirty(); } );\r
148         }\r
149 \r
150         var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),\r
151                 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );\r
152 \r
153         function isNotEmpty( node )\r
154         {\r
155                 return isNotWhitespace( node ) && isNotBookmark( node );\r
156         }\r
157 \r
158         function isNbsp( node )\r
159         {\r
160                 return node.type == CKEDITOR.NODE_TEXT\r
161                            && CKEDITOR.tools.trim( node.getText() ).match( /^(?:&nbsp;|\xa0)$/ );\r
162         }\r
163 \r
164         function restoreSelection( selection )\r
165         {\r
166                 if ( selection.isLocked )\r
167                 {\r
168                         selection.unlock();\r
169                         setTimeout( function() { selection.lock(); }, 0 );\r
170                 }\r
171         }\r
172 \r
173         /**\r
174          *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
175          *  non-exitable-block by padding extra br.(#3189)\r
176          */\r
177         function onSelectionChangeFixBody( evt )\r
178         {\r
179                 var editor = evt.editor,\r
180                         path = evt.data.path,\r
181                         blockLimit = path.blockLimit,\r
182                         selection = evt.data.selection,\r
183                         range = selection.getRanges()[0],\r
184                         body = editor.document.getBody(),\r
185                         enterMode = editor.config.enterMode;\r
186 \r
187                 // When enterMode set to block, we'll establing new paragraph only if we're\r
188                 // selecting inline contents right under body. (#3657)\r
189                 if ( enterMode != CKEDITOR.ENTER_BR\r
190                      && range.collapsed\r
191                          && blockLimit.getName() == 'body'\r
192                          && !path.block )\r
193                 {\r
194                         restoreDirty( editor );\r
195                         CKEDITOR.env.ie && restoreSelection( selection );\r
196 \r
197                         var fixedBlock = range.fixBlock( true,\r
198                                         editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'  );\r
199 \r
200                         // For IE, we should remove any filler node which was introduced before.\r
201                         if ( CKEDITOR.env.ie )\r
202                         {\r
203                                 var first = fixedBlock.getFirst( isNotEmpty );\r
204                                 first && isNbsp( first ) && first.remove();\r
205                         }\r
206 \r
207                         // If the fixed block is blank and already followed by a exitable\r
208                         // block, we should revert the fix. (#3684)\r
209                         if ( fixedBlock.getOuterHtml().match( emptyParagraphRegexp ) )\r
210                         {\r
211                                 var previousElement = fixedBlock.getPrevious( isNotWhitespace ),\r
212                                         nextElement = fixedBlock.getNext( isNotWhitespace );\r
213 \r
214                                 if ( previousElement && previousElement.getName\r
215                                          && !( previousElement.getName() in nonExitableElementNames )\r
216                                          && range.moveToElementEditStart( previousElement )\r
217                                          || nextElement && nextElement.getName\r
218                                            && !( nextElement.getName() in nonExitableElementNames )\r
219                                            && range.moveToElementEditStart( nextElement ) )\r
220                                 {\r
221                                         fixedBlock.remove();\r
222                                 }\r
223                         }\r
224 \r
225                         range.select();\r
226                         // Notify non-IE that selection has changed.\r
227                         if ( !CKEDITOR.env.ie )\r
228                                 editor.selectionChange();\r
229                 }\r
230 \r
231                 // All browsers are incapable to moving cursor out of certain non-exitable\r
232                 // blocks (e.g. table, list, pre) at the end of document, make this happen by\r
233                 // place a bogus node there, which would be later removed by dataprocessor.\r
234                 var walkerRange = new CKEDITOR.dom.range( editor.document ),\r
235                         walker = new CKEDITOR.dom.walker( walkerRange );\r
236                 walkerRange.selectNodeContents( body );\r
237                 walker.evaluator = function( node )\r
238                 {\r
239                         return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in nonExitableElementNames );\r
240                 };\r
241                 walker.guard = function( node, isMoveout )\r
242                 {\r
243                         return !( ( node.type == CKEDITOR.NODE_TEXT && isNotWhitespace( node ) ) || isMoveout );\r
244                 };\r
245 \r
246                 if ( walker.previous() )\r
247                 {\r
248                         restoreDirty( editor );\r
249                         CKEDITOR.env.ie && restoreSelection( selection );\r
250 \r
251                         var paddingBlock;\r
252                         if ( enterMode != CKEDITOR.ENTER_BR )\r
253                                 paddingBlock = body.append( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
254                         else\r
255                                 paddingBlock = body;\r
256 \r
257                         if ( !CKEDITOR.env.ie )\r
258                                 paddingBlock.appendBogus();\r
259                 }\r
260         }\r
261 \r
262         CKEDITOR.plugins.add( 'wysiwygarea',\r
263         {\r
264                 requires : [ 'editingblock' ],\r
265 \r
266                 init : function( editor )\r
267                 {\r
268                         var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )\r
269                                 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;\r
270 \r
271                         var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );\r
272 \r
273                         editor.on( 'editingBlockReady', function()\r
274                                 {\r
275                                         var mainElement,\r
276                                                 iframe,\r
277                                                 isLoadingData,\r
278                                                 isPendingFocus,\r
279                                                 frameLoaded,\r
280                                                 fireMode;\r
281 \r
282 \r
283                                         // Support for custom document.domain in IE.\r
284                                         var isCustomDomain = CKEDITOR.env.isCustomDomain();\r
285 \r
286                                         // Creates the iframe that holds the editable document.\r
287                                         var createIFrame = function( data )\r
288                                         {\r
289                                                 if ( iframe )\r
290                                                         iframe.remove();\r
291 \r
292                                                 frameLoaded = 0;\r
293 \r
294                                                 var setDataFn = !CKEDITOR.env.gecko && CKEDITOR.tools.addFunction( function( doc )\r
295                                                         {\r
296                                                                 CKEDITOR.tools.removeFunction( setDataFn );\r
297                                                                 doc.write( data );\r
298                                                         });\r
299 \r
300                                                 var srcScript =\r
301                                                         'document.open();' +\r
302 \r
303                                                         // The document domain must be set any time we\r
304                                                         // call document.open().\r
305                                                         ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
306 \r
307                                                         ( ( 'parent.CKEDITOR.tools.callFunction(' + setDataFn + ',document);' ) ) +\r
308 \r
309                                                         'document.close();';\r
310 \r
311                                                 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
312                                                         ' style="width:100%;height:100%"' +\r
313                                                         ' frameBorder="0"' +\r
314                                                         ' title="' + frameLabel + '"' +\r
315                                                         // With FF, the 'src' attribute should be left empty to\r
316                                                         // trigger iframe's 'load' event.\r
317                                                         ' src="' + ( CKEDITOR.env.gecko ? '' : 'javascript:void(function(){' + encodeURIComponent( srcScript ) + '}())' ) + '"' +\r
318                                                         ' tabIndex="' + editor.tabIndex + '"' +\r
319                                                         ' allowTransparency="true"' +\r
320                                                         '></iframe>' );\r
321 \r
322                                                 // With FF, it's better to load the data on iframe.load. (#3894,#4058)\r
323                                                 CKEDITOR.env.gecko && iframe.on( 'load', function( ev )\r
324                                                         {\r
325                                                                 ev.removeListener();\r
326 \r
327                                                                 var doc = iframe.getFrameDocument().$;\r
328 \r
329                                                                 doc.open();\r
330                                                                 doc.write( data );\r
331                                                                 doc.close();\r
332                                                         });\r
333 \r
334                                                 mainElement.append( iframe );\r
335                                         };\r
336 \r
337                                         // The script that launches the bootstrap logic on 'domReady', so the document\r
338                                         // is fully editable even before the editing iframe is fully loaded (#4455).\r
339                                         var activationScript =\r
340                                                 '<script id="cke_actscrpt" type="text/javascript" cke_temp="1">' +\r
341                                                         ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
342                                                         'parent.CKEDITOR._["contentDomReady' + editor.name + '"]( window );' +\r
343                                                 '</script>';\r
344 \r
345                                         // Editing area bootstrap code.\r
346                                         var contentDomReady = function( domWindow )\r
347                                         {\r
348                                                 if ( frameLoaded )\r
349                                                         return;\r
350                                                 frameLoaded = 1;\r
351 \r
352                                                 editor.fire( 'ariaWidget', iframe );\r
353 \r
354                                                 var domDocument = domWindow.document,\r
355                                                         body = domDocument.body;\r
356 \r
357                                                 // Remove this script from the DOM.\r
358                                                 var script = domDocument.getElementById( "cke_actscrpt" );\r
359                                                 script.parentNode.removeChild( script );\r
360 \r
361                                                 delete CKEDITOR._[ 'contentDomReady' + editor.name ];\r
362 \r
363                                                 body.spellcheck = !editor.config.disableNativeSpellChecker;\r
364 \r
365                                                 if ( CKEDITOR.env.ie )\r
366                                                 {\r
367                                                         // Don't display the focus border.\r
368                                                         body.hideFocus = true;\r
369 \r
370                                                         // Disable and re-enable the body to avoid IE from\r
371                                                         // taking the editing focus at startup. (#141 / #523)\r
372                                                         body.disabled = true;\r
373                                                         body.contentEditable = true;\r
374                                                         body.removeAttribute( 'disabled' );\r
375                                                 }\r
376                                                 else\r
377                                                         domDocument.designMode = 'on';\r
378 \r
379                                                 // IE, Opera and Safari may not support it and throw\r
380                                                 // errors.\r
381                                                 try { domDocument.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}\r
382                                                 try { domDocument.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}\r
383 \r
384                                                 domWindow       = editor.window         = new CKEDITOR.dom.window( domWindow );\r
385                                                 domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
386 \r
387                                                 // Gecko/Webkit need some help when selecting control type elements. (#3448)\r
388                                                 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )\r
389                                                 {\r
390                                                         domDocument.on( 'mousedown', function( ev )\r
391                                                         {\r
392                                                                 var control = ev.data.getTarget();\r
393                                                                 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )\r
394                                                                         editor.getSelection().selectElement( control );\r
395                                                         } );\r
396                                                 }\r
397 \r
398                                                 // Webkit: avoid from editing form control elements content.\r
399                                                 if ( CKEDITOR.env.webkit )\r
400                                                 {\r
401                                                         // Prevent from tick checkbox/radiobox/select\r
402                                                         domDocument.on( 'click', function( ev )\r
403                                                         {\r
404                                                                 if ( ev.data.getTarget().is( 'input', 'select' ) )\r
405                                                                         ev.data.preventDefault();\r
406                                                         } );\r
407 \r
408                                                         // Prevent from editig textfield/textarea value.\r
409                                                         domDocument.on( 'mouseup', function( ev )\r
410                                                         {\r
411                                                                 if ( ev.data.getTarget().is( 'input', 'textarea' ) )\r
412                                                                         ev.data.preventDefault();\r
413                                                         } );\r
414                                                 }\r
415 \r
416                                                 // IE standard compliant in editing frame doesn't focus the editor when\r
417                                                 // clicking outside actual content, manually apply the focus. (#1659)\r
418                                                 if ( CKEDITOR.env.ie\r
419                                                         && domDocument.$.compatMode == 'CSS1Compat' )\r
420                                                 {\r
421                                                         var htmlElement = domDocument.getDocumentElement();\r
422                                                         htmlElement.on( 'mousedown', function( evt )\r
423                                                         {\r
424                                                                 // Setting focus directly on editor doesn't work, we\r
425                                                                 // have to use here a temporary element to 'redirect'\r
426                                                                 // the focus.\r
427                                                                 if ( evt.data.getTarget().equals( htmlElement ) )\r
428                                                                         ieFocusGrabber.focus();\r
429                                                         } );\r
430                                                 }\r
431 \r
432                                                 var focusTarget = ( CKEDITOR.env.ie || CKEDITOR.env.webkit ) ?\r
433                                                                 domWindow : domDocument;\r
434 \r
435                                                 focusTarget.on( 'blur', function()\r
436                                                         {\r
437                                                                 editor.focusManager.blur();\r
438                                                         });\r
439 \r
440                                                 focusTarget.on( 'focus', function()\r
441                                                         {\r
442                                                                 // Gecko need a key event to 'wake up' the editing\r
443                                                                 // ability when document is empty.(#3864)\r
444                                                                 if ( CKEDITOR.env.gecko )\r
445                                                                 {\r
446                                                                         var first = body;\r
447                                                                         while ( first.firstChild )\r
448                                                                                 first = first.firstChild;\r
449 \r
450                                                                         if ( !first.nextSibling\r
451                                                                                 && ( 'BR' == first.tagName )\r
452                                                                                 && first.hasAttribute( '_moz_editor_bogus_node' ) )\r
453                                                                         {\r
454                                                                                 restoreDirty( editor );\r
455                                                                                 var keyEventSimulate = domDocument.$.createEvent( "KeyEvents" );\r
456                                                                                 keyEventSimulate.initKeyEvent( 'keypress', true, true, domWindow.$, false,\r
457                                                                                         false, false, false, 0, 32 );\r
458                                                                                 domDocument.$.dispatchEvent( keyEventSimulate );\r
459                                                                                 var bogusText = domDocument.getBody().getFirst() ;\r
460                                                                                 // Compensate the line maintaining <br> if enterMode is not block.\r
461                                                                                 if ( editor.config.enterMode == CKEDITOR.ENTER_BR )\r
462                                                                                         domDocument.createElement( 'br', { attributes: { '_moz_dirty' : "" } } )\r
463                                                                                                 .replace( bogusText );\r
464                                                                                 else\r
465                                                                                         bogusText.remove();\r
466                                                                         }\r
467                                                                 }\r
468 \r
469                                                                 editor.focusManager.focus();\r
470                                                         });\r
471 \r
472                                                 var keystrokeHandler = editor.keystrokeHandler;\r
473                                                 if ( keystrokeHandler )\r
474                                                         keystrokeHandler.attach( domDocument );\r
475 \r
476                                                 if ( CKEDITOR.env.ie )\r
477                                                 {\r
478                                                         // Override keystrokes which should have deletion behavior\r
479                                                         //  on control types in IE . (#4047)\r
480                                                         domDocument.on( 'keydown', function( evt )\r
481                                                         {\r
482                                                                 var keyCode = evt.data.getKeystroke();\r
483 \r
484                                                                 // Backspace OR Delete.\r
485                                                                 if ( keyCode in { 8 : 1, 46 : 1 } )\r
486                                                                 {\r
487                                                                         var sel = editor.getSelection(),\r
488                                                                                 control = sel.getSelectedElement();\r
489 \r
490                                                                         if ( control )\r
491                                                                         {\r
492                                                                                 // Make undo snapshot.\r
493                                                                                 editor.fire( 'saveSnapshot' );\r
494 \r
495                                                                                 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will\r
496                                                                                 // break up the selection, safely manage it here. (#4795)\r
497                                                                                 var bookmark = sel.getRanges()[ 0 ].createBookmark();\r
498                                                                                 // Remove the control manually.\r
499                                                                                 control.remove();\r
500                                                                                 sel.selectBookmarks( [ bookmark ] );\r
501 \r
502                                                                                 editor.fire( 'saveSnapshot' );\r
503 \r
504                                                                                 evt.data.preventDefault();\r
505                                                                         }\r
506                                                                 }\r
507                                                         } );\r
508 \r
509                                                         // PageUp/PageDown scrolling is broken in document\r
510                                                         // with standard doctype, manually fix it. (#4736)\r
511                                                         if ( domDocument.$.compatMode == 'CSS1Compat' )\r
512                                                         {\r
513                                                                 var pageUpDownKeys = { 33 : 1, 34 : 1 };\r
514                                                                 domDocument.on( 'keydown', function( evt )\r
515                                                                 {\r
516                                                                         if ( evt.data.getKeystroke() in pageUpDownKeys )\r
517                                                                         {\r
518                                                                                 setTimeout( function ()\r
519                                                                                 {\r
520                                                                                         editor.getSelection().scrollIntoView();\r
521                                                                                 }, 0 );\r
522                                                                         }\r
523                                                                 } );\r
524                                                         }\r
525                                                 }\r
526 \r
527                                                 // Adds the document body as a context menu target.\r
528                                                 if ( editor.contextMenu )\r
529                                                         editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );\r
530 \r
531                                                 setTimeout( function()\r
532                                                         {\r
533                                                                 editor.fire( 'contentDom' );\r
534 \r
535                                                                 if ( fireMode )\r
536                                                                 {\r
537                                                                         editor.mode = 'wysiwyg';\r
538                                                                         editor.fire( 'mode' );\r
539                                                                         fireMode = false;\r
540                                                                 }\r
541 \r
542                                                                 isLoadingData = false;\r
543 \r
544                                                                 if ( isPendingFocus )\r
545                                                                 {\r
546                                                                         editor.focus();\r
547                                                                         isPendingFocus = false;\r
548                                                                 }\r
549                                                                 setTimeout( function()\r
550                                                                 {\r
551                                                                         editor.fire( 'dataReady' );\r
552                                                                 }, 0 );\r
553 \r
554                                                                 /*\r
555                                                                  * IE BUG: IE might have rendered the iframe with invisible contents.\r
556                                                                  * (#3623). Push some inconsequential CSS style changes to force IE to\r
557                                                                  * refresh it.\r
558                                                                  *\r
559                                                                  * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not\r
560                                                                  * fix the problem. :(\r
561                                                                  */\r
562                                                                 if ( CKEDITOR.env.ie )\r
563                                                                 {\r
564                                                                         setTimeout( function()\r
565                                                                                 {\r
566                                                                                         if ( editor.document )\r
567                                                                                         {\r
568                                                                                                 var $body = editor.document.$.body;\r
569                                                                                                 $body.runtimeStyle.marginBottom = '0px';\r
570                                                                                                 $body.runtimeStyle.marginBottom = '';\r
571                                                                                         }\r
572                                                                                 }, 1000 );\r
573                                                                 }\r
574                                                         },\r
575                                                         0 );\r
576                                         };\r
577 \r
578                                         editor.addMode( 'wysiwyg',\r
579                                                 {\r
580                                                         load : function( holderElement, data, isSnapshot )\r
581                                                         {\r
582                                                                 mainElement = holderElement;\r
583 \r
584                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )\r
585                                                                         holderElement.setStyle( 'position', 'relative' );\r
586 \r
587                                                                 // The editor data "may be dirty" after this\r
588                                                                 // point.\r
589                                                                 editor.mayBeDirty = true;\r
590 \r
591                                                                 fireMode = true;\r
592 \r
593                                                                 if ( isSnapshot )\r
594                                                                         this.loadSnapshotData( data );\r
595                                                                 else\r
596                                                                         this.loadData( data );\r
597                                                         },\r
598 \r
599                                                         loadData : function( data )\r
600                                                         {\r
601                                                                 isLoadingData = true;\r
602 \r
603                                                                 var config = editor.config,\r
604                                                                         fullPage = config.fullPage,\r
605                                                                         docType = config.docType;\r
606 \r
607                                                                 // Build the additional stuff to be included into <head>.\r
608                                                                 var headExtra =\r
609                                                                         '<style type="text/css" cke_temp="1">' +\r
610                                                                                 editor._.styles.join( '\n' ) +\r
611                                                                         '</style>';\r
612 \r
613                                                                 !fullPage && ( headExtra =\r
614                                                                         CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +\r
615                                                                         headExtra );\r
616 \r
617                                                                 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" cke_temp="1" />' : '';\r
618 \r
619                                                                 if ( fullPage )\r
620                                                                 {\r
621                                                                         // Search and sweep out the doctype declaration.\r
622                                                                         data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )\r
623                                                                                 {\r
624                                                                                         editor.docType = docType = match;\r
625                                                                                         return '';\r
626                                                                                 });\r
627                                                                 }\r
628 \r
629                                                                 // Get the HTML version of the data.\r
630                                                                 if ( editor.dataProcessor )\r
631                                                                         data = editor.dataProcessor.toHtml( data, fixForBody );\r
632 \r
633                                                                 if ( fullPage )\r
634                                                                 {\r
635                                                                         // Check if the <body> tag is available.\r
636                                                                         if ( !(/<body[\s|>]/).test( data ) )\r
637                                                                                 data = '<body>' + data;\r
638 \r
639                                                                         // Check if the <html> tag is available.\r
640                                                                         if ( !(/<html[\s|>]/).test( data ) )\r
641                                                                                 data = '<html>' + data + '</html>';\r
642 \r
643                                                                         // Check if the <head> tag is available.\r
644                                                                         if ( !(/<head[\s|>]/).test( data ) )\r
645                                                                                 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;\r
646 \r
647                                                                         // The base must be the first tag in the HEAD, e.g. to get relative\r
648                                                                         // links on styles.\r
649                                                                         baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );\r
650 \r
651                                                                         // Inject the extra stuff into <head>.\r
652                                                                         // Attention: do not change it before testing it well. (V2)\r
653                                                                         // This is tricky... if the head ends with <meta ... content type>,\r
654                                                                         // Firefox will break. But, it works if we place our extra stuff as\r
655                                                                         // the last elements in the HEAD.\r
656                                                                         data = data.replace( /<\/head\s*>/, headExtra + '$&' );\r
657 \r
658                                                                         // Add the DOCTYPE back to it.\r
659                                                                         data = docType + data;\r
660                                                                 }\r
661                                                                 else\r
662                                                                 {\r
663                                                                         data =\r
664                                                                                 config.docType +\r
665                                                                                 '<html dir="' + config.contentsLangDirection + '">' +\r
666                                                                                 '<title>' + frameLabel + '</title>' +\r
667                                                                                 '<head>' +\r
668                                                                                         baseTag +\r
669                                                                                         headExtra +\r
670                                                                                 '</head>' +\r
671                                                                                 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +\r
672                                                                                                   ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +\r
673                                                                                                   '>' +\r
674                                                                                         data +\r
675                                                                                 '</html>';\r
676                                                                 }\r
677 \r
678                                                                 data += activationScript;\r
679 \r
680                                                                 CKEDITOR._[ 'contentDomReady' + editor.name ] = contentDomReady;\r
681 \r
682                                                                 // The iframe is recreated on each call of setData, so we need to clear DOM objects\r
683                                                                 this.onDispose();\r
684                                                                 createIFrame( data );\r
685                                                         },\r
686 \r
687                                                         getData : function()\r
688                                                         {\r
689                                                                 var config = editor.config,\r
690                                                                         fullPage = config.fullPage,\r
691                                                                         docType = fullPage && editor.docType,\r
692                                                                         doc = iframe.getFrameDocument();\r
693 \r
694                                                                 var data = fullPage\r
695                                                                         ? doc.getDocumentElement().getOuterHtml()\r
696                                                                         : doc.getBody().getHtml();\r
697 \r
698                                                                 if ( editor.dataProcessor )\r
699                                                                         data = editor.dataProcessor.toDataFormat( data, fixForBody );\r
700 \r
701                                                                 // Strip the last blank paragraph within document.\r
702                                                                 if ( config.ignoreEmptyParagraph )\r
703                                                                         data = data.replace( emptyParagraphRegexp, '' );\r
704 \r
705                                                                 if ( docType )\r
706                                                                         data = docType + '\n' + data;\r
707 \r
708                                                                 return data;\r
709                                                         },\r
710 \r
711                                                         getSnapshotData : function()\r
712                                                         {\r
713                                                                 return iframe.getFrameDocument().getBody().getHtml();\r
714                                                         },\r
715 \r
716                                                         loadSnapshotData : function( data )\r
717                                                         {\r
718                                                                 iframe.getFrameDocument().getBody().setHtml( data );\r
719                                                         },\r
720 \r
721                                                         onDispose : function()\r
722                                                         {\r
723                                                                 if ( !editor.document )\r
724                                                                         return;\r
725 \r
726                                                                 editor.document.getDocumentElement().clearCustomData();\r
727                                                                 editor.document.getBody().clearCustomData();\r
728 \r
729                                                                 editor.window.clearCustomData();\r
730                                                                 editor.document.clearCustomData();\r
731 \r
732                                                                 iframe.clearCustomData();\r
733                                                         },\r
734 \r
735                                                         unload : function( holderElement )\r
736                                                         {\r
737                                                                 this.onDispose();\r
738 \r
739                                                                 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;\r
740 \r
741                                                                 editor.fire( 'contentDomUnload' );\r
742                                                         },\r
743 \r
744                                                         focus : function()\r
745                                                         {\r
746                                                                 if ( isLoadingData )\r
747                                                                         isPendingFocus = true;\r
748                                                                 else if ( editor.window )\r
749                                                                 {\r
750                                                                         editor.window.focus();\r
751                                                                         editor.selectionChange();\r
752                                                                 }\r
753                                                         }\r
754                                                 });\r
755 \r
756                                         editor.on( 'insertHtml', onInsertHtml, null, null, 20 );\r
757                                         editor.on( 'insertElement', onInsertElement, null, null, 20 );\r
758                                         // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)\r
759                                         editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );\r
760                                 });\r
761 \r
762                         var titleBackup;\r
763                         // Setting voice label as window title, backup the original one\r
764                         // and restore it before running into use.\r
765                         editor.on( 'contentDom', function ()\r
766                                 {\r
767                                         var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );\r
768                                         title.setAttribute( '_cke_title', editor.document.$.title );\r
769                                         editor.document.$.title = frameLabel;\r
770                                 });\r
771 \r
772 \r
773                         // Create an invisible element to grab focus.\r
774                         if ( CKEDITOR.env.ie )\r
775                         {\r
776                                 var ieFocusGrabber;\r
777                                 editor.on( 'uiReady', function()\r
778                                 {\r
779                                         ieFocusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(\r
780                                                 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)\r
781                                                 '<span tabindex="-1" style="position:absolute; left:-10000" role="presentation"></span>' ) );\r
782 \r
783                                         ieFocusGrabber.on( 'focus', function()\r
784                                                 {\r
785                                                         editor.focus();\r
786                                                 } );\r
787                                 } );\r
788                                 editor.on( 'destroy', function()\r
789                                 {\r
790                                         ieFocusGrabber.clearCustomData();\r
791                                 } );\r
792                         }\r
793                 }\r
794         });\r
795 \r
796         // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)\r
797         if ( CKEDITOR.env.gecko )\r
798         {\r
799                 ( function ()\r
800                 {\r
801                         var body = document.body;\r
802 \r
803                         if ( !body )\r
804                                 window.addEventListener( 'load', arguments.callee, false );\r
805                         else\r
806                         {\r
807                                 body.setAttribute( 'onpageshow', body.getAttribute( 'onpageshow' )\r
808                                                 + ';event.persisted && CKEDITOR.tools.callFunction(' +\r
809                                                 CKEDITOR.tools.addFunction( function()\r
810                                                 {\r
811                                                         var allInstances = CKEDITOR.instances,\r
812                                                                 editor,\r
813                                                                 doc;\r
814                                                         for ( var i in allInstances )\r
815                                                         {\r
816                                                                 editor = allInstances[ i ];\r
817                                                                 doc = editor.document;\r
818                                                                 if ( doc )\r
819                                                                 {\r
820                                                                         doc.$.designMode = 'off';\r
821                                                                         doc.$.designMode = 'on';\r
822                                                                 }\r
823                                                         }\r
824                                                 } ) + ')' );\r
825                         }\r
826                 } )();\r
827 \r
828         }\r
829 })();\r
830 \r
831 /**\r
832  * Disables the ability of resize objects (image and tables) in the editing\r
833  * area.\r
834  * @type Boolean\r
835  * @default false\r
836  * @example\r
837  * config.disableObjectResizing = true;\r
838  */\r
839 CKEDITOR.config.disableObjectResizing = false;\r
840 \r
841 /**\r
842  * Disables the "table tools" offered natively by the browser (currently\r
843  * Firefox only) to make quick table editing operations, like adding or\r
844  * deleting rows and columns.\r
845  * @type Boolean\r
846  * @default true\r
847  * @example\r
848  * config.disableNativeTableHandles = false;\r
849  */\r
850 CKEDITOR.config.disableNativeTableHandles = true;\r
851 \r
852 /**\r
853  * Disables the built-in spell checker while typing natively available in the\r
854  * browser (currently Firefox and Safari only).<br /><br />\r
855  *\r
856  * Even if word suggestions will not appear in the CKEditor context menu, this\r
857  * feature is useful to help quickly identifying misspelled words.<br /><br />\r
858  *\r
859  * This setting is currently compatible with Firefox only due to limitations in\r
860  * other browsers.\r
861  * @type Boolean\r
862  * @default true\r
863  * @example\r
864  * config.disableNativeSpellChecker = false;\r
865  */\r
866 CKEDITOR.config.disableNativeSpellChecker = true;\r
867 \r
868 /**\r
869  * Whether the editor must output an empty value ("") if it's contents is made\r
870  * by an empty paragraph only.\r
871  * @type Boolean\r
872  * @default true\r
873  * @example\r
874  * config.ignoreEmptyParagraph = false;\r
875  */\r
876 CKEDITOR.config.ignoreEmptyParagraph = true;\r
877 \r
878 /**\r
879  * Fired when data is loaded and ready for retrieval in an editor instance.\r
880  * @name CKEDITOR.editor#dataReady\r
881  * @event\r
882  */\r