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