JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.3.2
[ckeditor.git] / _source / plugins / wysiwygarea / plugin.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview The "wysiwygarea" plugin. It registers the "wysiwyg" editing\r
8  *              mode, which handles the main editing area space.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         // List of elements in which has no way to move editing focus outside.\r
14         var nonExitableElementNames = { table:1,pre:1 };\r
15 \r
16         // Matching an empty paragraph at the end of document.\r
17         var emptyParagraphRegexp = /\s*<(p|div|address|h\d|center|li)[^>]*>\s*(?:<br[^>]*>|&nbsp;|\u00A0|&#160;)?\s*(:?<\/\1>)?\s*(?=$|<\/body>)/gi;\r
18 \r
19         var notWhitespaceEval = CKEDITOR.dom.walker.whitespaces( true );\r
20 \r
21         function onInsertHtml( evt )\r
22         {\r
23                 if ( this.mode == 'wysiwyg' )\r
24                 {\r
25                         this.focus();\r
26                         this.fire( 'saveSnapshot' );\r
27 \r
28                         var selection = this.getSelection(),\r
29                                 data = evt.data;\r
30 \r
31                         if ( this.dataProcessor )\r
32                                 data = this.dataProcessor.toHtml( data );\r
33 \r
34                         if ( CKEDITOR.env.ie )\r
35                         {\r
36                                 var selIsLocked = selection.isLocked;\r
37 \r
38                                 if ( selIsLocked )\r
39                                         selection.unlock();\r
40 \r
41                                 var $sel = selection.getNative();\r
42                                 if ( $sel.type == 'Control' )\r
43                                         $sel.clear();\r
44                                 $sel.createRange().pasteHTML( data );\r
45 \r
46                                 if ( selIsLocked )\r
47                                         this.getSelection().lock();\r
48                         }\r
49                         else\r
50                                 this.document.$.execCommand( 'inserthtml', false, data );\r
51 \r
52                         // Webkit does not scroll to the cursor position after pasting (#5558)\r
53                         if ( CKEDITOR.env.webkit )\r
54                         {\r
55                                 this.document.$.execCommand( 'inserthtml', false, '<span id="cke_paste_marker" cke_temp="1"></span>' );\r
56                                 var marker = this.document.getById( 'cke_paste_marker' );\r
57                                 marker.scrollIntoView();\r
58                                 marker.remove();\r
59                         }\r
60 \r
61                         CKEDITOR.tools.setTimeout( function()\r
62                                 {\r
63                                         this.fire( 'saveSnapshot' );\r
64                                 }, 0, this );\r
65                 }\r
66         }\r
67 \r
68         function onInsertElement( evt )\r
69         {\r
70                 if ( this.mode == 'wysiwyg' )\r
71                 {\r
72                         this.focus();\r
73                         this.fire( 'saveSnapshot' );\r
74 \r
75                         var element = evt.data,\r
76                                 elementName = element.getName(),\r
77                                 isBlock = CKEDITOR.dtd.$block[ elementName ];\r
78 \r
79                         var selection = this.getSelection(),\r
80                                 ranges = selection.getRanges();\r
81 \r
82                         var selIsLocked = selection.isLocked;\r
83 \r
84                         if ( selIsLocked )\r
85                                 selection.unlock();\r
86 \r
87                         var range, clone, lastElement, bookmark;\r
88 \r
89                         for ( var i = ranges.length - 1 ; i >= 0 ; i-- )\r
90                         {\r
91                                 range = ranges[ i ];\r
92 \r
93                                 // Remove the original contents.\r
94                                 range.deleteContents();\r
95 \r
96                                 clone = !i && element || element.clone( true );\r
97 \r
98                                 // If we're inserting a block at dtd-violated position, split\r
99                                 // the parent blocks until we reach blockLimit.\r
100                                 var current, dtd;\r
101                                 if ( isBlock )\r
102                                 {\r
103                                         while ( ( current = range.getCommonAncestor( false, true ) )\r
104                                                         && ( dtd = CKEDITOR.dtd[ current.getName() ] )\r
105                                                         && !( dtd && dtd [ elementName ] ) )\r
106                                         {\r
107                                                 // Split up inline elements.\r
108                                                 if ( current.getName() in CKEDITOR.dtd.span )\r
109                                                         range.splitElement( current );\r
110                                                 // If we're in an empty block which indicate a new paragraph,\r
111                                                 // simply replace it with the inserting block.(#3664)\r
112                                                 else if ( range.checkStartOfBlock()\r
113                                                          && range.checkEndOfBlock() )\r
114                                                 {\r
115                                                         range.setStartBefore( current );\r
116                                                         range.collapse( true );\r
117                                                         current.remove();\r
118                                                 }\r
119                                                 else\r
120                                                         range.splitBlock();\r
121                                         }\r
122                                 }\r
123 \r
124                                 // Insert the new node.\r
125                                 range.insertNode( clone );\r
126 \r
127                                 // Save the last element reference so we can make the\r
128                                 // selection later.\r
129                                 if ( !lastElement )\r
130                                         lastElement = clone;\r
131                         }\r
132 \r
133                         range.moveToPosition( lastElement, CKEDITOR.POSITION_AFTER_END );\r
134 \r
135                         // If we're inserting a block element immediatelly followed by\r
136                         // another block element, the selection must move there. (#3100,#5436)\r
137                         if ( isBlock )\r
138                         {\r
139                                 var next = lastElement.getNext( notWhitespaceEval ),\r
140                                         nextName = next && next.type == CKEDITOR.NODE_ELEMENT && next.getName();\r
141 \r
142                                 // Check if it's a block element that accepts text.\r
143                                 if ( nextName && CKEDITOR.dtd.$block[ nextName ] && CKEDITOR.dtd[ nextName ]['#'] )\r
144                                         range.moveToElementEditStart( next );\r
145                         }\r
146 \r
147                         selection.selectRanges( [ range ] );\r
148 \r
149                         if ( selIsLocked )\r
150                                 this.getSelection().lock();\r
151 \r
152                         // Save snaps after the whole execution completed.\r
153                         // This's a workaround for make DOM modification's happened after\r
154                         // 'insertElement' to be included either, e.g. Form-based dialogs' 'commitContents'\r
155                         // call.\r
156                         CKEDITOR.tools.setTimeout( function(){\r
157                                 this.fire( 'saveSnapshot' );\r
158                         }, 0, this );\r
159                 }\r
160         }\r
161 \r
162         // DOM modification here should not bother dirty flag.(#4385)\r
163         function restoreDirty( editor )\r
164         {\r
165                 if ( !editor.checkDirty() )\r
166                         setTimeout( function(){ editor.resetDirty(); } );\r
167         }\r
168 \r
169         var isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true ),\r
170                 isNotBookmark = CKEDITOR.dom.walker.bookmark( false, true );\r
171 \r
172         function isNotEmpty( node )\r
173         {\r
174                 return isNotWhitespace( node ) && isNotBookmark( node );\r
175         }\r
176 \r
177         function isNbsp( node )\r
178         {\r
179                 return node.type == CKEDITOR.NODE_TEXT\r
180                            && CKEDITOR.tools.trim( node.getText() ).match( /^(?:&nbsp;|\xa0)$/ );\r
181         }\r
182 \r
183         function restoreSelection( selection )\r
184         {\r
185                 if ( selection.isLocked )\r
186                 {\r
187                         selection.unlock();\r
188                         setTimeout( function() { selection.lock(); }, 0 );\r
189                 }\r
190         }\r
191 \r
192         function isBlankParagraph( block )\r
193         {\r
194                 return block.getOuterHtml().match( emptyParagraphRegexp );\r
195         }\r
196 \r
197         isNotWhitespace = CKEDITOR.dom.walker.whitespaces( true );\r
198 \r
199         // Gecko need a key event to 'wake up' the editing\r
200         // ability when document is empty.(#3864, #5781)\r
201         function activateEditing( editor )\r
202         {\r
203                 var win = editor.window,\r
204                         doc = editor.document,\r
205                         body = editor.document.getBody(),\r
206                         bodyChildsNum = body.getChildren().count();\r
207 \r
208                 if ( !bodyChildsNum || ( bodyChildsNum == 1&& body.getFirst().hasAttribute( '_moz_editor_bogus_node' ) ) )\r
209                 {\r
210                         restoreDirty( editor );\r
211 \r
212                         // Simulating keyboard character input by dispatching a keydown of white-space text.\r
213                         var keyEventSimulate = doc.$.createEvent( "KeyEvents" );\r
214                         keyEventSimulate.initKeyEvent( 'keypress', true, true, win.$, false,\r
215                                 false, false, false, 0, 32 );\r
216                         doc.$.dispatchEvent( keyEventSimulate );\r
217 \r
218                         // Restore the original document status by placing the cursor before a bogus br created (#5021).\r
219                         bodyChildsNum && body.getFirst().remove();\r
220                         doc.getBody().appendBogus();\r
221                         var nativeRange = new CKEDITOR.dom.range( doc );\r
222                         nativeRange.setStartAt( body , CKEDITOR.POSITION_AFTER_START );\r
223                         nativeRange.select();\r
224                 }\r
225         }\r
226 \r
227         /**\r
228          *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent\r
229          *  non-exitable-block by padding extra br.(#3189)\r
230          */\r
231         function onSelectionChangeFixBody( evt )\r
232         {\r
233                 var editor = evt.editor,\r
234                         path = evt.data.path,\r
235                         blockLimit = path.blockLimit,\r
236                         selection = evt.data.selection,\r
237                         range = selection.getRanges()[0],\r
238                         body = editor.document.getBody(),\r
239                         enterMode = editor.config.enterMode;\r
240 \r
241                 CKEDITOR.env.gecko && activateEditing( editor );\r
242 \r
243                 // When enterMode set to block, we'll establing new paragraph only if we're\r
244                 // selecting inline contents right under body. (#3657)\r
245                 if ( enterMode != CKEDITOR.ENTER_BR\r
246                      && range.collapsed\r
247                          && blockLimit.getName() == 'body'\r
248                          && !path.block )\r
249                 {\r
250                         editor.fire( 'updateSnapshot' );\r
251                         restoreDirty( editor );\r
252                         CKEDITOR.env.ie && restoreSelection( selection );\r
253 \r
254                         var fixedBlock = range.fixBlock( true,\r
255                                         editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'  );\r
256 \r
257                         // For IE, we should remove any filler node which was introduced before.\r
258                         if ( CKEDITOR.env.ie )\r
259                         {\r
260                                 var first = fixedBlock.getFirst( isNotEmpty );\r
261                                 first && isNbsp( first ) && first.remove();\r
262                         }\r
263 \r
264                         // If the fixed block is actually blank and is already followed by an exitable blank\r
265                         // block, we should revert the fix and move into the existed one. (#3684)\r
266                         if ( isBlankParagraph( fixedBlock ) )\r
267                         {\r
268                                 var element = fixedBlock.getNext( isNotWhitespace );\r
269                                 if ( element &&\r
270                                          element.type == CKEDITOR.NODE_ELEMENT &&\r
271                                          !nonExitableElementNames[ element.getName() ] )\r
272                                 {\r
273                                         range.moveToElementEditStart( element );\r
274                                         fixedBlock.remove();\r
275                                 }\r
276                                 else\r
277                                 {\r
278                                         element = fixedBlock.getPrevious( isNotWhitespace );\r
279                                         if ( element &&\r
280                                                  element.type == CKEDITOR.NODE_ELEMENT &&\r
281                                                  !nonExitableElementNames[ element.getName() ] )\r
282                                         {\r
283                                                 range.moveToElementEditEnd( element );\r
284                                                 fixedBlock.remove();\r
285                                         }\r
286                                 }\r
287                         }\r
288 \r
289                         range.select();\r
290                         // Notify non-IE that selection has changed.\r
291                         if ( !CKEDITOR.env.ie )\r
292                                 editor.selectionChange();\r
293                 }\r
294 \r
295                 // All browsers are incapable to moving cursor out of certain non-exitable\r
296                 // blocks (e.g. table, list, pre) at the end of document, make this happen by\r
297                 // place a bogus node there, which would be later removed by dataprocessor.\r
298                 var walkerRange = new CKEDITOR.dom.range( editor.document ),\r
299                         walker = new CKEDITOR.dom.walker( walkerRange );\r
300                 walkerRange.selectNodeContents( body );\r
301                 walker.evaluator = function( node )\r
302                 {\r
303                         return node.type == CKEDITOR.NODE_ELEMENT && ( node.getName() in nonExitableElementNames );\r
304                 };\r
305                 walker.guard = function( node, isMoveout )\r
306                 {\r
307                         return !( ( node.type == CKEDITOR.NODE_TEXT && isNotWhitespace( node ) ) || isMoveout );\r
308                 };\r
309 \r
310                 if ( walker.previous() )\r
311                 {\r
312                         editor.fire( 'updateSnapshot' );\r
313                         restoreDirty( editor );\r
314                         CKEDITOR.env.ie && restoreSelection( selection );\r
315 \r
316                         var paddingBlock;\r
317                         if ( enterMode != CKEDITOR.ENTER_BR )\r
318                                 paddingBlock = body.append( new CKEDITOR.dom.element( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) );\r
319                         else\r
320                                 paddingBlock = body;\r
321 \r
322                         if ( !CKEDITOR.env.ie )\r
323                                 paddingBlock.appendBogus();\r
324                 }\r
325         }\r
326 \r
327         CKEDITOR.plugins.add( 'wysiwygarea',\r
328         {\r
329                 requires : [ 'editingblock' ],\r
330 \r
331                 init : function( editor )\r
332                 {\r
333                         var fixForBody = ( editor.config.enterMode != CKEDITOR.ENTER_BR )\r
334                                 ? editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p' : false;\r
335 \r
336                         var frameLabel = editor.lang.editorTitle.replace( '%1', editor.name );\r
337 \r
338                         var contentDomReadyHandler;\r
339                         editor.on( 'editingBlockReady', function()\r
340                                 {\r
341                                         var mainElement,\r
342                                                 iframe,\r
343                                                 isLoadingData,\r
344                                                 isPendingFocus,\r
345                                                 frameLoaded,\r
346                                                 fireMode;\r
347 \r
348 \r
349                                         // Support for custom document.domain in IE.\r
350                                         var isCustomDomain = CKEDITOR.env.isCustomDomain();\r
351 \r
352                                         // Creates the iframe that holds the editable document.\r
353                                         var createIFrame = function( data )\r
354                                         {\r
355                                                 if ( iframe )\r
356                                                         iframe.remove();\r
357 \r
358 \r
359                                                 var srcScript =\r
360                                                         'document.open();' +\r
361 \r
362                                                         // The document domain must be set any time we\r
363                                                         // call document.open().\r
364                                                         ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
365 \r
366                                                         'document.close();';\r
367 \r
368                                                 iframe = CKEDITOR.dom.element.createFromHtml( '<iframe' +\r
369                                                         ' style="width:100%;height:100%"' +\r
370                                                         ' frameBorder="0"' +\r
371                                                         ' title="' + frameLabel + '"' +\r
372                                                         // With IE, the custom domain has to be taken care at first,\r
373                                                         // for other browers, the 'src' attribute should be left empty to\r
374                                                         // trigger iframe's 'load' event.\r
375                                                         ' src="' + ( CKEDITOR.env.ie ? 'javascript:void(function(){' + encodeURIComponent( srcScript ) + '}())' : '' ) + '"' +\r
376                                                         ' tabIndex="' + ( CKEDITOR.env.webkit? -1 : editor.tabIndex ) + '"' +\r
377                                                         ' allowTransparency="true"' +\r
378                                                         '></iframe>' );\r
379 \r
380                                                 // With FF, it's better to load the data on iframe.load. (#3894,#4058)\r
381                                                 iframe.on( 'load', function( ev )\r
382                                                         {\r
383                                                                 frameLoaded = 1;\r
384                                                                 ev.removeListener();\r
385 \r
386                                                                 var doc = iframe.getFrameDocument().$;\r
387 \r
388                                                                 // Don't leave any history log in IE. (#5657)\r
389                                                                 doc.open( "text/html","replace" );\r
390                                                                 doc.write( data );\r
391                                                                 doc.close();\r
392                                                         });\r
393 \r
394                                                 mainElement.append( iframe );\r
395                                         };\r
396 \r
397                                         // The script that launches the bootstrap logic on 'domReady', so the document\r
398                                         // is fully editable even before the editing iframe is fully loaded (#4455).\r
399                                         contentDomReadyHandler = CKEDITOR.tools.addFunction( contentDomReady );\r
400                                         var activationScript =\r
401                                                 '<script id="cke_actscrpt" type="text/javascript" cke_temp="1">' +\r
402                                                         ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) +\r
403                                                         'window.parent.CKEDITOR.tools.callFunction( ' + contentDomReadyHandler + ', window );' +\r
404                                                 '</script>';\r
405 \r
406                                         // Editing area bootstrap code.\r
407                                         function contentDomReady( domWindow )\r
408                                         {\r
409                                                 if ( !frameLoaded )\r
410                                                         return;\r
411                                                 frameLoaded = 0;\r
412 \r
413                                                 editor.fire( 'ariaWidget', iframe );\r
414 \r
415                                                 var domDocument = domWindow.document,\r
416                                                         body = domDocument.body;\r
417 \r
418                                                 // Remove this script from the DOM.\r
419                                                 var script = domDocument.getElementById( "cke_actscrpt" );\r
420                                                 script.parentNode.removeChild( script );\r
421 \r
422                                                 body.spellcheck = !editor.config.disableNativeSpellChecker;\r
423 \r
424                                                 if ( CKEDITOR.env.ie )\r
425                                                 {\r
426                                                         // Don't display the focus border.\r
427                                                         body.hideFocus = true;\r
428 \r
429                                                         // Disable and re-enable the body to avoid IE from\r
430                                                         // taking the editing focus at startup. (#141 / #523)\r
431                                                         body.disabled = true;\r
432                                                         body.contentEditable = true;\r
433                                                         body.removeAttribute( 'disabled' );\r
434                                                 }\r
435                                                 else\r
436                                                 {\r
437                                                         // Avoid opening design mode in a frame window thread,\r
438                                                         // which will cause host page scrolling.(#4397)\r
439                                                         setTimeout( function()\r
440                                                         {\r
441                                                                 // Prefer 'contentEditable' instead of 'designMode'. (#3593)\r
442                                                                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900\r
443                                                                                 || CKEDITOR.env.opera )\r
444                                                                         domDocument.$.body.contentEditable = true;\r
445                                                                 else if ( CKEDITOR.env.webkit )\r
446                                                                         domDocument.$.body.parentNode.contentEditable = true;\r
447                                                                 else\r
448                                                                         domDocument.$.designMode = 'on';\r
449                                                         }, 0 );\r
450                                                 }\r
451 \r
452                                                 CKEDITOR.env.gecko && CKEDITOR.tools.setTimeout( activateEditing, 0, null, editor );\r
453 \r
454                                                 domWindow       = editor.window         = new CKEDITOR.dom.window( domWindow );\r
455                                                 domDocument     = editor.document       = new CKEDITOR.dom.document( domDocument );\r
456 \r
457                                                 domDocument.on( 'dblclick', function( evt )\r
458                                                 {\r
459                                                         var element = evt.data.getTarget(),\r
460                                                                 data = { element : element, dialog : '' };\r
461                                                         editor.fire( 'doubleclick', data );\r
462                                                         data.dialog && editor.openDialog( data.dialog );\r
463                                                 });\r
464 \r
465                                                 // Gecko/Webkit need some help when selecting control type elements. (#3448)\r
466                                                 if ( !( CKEDITOR.env.ie || CKEDITOR.env.opera) )\r
467                                                 {\r
468                                                         domDocument.on( 'mousedown', function( ev )\r
469                                                         {\r
470                                                                 var control = ev.data.getTarget();\r
471                                                                 if ( control.is( 'img', 'hr', 'input', 'textarea', 'select' ) )\r
472                                                                         editor.getSelection().selectElement( control );\r
473                                                         } );\r
474                                                 }\r
475 \r
476                                                 if ( CKEDITOR.env.gecko )\r
477                                                 {\r
478                                                         domDocument.on( 'mouseup', function( ev )\r
479                                                         {\r
480                                                                 if ( ev.data.$.button == 2 )\r
481                                                                 {\r
482                                                                         var target = ev.data.getTarget();\r
483 \r
484                                                                         // Prevent right click from selecting an empty block even\r
485                                                                         // when selection is anchored inside it. (#5845)\r
486                                                                         if ( !target.getOuterHtml().replace( emptyParagraphRegexp, '' ) )\r
487                                                                         {\r
488                                                                                 var range = new CKEDITOR.dom.range( domDocument );\r
489                                                                                 range.moveToElementEditStart( target );\r
490                                                                                 range.select( true );\r
491                                                                         }\r
492                                                                 }\r
493                                                         } );\r
494                                                 }\r
495 \r
496                                                 // Webkit: avoid from editing form control elements content.\r
497                                                 if ( CKEDITOR.env.webkit )\r
498                                                 {\r
499                                                         // Prevent from tick checkbox/radiobox/select\r
500                                                         domDocument.on( 'click', function( ev )\r
501                                                         {\r
502                                                                 if ( ev.data.getTarget().is( 'input', 'select' ) )\r
503                                                                         ev.data.preventDefault();\r
504                                                         } );\r
505 \r
506                                                         // Prevent from editig textfield/textarea value.\r
507                                                         domDocument.on( 'mouseup', function( ev )\r
508                                                         {\r
509                                                                 if ( ev.data.getTarget().is( 'input', 'textarea' ) )\r
510                                                                         ev.data.preventDefault();\r
511                                                         } );\r
512                                                 }\r
513 \r
514                                                 // IE standard compliant in editing frame doesn't focus the editor when\r
515                                                 // clicking outside actual content, manually apply the focus. (#1659)\r
516                                                 if ( CKEDITOR.env.ie\r
517                                                         && domDocument.$.compatMode == 'CSS1Compat'\r
518                                                                 || CKEDITOR.env.gecko\r
519                                                                 || CKEDITOR.env.opera )\r
520                                                 {\r
521                                                         var htmlElement = domDocument.getDocumentElement();\r
522                                                         htmlElement.on( 'mousedown', function( evt )\r
523                                                         {\r
524                                                                 // Setting focus directly on editor doesn't work, we\r
525                                                                 // have to use here a temporary element to 'redirect'\r
526                                                                 // the focus.\r
527                                                                 if ( evt.data.getTarget().equals( htmlElement ) )\r
528                                                                 {\r
529                                                                         if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
530                                                                                 blinkCursor();\r
531                                                                         focusGrabber.focus();\r
532                                                                 }\r
533                                                         } );\r
534                                                 }\r
535 \r
536                                                 domWindow.on( 'blur', function()\r
537                                                         {\r
538                                                                 editor.focusManager.blur();\r
539                                                         });\r
540 \r
541                                                 domWindow.on( 'focus', function()\r
542                                                         {\r
543                                                                 var doc = editor.document;\r
544 \r
545                                                                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version >= 10900 )\r
546                                                                         blinkCursor();\r
547                                                                 else if ( CKEDITOR.env.opera )\r
548                                                                         doc.getBody().focus();\r
549 \r
550                                                                 editor.focusManager.focus();\r
551                                                         });\r
552 \r
553                                                 var keystrokeHandler = editor.keystrokeHandler;\r
554                                                 if ( keystrokeHandler )\r
555                                                         keystrokeHandler.attach( domDocument );\r
556 \r
557                                                 if ( CKEDITOR.env.ie )\r
558                                                 {\r
559                                                         domDocument.getDocumentElement().addClass( domDocument.$.compatMode );\r
560                                                         // Override keystrokes which should have deletion behavior\r
561                                                         //  on control types in IE . (#4047)\r
562                                                         domDocument.on( 'keydown', function( evt )\r
563                                                         {\r
564                                                                 var keyCode = evt.data.getKeystroke();\r
565 \r
566                                                                 // Backspace OR Delete.\r
567                                                                 if ( keyCode in { 8 : 1, 46 : 1 } )\r
568                                                                 {\r
569                                                                         var sel = editor.getSelection(),\r
570                                                                                 control = sel.getSelectedElement();\r
571 \r
572                                                                         if ( control )\r
573                                                                         {\r
574                                                                                 // Make undo snapshot.\r
575                                                                                 editor.fire( 'saveSnapshot' );\r
576 \r
577                                                                                 // Delete any element that 'hasLayout' (e.g. hr,table) in IE8 will\r
578                                                                                 // break up the selection, safely manage it here. (#4795)\r
579                                                                                 var bookmark = sel.getRanges()[ 0 ].createBookmark();\r
580                                                                                 // Remove the control manually.\r
581                                                                                 control.remove();\r
582                                                                                 sel.selectBookmarks( [ bookmark ] );\r
583 \r
584                                                                                 editor.fire( 'saveSnapshot' );\r
585 \r
586                                                                                 evt.data.preventDefault();\r
587                                                                         }\r
588                                                                 }\r
589                                                         } );\r
590 \r
591                                                         // PageUp/PageDown scrolling is broken in document\r
592                                                         // with standard doctype, manually fix it. (#4736)\r
593                                                         if ( domDocument.$.compatMode == 'CSS1Compat' )\r
594                                                         {\r
595                                                                 var pageUpDownKeys = { 33 : 1, 34 : 1 };\r
596                                                                 domDocument.on( 'keydown', function( evt )\r
597                                                                 {\r
598                                                                         if ( evt.data.getKeystroke() in pageUpDownKeys )\r
599                                                                         {\r
600                                                                                 setTimeout( function ()\r
601                                                                                 {\r
602                                                                                         editor.getSelection().scrollIntoView();\r
603                                                                                 }, 0 );\r
604                                                                         }\r
605                                                                 } );\r
606                                                         }\r
607                                                 }\r
608 \r
609                                                 // Adds the document body as a context menu target.\r
610                                                 if ( editor.contextMenu )\r
611                                                         editor.contextMenu.addTarget( domDocument, editor.config.browserContextMenuOnCtrl !== false );\r
612 \r
613                                                 setTimeout( function()\r
614                                                         {\r
615                                                                 editor.fire( 'contentDom' );\r
616 \r
617                                                                 if ( fireMode )\r
618                                                                 {\r
619                                                                         editor.mode = 'wysiwyg';\r
620                                                                         editor.fire( 'mode' );\r
621                                                                         fireMode = false;\r
622                                                                 }\r
623 \r
624                                                                 isLoadingData = false;\r
625 \r
626                                                                 if ( isPendingFocus )\r
627                                                                 {\r
628                                                                         editor.focus();\r
629                                                                         isPendingFocus = false;\r
630                                                                 }\r
631                                                                 setTimeout( function()\r
632                                                                 {\r
633                                                                         editor.fire( 'dataReady' );\r
634                                                                 }, 0 );\r
635 \r
636                                                                 // IE, Opera and Safari may not support it and throw errors.\r
637                                                                 try { editor.document.$.execCommand( 'enableObjectResizing', false, !editor.config.disableObjectResizing ) ; } catch(e) {}\r
638                                                                 try { editor.document.$.execCommand( 'enableInlineTableEditing', false, !editor.config.disableNativeTableHandles ) ; } catch(e) {}\r
639 \r
640                                                                 /*\r
641                                                                  * IE BUG: IE might have rendered the iframe with invisible contents.\r
642                                                                  * (#3623). Push some inconsequential CSS style changes to force IE to\r
643                                                                  * refresh it.\r
644                                                                  *\r
645                                                                  * Also, for some unknown reasons, short timeouts (e.g. 100ms) do not\r
646                                                                  * fix the problem. :(\r
647                                                                  */\r
648                                                                 if ( CKEDITOR.env.ie )\r
649                                                                 {\r
650                                                                         setTimeout( function()\r
651                                                                                 {\r
652                                                                                         if ( editor.document )\r
653                                                                                         {\r
654                                                                                                 var $body = editor.document.$.body;\r
655                                                                                                 $body.runtimeStyle.marginBottom = '0px';\r
656                                                                                                 $body.runtimeStyle.marginBottom = '';\r
657                                                                                         }\r
658                                                                                 }, 1000 );\r
659                                                                 }\r
660                                                         },\r
661                                                         0 );\r
662                                         }\r
663 \r
664                                         editor.addMode( 'wysiwyg',\r
665                                                 {\r
666                                                         load : function( holderElement, data, isSnapshot )\r
667                                                         {\r
668                                                                 mainElement = holderElement;\r
669 \r
670                                                                 if ( CKEDITOR.env.ie && CKEDITOR.env.quirks )\r
671                                                                         holderElement.setStyle( 'position', 'relative' );\r
672 \r
673                                                                 // The editor data "may be dirty" after this\r
674                                                                 // point.\r
675                                                                 editor.mayBeDirty = true;\r
676 \r
677                                                                 fireMode = true;\r
678 \r
679                                                                 if ( isSnapshot )\r
680                                                                         this.loadSnapshotData( data );\r
681                                                                 else\r
682                                                                         this.loadData( data );\r
683                                                         },\r
684 \r
685                                                         loadData : function( data )\r
686                                                         {\r
687                                                                 isLoadingData = true;\r
688 \r
689                                                                 var config = editor.config,\r
690                                                                         fullPage = config.fullPage,\r
691                                                                         docType = config.docType;\r
692 \r
693                                                                 // Build the additional stuff to be included into <head>.\r
694                                                                 var headExtra =\r
695                                                                         '<style type="text/css" cke_temp="1">' +\r
696                                                                                 editor._.styles.join( '\n' ) +\r
697                                                                         '</style>';\r
698 \r
699                                                                 !fullPage && ( headExtra =\r
700                                                                         CKEDITOR.tools.buildStyleHtml( editor.config.contentsCss ) +\r
701                                                                         headExtra );\r
702 \r
703                                                                 var baseTag = config.baseHref ? '<base href="' + config.baseHref + '" cke_temp="1" />' : '';\r
704 \r
705                                                                 if ( fullPage )\r
706                                                                 {\r
707                                                                         // Search and sweep out the doctype declaration.\r
708                                                                         data = data.replace( /<!DOCTYPE[^>]*>/i, function( match )\r
709                                                                                 {\r
710                                                                                         editor.docType = docType = match;\r
711                                                                                         return '';\r
712                                                                                 });\r
713                                                                 }\r
714 \r
715                                                                 // Get the HTML version of the data.\r
716                                                                 if ( editor.dataProcessor )\r
717                                                                         data = editor.dataProcessor.toHtml( data, fixForBody );\r
718 \r
719                                                                 if ( fullPage )\r
720                                                                 {\r
721                                                                         // Check if the <body> tag is available.\r
722                                                                         if ( !(/<body[\s|>]/).test( data ) )\r
723                                                                                 data = '<body>' + data;\r
724 \r
725                                                                         // Check if the <html> tag is available.\r
726                                                                         if ( !(/<html[\s|>]/).test( data ) )\r
727                                                                                 data = '<html>' + data + '</html>';\r
728 \r
729                                                                         // Check if the <head> tag is available.\r
730                                                                         if ( !(/<head[\s|>]/).test( data ) )\r
731                                                                                 data = data.replace( /<html[^>]*>/, '$&<head><title></title></head>' ) ;\r
732                                                                         else if ( !(/<title[\s|>]/).test( data ) )\r
733                                                                                 data = data.replace( /<head[^>]*>/, '$&<title></title>' ) ;\r
734 \r
735                                                                         // The base must be the first tag in the HEAD, e.g. to get relative\r
736                                                                         // links on styles.\r
737                                                                         baseTag && ( data = data.replace( /<head>/, '$&' + baseTag ) );\r
738 \r
739                                                                         // Inject the extra stuff into <head>.\r
740                                                                         // Attention: do not change it before testing it well. (V2)\r
741                                                                         // This is tricky... if the head ends with <meta ... content type>,\r
742                                                                         // Firefox will break. But, it works if we place our extra stuff as\r
743                                                                         // the last elements in the HEAD.\r
744                                                                         data = data.replace( /<\/head\s*>/, headExtra + '$&' );\r
745 \r
746                                                                         // Add the DOCTYPE back to it.\r
747                                                                         data = docType + data;\r
748                                                                 }\r
749                                                                 else\r
750                                                                 {\r
751                                                                         data =\r
752                                                                                 config.docType +\r
753                                                                                 '<html dir="' + config.contentsLangDirection + '"' +\r
754                                                                                         ' lang="' + ( config.contentsLanguage || editor.langCode ) + '">' +\r
755                                                                                 '<head>' +\r
756                                                                                         '<title>' + frameLabel + '</title>' +\r
757                                                                                         baseTag +\r
758                                                                                         headExtra +\r
759                                                                                 '</head>' +\r
760                                                                                 '<body' + ( config.bodyId ? ' id="' + config.bodyId + '"' : '' ) +\r
761                                                                                                   ( config.bodyClass ? ' class="' + config.bodyClass + '"' : '' ) +\r
762                                                                                                   '>' +\r
763                                                                                         data +\r
764                                                                                 '</html>';\r
765                                                                 }\r
766 \r
767                                                                 data += activationScript;\r
768 \r
769 \r
770                                                                 // The iframe is recreated on each call of setData, so we need to clear DOM objects\r
771                                                                 this.onDispose();\r
772                                                                 createIFrame( data );\r
773                                                         },\r
774 \r
775                                                         getData : function()\r
776                                                         {\r
777                                                                 var config = editor.config,\r
778                                                                         fullPage = config.fullPage,\r
779                                                                         docType = fullPage && editor.docType,\r
780                                                                         doc = iframe.getFrameDocument();\r
781 \r
782                                                                 var data = fullPage\r
783                                                                         ? doc.getDocumentElement().getOuterHtml()\r
784                                                                         : doc.getBody().getHtml();\r
785 \r
786                                                                 if ( editor.dataProcessor )\r
787                                                                         data = editor.dataProcessor.toDataFormat( data, fixForBody );\r
788 \r
789                                                                 // Strip the last blank paragraph within document.\r
790                                                                 if ( config.ignoreEmptyParagraph )\r
791                                                                         data = data.replace( emptyParagraphRegexp, '' );\r
792 \r
793                                                                 if ( docType )\r
794                                                                         data = docType + '\n' + data;\r
795 \r
796                                                                 return data;\r
797                                                         },\r
798 \r
799                                                         getSnapshotData : function()\r
800                                                         {\r
801                                                                 return iframe.getFrameDocument().getBody().getHtml();\r
802                                                         },\r
803 \r
804                                                         loadSnapshotData : function( data )\r
805                                                         {\r
806                                                                 iframe.getFrameDocument().getBody().setHtml( data );\r
807                                                         },\r
808 \r
809                                                         onDispose : function()\r
810                                                         {\r
811                                                                 if ( !editor.document )\r
812                                                                         return;\r
813 \r
814                                                                 editor.document.getDocumentElement().clearCustomData();\r
815                                                                 editor.document.getBody().clearCustomData();\r
816 \r
817                                                                 editor.window.clearCustomData();\r
818                                                                 editor.document.clearCustomData();\r
819 \r
820                                                                 iframe.clearCustomData();\r
821 \r
822                                                                 /*\r
823                                                                 * IE BUG: When destroying editor DOM with the selection remains inside\r
824                                                                 * editing area would break IE7/8's selection system, we have to put the editing\r
825                                                                 * iframe offline first. (#3812 and #5441)\r
826                                                                 */\r
827                                                                 iframe.remove();\r
828                                                         },\r
829 \r
830                                                         unload : function( holderElement )\r
831                                                         {\r
832                                                                 this.onDispose();\r
833 \r
834                                                                 editor.window = editor.document = iframe = mainElement = isPendingFocus = null;\r
835 \r
836                                                                 editor.fire( 'contentDomUnload' );\r
837                                                         },\r
838 \r
839                                                         focus : function()\r
840                                                         {\r
841                                                                 if ( isLoadingData )\r
842                                                                         isPendingFocus = true;\r
843                                                                 else if ( editor.window )\r
844                                                                 {\r
845                                                                         editor.window.focus();\r
846 \r
847                                                                         editor.selectionChange();\r
848                                                                 }\r
849                                                         }\r
850                                                 });\r
851 \r
852                                         editor.on( 'insertHtml', onInsertHtml, null, null, 20 );\r
853                                         editor.on( 'insertElement', onInsertElement, null, null, 20 );\r
854                                         // Auto fixing on some document structure weakness to enhance usabilities. (#3190 and #3189)\r
855                                         editor.on( 'selectionChange', onSelectionChangeFixBody, null, null, 1 );\r
856                                 });\r
857 \r
858                         var titleBackup;\r
859                         // Setting voice label as window title, backup the original one\r
860                         // and restore it before running into use.\r
861                         editor.on( 'contentDom', function ()\r
862                                 {\r
863                                         var title = editor.document.getElementsByTag( 'title' ).getItem( 0 );\r
864                                         title.setAttribute( '_cke_title', editor.document.$.title );\r
865                                         editor.document.$.title = frameLabel;\r
866                                 });\r
867 \r
868                         // IE8 stricts mode doesn't have 'contentEditable' in effect\r
869                         // on element unless it has layout. (#5562)\r
870                         if ( CKEDITOR.env.ie8Compat )\r
871                         {\r
872                                 editor.addCss( 'html.CSS1Compat [contenteditable=false]{ min-height:0 !important;}' );\r
873 \r
874                                 var selectors = [];\r
875                                 for ( var tag in CKEDITOR.dtd.$removeEmpty )\r
876                                         selectors.push( 'html.CSS1Compat ' + tag + '[contenteditable=false]' );\r
877                                 editor.addCss( selectors.join( ',' ) + '{ display:inline-block;}' );\r
878                         }\r
879 \r
880                         // Switch on design mode for a short while and close it after then.\r
881                         function blinkCursor( retry )\r
882                         {\r
883                                 CKEDITOR.tools.tryThese(\r
884                                         function()\r
885                                         {\r
886                                                 editor.document.$.designMode = 'on';\r
887                                                 setTimeout( function ()\r
888                                                 {\r
889                                                         editor.document.$.designMode = 'off';\r
890                                                         editor.document.getBody().focus();\r
891                                                 }, 50 );\r
892                                         },\r
893                                         function()\r
894                                         {\r
895                                                 // The above call is known to fail when parent DOM\r
896                                                 // tree layout changes may break design mode. (#5782)\r
897                                                 // Refresh the 'contentEditable' is a cue to this.\r
898                                                 editor.document.$.designMode = 'off';\r
899                                                 var body = editor.document.getBody();\r
900                                                 body.setAttribute( 'contentEditable', false );\r
901                                                 body.setAttribute( 'contentEditable', true );\r
902                                                 // Try it again once..\r
903                                                 !retry && blinkCursor( 1 );\r
904                                         });\r
905                         }\r
906 \r
907                         // Create an invisible element to grab focus.\r
908                         if ( CKEDITOR.env.gecko || CKEDITOR.env.ie || CKEDITOR.env.opera )\r
909                         {\r
910                                 var focusGrabber;\r
911                                 editor.on( 'uiReady', function()\r
912                                 {\r
913                                         focusGrabber = editor.container.append( CKEDITOR.dom.element.createFromHtml(\r
914                                                 // Use 'span' instead of anything else to fly under the screen-reader radar. (#5049)\r
915                                                 '<span tabindex="-1" style="position:absolute; left:-10000" role="presentation"></span>' ) );\r
916 \r
917                                         focusGrabber.on( 'focus', function()\r
918                                                 {\r
919                                                         editor.focus();\r
920                                                 } );\r
921                                 } );\r
922                                 editor.on( 'destroy', function()\r
923                                 {\r
924                                         CKEDITOR.tools.removeFunction( contentDomReadyHandler );\r
925                                         focusGrabber.clearCustomData();\r
926                                 } );\r
927                         }\r
928 \r
929                         // Disable form elements editing mode provided by some browers. (#5746)\r
930                         editor.on( 'insertElement', function ( evt )\r
931                         {\r
932                                 var element = evt.data;\r
933                                 if ( element.type == CKEDITOR.NODE_ELEMENT\r
934                                                 && ( element.is( 'input' ) || element.is( 'textarea' ) ) )\r
935                                 {\r
936                                         element.setAttribute( 'contentEditable', false );\r
937                                 }\r
938                         });\r
939 \r
940                 }\r
941         });\r
942 \r
943         // Fixing Firefox 'Back-Forward Cache' break design mode. (#4514)\r
944         if ( CKEDITOR.env.gecko )\r
945         {\r
946                 ( function ()\r
947                 {\r
948                         var body = document.body;\r
949 \r
950                         if ( !body )\r
951                                 window.addEventListener( 'load', arguments.callee, false );\r
952                         else\r
953                         {\r
954                                 var currentHandler = body.getAttribute( 'onpageshow' );\r
955                                 body.setAttribute( 'onpageshow', ( currentHandler ? currentHandler + ';' : '') +\r
956                                                         'event.persisted && (function(){' +\r
957                                                                 'var allInstances = CKEDITOR.instances, editor, doc;' +\r
958                                                                 'for ( var i in allInstances )' +\r
959                                                                 '{' +\r
960                                                                 '       editor = allInstances[ i ];' +\r
961                                                                 '       doc = editor.document;' +\r
962                                                                 '       if ( doc )' +\r
963                                                                 '       {' +\r
964                                                                 '               doc.$.designMode = "off";' +\r
965                                                                 '               doc.$.designMode = "on";' +\r
966                                                                 '       }' +\r
967                                                                 '}' +\r
968                                                 '})();' );\r
969                         }\r
970                 } )();\r
971 \r
972         }\r
973 })();\r
974 \r
975 /**\r
976  * Disables the ability of resize objects (image and tables) in the editing\r
977  * area.\r
978  * @type Boolean\r
979  * @default false\r
980  * @example\r
981  * config.disableObjectResizing = true;\r
982  */\r
983 CKEDITOR.config.disableObjectResizing = false;\r
984 \r
985 /**\r
986  * Disables the "table tools" offered natively by the browser (currently\r
987  * Firefox only) to make quick table editing operations, like adding or\r
988  * deleting rows and columns.\r
989  * @type Boolean\r
990  * @default true\r
991  * @example\r
992  * config.disableNativeTableHandles = false;\r
993  */\r
994 CKEDITOR.config.disableNativeTableHandles = true;\r
995 \r
996 /**\r
997  * Disables the built-in spell checker while typing natively available in the\r
998  * browser (currently Firefox and Safari only).<br /><br />\r
999  *\r
1000  * Even if word suggestions will not appear in the CKEditor context menu, this\r
1001  * feature is useful to help quickly identifying misspelled words.<br /><br />\r
1002  *\r
1003  * This setting is currently compatible with Firefox only due to limitations in\r
1004  * other browsers.\r
1005  * @type Boolean\r
1006  * @default true\r
1007  * @example\r
1008  * config.disableNativeSpellChecker = false;\r
1009  */\r
1010 CKEDITOR.config.disableNativeSpellChecker = true;\r
1011 \r
1012 /**\r
1013  * Whether the editor must output an empty value ("") if it's contents is made\r
1014  * by an empty paragraph only.\r
1015  * @type Boolean\r
1016  * @default true\r
1017  * @example\r
1018  * config.ignoreEmptyParagraph = false;\r
1019  */\r
1020 CKEDITOR.config.ignoreEmptyParagraph = true;\r
1021 \r
1022 /**\r
1023  * Fired when data is loaded and ready for retrieval in an editor instance.\r
1024  * @name CKEDITOR.editor#dataReady\r
1025  * @event\r
1026  */\r