JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.1
[ckeditor.git] / _source / core / editor.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.editor} class, which represents an\r
8  *              editor instance.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         // The counter for automatic instance names.\r
14         var nameCounter = 0;\r
15 \r
16         var getNewName = function()\r
17         {\r
18                 var name = 'editor' + ( ++nameCounter );\r
19                 return ( CKEDITOR.instances && CKEDITOR.instances[ name ] ) ? getNewName() : name;\r
20         };\r
21 \r
22         // ##### START: Config Privates\r
23 \r
24         // These function loads custom configuration files and cache the\r
25         // CKEDITOR.editorConfig functions defined on them, so there is no need to\r
26         // download them more than once for several instances.\r
27         var loadConfigLoaded = {};\r
28         var loadConfig = function( editor )\r
29         {\r
30                 var customConfig = editor.config.customConfig;\r
31 \r
32                 // Check if there is a custom config to load.\r
33                 if ( !customConfig )\r
34                         return false;\r
35 \r
36                 customConfig = CKEDITOR.getUrl( customConfig );\r
37 \r
38                 var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );\r
39 \r
40                 // If the custom config has already been downloaded, reuse it.\r
41                 if ( loadedConfig.fn )\r
42                 {\r
43                         // Call the cached CKEDITOR.editorConfig defined in the custom\r
44                         // config file for the editor instance depending on it.\r
45                         loadedConfig.fn.call( editor, editor.config );\r
46 \r
47                         // If there is no other customConfig in the chain, fire the\r
48                         // "configLoaded" event.\r
49                         if ( CKEDITOR.getUrl( editor.config.customConfig ) == customConfig || !loadConfig( editor ) )\r
50                                 editor.fireOnce( 'customConfigLoaded' );\r
51                 }\r
52                 else\r
53                 {\r
54                         // Load the custom configuration file.\r
55                         CKEDITOR.scriptLoader.load( customConfig, function()\r
56                                 {\r
57                                         // If the CKEDITOR.editorConfig function has been properly\r
58                                         // defined in the custom configuration file, cache it.\r
59                                         if ( CKEDITOR.editorConfig )\r
60                                                 loadedConfig.fn = CKEDITOR.editorConfig;\r
61                                         else\r
62                                                 loadedConfig.fn = function(){};\r
63 \r
64                                         // Call the load config again. This time the custom\r
65                                         // config is already cached and so it will get loaded.\r
66                                         loadConfig( editor );\r
67                                 });\r
68                 }\r
69 \r
70                 return true;\r
71         };\r
72 \r
73         var initConfig = function( editor, instanceConfig )\r
74         {\r
75                 // Setup the lister for the "customConfigLoaded" event.\r
76                 editor.on( 'customConfigLoaded', function()\r
77                         {\r
78                                 if ( instanceConfig )\r
79                                 {\r
80                                         // Register the events that may have been set at the instance\r
81                                         // configuration object.\r
82                                         if ( instanceConfig.on )\r
83                                         {\r
84                                                 for ( var eventName in instanceConfig.on )\r
85                                                 {\r
86                                                         editor.on( eventName, instanceConfig.on[ eventName ] );\r
87                                                 }\r
88                                         }\r
89 \r
90                                         // Overwrite the settings from the in-page config.\r
91                                         CKEDITOR.tools.extend( editor.config, instanceConfig, true );\r
92 \r
93                                         delete editor.config.on;\r
94                                 }\r
95 \r
96                                 onConfigLoaded( editor );\r
97                         });\r
98 \r
99                 // The instance config may override the customConfig setting to avoid\r
100                 // loading the default ~/config.js file.\r
101                 if ( instanceConfig && instanceConfig.customConfig != undefined )\r
102                         editor.config.customConfig = instanceConfig.customConfig;\r
103 \r
104                 // Load configs from the custom configuration files.\r
105                 if ( !loadConfig( editor ) )\r
106                         editor.fireOnce( 'customConfigLoaded' );\r
107         };\r
108 \r
109         // ##### END: Config Privates\r
110 \r
111         var onConfigLoaded = function( editor )\r
112         {\r
113                 // Set config related properties.\r
114 \r
115                 var skin = editor.config.skin.split( ',' ),\r
116                         skinName = skin[ 0 ],\r
117                         skinPath = CKEDITOR.getUrl( skin[ 1 ] || (\r
118                                 '_source/' +    // @Packager.RemoveLine\r
119                                 'skins/' + skinName + '/' ) );\r
120 \r
121                 editor.skinName = skinName;\r
122                 editor.skinPath = skinPath;\r
123                 editor.skinClass = 'cke_skin_' + skinName;\r
124 \r
125                 editor.tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;\r
126 \r
127                 // Fire the "configLoaded" event.\r
128                 editor.fireOnce( 'configLoaded' );\r
129 \r
130                 // Load language file.\r
131                 loadSkin( editor );\r
132         };\r
133 \r
134         var loadLang = function( editor )\r
135         {\r
136                 CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )\r
137                         {\r
138                                 editor.langCode = languageCode;\r
139 \r
140                                 // As we'll be adding plugin specific entries that could come\r
141                                 // from different language code files, we need a copy of lang,\r
142                                 // not a direct reference to it.\r
143                                 editor.lang = CKEDITOR.tools.prototypedCopy( lang );\r
144 \r
145                                 // We're not able to support RTL in Firefox 2 at this time.\r
146                                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )\r
147                                         editor.lang.dir = 'ltr';\r
148 \r
149                                 loadPlugins( editor );\r
150                         });\r
151         };\r
152 \r
153         var loadPlugins = function( editor )\r
154         {\r
155                 var config                      = editor.config,\r
156                         plugins                 = config.plugins,\r
157                         extraPlugins    = config.extraPlugins,\r
158                         removePlugins   = config.removePlugins;\r
159 \r
160                 if ( extraPlugins )\r
161                 {\r
162                         // Remove them first to avoid duplications.\r
163                         var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );\r
164                         plugins = plugins.replace( removeRegex, '' );\r
165 \r
166                         plugins += ',' + extraPlugins;\r
167                 }\r
168 \r
169                 if ( removePlugins )\r
170                 {\r
171                         removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );\r
172                         plugins = plugins.replace( removeRegex, '' );\r
173                 }\r
174 \r
175                 // Load all plugins defined in the "plugins" setting.\r
176                 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )\r
177                         {\r
178                                 // The list of plugins.\r
179                                 var pluginsArray = [];\r
180 \r
181                                 // The language code to get loaded for each plugin. Null\r
182                                 // entries will be appended for plugins with no language files.\r
183                                 var languageCodes = [];\r
184 \r
185                                 // The list of URLs to language files.\r
186                                 var languageFiles = [];\r
187 \r
188                                 // Cache the loaded plugin names.\r
189                                 editor.plugins = plugins;\r
190 \r
191                                 // Loop through all plugins, to build the list of language\r
192                                 // files to get loaded.\r
193                                 for ( var pluginName in plugins )\r
194                                 {\r
195                                         var plugin = plugins[ pluginName ],\r
196                                                 pluginLangs = plugin.lang,\r
197                                                 pluginPath = CKEDITOR.plugins.getPath( pluginName ),\r
198                                                 lang = null;\r
199 \r
200                                         // Set the plugin path in the plugin.\r
201                                         plugin.path = pluginPath;\r
202 \r
203                                         // If the plugin has "lang".\r
204                                         if ( pluginLangs )\r
205                                         {\r
206                                                 // Resolve the plugin language. If the current language\r
207                                                 // is not available, get the first one (default one).\r
208                                                 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );\r
209 \r
210                                                 if ( !plugin.lang[ lang ] )\r
211                                                 {\r
212                                                         // Put the language file URL into the list of files to\r
213                                                         // get downloaded.\r
214                                                         languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );\r
215                                                 }\r
216                                                 else\r
217                                                 {\r
218                                                         CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );\r
219                                                         lang = null;\r
220                                                 }\r
221                                         }\r
222 \r
223                                         // Save the language code, so we know later which\r
224                                         // language has been resolved to this plugin.\r
225                                         languageCodes.push( lang );\r
226 \r
227                                         pluginsArray.push( plugin );\r
228                                 }\r
229 \r
230                                 // Load all plugin specific language files in a row.\r
231                                 CKEDITOR.scriptLoader.load( languageFiles, function()\r
232                                         {\r
233                                                 // Initialize all plugins that have the "beforeInit" and "init" methods defined.\r
234                                                 var methods = [ 'beforeInit', 'init', 'afterInit' ];\r
235                                                 for ( var m = 0 ; m < methods.length ; m++ )\r
236                                                 {\r
237                                                         for ( var i = 0 ; i < pluginsArray.length ; i++ )\r
238                                                         {\r
239                                                                 var plugin = pluginsArray[ i ];\r
240 \r
241                                                                 // Uses the first loop to update the language entries also.\r
242                                                                 if ( m === 0 && languageCodes[ i ] && plugin.lang )\r
243                                                                         CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );\r
244 \r
245                                                                 // Call the plugin method (beforeInit and init).\r
246                                                                 if ( plugin[ methods[ m ] ] )\r
247                                                                         plugin[ methods[ m ] ]( editor );\r
248                                                         }\r
249                                                 }\r
250 \r
251                                                 // Load the editor skin.\r
252                                                 editor.fire( 'pluginsLoaded' );\r
253                                                 loadTheme( editor );\r
254                                         });\r
255                         });\r
256         };\r
257 \r
258         var loadSkin = function( editor )\r
259         {\r
260                 CKEDITOR.skins.load( editor, 'editor', function()\r
261                         {\r
262                                 loadLang( editor );\r
263                         });\r
264         };\r
265 \r
266         var loadTheme = function( editor )\r
267         {\r
268                 var theme = editor.config.theme;\r
269                 CKEDITOR.themes.load( theme, function()\r
270                         {\r
271                                 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );\r
272                                 editorTheme.path = CKEDITOR.themes.getPath( theme );\r
273                                 editorTheme.build( editor );\r
274 \r
275                                 if ( editor.config.autoUpdateElement )\r
276                                         attachToForm( editor );\r
277                         });\r
278         };\r
279 \r
280         var attachToForm = function( editor )\r
281         {\r
282                 var element = editor.element;\r
283 \r
284                 // If are replacing a textarea, we must\r
285                 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )\r
286                 {\r
287                         var form = element.$.form && new CKEDITOR.dom.element( element.$.form );\r
288                         if ( form )\r
289                         {\r
290                                 function onSubmit()\r
291                                 {\r
292                                         editor.updateElement();\r
293                                 }\r
294                                 form.on( 'submit',onSubmit );\r
295 \r
296                                 // Setup the submit function because it doesn't fire the\r
297                                 // "submit" event.\r
298                                 if ( !form.$.submit.nodeName )\r
299                                 {\r
300                                         form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )\r
301                                                 {\r
302                                                         return function()\r
303                                                                 {\r
304                                                                         editor.updateElement();\r
305 \r
306                                                                         // For IE, the DOM submit function is not a\r
307                                                                         // function, so we need thid check.\r
308                                                                         if ( originalSubmit.apply )\r
309                                                                                 originalSubmit.apply( this, arguments );\r
310                                                                         else\r
311                                                                                 originalSubmit();\r
312                                                                 };\r
313                                                 });\r
314                                 }\r
315 \r
316                                 // Remove 'submit' events registered on form element before destroying.(#3988)\r
317                                 editor.on( 'destroy', function()\r
318                                 {\r
319                                         form.removeListener( 'submit', onSubmit );\r
320                                 } );\r
321                         }\r
322                 }\r
323         };\r
324 \r
325         function updateCommandsMode()\r
326         {\r
327                 var command,\r
328                         commands = this._.commands,\r
329                         mode = this.mode;\r
330 \r
331                 for ( var name in commands )\r
332                 {\r
333                         command = commands[ name ];\r
334                         command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();\r
335                 }\r
336         }\r
337 \r
338         /**\r
339          * Initializes the editor instance. This function is called by the editor\r
340          * contructor (editor_basic.js).\r
341          * @private\r
342          */\r
343         CKEDITOR.editor.prototype._init = function()\r
344                 {\r
345                         // Get the properties that have been saved in the editor_base\r
346                         // implementation.\r
347                         var element                     = CKEDITOR.dom.element.get( this._.element ),\r
348                                 instanceConfig  = this._.instanceConfig;\r
349                         delete this._.element;\r
350                         delete this._.instanceConfig;\r
351 \r
352                         this._.commands = {};\r
353                         this._.styles = [];\r
354 \r
355                         /**\r
356                          * The DOM element that has been replaced by this editor instance. This\r
357                          * element holds the editor data on load and post.\r
358                          * @name CKEDITOR.editor.prototype.element\r
359                          * @type CKEDITOR.dom.element\r
360                          * @example\r
361                          * var editor = CKEDITOR.instances.editor1;\r
362                          * alert( <b>editor.element</b>.getName() );  "textarea"\r
363                          */\r
364                         this.element = element;\r
365 \r
366                         /**\r
367                          * The editor instance name. It hay be the replaced element id, name or\r
368                          * a default name using a progressive counter (editor1, editor2, ...).\r
369                          * @name CKEDITOR.editor.prototype.name\r
370                          * @type String\r
371                          * @example\r
372                          * var editor = CKEDITOR.instances.editor1;\r
373                          * alert( <b>editor.name</b> );  "editor1"\r
374                          */\r
375                         this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
376                                                         && ( element.getId() || element.getNameAtt() ) )\r
377                                                 || getNewName();\r
378 \r
379                         if ( this.name in CKEDITOR.instances )\r
380                                 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';\r
381 \r
382                         /**\r
383                          * The configurations for this editor instance. It inherits all\r
384                          * settings defined in (@link CKEDITOR.config}, combined with settings\r
385                          * loaded from custom configuration files and those defined inline in\r
386                          * the page when creating the editor.\r
387                          * @name CKEDITOR.editor.prototype.config\r
388                          * @type Object\r
389                          * @example\r
390                          * var editor = CKEDITOR.instances.editor1;\r
391                          * alert( <b>editor.config.theme</b> );  "default" e.g.\r
392                          */\r
393                         this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );\r
394 \r
395                         /**\r
396                          * Namespace containing UI features related to this editor instance.\r
397                          * @name CKEDITOR.editor.prototype.ui\r
398                          * @type CKEDITOR.ui\r
399                          * @example\r
400                          */\r
401                         this.ui = new CKEDITOR.ui( this );\r
402 \r
403                         /**\r
404                          * Controls the focus state of this editor instance. This property\r
405                          * is rarely used for normal API operations. It is mainly\r
406                          * destinated to developer adding UI elements to the editor interface.\r
407                          * @name CKEDITOR.editor.prototype.focusManager\r
408                          * @type CKEDITOR.focusManager\r
409                          * @example\r
410                          */\r
411                         this.focusManager = new CKEDITOR.focusManager( this );\r
412 \r
413                         CKEDITOR.fire( 'instanceCreated', null, this );\r
414 \r
415                         this.on( 'mode', updateCommandsMode, null, null, 1 );\r
416 \r
417                         initConfig( this, instanceConfig );\r
418                 };\r
419 })();\r
420 \r
421 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,\r
422         /** @lends CKEDITOR.editor.prototype */\r
423         {\r
424                 /**\r
425                  * Adds a command definition to the editor instance. Commands added with\r
426                  * this function can be later executed with {@link #execCommand}.\r
427                  * @param {String} commandName The indentifier name of the command.\r
428                  * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.\r
429                  * @example\r
430                  * editorInstance.addCommand( 'sample',\r
431                  * {\r
432                  *     exec : function( editor )\r
433                  *     {\r
434                  *         alert( 'Executing a command for the editor name "' + editor.name + '"!' );\r
435                  *     }\r
436                  * });\r
437                  */\r
438                 addCommand : function( commandName, commandDefinition )\r
439                 {\r
440                         return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );\r
441                 },\r
442 \r
443                 /**\r
444                  * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.\r
445                  * Note: This function should be called before editor is loaded to take effect.\r
446                  * @param css {String} CSS text.\r
447                  * @example\r
448                  * editorInstance.addCss( 'body { background-color: grey; }' );\r
449                  */\r
450                 addCss : function( css )\r
451                 {\r
452                         this._.styles.push( css );\r
453                 },\r
454 \r
455                 /**\r
456                  * Destroys the editor instance, releasing all resources used by it.\r
457                  * If the editor replaced an element, the element will be recovered.\r
458                  * @param {Boolean} [noUpdate] If the instance is replacing a DOM\r
459                  *              element, this parameter indicates whether or not to update the\r
460                  *              element with the instance contents.\r
461                  * @example\r
462                  * alert( CKEDITOR.instances.editor1 );  e.g "object"\r
463                  * <b>CKEDITOR.instances.editor1.destroy()</b>;\r
464                  * alert( CKEDITOR.instances.editor1 );  "undefined"\r
465                  */\r
466                 destroy : function( noUpdate )\r
467                 {\r
468                         if ( !noUpdate )\r
469                                 this.updateElement();\r
470 \r
471                         if ( this.mode )\r
472                         {\r
473                                 // ->           currentMode.unload( holderElement );\r
474                                 this._.modes[ this.mode ].unload( this.getThemeSpace( 'contents' ) );\r
475                         }\r
476 \r
477                         this.theme.destroy( this );\r
478 \r
479                         var toolbars,\r
480                                 index = 0,\r
481                                 j,\r
482                                 items,\r
483                                 instance;\r
484 \r
485                         if ( this.toolbox )\r
486                         {\r
487                                 toolbars = this.toolbox.toolbars;\r
488                                 for ( ; index < toolbars.length ; index++ )\r
489                                 {\r
490                                         items = toolbars[ index ].items;\r
491                                         for ( j = 0 ; j < items.length ; j++ )\r
492                                         {\r
493                                                 instance = items[ j ];\r
494                                                 if ( instance.clickFn ) CKEDITOR.tools.removeFunction( instance.clickFn );\r
495                                                 if ( instance.keyDownFn ) CKEDITOR.tools.removeFunction( instance.keyDownFn );\r
496 \r
497                                                 if ( instance.index ) CKEDITOR.ui.button._.instances[ instance.index ] = null;\r
498                                         }\r
499                                 }\r
500                         }\r
501 \r
502                         if ( this.contextMenu )\r
503                                 CKEDITOR.tools.removeFunction( this.contextMenu._.functionId );\r
504 \r
505                         if ( this._.filebrowserFn )\r
506                                 CKEDITOR.tools.removeFunction( this._.filebrowserFn );\r
507 \r
508                         this.fire( 'destroy' );\r
509                         CKEDITOR.remove( this );\r
510                         CKEDITOR.fire( 'instanceDestroyed', null, this );\r
511                 },\r
512 \r
513                 /**\r
514                  * Executes a command.\r
515                  * @param {String} commandName The indentifier name of the command.\r
516                  * @param {Object} [data] Data to be passed to the command\r
517                  * @returns {Boolean} "true" if the command has been successfuly\r
518                  *              executed, otherwise "false".\r
519                  * @example\r
520                  * editorInstance.execCommand( 'Bold' );\r
521                  */\r
522                 execCommand : function( commandName, data )\r
523                 {\r
524                         var command = this.getCommand( commandName );\r
525 \r
526                         var eventData =\r
527                         {\r
528                                 name: commandName,\r
529                                 commandData: data,\r
530                                 command: command\r
531                         };\r
532 \r
533                         if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )\r
534                         {\r
535                                 if ( this.fire( 'beforeCommandExec', eventData ) !== true )\r
536                                 {\r
537                                         eventData.returnValue = command.exec( eventData.commandData );\r
538 \r
539                                         // Fire the 'afterCommandExec' immediately if command is synchronous.\r
540                                         if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )\r
541                                                 return eventData.returnValue;\r
542                                 }\r
543                         }\r
544 \r
545                         // throw 'Unknown command name "' + commandName + '"';\r
546                         return false;\r
547                 },\r
548 \r
549                 /**\r
550                  * Gets one of the registered commands. Note that, after registering a\r
551                  * command definition with addCommand, it is transformed internally\r
552                  * into an instance of {@link CKEDITOR.command}, which will be then\r
553                  * returned by this function.\r
554                  * @param {String} commandName The name of the command to be returned.\r
555                  * This is the same used to register the command with addCommand.\r
556                  * @returns {CKEDITOR.command} The command object identified by the\r
557                  * provided name.\r
558                  */\r
559                 getCommand : function( commandName )\r
560                 {\r
561                         return this._.commands[ commandName ];\r
562                 },\r
563 \r
564                 /**\r
565                  * Gets the editor data. The data will be in raw format. It is the same\r
566                  * data that is posted by the editor.\r
567                  * @type String\r
568                  * @returns (String) The editor data.\r
569                  * @example\r
570                  * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )\r
571                  *     alert( 'There is no data available' );\r
572                  */\r
573                 getData : function()\r
574                 {\r
575                         this.fire( 'beforeGetData' );\r
576 \r
577                         var eventData = this._.data;\r
578 \r
579                         if ( typeof eventData != 'string' )\r
580                         {\r
581                                 var element = this.element;\r
582                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
583                                         eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();\r
584                                 else\r
585                                         eventData = '';\r
586                         }\r
587 \r
588                         eventData = { dataValue : eventData };\r
589 \r
590                         // Fire "getData" so data manipulation may happen.\r
591                         this.fire( 'getData', eventData );\r
592 \r
593                         return eventData.dataValue;\r
594                 },\r
595 \r
596                 getSnapshot : function()\r
597                 {\r
598                         var data = this.fire( 'getSnapshot' );\r
599 \r
600                         if ( typeof data != 'string' )\r
601                         {\r
602                                 var element = this.element;\r
603                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
604                                         data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();\r
605                         }\r
606 \r
607                         return data;\r
608                 },\r
609 \r
610                 loadSnapshot : function( snapshot )\r
611                 {\r
612                         this.fire( 'loadSnapshot', snapshot );\r
613                 },\r
614 \r
615                 /**\r
616                  * Sets the editor data. The data must be provided in raw format (HTML).<br />\r
617                  * <br />\r
618                  * Note that this menthod is asynchronous. The "callback" parameter must\r
619                  * be used if interaction with the editor is needed after setting the data.\r
620                  * @param {String} data HTML code to replace the curent content in the\r
621                  *              editor.\r
622                  * @param {Function} callback Function to be called after the setData\r
623                  *              is completed.\r
624                  * @example\r
625                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;This is the editor data.&lt;/p&gt;' );\r
626                  * @example\r
627                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;Some other editor data.&lt;/p&gt;', function()\r
628                  *     {\r
629                  *         this.checkDirty();    // true\r
630                  *     });\r
631                  */\r
632                 setData : function( data , callback )\r
633                 {\r
634                         if( callback )\r
635                         {\r
636                                 this.on( 'dataReady', function( evt )\r
637                                 {\r
638                                         evt.removeListener();\r
639                                         callback.call( evt.editor );\r
640                                 } );\r
641                         }\r
642 \r
643                         // Fire "setData" so data manipulation may happen.\r
644                         var eventData = { dataValue : data };\r
645                         this.fire( 'setData', eventData );\r
646 \r
647                         this._.data = eventData.dataValue;\r
648 \r
649                         this.fire( 'afterSetData', eventData );\r
650                 },\r
651 \r
652                 /**\r
653                  * Inserts HTML into the currently selected position in the editor.\r
654                  * @param {String} data HTML code to be inserted into the editor.\r
655                  * @example\r
656                  * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>;\r
657                  */\r
658                 insertHtml : function( data )\r
659                 {\r
660                         this.fire( 'insertHtml', data );\r
661                 },\r
662 \r
663                 /**\r
664                  * Inserts an element into the currently selected position in the\r
665                  * editor.\r
666                  * @param {CKEDITOR.dom.element} element The element to be inserted\r
667                  *              into the editor.\r
668                  * @example\r
669                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;img src="hello.png" border="0" title="Hello" /&gt;' );\r
670                  * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;\r
671                  */\r
672                 insertElement : function( element )\r
673                 {\r
674                         this.fire( 'insertElement', element );\r
675                 },\r
676 \r
677                 checkDirty : function()\r
678                 {\r
679                         return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );\r
680                 },\r
681 \r
682                 resetDirty : function()\r
683                 {\r
684                         if ( this.mayBeDirty )\r
685                                 this._.previousValue = this.getSnapshot();\r
686                 },\r
687 \r
688                 /**\r
689                  * Updates the &lt;textarea&gt; element that has been replaced by the editor with\r
690                  * the current data available in the editor.\r
691                  * @example\r
692                  * CKEDITOR.instances.editor1.updateElement();\r
693                  * alert( document.getElementById( 'editor1' ).value );  // The current editor data.\r
694                  */\r
695                 updateElement : function()\r
696                 {\r
697                         var element = this.element;\r
698                         if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
699                         {\r
700                                 var data = this.getData();\r
701 \r
702                                 if ( this.config.htmlEncodeOutput )\r
703                                         data = CKEDITOR.tools.htmlEncode( data );\r
704 \r
705                                 if ( element.is( 'textarea' ) )\r
706                                         element.setValue( data );\r
707                                 else\r
708                                         element.setHtml( data );\r
709                         }\r
710                 }\r
711         });\r
712 \r
713 CKEDITOR.on( 'loaded', function()\r
714         {\r
715                 // Run the full initialization for pending editors.\r
716                 var pending = CKEDITOR.editor._pending;\r
717                 if ( pending )\r
718                 {\r
719                         delete CKEDITOR.editor._pending;\r
720 \r
721                         for ( var i = 0 ; i < pending.length ; i++ )\r
722                                 pending[ i ]._init();\r
723                 }\r
724         });\r
725 \r
726 /**\r
727  * Whether escape HTML when editor update original input element.\r
728  * @name CKEDITOR.config.htmlEncodeOutput\r
729  * @since 3.1\r
730  * @type Boolean\r
731  * @default false\r
732  * @example\r
733  * config.htmlEncodeOutput = true;\r
734  */\r
735 \r
736 /**\r
737  * Fired when a CKEDITOR instance is created, but still before initializing it.\r
738  * To interact with a fully initialized instance, use the\r
739  * {@link CKEDITOR#instanceReady} event instead.\r
740  * @name CKEDITOR#instanceCreated\r
741  * @event\r
742  * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
743  */\r
744 \r
745 /**\r
746  * Fired when a CKEDITOR instance is destroyed.\r
747  * @name CKEDITOR#instanceDestroyed\r
748  * @event\r
749  * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.\r
750  */\r
751 \r
752 /**\r
753  * Fired when all plugins are loaded and initialized into the editor instance.\r
754  * @name CKEDITOR#pluginsLoaded\r
755  * @event\r
756  */\r