JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / core / editor.js
1 /*\r
2 Copyright (c) 2003-2011, 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                 /**\r
122                  * The name of the skin used by this editor instance. The skin name can\r
123                  * be set though the {@link CKEDITOR.config.skin} setting.\r
124                  * @name CKEDITOR.editor.prototype.skinName\r
125                  * @type String\r
126                  * @example\r
127                  * alert( editor.skinName );  // "kama" (e.g.)\r
128                  */\r
129                 editor.skinName = skinName;\r
130 \r
131                 /**\r
132                  * The full URL of the skin directory.\r
133                  * @name CKEDITOR.editor.prototype.skinPath\r
134                  * @type String\r
135                  * @example\r
136                  * alert( editor.skinPath );  // "http://example.com/ckeditor/skins/kama/" (e.g.)\r
137                  */\r
138                 editor.skinPath = skinPath;\r
139 \r
140                 /**\r
141                  * The CSS class name used for skin identification purposes.\r
142                  * @name CKEDITOR.editor.prototype.skinClass\r
143                  * @type String\r
144                  * @example\r
145                  * alert( editor.skinClass );  // "cke_skin_kama" (e.g.)\r
146                  */\r
147                 editor.skinClass = 'cke_skin_' + skinName;\r
148 \r
149                 /**\r
150                  * The <a href="http://en.wikipedia.org/wiki/Tabbing_navigation">tabbing\r
151                  * navigation</a> order that has been calculated for this editor\r
152                  * instance. This can be set by the {@link CKEDITOR.config.tabIndex}\r
153                  * setting or taken from the "tabindex" attribute of the\r
154                  * {@link #element} associated to the editor.\r
155                  * @name CKEDITOR.editor.prototype.tabIndex\r
156                  * @type Number\r
157                  * @default 0 (zero)\r
158                  * @example\r
159                  * alert( editor.tabIndex );  // "0" (e.g.)\r
160                  */\r
161                 editor.tabIndex = editor.config.tabIndex || editor.element.getAttribute( 'tabindex' ) || 0;\r
162 \r
163                 // Fire the "configLoaded" event.\r
164                 editor.fireOnce( 'configLoaded' );\r
165 \r
166                 // Load language file.\r
167                 loadSkin( editor );\r
168         };\r
169 \r
170         var loadLang = function( editor )\r
171         {\r
172                 CKEDITOR.lang.load( editor.config.language, editor.config.defaultLanguage, function( languageCode, lang )\r
173                         {\r
174                                 /**\r
175                                  * The code for the language resources that have been loaded\r
176                                  * for the user internface elements of this editor instance.\r
177                                  * @name CKEDITOR.editor.prototype.langCode\r
178                                  * @type String\r
179                                  * @example\r
180                                  * alert( editor.langCode );  // "en" (e.g.)\r
181                                  */\r
182                                 editor.langCode = languageCode;\r
183 \r
184                                 /**\r
185                                  * An object holding all language strings used by the editor\r
186                                  * interface.\r
187                                  * @name CKEDITOR.editor.prototype.lang\r
188                                  * @type CKEDITOR.lang\r
189                                  * @example\r
190                                  * alert( editor.lang.bold );  // "Negrito" (e.g. if language is Portuguese)\r
191                                  */\r
192                                 // As we'll be adding plugin specific entries that could come\r
193                                 // from different language code files, we need a copy of lang,\r
194                                 // not a direct reference to it.\r
195                                 editor.lang = CKEDITOR.tools.prototypedCopy( lang );\r
196 \r
197                                 // We're not able to support RTL in Firefox 2 at this time.\r
198                                 if ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 && editor.lang.dir == 'rtl' )\r
199                                         editor.lang.dir = 'ltr';\r
200 \r
201                                 var config = editor.config;\r
202                                 config.contentsLangDirection == 'ui' && ( config.contentsLangDirection = editor.lang.dir );\r
203 \r
204                                 loadPlugins( editor );\r
205                         });\r
206         };\r
207 \r
208         var loadPlugins = function( editor )\r
209         {\r
210                 var config                      = editor.config,\r
211                         plugins                 = config.plugins,\r
212                         extraPlugins    = config.extraPlugins,\r
213                         removePlugins   = config.removePlugins;\r
214 \r
215                 if ( extraPlugins )\r
216                 {\r
217                         // Remove them first to avoid duplications.\r
218                         var removeRegex = new RegExp( '(?:^|,)(?:' + extraPlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );\r
219                         plugins = plugins.replace( removeRegex, '' );\r
220 \r
221                         plugins += ',' + extraPlugins;\r
222                 }\r
223 \r
224                 if ( removePlugins )\r
225                 {\r
226                         removeRegex = new RegExp( '(?:^|,)(?:' + removePlugins.replace( /\s*,\s*/g, '|' ) + ')(?=,|$)' , 'g' );\r
227                         plugins = plugins.replace( removeRegex, '' );\r
228                 }\r
229 \r
230                 // Load the Adobe AIR plugin conditionally.\r
231                 CKEDITOR.env.air && ( plugins += ',adobeair' );\r
232 \r
233                 // Load all plugins defined in the "plugins" setting.\r
234                 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )\r
235                         {\r
236                                 // The list of plugins.\r
237                                 var pluginsArray = [];\r
238 \r
239                                 // The language code to get loaded for each plugin. Null\r
240                                 // entries will be appended for plugins with no language files.\r
241                                 var languageCodes = [];\r
242 \r
243                                 // The list of URLs to language files.\r
244                                 var languageFiles = [];\r
245 \r
246                                 /**\r
247                                  * And object holding references to all plugins used by this\r
248                                  * editor istance.\r
249                                  * @name CKEDITOR.editor.prototype.plugins\r
250                                  * @type Object\r
251                                  * @example\r
252                                  * alert( editor.plugins.dialog.path );  // "http://example.com/ckeditor/plugins/dialog/" (e.g.)\r
253                                  */\r
254                                 editor.plugins = plugins;\r
255 \r
256                                 // Loop through all plugins, to build the list of language\r
257                                 // files to get loaded.\r
258                                 for ( var pluginName in plugins )\r
259                                 {\r
260                                         var plugin = plugins[ pluginName ],\r
261                                                 pluginLangs = plugin.lang,\r
262                                                 pluginPath = CKEDITOR.plugins.getPath( pluginName ),\r
263                                                 lang = null;\r
264 \r
265                                         // Set the plugin path in the plugin.\r
266                                         plugin.path = pluginPath;\r
267 \r
268                                         // If the plugin has "lang".\r
269                                         if ( pluginLangs )\r
270                                         {\r
271                                                 // Resolve the plugin language. If the current language\r
272                                                 // is not available, get the first one (default one).\r
273                                                 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );\r
274 \r
275                                                 if ( !plugin.langEntries || !plugin.langEntries[ lang ] )\r
276                                                 {\r
277                                                         // Put the language file URL into the list of files to\r
278                                                         // get downloaded.\r
279                                                         languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );\r
280                                                 }\r
281                                                 else\r
282                                                 {\r
283                                                         CKEDITOR.tools.extend( editor.lang, plugin.langEntries[ lang ] );\r
284                                                         lang = null;\r
285                                                 }\r
286                                         }\r
287 \r
288                                         // Save the language code, so we know later which\r
289                                         // language has been resolved to this plugin.\r
290                                         languageCodes.push( lang );\r
291 \r
292                                         pluginsArray.push( plugin );\r
293                                 }\r
294 \r
295                                 // Load all plugin specific language files in a row.\r
296                                 CKEDITOR.scriptLoader.load( languageFiles, function()\r
297                                         {\r
298                                                 // Initialize all plugins that have the "beforeInit" and "init" methods defined.\r
299                                                 var methods = [ 'beforeInit', 'init', 'afterInit' ];\r
300                                                 for ( var m = 0 ; m < methods.length ; m++ )\r
301                                                 {\r
302                                                         for ( var i = 0 ; i < pluginsArray.length ; i++ )\r
303                                                         {\r
304                                                                 var plugin = pluginsArray[ i ];\r
305 \r
306                                                                 // Uses the first loop to update the language entries also.\r
307                                                                 if ( m === 0 && languageCodes[ i ] && plugin.lang )\r
308                                                                         CKEDITOR.tools.extend( editor.lang, plugin.langEntries[ languageCodes[ i ] ] );\r
309 \r
310                                                                 // Call the plugin method (beforeInit and init).\r
311                                                                 if ( plugin[ methods[ m ] ] )\r
312                                                                         plugin[ methods[ m ] ]( editor );\r
313                                                         }\r
314                                                 }\r
315 \r
316                                                 // Load the editor skin.\r
317                                                 editor.fire( 'pluginsLoaded' );\r
318                                                 loadTheme( editor );\r
319                                         });\r
320                         });\r
321         };\r
322 \r
323         var loadSkin = function( editor )\r
324         {\r
325                 CKEDITOR.skins.load( editor, 'editor', function()\r
326                         {\r
327                                 loadLang( editor );\r
328                         });\r
329         };\r
330 \r
331         var loadTheme = function( editor )\r
332         {\r
333                 var theme = editor.config.theme;\r
334                 CKEDITOR.themes.load( theme, function()\r
335                         {\r
336                                 /**\r
337                                  * The theme used by this editor instance.\r
338                                  * @name CKEDITOR.editor.prototype.theme\r
339                                  * @type CKEDITOR.theme\r
340                                  * @example\r
341                                  * alert( editor.theme );  "http://example.com/ckeditor/themes/default/" (e.g.)\r
342                                  */\r
343                                 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );\r
344                                 editorTheme.path = CKEDITOR.themes.getPath( theme );\r
345                                 editorTheme.build( editor );\r
346 \r
347                                 if ( editor.config.autoUpdateElement )\r
348                                         attachToForm( editor );\r
349                         });\r
350         };\r
351 \r
352         var attachToForm = function( editor )\r
353         {\r
354                 var element = editor.element;\r
355 \r
356                 // If are replacing a textarea, we must\r
357                 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )\r
358                 {\r
359                         var form = element.$.form && new CKEDITOR.dom.element( element.$.form );\r
360                         if ( form )\r
361                         {\r
362                                 function onSubmit()\r
363                                 {\r
364                                         editor.updateElement();\r
365                                 }\r
366                                 form.on( 'submit',onSubmit );\r
367 \r
368                                 // Setup the submit function because it doesn't fire the\r
369                                 // "submit" event.\r
370                                 if ( !form.$.submit.nodeName && !form.$.submit.length )\r
371                                 {\r
372                                         form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )\r
373                                                 {\r
374                                                         return function()\r
375                                                                 {\r
376                                                                         editor.updateElement();\r
377 \r
378                                                                         // For IE, the DOM submit function is not a\r
379                                                                         // function, so we need thid check.\r
380                                                                         if ( originalSubmit.apply )\r
381                                                                                 originalSubmit.apply( this, arguments );\r
382                                                                         else\r
383                                                                                 originalSubmit();\r
384                                                                 };\r
385                                                 });\r
386                                 }\r
387 \r
388                                 // Remove 'submit' events registered on form element before destroying.(#3988)\r
389                                 editor.on( 'destroy', function()\r
390                                 {\r
391                                         form.removeListener( 'submit', onSubmit );\r
392                                 } );\r
393                         }\r
394                 }\r
395         };\r
396 \r
397         function updateCommandsMode()\r
398         {\r
399                 var command,\r
400                         commands = this._.commands,\r
401                         mode = this.mode;\r
402 \r
403                 for ( var name in commands )\r
404                 {\r
405                         command = commands[ name ];\r
406                         command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();\r
407                 }\r
408         }\r
409 \r
410         /**\r
411          * Initializes the editor instance. This function is called by the editor\r
412          * contructor (editor_basic.js).\r
413          * @private\r
414          */\r
415         CKEDITOR.editor.prototype._init = function()\r
416                 {\r
417                         // Get the properties that have been saved in the editor_base\r
418                         // implementation.\r
419                         var element                     = CKEDITOR.dom.element.get( this._.element ),\r
420                                 instanceConfig  = this._.instanceConfig;\r
421                         delete this._.element;\r
422                         delete this._.instanceConfig;\r
423 \r
424                         this._.commands = {};\r
425                         this._.styles = [];\r
426 \r
427                         /**\r
428                          * The DOM element that has been replaced by this editor instance. This\r
429                          * element holds the editor data on load and post.\r
430                          * @name CKEDITOR.editor.prototype.element\r
431                          * @type CKEDITOR.dom.element\r
432                          * @example\r
433                          * var editor = CKEDITOR.instances.editor1;\r
434                          * alert( <b>editor.element</b>.getName() );  "textarea"\r
435                          */\r
436                         this.element = element;\r
437 \r
438                         /**\r
439                          * The editor instance name. It hay be the replaced element id, name or\r
440                          * a default name using a progressive counter (editor1, editor2, ...).\r
441                          * @name CKEDITOR.editor.prototype.name\r
442                          * @type String\r
443                          * @example\r
444                          * var editor = CKEDITOR.instances.editor1;\r
445                          * alert( <b>editor.name</b> );  "editor1"\r
446                          */\r
447                         this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
448                                                         && ( element.getId() || element.getNameAtt() ) )\r
449                                                 || getNewName();\r
450 \r
451                         if ( this.name in CKEDITOR.instances )\r
452                                 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';\r
453 \r
454                         /**\r
455                          * A unique random string assigned to each editor instance in the page.\r
456                          * @name CKEDITOR.editor.prototype.id\r
457                          * @type String\r
458                          */\r
459                         this.id = CKEDITOR.tools.getNextId();\r
460 \r
461                         /**\r
462                          * The configurations for this editor instance. It inherits all\r
463                          * settings defined in (@link CKEDITOR.config}, combined with settings\r
464                          * loaded from custom configuration files and those defined inline in\r
465                          * the page when creating the editor.\r
466                          * @name CKEDITOR.editor.prototype.config\r
467                          * @type Object\r
468                          * @example\r
469                          * var editor = CKEDITOR.instances.editor1;\r
470                          * alert( <b>editor.config.theme</b> );  "default" e.g.\r
471                          */\r
472                         this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );\r
473 \r
474                         /**\r
475                          * Namespace containing UI features related to this editor instance.\r
476                          * @name CKEDITOR.editor.prototype.ui\r
477                          * @type CKEDITOR.ui\r
478                          * @example\r
479                          */\r
480                         this.ui = new CKEDITOR.ui( this );\r
481 \r
482                         /**\r
483                          * Controls the focus state of this editor instance. This property\r
484                          * is rarely used for normal API operations. It is mainly\r
485                          * destinated to developer adding UI elements to the editor interface.\r
486                          * @name CKEDITOR.editor.prototype.focusManager\r
487                          * @type CKEDITOR.focusManager\r
488                          * @example\r
489                          */\r
490                         this.focusManager = new CKEDITOR.focusManager( this );\r
491 \r
492                         CKEDITOR.fire( 'instanceCreated', null, this );\r
493 \r
494                         this.on( 'mode', updateCommandsMode, null, null, 1 );\r
495 \r
496                         initConfig( this, instanceConfig );\r
497                 };\r
498 })();\r
499 \r
500 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,\r
501         /** @lends CKEDITOR.editor.prototype */\r
502         {\r
503                 /**\r
504                  * Adds a command definition to the editor instance. Commands added with\r
505                  * this function can be later executed with {@link #execCommand}.\r
506                  * @param {String} commandName The indentifier name of the command.\r
507                  * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.\r
508                  * @example\r
509                  * editorInstance.addCommand( 'sample',\r
510                  * {\r
511                  *     exec : function( editor )\r
512                  *     {\r
513                  *         alert( 'Executing a command for the editor name "' + editor.name + '"!' );\r
514                  *     }\r
515                  * });\r
516                  */\r
517                 addCommand : function( commandName, commandDefinition )\r
518                 {\r
519                         return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );\r
520                 },\r
521 \r
522                 /**\r
523                  * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.\r
524                  * Note: This function should be called before editor is loaded to take effect.\r
525                  * @param css {String} CSS text.\r
526                  * @example\r
527                  * editorInstance.addCss( 'body { background-color: grey; }' );\r
528                  */\r
529                 addCss : function( css )\r
530                 {\r
531                         this._.styles.push( css );\r
532                 },\r
533 \r
534                 /**\r
535                  * Destroys the editor instance, releasing all resources used by it.\r
536                  * If the editor replaced an element, the element will be recovered.\r
537                  * @param {Boolean} [noUpdate] If the instance is replacing a DOM\r
538                  *              element, this parameter indicates whether or not to update the\r
539                  *              element with the instance contents.\r
540                  * @example\r
541                  * alert( CKEDITOR.instances.editor1 );  e.g "object"\r
542                  * <b>CKEDITOR.instances.editor1.destroy()</b>;\r
543                  * alert( CKEDITOR.instances.editor1 );  "undefined"\r
544                  */\r
545                 destroy : function( noUpdate )\r
546                 {\r
547                         if ( !noUpdate )\r
548                                 this.updateElement();\r
549 \r
550                         this.fire( 'destroy' );\r
551                         this.theme && this.theme.destroy( this );\r
552 \r
553                         CKEDITOR.remove( this );\r
554                         CKEDITOR.fire( 'instanceDestroyed', null, this );\r
555                 },\r
556 \r
557                 /**\r
558                  * Executes a command.\r
559                  * @param {String} commandName The indentifier name of the command.\r
560                  * @param {Object} [data] Data to be passed to the command\r
561                  * @returns {Boolean} "true" if the command has been successfuly\r
562                  *              executed, otherwise "false".\r
563                  * @example\r
564                  * editorInstance.execCommand( 'Bold' );\r
565                  */\r
566                 execCommand : function( commandName, data )\r
567                 {\r
568                         var command = this.getCommand( commandName );\r
569 \r
570                         var eventData =\r
571                         {\r
572                                 name: commandName,\r
573                                 commandData: data,\r
574                                 command: command\r
575                         };\r
576 \r
577                         if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )\r
578                         {\r
579                                 if ( this.fire( 'beforeCommandExec', eventData ) !== true )\r
580                                 {\r
581                                         eventData.returnValue = command.exec( eventData.commandData );\r
582 \r
583                                         // Fire the 'afterCommandExec' immediately if command is synchronous.\r
584                                         if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )\r
585                                                 return eventData.returnValue;\r
586                                 }\r
587                         }\r
588 \r
589                         // throw 'Unknown command name "' + commandName + '"';\r
590                         return false;\r
591                 },\r
592 \r
593                 /**\r
594                  * Gets one of the registered commands. Note that, after registering a\r
595                  * command definition with addCommand, it is transformed internally\r
596                  * into an instance of {@link CKEDITOR.command}, which will be then\r
597                  * returned by this function.\r
598                  * @param {String} commandName The name of the command to be returned.\r
599                  * This is the same used to register the command with addCommand.\r
600                  * @returns {CKEDITOR.command} The command object identified by the\r
601                  * provided name.\r
602                  */\r
603                 getCommand : function( commandName )\r
604                 {\r
605                         return this._.commands[ commandName ];\r
606                 },\r
607 \r
608                 /**\r
609                  * Gets the editor data. The data will be in raw format. It is the same\r
610                  * data that is posted by the editor.\r
611                  * @type String\r
612                  * @returns (String) The editor data.\r
613                  * @example\r
614                  * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )\r
615                  *     alert( 'There is no data available' );\r
616                  */\r
617                 getData : function()\r
618                 {\r
619                         this.fire( 'beforeGetData' );\r
620 \r
621                         var eventData = this._.data;\r
622 \r
623                         if ( typeof eventData != 'string' )\r
624                         {\r
625                                 var element = this.element;\r
626                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
627                                         eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();\r
628                                 else\r
629                                         eventData = '';\r
630                         }\r
631 \r
632                         eventData = { dataValue : eventData };\r
633 \r
634                         // Fire "getData" so data manipulation may happen.\r
635                         this.fire( 'getData', eventData );\r
636 \r
637                         return eventData.dataValue;\r
638                 },\r
639 \r
640                 /**\r
641                  * Gets the "raw data" currently available in the editor. This is a\r
642                  * fast method which return the data as is, without processing, so it's\r
643                  * not recommended to use it on resulting pages. It can be used instead\r
644                  * combined with the {@link #loadSnapshot} so one can automatic save\r
645                  * the editor data from time to time while the user is using the\r
646                  * editor, to avoid data loss, without risking performance issues.\r
647                  * @example\r
648                  * alert( editor.getSnapshot() );\r
649                  */\r
650                 getSnapshot : function()\r
651                 {\r
652                         var data = this.fire( 'getSnapshot' );\r
653 \r
654                         if ( typeof data != 'string' )\r
655                         {\r
656                                 var element = this.element;\r
657                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
658                                         data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();\r
659                         }\r
660 \r
661                         return data;\r
662                 },\r
663 \r
664                 /**\r
665                  * Loads "raw data" in the editor. This data is loaded with processing\r
666                  * straight to the editing area. It should not be used as a way to load\r
667                  * any kind of data, but instead in combination with\r
668                  * {@link #getSnapshot} produced data.\r
669                  * @example\r
670                  * var data = editor.getSnapshot();\r
671                  * editor.<b>loadSnapshot( data )</b>;\r
672                  */\r
673                 loadSnapshot : function( snapshot )\r
674                 {\r
675                         this.fire( 'loadSnapshot', snapshot );\r
676                 },\r
677 \r
678                 /**\r
679                  * Sets the editor data. The data must be provided in raw format (HTML).<br />\r
680                  * <br />\r
681                  * Note that this menthod is asynchronous. The "callback" parameter must\r
682                  * be used if interaction with the editor is needed after setting the data.\r
683                  * @param {String} data HTML code to replace the curent content in the\r
684                  *              editor.\r
685                  * @param {Function} callback Function to be called after the setData\r
686                  *              is completed.\r
687                  *@param {Boolean} internal Whether suppress  any event firing when copying data internally inside editor.\r
688                  * @example\r
689                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;This is the editor data.&lt;/p&gt;' );\r
690                  * @example\r
691                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;Some other editor data.&lt;/p&gt;', function()\r
692                  *     {\r
693                  *         this.checkDirty();    // true\r
694                  *     });\r
695                  */\r
696                 setData : function( data , callback, internal )\r
697                 {\r
698                         if( callback )\r
699                         {\r
700                                 this.on( 'dataReady', function( evt )\r
701                                 {\r
702                                         evt.removeListener();\r
703                                         callback.call( evt.editor );\r
704                                 } );\r
705                         }\r
706 \r
707                         // Fire "setData" so data manipulation may happen.\r
708                         var eventData = { dataValue : data };\r
709                         !internal && this.fire( 'setData', eventData );\r
710 \r
711                         this._.data = eventData.dataValue;\r
712 \r
713                         !internal && this.fire( 'afterSetData', eventData );\r
714                 },\r
715 \r
716                 /**\r
717                  * Inserts HTML into the currently selected position in the editor.\r
718                  * @param {String} data HTML code to be inserted into the editor.\r
719                  * @example\r
720                  * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>;\r
721                  */\r
722                 insertHtml : function( data )\r
723                 {\r
724                         this.fire( 'insertHtml', data );\r
725                 },\r
726 \r
727                 /**\r
728                  * Insert text content into the currently selected position in the\r
729                  * editor, in WYSIWYG mode, styles of the selected element will be applied to the inserted text,\r
730                  * spaces around the text will be leaving untouched.\r
731                  * <strong>Note:</strong> two subsequent line-breaks will introduce one paragraph, which element depends on {@link CKEDITOR.config.enterMode};\r
732                  * A single line-break will be instead translated into one &lt;br /&gt;.\r
733                  * @since 3.5\r
734                  * @param {String} text Text to be inserted into the editor.\r
735                  * @example\r
736                  * CKEDITOR.instances.editor1.<b>insertText( ' line1 \n\n line2' )</b>;\r
737                  */\r
738                 insertText : function( text )\r
739                 {\r
740                         this.fire( 'insertText', text );\r
741                 },\r
742 \r
743                 /**\r
744                  * Inserts an element into the currently selected position in the\r
745                  * editor.\r
746                  * @param {CKEDITOR.dom.element} element The element to be inserted\r
747                  *              into the editor.\r
748                  * @example\r
749                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;img src="hello.png" border="0" title="Hello" /&gt;' );\r
750                  * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;\r
751                  */\r
752                 insertElement : function( element )\r
753                 {\r
754                         this.fire( 'insertElement', element );\r
755                 },\r
756 \r
757                 /**\r
758                  * Checks whether the current editor contents present changes when\r
759                  * compared to the contents loaded into the editor at startup, or to\r
760                  * the contents available in the editor when {@link #resetDirty} has\r
761                  * been called.\r
762                  * @returns {Boolean} "true" is the contents present changes.\r
763                  * @example\r
764                  * function beforeUnload( e )\r
765                  * {\r
766                  *     if ( CKEDITOR.instances.editor1.<b>checkDirty()</b> )\r
767                  *              return e.returnValue = "You'll loose the changes made in the editor.";\r
768                  * }\r
769                  *\r
770                  * if ( window.addEventListener )\r
771                  *     window.addEventListener( 'beforeunload', beforeUnload, false );\r
772                  * else\r
773                  *     window.attachEvent( 'onbeforeunload', beforeUnload );\r
774                  */\r
775                 checkDirty : function()\r
776                 {\r
777                         return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );\r
778                 },\r
779 \r
780                 /**\r
781                  * Resets the "dirty state" of the editor so subsequent calls to\r
782                  * {@link #checkDirty} will return "false" if the user will not make\r
783                  * further changes to the contents.\r
784                  * @example\r
785                  * alert( editor.checkDirty() );  // "true" (e.g.)\r
786                  * editor.<b>resetDirty()</b>;\r
787                  * alert( editor.checkDirty() );  // "false"\r
788                  */\r
789                 resetDirty : function()\r
790                 {\r
791                         if ( this.mayBeDirty )\r
792                                 this._.previousValue = this.getSnapshot();\r
793                 },\r
794 \r
795                 /**\r
796                  * Updates the &lt;textarea&gt; element that has been replaced by the editor with\r
797                  * the current data available in the editor.\r
798                  * @example\r
799                  * CKEDITOR.instances.editor1.updateElement();\r
800                  * alert( document.getElementById( 'editor1' ).value );  // The current editor data.\r
801                  */\r
802                 updateElement : function()\r
803                 {\r
804                         var element = this.element;\r
805                         if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
806                         {\r
807                                 var data = this.getData();\r
808 \r
809                                 if ( this.config.htmlEncodeOutput )\r
810                                         data = CKEDITOR.tools.htmlEncode( data );\r
811 \r
812                                 if ( element.is( 'textarea' ) )\r
813                                         element.setValue( data );\r
814                                 else\r
815                                         element.setHtml( data );\r
816                         }\r
817                 }\r
818         });\r
819 \r
820 CKEDITOR.on( 'loaded', function()\r
821         {\r
822                 // Run the full initialization for pending editors.\r
823                 var pending = CKEDITOR.editor._pending;\r
824                 if ( pending )\r
825                 {\r
826                         delete CKEDITOR.editor._pending;\r
827 \r
828                         for ( var i = 0 ; i < pending.length ; i++ )\r
829                                 pending[ i ]._init();\r
830                 }\r
831         });\r
832 \r
833 /**\r
834  * Whether escape HTML when editor update original input element.\r
835  * @name CKEDITOR.config.htmlEncodeOutput\r
836  * @since 3.1\r
837  * @type Boolean\r
838  * @default false\r
839  * @example\r
840  * config.htmlEncodeOutput = true;\r
841  */\r
842 \r
843 /**\r
844  * Fired when a CKEDITOR instance is created, but still before initializing it.\r
845  * To interact with a fully initialized instance, use the\r
846  * {@link CKEDITOR#instanceReady} event instead.\r
847  * @name CKEDITOR#instanceCreated\r
848  * @event\r
849  * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
850  */\r
851 \r
852 /**\r
853  * Fired when a CKEDITOR instance is destroyed.\r
854  * @name CKEDITOR#instanceDestroyed\r
855  * @event\r
856  * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.\r
857  */\r
858 \r
859 /**\r
860  * Fired when all plugins are loaded and initialized into the editor instance.\r
861  * @name CKEDITOR.editor#pluginsLoaded\r
862  * @event\r
863  * @param {CKEDITOR.editor} editor This editor instance.\r
864  */\r
865 \r
866 /**\r
867  * Fired before the command execution when {@link #execCommand} is called.\r
868  * @name CKEDITOR.editor#beforeCommandExec\r
869  * @event\r
870  * @param {CKEDITOR.editor} editor This editor instance.\r
871  * @param {String} data.name The command name.\r
872  * @param {Object} data.commandData The data to be sent to the command. This\r
873  *              can be manipulated by the event listener.\r
874  * @param {CKEDITOR.command} data.command The command itself.\r
875  */\r
876 \r
877 /**\r
878  * Fired after the command execution when {@link #execCommand} is called.\r
879  * @name CKEDITOR.editor#afterCommandExec\r
880  * @event\r
881  * @param {CKEDITOR.editor} editor This editor instance.\r
882  * @param {String} data.name The command name.\r
883  * @param {Object} data.commandData The data sent to the command.\r
884  * @param {CKEDITOR.command} data.command The command itself.\r
885  * @param {Object} data.returnValue The value returned by the command execution.\r
886  */\r
887 \r
888 /**\r
889  * Fired every custom configuration file is loaded, before the final\r
890  * configurations initialization.<br />\r
891  * <br />\r
892  * Custom configuration files can be loaded thorugh the\r
893  * {@link CKEDITOR.config.customConfig} setting. Several files can be loading\r
894  * by chaning this setting.\r
895  * @name CKEDITOR.editor#customConfigLoaded\r
896  * @event\r
897  * @param {CKEDITOR.editor} editor This editor instance.\r
898  * @example\r
899  */\r
900 \r
901 /**\r
902  * Fired once the editor configuration is ready (loaded and processed).\r
903  * @name CKEDITOR.editor#configLoaded\r
904  * @event\r
905  * @param {CKEDITOR.editor} editor This editor instance.\r
906  * @example\r
907  * if( editor.config.fullPage )\r
908  *     alert( 'This is a full page editor' );\r
909  */\r
910 \r
911 /**\r
912  * Fired when this editor instance is destroyed. The editor at this\r
913  * point isn't usable and this event should be used to perform clean up\r
914  * in any plugin.\r
915  * @name CKEDITOR.editor#destroy\r
916  * @event\r
917  */\r
918 \r
919 /**\r
920  * Internal event to get the current data.\r
921  * @name CKEDITOR.editor#beforeGetData\r
922  * @event\r
923  */\r
924 \r
925 /**\r
926  * Internal event to perform the #getSnapshot call.\r
927  * @name CKEDITOR.editor#getSnapshot\r
928  * @event\r
929  */\r
930 \r
931 /**\r
932  * Internal event to perform the #loadSnapshot call.\r
933  * @name CKEDITOR.editor#loadSnapshot\r
934  * @event\r
935  */\r
936 \r
937 \r
938 /**\r
939  * Event fired before the #getData call returns allowing additional manipulation.\r
940  * @name CKEDITOR.editor#getData\r
941  * @event\r
942  * @param {CKEDITOR.editor} editor This editor instance.\r
943  * @param {String} data.dataValue The data that will be returned.\r
944  */\r
945 \r
946 /**\r
947  * Event fired before the #setData call is executed allowing additional manipulation.\r
948  * @name CKEDITOR.editor#setData\r
949  * @event\r
950  * @param {CKEDITOR.editor} editor This editor instance.\r
951  * @param {String} data.dataValue The data that will be used.\r
952  */\r
953 \r
954 /**\r
955  * Event fired at the end of the #setData call is executed. Usually it's better to use the\r
956  * {@link CKEDITOR.editor.prototype.dataReady} event.\r
957  * @name CKEDITOR.editor#afterSetData\r
958  * @event\r
959  * @param {CKEDITOR.editor} editor This editor instance.\r
960  * @param {String} data.dataValue The data that has been set.\r
961  */\r
962 \r
963 /**\r
964  * Internal event to perform the #insertHtml call\r
965  * @name CKEDITOR.editor#insertHtml\r
966  * @event\r
967  * @param {CKEDITOR.editor} editor This editor instance.\r
968  * @param {String} data The HTML to insert.\r
969  */\r
970 \r
971 /**\r
972  * Internal event to perform the #insertText call\r
973  * @name CKEDITOR.editor#insertText\r
974  * @event\r
975  * @param {CKEDITOR.editor} editor This editor instance.\r
976  * @param {String} text The text to insert.\r
977  */\r
978 \r
979 /**\r
980  * Internal event to perform the #insertElement call\r
981  * @name CKEDITOR.editor#insertElement\r
982  * @event\r
983  * @param {CKEDITOR.editor} editor This editor instance.\r
984  * @param {Object} element The element to insert.\r
985  */\r