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