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