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