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