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