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