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