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