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