JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / core / editor.js
1 /*\r
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.editor} class, which represents an\r
8  *              editor instance.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         // The counter for automatic instance names.\r
14         var nameCounter = 0;\r
15 \r
16         var getNewName = function()\r
17         {\r
18                 var name = 'editor' + ( ++nameCounter );\r
19                 return ( CKEDITOR.instances && CKEDITOR.instances[ name ] ) ? getNewName() : name;\r
20         };\r
21 \r
22         // ##### START: Config Privates\r
23 \r
24         // These function loads custom configuration files and cache the\r
25         // CKEDITOR.editorConfig functions defined on them, so there is no need to\r
26         // download them more than once for several instances.\r
27         var loadConfigLoaded = {};\r
28         var loadConfig = function( editor )\r
29         {\r
30                 var customConfig = editor.config.customConfig;\r
31 \r
32                 // Check if there is a custom config to load.\r
33                 if ( !customConfig )\r
34                         return false;\r
35 \r
36                 customConfig = CKEDITOR.getUrl( customConfig );\r
37 \r
38                 var loadedConfig = loadConfigLoaded[ customConfig ] || ( loadConfigLoaded[ customConfig ] = {} );\r
39 \r
40                 // If the custom config has already been downloaded, reuse it.\r
41                 if ( loadedConfig.fn )\r
42                 {\r
43                         // Call the cached CKEDITOR.editorConfig defined in the custom\r
44                         // config file for the editor instance depending on it.\r
45                         loadedConfig.fn.call( editor, editor.config );\r
46 \r
47                         // If there is no other customConfig in the chain, fire the\r
48                         // "configLoaded" event.\r
49                         if ( CKEDITOR.getUrl( editor.config.customConfig ) == customConfig || !loadConfig( editor ) )\r
50                                 editor.fireOnce( 'customConfigLoaded' );\r
51                 }\r
52                 else\r
53                 {\r
54                         // Load the custom configuration file.\r
55                         CKEDITOR.scriptLoader.load( customConfig, function()\r
56                                 {\r
57                                         // If the CKEDITOR.editorConfig function has been properly\r
58                                         // defined in the custom configuration file, cache it.\r
59                                         if ( CKEDITOR.editorConfig )\r
60                                                 loadedConfig.fn = CKEDITOR.editorConfig;\r
61                                         else\r
62                                                 loadedConfig.fn = function(){};\r
63 \r
64                                         // Call the load config again. This time the custom\r
65                                         // config is already cached and so it will get loaded.\r
66                                         loadConfig( editor );\r
67                                 });\r
68                 }\r
69 \r
70                 return true;\r
71         };\r
72 \r
73         var initConfig = function( editor, instanceConfig )\r
74         {\r
75                 // Setup the lister for the "customConfigLoaded" event.\r
76                 editor.on( 'customConfigLoaded', function()\r
77                         {\r
78                                 if ( instanceConfig )\r
79                                 {\r
80                                         // Register the events that may have been set at the instance\r
81                                         // configuration object.\r
82                                         if ( instanceConfig.on )\r
83                                         {\r
84                                                 for ( var eventName in instanceConfig.on )\r
85                                                 {\r
86                                                         editor.on( eventName, instanceConfig.on[ eventName ] );\r
87                                                 }\r
88                                         }\r
89 \r
90                                         // Overwrite the settings from the in-page config.\r
91                                         CKEDITOR.tools.extend( editor.config, instanceConfig, true );\r
92 \r
93                                         delete editor.config.on;\r
94                                 }\r
95 \r
96                                 onConfigLoaded( editor );\r
97                         });\r
98 \r
99                 // The instance config may override the customConfig setting to avoid\r
100                 // loading the default ~/config.js file.\r
101                 if ( instanceConfig && instanceConfig.customConfig != undefined )\r
102                         editor.config.customConfig = instanceConfig.customConfig;\r
103 \r
104                 // Load configs from the custom configuration files.\r
105                 if ( !loadConfig( editor ) )\r
106                         editor.fireOnce( 'customConfigLoaded' );\r
107         };\r
108 \r
109         // ##### END: Config Privates\r
110 \r
111         var onConfigLoaded = function( editor )\r
112         {\r
113                 // Set config related properties.\r
114 \r
115                 var skin = editor.config.skin.split( ',' ),\r
116                         skinName = skin[ 0 ],\r
117                         skinPath = CKEDITOR.getUrl( skin[ 1 ] || (\r
118                                 '_source/' +    // @Packager.RemoveLine\r
119                                 'skins/' + skinName + '/' ) );\r
120 \r
121                 /**\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 all plugins defined in the "plugins" setting.\r
231                 CKEDITOR.plugins.load( plugins.split( ',' ), function( plugins )\r
232                         {\r
233                                 // The list of plugins.\r
234                                 var pluginsArray = [];\r
235 \r
236                                 // The language code to get loaded for each plugin. Null\r
237                                 // entries will be appended for plugins with no language files.\r
238                                 var languageCodes = [];\r
239 \r
240                                 // The list of URLs to language files.\r
241                                 var languageFiles = [];\r
242 \r
243                                 /**\r
244                                  * And object holding references to all plugins used by this\r
245                                  * editor istance.\r
246                                  * @name CKEDITOR.editor.prototype.plugins\r
247                                  * @type Object\r
248                                  * @example\r
249                                  * alert( editor.plugins.dialog.path );  // "http://example.com/ckeditor/plugins/dialog/" (e.g.)\r
250                                  */\r
251                                 editor.plugins = plugins;\r
252 \r
253                                 // Loop through all plugins, to build the list of language\r
254                                 // files to get loaded.\r
255                                 for ( var pluginName in plugins )\r
256                                 {\r
257                                         var plugin = plugins[ pluginName ],\r
258                                                 pluginLangs = plugin.lang,\r
259                                                 pluginPath = CKEDITOR.plugins.getPath( pluginName ),\r
260                                                 lang = null;\r
261 \r
262                                         // Set the plugin path in the plugin.\r
263                                         plugin.path = pluginPath;\r
264 \r
265                                         // If the plugin has "lang".\r
266                                         if ( pluginLangs )\r
267                                         {\r
268                                                 // Resolve the plugin language. If the current language\r
269                                                 // is not available, get the first one (default one).\r
270                                                 lang = ( CKEDITOR.tools.indexOf( pluginLangs, editor.langCode ) >= 0 ? editor.langCode : pluginLangs[ 0 ] );\r
271 \r
272                                                 if ( !plugin.lang[ lang ] )\r
273                                                 {\r
274                                                         // Put the language file URL into the list of files to\r
275                                                         // get downloaded.\r
276                                                         languageFiles.push( CKEDITOR.getUrl( pluginPath + 'lang/' + lang + '.js' ) );\r
277                                                 }\r
278                                                 else\r
279                                                 {\r
280                                                         CKEDITOR.tools.extend( editor.lang, plugin.lang[ lang ] );\r
281                                                         lang = null;\r
282                                                 }\r
283                                         }\r
284 \r
285                                         // Save the language code, so we know later which\r
286                                         // language has been resolved to this plugin.\r
287                                         languageCodes.push( lang );\r
288 \r
289                                         pluginsArray.push( plugin );\r
290                                 }\r
291 \r
292                                 // Load all plugin specific language files in a row.\r
293                                 CKEDITOR.scriptLoader.load( languageFiles, function()\r
294                                         {\r
295                                                 // Initialize all plugins that have the "beforeInit" and "init" methods defined.\r
296                                                 var methods = [ 'beforeInit', 'init', 'afterInit' ];\r
297                                                 for ( var m = 0 ; m < methods.length ; m++ )\r
298                                                 {\r
299                                                         for ( var i = 0 ; i < pluginsArray.length ; i++ )\r
300                                                         {\r
301                                                                 var plugin = pluginsArray[ i ];\r
302 \r
303                                                                 // Uses the first loop to update the language entries also.\r
304                                                                 if ( m === 0 && languageCodes[ i ] && plugin.lang )\r
305                                                                         CKEDITOR.tools.extend( editor.lang, plugin.lang[ languageCodes[ i ] ] );\r
306 \r
307                                                                 // Call the plugin method (beforeInit and init).\r
308                                                                 if ( plugin[ methods[ m ] ] )\r
309                                                                         plugin[ methods[ m ] ]( editor );\r
310                                                         }\r
311                                                 }\r
312 \r
313                                                 // Load the editor skin.\r
314                                                 editor.fire( 'pluginsLoaded' );\r
315                                                 loadTheme( editor );\r
316                                         });\r
317                         });\r
318         };\r
319 \r
320         var loadSkin = function( editor )\r
321         {\r
322                 CKEDITOR.skins.load( editor, 'editor', function()\r
323                         {\r
324                                 loadLang( editor );\r
325                         });\r
326         };\r
327 \r
328         var loadTheme = function( editor )\r
329         {\r
330                 var theme = editor.config.theme;\r
331                 CKEDITOR.themes.load( theme, function()\r
332                         {\r
333                                 /**\r
334                                  * The theme used by this editor instance.\r
335                                  * @name CKEDITOR.editor.prototype.theme\r
336                                  * @type CKEDITOR.theme\r
337                                  * @example\r
338                                  * alert( editor.theme );  "http://example.com/ckeditor/themes/default/" (e.g.)\r
339                                  */\r
340                                 var editorTheme = editor.theme = CKEDITOR.themes.get( theme );\r
341                                 editorTheme.path = CKEDITOR.themes.getPath( theme );\r
342                                 editorTheme.build( editor );\r
343 \r
344                                 if ( editor.config.autoUpdateElement )\r
345                                         attachToForm( editor );\r
346                         });\r
347         };\r
348 \r
349         var attachToForm = function( editor )\r
350         {\r
351                 var element = editor.element;\r
352 \r
353                 // If are replacing a textarea, we must\r
354                 if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )\r
355                 {\r
356                         var form = element.$.form && new CKEDITOR.dom.element( element.$.form );\r
357                         if ( form )\r
358                         {\r
359                                 function onSubmit()\r
360                                 {\r
361                                         editor.updateElement();\r
362                                 }\r
363                                 form.on( 'submit',onSubmit );\r
364 \r
365                                 // Setup the submit function because it doesn't fire the\r
366                                 // "submit" event.\r
367                                 if ( !form.$.submit.nodeName )\r
368                                 {\r
369                                         form.$.submit = CKEDITOR.tools.override( form.$.submit, function( originalSubmit )\r
370                                                 {\r
371                                                         return function()\r
372                                                                 {\r
373                                                                         editor.updateElement();\r
374 \r
375                                                                         // For IE, the DOM submit function is not a\r
376                                                                         // function, so we need thid check.\r
377                                                                         if ( originalSubmit.apply )\r
378                                                                                 originalSubmit.apply( this, arguments );\r
379                                                                         else\r
380                                                                                 originalSubmit();\r
381                                                                 };\r
382                                                 });\r
383                                 }\r
384 \r
385                                 // Remove 'submit' events registered on form element before destroying.(#3988)\r
386                                 editor.on( 'destroy', function()\r
387                                 {\r
388                                         form.removeListener( 'submit', onSubmit );\r
389                                 } );\r
390                         }\r
391                 }\r
392         };\r
393 \r
394         function updateCommandsMode()\r
395         {\r
396                 var command,\r
397                         commands = this._.commands,\r
398                         mode = this.mode;\r
399 \r
400                 for ( var name in commands )\r
401                 {\r
402                         command = commands[ name ];\r
403                         command[ command.startDisabled ? 'disable' : command.modes[ mode ] ? 'enable' : 'disable' ]();\r
404                 }\r
405         }\r
406 \r
407         /**\r
408          * Initializes the editor instance. This function is called by the editor\r
409          * contructor (editor_basic.js).\r
410          * @private\r
411          */\r
412         CKEDITOR.editor.prototype._init = function()\r
413                 {\r
414                         // Get the properties that have been saved in the editor_base\r
415                         // implementation.\r
416                         var element                     = CKEDITOR.dom.element.get( this._.element ),\r
417                                 instanceConfig  = this._.instanceConfig;\r
418                         delete this._.element;\r
419                         delete this._.instanceConfig;\r
420 \r
421                         this._.commands = {};\r
422                         this._.styles = [];\r
423 \r
424                         /**\r
425                          * The DOM element that has been replaced by this editor instance. This\r
426                          * element holds the editor data on load and post.\r
427                          * @name CKEDITOR.editor.prototype.element\r
428                          * @type CKEDITOR.dom.element\r
429                          * @example\r
430                          * var editor = CKEDITOR.instances.editor1;\r
431                          * alert( <b>editor.element</b>.getName() );  "textarea"\r
432                          */\r
433                         this.element = element;\r
434 \r
435                         /**\r
436                          * The editor instance name. It hay be the replaced element id, name or\r
437                          * a default name using a progressive counter (editor1, editor2, ...).\r
438                          * @name CKEDITOR.editor.prototype.name\r
439                          * @type String\r
440                          * @example\r
441                          * var editor = CKEDITOR.instances.editor1;\r
442                          * alert( <b>editor.name</b> );  "editor1"\r
443                          */\r
444                         this.name = ( element && ( this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
445                                                         && ( element.getId() || element.getNameAtt() ) )\r
446                                                 || getNewName();\r
447 \r
448                         if ( this.name in CKEDITOR.instances )\r
449                                 throw '[CKEDITOR.editor] The instance "' + this.name + '" already exists.';\r
450 \r
451                         /**\r
452                          * A unique random string assigned to each editor instance in the page.\r
453                          * @name CKEDITOR.editor.prototype.id\r
454                          * @type String\r
455                          */\r
456                         this.id = CKEDITOR.tools.getNextId();\r
457 \r
458                         /**\r
459                          * The configurations for this editor instance. It inherits all\r
460                          * settings defined in (@link CKEDITOR.config}, combined with settings\r
461                          * loaded from custom configuration files and those defined inline in\r
462                          * the page when creating the editor.\r
463                          * @name CKEDITOR.editor.prototype.config\r
464                          * @type Object\r
465                          * @example\r
466                          * var editor = CKEDITOR.instances.editor1;\r
467                          * alert( <b>editor.config.theme</b> );  "default" e.g.\r
468                          */\r
469                         this.config = CKEDITOR.tools.prototypedCopy( CKEDITOR.config );\r
470 \r
471                         /**\r
472                          * Namespace containing UI features related to this editor instance.\r
473                          * @name CKEDITOR.editor.prototype.ui\r
474                          * @type CKEDITOR.ui\r
475                          * @example\r
476                          */\r
477                         this.ui = new CKEDITOR.ui( this );\r
478 \r
479                         /**\r
480                          * Controls the focus state of this editor instance. This property\r
481                          * is rarely used for normal API operations. It is mainly\r
482                          * destinated to developer adding UI elements to the editor interface.\r
483                          * @name CKEDITOR.editor.prototype.focusManager\r
484                          * @type CKEDITOR.focusManager\r
485                          * @example\r
486                          */\r
487                         this.focusManager = new CKEDITOR.focusManager( this );\r
488 \r
489                         CKEDITOR.fire( 'instanceCreated', null, this );\r
490 \r
491                         this.on( 'mode', updateCommandsMode, null, null, 1 );\r
492 \r
493                         initConfig( this, instanceConfig );\r
494                 };\r
495 })();\r
496 \r
497 CKEDITOR.tools.extend( CKEDITOR.editor.prototype,\r
498         /** @lends CKEDITOR.editor.prototype */\r
499         {\r
500                 /**\r
501                  * Adds a command definition to the editor instance. Commands added with\r
502                  * this function can be later executed with {@link #execCommand}.\r
503                  * @param {String} commandName The indentifier name of the command.\r
504                  * @param {CKEDITOR.commandDefinition} commandDefinition The command definition.\r
505                  * @example\r
506                  * editorInstance.addCommand( 'sample',\r
507                  * {\r
508                  *     exec : function( editor )\r
509                  *     {\r
510                  *         alert( 'Executing a command for the editor name "' + editor.name + '"!' );\r
511                  *     }\r
512                  * });\r
513                  */\r
514                 addCommand : function( commandName, commandDefinition )\r
515                 {\r
516                         return this._.commands[ commandName ] = new CKEDITOR.command( this, commandDefinition );\r
517                 },\r
518 \r
519                 /**\r
520                  * Add a trunk of css text to the editor which will be applied to the wysiwyg editing document.\r
521                  * Note: This function should be called before editor is loaded to take effect.\r
522                  * @param css {String} CSS text.\r
523                  * @example\r
524                  * editorInstance.addCss( 'body { background-color: grey; }' );\r
525                  */\r
526                 addCss : function( css )\r
527                 {\r
528                         this._.styles.push( css );\r
529                 },\r
530 \r
531                 /**\r
532                  * Destroys the editor instance, releasing all resources used by it.\r
533                  * If the editor replaced an element, the element will be recovered.\r
534                  * @param {Boolean} [noUpdate] If the instance is replacing a DOM\r
535                  *              element, this parameter indicates whether or not to update the\r
536                  *              element with the instance contents.\r
537                  * @example\r
538                  * alert( CKEDITOR.instances.editor1 );  e.g "object"\r
539                  * <b>CKEDITOR.instances.editor1.destroy()</b>;\r
540                  * alert( CKEDITOR.instances.editor1 );  "undefined"\r
541                  */\r
542                 destroy : function( noUpdate )\r
543                 {\r
544                         if ( !noUpdate )\r
545                                 this.updateElement();\r
546 \r
547                         if ( this.mode )\r
548                         {\r
549                                 // ->           currentMode.unload( holderElement );\r
550                                 this._.modes[ this.mode ].unload( this.getThemeSpace( 'contents' ) );\r
551                         }\r
552 \r
553                         this.theme.destroy( this );\r
554 \r
555                         var toolbars,\r
556                                 index = 0,\r
557                                 j,\r
558                                 items,\r
559                                 instance;\r
560 \r
561                         if ( this.toolbox )\r
562                         {\r
563                                 toolbars = this.toolbox.toolbars;\r
564                                 for ( ; index < toolbars.length ; index++ )\r
565                                 {\r
566                                         items = toolbars[ index ].items;\r
567                                         for ( j = 0 ; j < items.length ; j++ )\r
568                                         {\r
569                                                 instance = items[ j ];\r
570                                                 if ( instance.clickFn ) CKEDITOR.tools.removeFunction( instance.clickFn );\r
571                                                 if ( instance.keyDownFn ) CKEDITOR.tools.removeFunction( instance.keyDownFn );\r
572 \r
573                                                 if ( instance.index ) CKEDITOR.ui.button._.instances[ instance.index ] = null;\r
574                                         }\r
575                                 }\r
576                         }\r
577 \r
578                         if ( this.contextMenu )\r
579                                 CKEDITOR.tools.removeFunction( this.contextMenu._.functionId );\r
580 \r
581                         if ( this._.filebrowserFn )\r
582                                 CKEDITOR.tools.removeFunction( this._.filebrowserFn );\r
583 \r
584                         this.fire( 'destroy' );\r
585                         CKEDITOR.remove( this );\r
586                         CKEDITOR.fire( 'instanceDestroyed', null, this );\r
587                 },\r
588 \r
589                 /**\r
590                  * Executes a command.\r
591                  * @param {String} commandName The indentifier name of the command.\r
592                  * @param {Object} [data] Data to be passed to the command\r
593                  * @returns {Boolean} "true" if the command has been successfuly\r
594                  *              executed, otherwise "false".\r
595                  * @example\r
596                  * editorInstance.execCommand( 'Bold' );\r
597                  */\r
598                 execCommand : function( commandName, data )\r
599                 {\r
600                         var command = this.getCommand( commandName );\r
601 \r
602                         var eventData =\r
603                         {\r
604                                 name: commandName,\r
605                                 commandData: data,\r
606                                 command: command\r
607                         };\r
608 \r
609                         if ( command && command.state != CKEDITOR.TRISTATE_DISABLED )\r
610                         {\r
611                                 if ( this.fire( 'beforeCommandExec', eventData ) !== true )\r
612                                 {\r
613                                         eventData.returnValue = command.exec( eventData.commandData );\r
614 \r
615                                         // Fire the 'afterCommandExec' immediately if command is synchronous.\r
616                                         if ( !command.async && this.fire( 'afterCommandExec', eventData ) !== true )\r
617                                                 return eventData.returnValue;\r
618                                 }\r
619                         }\r
620 \r
621                         // throw 'Unknown command name "' + commandName + '"';\r
622                         return false;\r
623                 },\r
624 \r
625                 /**\r
626                  * Gets one of the registered commands. Note that, after registering a\r
627                  * command definition with addCommand, it is transformed internally\r
628                  * into an instance of {@link CKEDITOR.command}, which will be then\r
629                  * returned by this function.\r
630                  * @param {String} commandName The name of the command to be returned.\r
631                  * This is the same used to register the command with addCommand.\r
632                  * @returns {CKEDITOR.command} The command object identified by the\r
633                  * provided name.\r
634                  */\r
635                 getCommand : function( commandName )\r
636                 {\r
637                         return this._.commands[ commandName ];\r
638                 },\r
639 \r
640                 /**\r
641                  * Gets the editor data. The data will be in raw format. It is the same\r
642                  * data that is posted by the editor.\r
643                  * @type String\r
644                  * @returns (String) The editor data.\r
645                  * @example\r
646                  * if ( CKEDITOR.instances.editor1.<b>getData()</b> == '' )\r
647                  *     alert( 'There is no data available' );\r
648                  */\r
649                 getData : function()\r
650                 {\r
651                         this.fire( 'beforeGetData' );\r
652 \r
653                         var eventData = this._.data;\r
654 \r
655                         if ( typeof eventData != 'string' )\r
656                         {\r
657                                 var element = this.element;\r
658                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
659                                         eventData = element.is( 'textarea' ) ? element.getValue() : element.getHtml();\r
660                                 else\r
661                                         eventData = '';\r
662                         }\r
663 \r
664                         eventData = { dataValue : eventData };\r
665 \r
666                         // Fire "getData" so data manipulation may happen.\r
667                         this.fire( 'getData', eventData );\r
668 \r
669                         return eventData.dataValue;\r
670                 },\r
671 \r
672                 /**\r
673                  * Gets the "raw data" currently available in the editor. This is a\r
674                  * fast method which return the data as is, without processing, so it's\r
675                  * not recommended to use it on resulting pages. It can be used instead\r
676                  * combined with the {@link #loadSnapshot} so one can automatic save\r
677                  * the editor data from time to time while the user is using the\r
678                  * editor, to avoid data loss, without risking performance issues.\r
679                  * @example\r
680                  * alert( editor.getSnapshot() );\r
681                  */\r
682                 getSnapshot : function()\r
683                 {\r
684                         var data = this.fire( 'getSnapshot' );\r
685 \r
686                         if ( typeof data != 'string' )\r
687                         {\r
688                                 var element = this.element;\r
689                                 if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
690                                         data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();\r
691                         }\r
692 \r
693                         return data;\r
694                 },\r
695 \r
696                 /**\r
697                  * Loads "raw data" in the editor. This data is loaded with processing\r
698                  * straight to the editing area. It should not be used as a way to load\r
699                  * any kind of data, but instead in combination with\r
700                  * {@link #getSnapshot} produced data.\r
701                  * @example\r
702                  * var data = editor.getSnapshot();\r
703                  * editor.<b>loadSnapshot( data )</b>;\r
704                  */\r
705                 loadSnapshot : function( snapshot )\r
706                 {\r
707                         this.fire( 'loadSnapshot', snapshot );\r
708                 },\r
709 \r
710                 /**\r
711                  * Sets the editor data. The data must be provided in raw format (HTML).<br />\r
712                  * <br />\r
713                  * Note that this menthod is asynchronous. The "callback" parameter must\r
714                  * be used if interaction with the editor is needed after setting the data.\r
715                  * @param {String} data HTML code to replace the curent content in the\r
716                  *              editor.\r
717                  * @param {Function} callback Function to be called after the setData\r
718                  *              is completed.\r
719                  * @example\r
720                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;This is the editor data.&lt;/p&gt;' );\r
721                  * @example\r
722                  * CKEDITOR.instances.editor1.<b>setData</b>( '&lt;p&gt;Some other editor data.&lt;/p&gt;', function()\r
723                  *     {\r
724                  *         this.checkDirty();    // true\r
725                  *     });\r
726                  */\r
727                 setData : function( data , callback )\r
728                 {\r
729                         if( callback )\r
730                         {\r
731                                 this.on( 'dataReady', function( evt )\r
732                                 {\r
733                                         evt.removeListener();\r
734                                         callback.call( evt.editor );\r
735                                 } );\r
736                         }\r
737 \r
738                         // Fire "setData" so data manipulation may happen.\r
739                         var eventData = { dataValue : data };\r
740                         this.fire( 'setData', eventData );\r
741 \r
742                         this._.data = eventData.dataValue;\r
743 \r
744                         this.fire( 'afterSetData', eventData );\r
745                 },\r
746 \r
747                 /**\r
748                  * Inserts HTML into the currently selected position in the editor.\r
749                  * @param {String} data HTML code to be inserted into the editor.\r
750                  * @example\r
751                  * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>;\r
752                  */\r
753                 insertHtml : function( data )\r
754                 {\r
755                         this.fire( 'insertHtml', data );\r
756                 },\r
757 \r
758                 /**\r
759                  * Inserts an element into the currently selected position in the\r
760                  * editor.\r
761                  * @param {CKEDITOR.dom.element} element The element to be inserted\r
762                  *              into the editor.\r
763                  * @example\r
764                  * var element = CKEDITOR.dom.element.createFromHtml( '&lt;img src="hello.png" border="0" title="Hello" /&gt;' );\r
765                  * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;\r
766                  */\r
767                 insertElement : function( element )\r
768                 {\r
769                         this.fire( 'insertElement', element );\r
770                 },\r
771 \r
772                 /**\r
773                  * Checks whether the current editor contents present changes when\r
774                  * compared to the contents loaded into the editor at startup, or to\r
775                  * the contents available in the editor when {@link #resetDirty} has\r
776                  * been called.\r
777                  * @returns {Boolean} "true" is the contents present changes.\r
778                  * @example\r
779                  * function beforeUnload( e )\r
780                  * {\r
781                  *     if ( CKEDITOR.instances.editor1.<b>checkDirty()</b> )\r
782                  *              return e.returnValue = "You'll loose the changes made in the editor.";\r
783                  * }\r
784                  *\r
785                  * if ( window.addEventListener )\r
786                  *     window.addEventListener( 'beforeunload', beforeUnload, false );\r
787                  * else\r
788                  *     window.attachEvent( 'onbeforeunload', beforeUnload );\r
789                  */\r
790                 checkDirty : function()\r
791                 {\r
792                         return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );\r
793                 },\r
794 \r
795                 /**\r
796                  * Resets the "dirty state" of the editor so subsequent calls to\r
797                  * {@link #checkDirty} will return "false" if the user will not make\r
798                  * further changes to the contents.\r
799                  * @example\r
800                  * alert( editor.checkDirty() );  // "true" (e.g.)\r
801                  * editor.<b>resetDirty()</b>;\r
802                  * alert( editor.checkDirty() );  // "false"\r
803                  */\r
804                 resetDirty : function()\r
805                 {\r
806                         if ( this.mayBeDirty )\r
807                                 this._.previousValue = this.getSnapshot();\r
808                 },\r
809 \r
810                 /**\r
811                  * Updates the &lt;textarea&gt; element that has been replaced by the editor with\r
812                  * the current data available in the editor.\r
813                  * @example\r
814                  * CKEDITOR.instances.editor1.updateElement();\r
815                  * alert( document.getElementById( 'editor1' ).value );  // The current editor data.\r
816                  */\r
817                 updateElement : function()\r
818                 {\r
819                         var element = this.element;\r
820                         if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )\r
821                         {\r
822                                 var data = this.getData();\r
823 \r
824                                 if ( this.config.htmlEncodeOutput )\r
825                                         data = CKEDITOR.tools.htmlEncode( data );\r
826 \r
827                                 if ( element.is( 'textarea' ) )\r
828                                         element.setValue( data );\r
829                                 else\r
830                                         element.setHtml( data );\r
831                         }\r
832                 }\r
833         });\r
834 \r
835 CKEDITOR.on( 'loaded', function()\r
836         {\r
837                 // Run the full initialization for pending editors.\r
838                 var pending = CKEDITOR.editor._pending;\r
839                 if ( pending )\r
840                 {\r
841                         delete CKEDITOR.editor._pending;\r
842 \r
843                         for ( var i = 0 ; i < pending.length ; i++ )\r
844                                 pending[ i ]._init();\r
845                 }\r
846         });\r
847 \r
848 /**\r
849  * Whether escape HTML when editor update original input element.\r
850  * @name CKEDITOR.config.htmlEncodeOutput\r
851  * @since 3.1\r
852  * @type Boolean\r
853  * @default false\r
854  * @example\r
855  * config.htmlEncodeOutput = true;\r
856  */\r
857 \r
858 /**\r
859  * Fired when a CKEDITOR instance is created, but still before initializing it.\r
860  * To interact with a fully initialized instance, use the\r
861  * {@link CKEDITOR#instanceReady} event instead.\r
862  * @name CKEDITOR#instanceCreated\r
863  * @event\r
864  * @param {CKEDITOR.editor} editor The editor instance that has been created.\r
865  */\r
866 \r
867 /**\r
868  * Fired when a CKEDITOR instance is destroyed.\r
869  * @name CKEDITOR#instanceDestroyed\r
870  * @event\r
871  * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.\r
872  */\r
873 \r
874 /**\r
875  * Fired when all plugins are loaded and initialized into the editor instance.\r
876  * @name CKEDITOR#pluginsLoaded\r
877  * @event\r
878  * @param {CKEDITOR.editor} editor The editor instance that has been destroyed.\r
879  */\r
880 \r
881 /**\r
882  * Fired before the command execution when {@link #execCommand} is called.\r
883  * @name CKEDITOR.editor#beforeCommandExec\r
884  * @event\r
885  * @param {CKEDITOR.editor} editor This editor instance.\r
886  * @param {String} data.name The command name.\r
887  * @param {Object} data.commandData The data to be sent to the command. This\r
888  *              can be manipulated by the event listener.\r
889  * @param {CKEDITOR.command} data.command The command itself.\r
890  */\r
891 \r
892 /**\r
893  * Fired after the command execution when {@link #execCommand} is called.\r
894  * @name CKEDITOR.editor#afterCommandExec\r
895  * @event\r
896  * @param {CKEDITOR.editor} editor This editor instance.\r
897  * @param {String} data.name The command name.\r
898  * @param {Object} data.commandData The data sent to the command.\r
899  * @param {CKEDITOR.command} data.command The command itself.\r
900  * @param {Object} data.returnValue The value returned by the command execution.\r
901  */\r
902 \r
903 /**\r
904  * Fired every custom configuration file is loaded, before the final\r
905  * configurations initialization.<br />\r
906  * <br />\r
907  * Custom configuration files can be loaded thorugh the\r
908  * {@link CKEDITOR.config.customConfig} setting. Several files can be loading\r
909  * by chaning this setting.\r
910  * @name CKEDITOR.editor#customConfigLoaded\r
911  * @event\r
912  * @param {CKEDITOR.editor} editor This editor instance.\r
913  * @example\r
914  */\r
915 \r
916 /**\r
917  * Fired once the editor configuration is ready (loaded and processed).\r
918  * @name CKEDITOR.editor#configLoaded\r
919  * @event\r
920  * @param {CKEDITOR.editor} editor This editor instance.\r
921  * @example\r
922  * if( editor.config.fullPage )\r
923  *     alert( 'This is a full page editor' );\r
924  */\r