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