JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
a1bff9af0da81409fca280cd54ae3a24aff4b923
[ckeditor.git] / _source / core / tools.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.tools} object, which contains\r
8  *              utility functions.\r
9  */\r
10 \r
11 (function()\r
12 {\r
13         var functions = [];\r
14 \r
15         CKEDITOR.on( 'reset', function()\r
16                 {\r
17                         functions = [];\r
18                 });\r
19 \r
20         /**\r
21          * Utility functions.\r
22          * @namespace\r
23          * @example\r
24          */\r
25         CKEDITOR.tools =\r
26         {\r
27                 /**\r
28                  * Compare the elements of two arrays.\r
29                  * @param {Array} arrayA An array to be compared.\r
30                  * @param {Array} arrayB The other array to be compared.\r
31                  * @returns {Boolean} "true" is the arrays have the same lenght and\r
32                  *              their elements match.\r
33                  * @example\r
34                  * var a = [ 1, 'a', 3 ];\r
35                  * var b = [ 1, 3, 'a' ];\r
36                  * var c = [ 1, 'a', 3 ];\r
37                  * var d = [ 1, 'a', 3, 4 ];\r
38                  *\r
39                  * alert( CKEDITOR.tools.arrayCompare( a, b ) );  // false\r
40                  * alert( CKEDITOR.tools.arrayCompare( a, c ) );  // true\r
41                  * alert( CKEDITOR.tools.arrayCompare( a, d ) );  // false\r
42                  */\r
43                 arrayCompare : function( arrayA, arrayB )\r
44                 {\r
45                         if ( !arrayA && !arrayB )\r
46                                 return true;\r
47 \r
48                         if ( !arrayA || !arrayB || arrayA.length != arrayB.length )\r
49                                 return false;\r
50 \r
51                         for ( var i = 0 ; i < arrayA.length ; i++ )\r
52                         {\r
53                                 if ( arrayA[ i ] != arrayB[ i ] )\r
54                                         return false;\r
55                         }\r
56 \r
57                         return true;\r
58                 },\r
59 \r
60                 /**\r
61                  * Creates a deep copy of an object.\r
62                  * Attention: there is no support for recursive references.\r
63                  * @param {Object} object The object to be cloned.\r
64                  * @returns {Object} The object clone.\r
65                  * @example\r
66                  * var obj =\r
67                  *     {\r
68                  *         name : 'John',\r
69                  *         cars :\r
70                  *             {\r
71                  *                 Mercedes : { color : 'blue' },\r
72                  *                 Porsche : { color : 'red' }\r
73                  *             }\r
74                  *     };\r
75                  * var clone = CKEDITOR.tools.clone( obj );\r
76                  * clone.name = 'Paul';\r
77                  * clone.cars.Porsche.color = 'silver';\r
78                  * alert( obj.name );   // John\r
79                  * alert( clone.name ); // Paul\r
80                  * alert( obj.cars.Porsche.color );     // red\r
81                  * alert( clone.cars.Porsche.color );   // silver\r
82                  */\r
83                 clone : function( obj )\r
84                 {\r
85                         var clone;\r
86 \r
87                         // Array.\r
88                         if ( obj && ( obj instanceof Array ) )\r
89                         {\r
90                                 clone = [];\r
91 \r
92                                 for ( var i = 0 ; i < obj.length ; i++ )\r
93                                         clone[ i ] = this.clone( obj[ i ] );\r
94 \r
95                                 return clone;\r
96                         }\r
97 \r
98                         // "Static" types.\r
99                         if ( obj === null\r
100                                 || ( typeof( obj ) != 'object' )\r
101                                 || ( obj instanceof String )\r
102                                 || ( obj instanceof Number )\r
103                                 || ( obj instanceof Boolean )\r
104                                 || ( obj instanceof Date )\r
105                                 || ( obj instanceof RegExp) )\r
106                         {\r
107                                 return obj;\r
108                         }\r
109 \r
110                         // Objects.\r
111                         clone = new obj.constructor();\r
112 \r
113                         for ( var propertyName in obj )\r
114                         {\r
115                                 var property = obj[ propertyName ];\r
116                                 clone[ propertyName ] = this.clone( property );\r
117                         }\r
118 \r
119                         return clone;\r
120                 },\r
121 \r
122                 /**\r
123                  * Turn the first letter of string to upper-case.\r
124                  * @param {String} str\r
125                  */\r
126                 capitalize: function( str )\r
127                 {\r
128                         return str.charAt( 0 ).toUpperCase() + str.substring( 1 ).toLowerCase();\r
129                 },\r
130 \r
131                 /**\r
132                  * Copy the properties from one object to another. By default, properties\r
133                  * already present in the target object <strong>are not</strong> overwritten.\r
134                  * @param {Object} target The object to be extended.\r
135                  * @param {Object} source[,souce(n)] The objects from which copy\r
136                  *              properties. Any number of objects can be passed to this function.\r
137                  * @param {Boolean} [overwrite] If 'true' is specified it indicates that\r
138                  *            properties already present in the target object could be\r
139                  *            overwritten by subsequent objects.\r
140                  * @param {Object} [properties] Only properties within the specified names\r
141                  *            list will be received from the source object.\r
142                  * @returns {Object} the extended object (target).\r
143                  * @example\r
144                  * // Create the sample object.\r
145                  * var myObject =\r
146                  * {\r
147                  *     prop1 : true\r
148                  * };\r
149                  *\r
150                  * // Extend the above object with two properties.\r
151                  * CKEDITOR.tools.extend( myObject,\r
152                  *     {\r
153                  *         prop2 : true,\r
154                  *         prop3 : true\r
155                  *     } );\r
156                  *\r
157                  * // Alert "prop1", "prop2" and "prop3".\r
158                  * for ( var p in myObject )\r
159                  *     alert( p );\r
160                  */\r
161                 extend : function( target )\r
162                 {\r
163                         var argsLength = arguments.length,\r
164                                 overwrite, propertiesList;\r
165 \r
166                         if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')\r
167                                 argsLength--;\r
168                         else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )\r
169                         {\r
170                                 propertiesList = arguments [ argsLength -1 ];\r
171                                 argsLength-=2;\r
172                         }\r
173                         for ( var i = 1 ; i < argsLength ; i++ )\r
174                         {\r
175                                 var source = arguments[ i ];\r
176                                 for ( var propertyName in source )\r
177                                 {\r
178                                         // Only copy existed fields if in overwrite mode.\r
179                                         if ( overwrite === true || target[ propertyName ] == undefined )\r
180                                         {\r
181                                                 // Only copy  specified fields if list is provided.\r
182                                                 if ( !propertiesList || ( propertyName in propertiesList ) )\r
183                                                         target[ propertyName ] = source[ propertyName ];\r
184 \r
185                                         }\r
186                                 }\r
187                         }\r
188 \r
189                         return target;\r
190                 },\r
191 \r
192                 /**\r
193                  * Creates an object which is an instance of a class which prototype is a\r
194                  * predefined object. All properties defined in the source object are\r
195                  * automatically inherited by the resulting object, including future\r
196                  * changes to it.\r
197                  * @param {Object} source The source object to be used as the prototype for\r
198                  *              the final object.\r
199                  * @returns {Object} The resulting copy.\r
200                  */\r
201                 prototypedCopy : function( source )\r
202                 {\r
203                         var copy = function()\r
204                         {};\r
205                         copy.prototype = source;\r
206                         return new copy();\r
207                 },\r
208 \r
209                 /**\r
210                  * Checks if an object is an Array.\r
211                  * @param {Object} object The object to be checked.\r
212                  * @type Boolean\r
213                  * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.\r
214                  * @example\r
215                  * alert( CKEDITOR.tools.isArray( [] ) );      // "true"\r
216                  * alert( CKEDITOR.tools.isArray( 'Test' ) );  // "false"\r
217                  */\r
218                 isArray : function( object )\r
219                 {\r
220                         return ( !!object && object instanceof Array );\r
221                 },\r
222 \r
223                 /**\r
224                  * Whether the object contains no properties of it's own.\r
225                  * @param object\r
226                  */\r
227                 isEmpty : function ( object )\r
228                 {\r
229                         for ( var i in object )\r
230                         {\r
231                                 if ( object.hasOwnProperty( i ) )\r
232                                         return false;\r
233                         }\r
234                         return true;\r
235                 },\r
236 \r
237                 /**\r
238                  * Transforms a CSS property name to its relative DOM style name.\r
239                  * @param {String} cssName The CSS property name.\r
240                  * @returns {String} The transformed name.\r
241                  * @example\r
242                  * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) );  // "backgroundColor"\r
243                  * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) );             // "cssFloat"\r
244                  */\r
245                 cssStyleToDomStyle : ( function()\r
246                 {\r
247                         var test = document.createElement( 'div' ).style;\r
248 \r
249                         var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'\r
250                                 : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'\r
251                                 : 'float';\r
252 \r
253                         return function( cssName )\r
254                         {\r
255                                 if ( cssName == 'float' )\r
256                                         return cssFloat;\r
257                                 else\r
258                                 {\r
259                                         return cssName.replace( /-./g, function( match )\r
260                                                 {\r
261                                                         return match.substr( 1 ).toUpperCase();\r
262                                                 });\r
263                                 }\r
264                         };\r
265                 } )(),\r
266 \r
267                 /**\r
268                  * Build the HTML snippet of a set of &lt;style>/&lt;link>.\r
269                  * @param css {String|Array} Each of which are url (absolute) of a CSS file or\r
270                  * a trunk of style text.\r
271                  */\r
272                 buildStyleHtml : function ( css )\r
273                 {\r
274                         css = [].concat( css );\r
275                         var item, retval = [];\r
276                         for ( var i = 0; i < css.length; i++ )\r
277                         {\r
278                                 item = css[ i ];\r
279                                 // Is CSS style text ?\r
280                                 if ( /@import|[{}]/.test(item) )\r
281                                         retval.push('<style>' + item + '</style>');\r
282                                 else\r
283                                         retval.push('<link type="text/css" rel=stylesheet href="' + item + '">');\r
284                         }\r
285                         return retval.join( '' );\r
286                 },\r
287 \r
288                 /**\r
289                  * Replace special HTML characters in a string with their relative HTML\r
290                  * entity values.\r
291                  * @param {String} text The string to be encoded.\r
292                  * @returns {String} The encode string.\r
293                  * @example\r
294                  * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) );  // "A &amp;gt; B &amp;amp; C &amp;lt; D"\r
295                  */\r
296                 htmlEncode : function( text )\r
297                 {\r
298                         var standard = function( text )\r
299                         {\r
300                                 var span = new CKEDITOR.dom.element( 'span' );\r
301                                 span.setText( text );\r
302                                 return span.getHtml();\r
303                         };\r
304 \r
305                         var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?\r
306                                 function( text )\r
307                                 {\r
308                                         // #3874 IE and Safari encode line-break into <br>\r
309                                         return standard( text ).replace( /<br>/gi, '\n' );\r
310                                 } :\r
311                                 standard;\r
312 \r
313                         var fix2 = ( standard( '>' ) == '>' ) ?\r
314                                 function( text )\r
315                                 {\r
316                                         // WebKit does't encode the ">" character, which makes sense, but\r
317                                         // it's different than other browsers.\r
318                                         return fix1( text ).replace( />/g, '&gt;' );\r
319                                 } :\r
320                                 fix1;\r
321 \r
322                         var fix3 = ( standard( '  ' ) == '&nbsp; ' ) ?\r
323                                 function( text )\r
324                                 {\r
325                                         // #3785 IE8 changes spaces (>= 2) to &nbsp;\r
326                                         return fix2( text ).replace( /&nbsp;/g, ' ' );\r
327                                 } :\r
328                                 fix2;\r
329 \r
330                         this.htmlEncode = fix3;\r
331 \r
332                         return this.htmlEncode( text );\r
333                 },\r
334 \r
335                 /**\r
336                  * Replace special HTML characters in HTMLElement's attribute with their relative HTML entity values.\r
337                  * @param {String} The attribute's value to be encoded.\r
338                  * @returns {String} The encode value.\r
339                  * @example\r
340                  * element.setAttribute( 'title', '<a " b >' );\r
341                  * alert( CKEDITOR.tools.htmlEncodeAttr( element.getAttribute( 'title' ) );  // "&gt;a &quot; b &lt;"\r
342                  */\r
343                 htmlEncodeAttr : function( text )\r
344                 {\r
345                         return text.replace( /"/g, '&quot;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' );\r
346                 },\r
347 \r
348                 /**\r
349                  * Replace characters can't be represented through CSS Selectors string\r
350                  * by CSS Escape Notation where the character escape sequence consists\r
351                  * of a backslash character (\) followed by the orginal characters.\r
352                  * Ref: http://www.w3.org/TR/css3-selectors/#grammar\r
353                  * @param cssSelectText\r
354                  * @return the escaped selector text.\r
355                  */\r
356                 escapeCssSelector : function( cssSelectText )\r
357                 {\r
358                         return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' );\r
359                 },\r
360 \r
361                 /**\r
362                  * Gets a unique number for this CKEDITOR execution session. It returns\r
363                  * progressive numbers starting at 1.\r
364                  * @function\r
365                  * @returns {Number} A unique number.\r
366                  * @example\r
367                  * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "1" (e.g.)\r
368                  * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "2"\r
369                  */\r
370                 getNextNumber : (function()\r
371                 {\r
372                         var last = 0;\r
373                         return function()\r
374                         {\r
375                                 return ++last;\r
376                         };\r
377                 })(),\r
378 \r
379                 /**\r
380                  * Gets a unique ID for CKEditor's interface elements. It returns a\r
381                  * string with the "cke_" prefix and a progressive number.\r
382                  * @function\r
383                  * @returns {String} A unique ID.\r
384                  * @example\r
385                  * alert( CKEDITOR.tools.<b>getNextId()</b> );  // "cke_1" (e.g.)\r
386                  * alert( CKEDITOR.tools.<b>getNextId()</b> );  // "cke_2"\r
387                  */\r
388                 getNextId : function()\r
389                 {\r
390                         return 'cke_' + this.getNextNumber();\r
391                 },\r
392 \r
393                 /**\r
394                  * Creates a function override.\r
395                  * @param {Function} originalFunction The function to be overridden.\r
396                  * @param {Function} functionBuilder A function that returns the new\r
397                  *              function. The original function reference will be passed to this\r
398                  *              function.\r
399                  * @returns {Function} The new function.\r
400                  * @example\r
401                  * var example =\r
402                  * {\r
403                  *     myFunction : function( name )\r
404                  *     {\r
405                  *         alert( 'Name: ' + name );\r
406                  *     }\r
407                  * };\r
408                  *\r
409                  * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )\r
410                  *     {\r
411                  *         return function( name )\r
412                  *             {\r
413                  *                 alert( 'Override Name: ' + name );\r
414                  *                 myFunctionOriginal.call( this, name );\r
415                  *             };\r
416                  *     });\r
417                  */\r
418                 override : function( originalFunction, functionBuilder )\r
419                 {\r
420                         return functionBuilder( originalFunction );\r
421                 },\r
422 \r
423                 /**\r
424                  * Executes a function after specified delay.\r
425                  * @param {Function} func The function to be executed.\r
426                  * @param {Number} [milliseconds] The amount of time (millisecods) to wait\r
427                  *              to fire the function execution. Defaults to zero.\r
428                  * @param {Object} [scope] The object to hold the function execution scope\r
429                  *              (the "this" object). By default the "window" object.\r
430                  * @param {Object|Array} [args] A single object, or an array of objects, to\r
431                  *              pass as arguments to the function.\r
432                  * @param {Object} [ownerWindow] The window that will be used to set the\r
433                  *              timeout. By default the current "window".\r
434                  * @returns {Object} A value that can be used to cancel the function execution.\r
435                  * @example\r
436                  * CKEDITOR.tools.<b>setTimeout(\r
437                  *     function()\r
438                  *     {\r
439                  *         alert( 'Executed after 2 seconds' );\r
440                  *     },\r
441                  *     2000 )</b>;\r
442                  */\r
443                 setTimeout : function( func, milliseconds, scope, args, ownerWindow )\r
444                 {\r
445                         if ( !ownerWindow )\r
446                                 ownerWindow = window;\r
447 \r
448                         if ( !scope )\r
449                                 scope = ownerWindow;\r
450 \r
451                         return ownerWindow.setTimeout(\r
452                                 function()\r
453                                 {\r
454                                         if ( args )\r
455                                                 func.apply( scope, [].concat( args ) ) ;\r
456                                         else\r
457                                                 func.apply( scope ) ;\r
458                                 },\r
459                                 milliseconds || 0 );\r
460                 },\r
461 \r
462                 /**\r
463                  * Remove spaces from the start and the end of a string. The following\r
464                  * characters are removed: space, tab, line break, line feed.\r
465                  * @function\r
466                  * @param {String} str The text from which remove the spaces.\r
467                  * @returns {String} The modified string without the boundary spaces.\r
468                  * @example\r
469                  * alert( CKEDITOR.tools.trim( '  example ' );  // "example"\r
470                  */\r
471                 trim : (function()\r
472                 {\r
473                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
474                         var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;\r
475                         return function( str )\r
476                         {\r
477                                 return str.replace( trimRegex, '' ) ;\r
478                         };\r
479                 })(),\r
480 \r
481                 /**\r
482                  * Remove spaces from the start (left) of a string. The following\r
483                  * characters are removed: space, tab, line break, line feed.\r
484                  * @function\r
485                  * @param {String} str The text from which remove the spaces.\r
486                  * @returns {String} The modified string excluding the removed spaces.\r
487                  * @example\r
488                  * alert( CKEDITOR.tools.ltrim( '  example ' );  // "example "\r
489                  */\r
490                 ltrim : (function()\r
491                 {\r
492                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
493                         var trimRegex = /^[ \t\n\r]+/g;\r
494                         return function( str )\r
495                         {\r
496                                 return str.replace( trimRegex, '' ) ;\r
497                         };\r
498                 })(),\r
499 \r
500                 /**\r
501                  * Remove spaces from the end (right) of a string. The following\r
502                  * characters are removed: space, tab, line break, line feed.\r
503                  * @function\r
504                  * @param {String} str The text from which remove the spaces.\r
505                  * @returns {String} The modified string excluding the removed spaces.\r
506                  * @example\r
507                  * alert( CKEDITOR.tools.ltrim( '  example ' );  // "  example"\r
508                  */\r
509                 rtrim : (function()\r
510                 {\r
511                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
512                         var trimRegex = /[ \t\n\r]+$/g;\r
513                         return function( str )\r
514                         {\r
515                                 return str.replace( trimRegex, '' ) ;\r
516                         };\r
517                 })(),\r
518 \r
519                 /**\r
520                  * Returns the index of an element in an array.\r
521                  * @param {Array} array The array to be searched.\r
522                  * @param {Object} entry The element to be found.\r
523                  * @returns {Number} The (zero based) index of the first entry that matches\r
524                  *              the entry, or -1 if not found.\r
525                  * @example\r
526                  * var letters = [ 'a', 'b', 0, 'c', false ];\r
527                  * alert( CKEDITOR.tools.indexOf( letters, '0' ) );  "-1" because 0 !== '0'\r
528                  * alert( CKEDITOR.tools.indexOf( letters, false ) );  "4" because 0 !== false\r
529                  */\r
530                 indexOf :\r
531                         // #2514: We should try to use Array.indexOf if it does exist.\r
532                         ( Array.prototype.indexOf ) ?\r
533                                 function( array, entry )\r
534                                         {\r
535                                                 return array.indexOf( entry );\r
536                                         }\r
537                         :\r
538                                 function( array, entry )\r
539                                 {\r
540                                         for ( var i = 0, len = array.length ; i < len ; i++ )\r
541                                         {\r
542                                                 if ( array[ i ] === entry )\r
543                                                         return i;\r
544                                         }\r
545                                         return -1;\r
546                                 },\r
547 \r
548                 /**\r
549                  * Creates a function that will always execute in the context of a\r
550                  * specified object.\r
551                  * @param {Function} func The function to be executed.\r
552                  * @param {Object} obj The object to which bind the execution context.\r
553                  * @returns {Function} The function that can be used to execute the\r
554                  *              "func" function in the context of "obj".\r
555                  * @example\r
556                  * var obj = { text : 'My Object' };\r
557                  *\r
558                  * function alertText()\r
559                  * {\r
560                  *     alert( this.text );\r
561                  * }\r
562                  *\r
563                  * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;\r
564                  * newFunc();  // Alerts "My Object".\r
565                  */\r
566                 bind : function( func, obj )\r
567                 {\r
568                         return function() { return func.apply( obj, arguments ); };\r
569                 },\r
570 \r
571                 /**\r
572                  * Class creation based on prototype inheritance, with supports of the\r
573                  * following features:\r
574                  * <ul>\r
575                  * <li> Static fields </li>\r
576                  * <li> Private fields </li>\r
577                  * <li> Public (prototype) fields </li>\r
578                  * <li> Chainable base class constructor </li>\r
579                  * </ul>\r
580                  * @param {Object} definition The class definition object.\r
581                  * @returns {Function} A class-like JavaScript function.\r
582                  */\r
583                 createClass : function( definition )\r
584                 {\r
585                         var $ = definition.$,\r
586                                 baseClass = definition.base,\r
587                                 privates = definition.privates || definition._,\r
588                                 proto = definition.proto,\r
589                                 statics = definition.statics;\r
590 \r
591                         if ( privates )\r
592                         {\r
593                                 var originalConstructor = $;\r
594                                 $ = function()\r
595                                 {\r
596                                         // Create (and get) the private namespace.\r
597                                         var _ = this._ || ( this._ = {} );\r
598 \r
599                                         // Make some magic so "this" will refer to the main\r
600                                         // instance when coding private functions.\r
601                                         for ( var privateName in privates )\r
602                                         {\r
603                                                 var priv = privates[ privateName ];\r
604 \r
605                                                 _[ privateName ] =\r
606                                                         ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;\r
607                                         }\r
608 \r
609                                         originalConstructor.apply( this, arguments );\r
610                                 };\r
611                         }\r
612 \r
613                         if ( baseClass )\r
614                         {\r
615                                 $.prototype = this.prototypedCopy( baseClass.prototype );\r
616                                 $.prototype.constructor = $;\r
617                                 $.prototype.base = function()\r
618                                 {\r
619                                         this.base = baseClass.prototype.base;\r
620                                         baseClass.apply( this, arguments );\r
621                                         this.base = arguments.callee;\r
622                                 };\r
623                         }\r
624 \r
625                         if ( proto )\r
626                                 this.extend( $.prototype, proto, true );\r
627 \r
628                         if ( statics )\r
629                                 this.extend( $, statics, true );\r
630 \r
631                         return $;\r
632                 },\r
633 \r
634                 /**\r
635                  * Creates a function reference that can be called later using\r
636                  * CKEDITOR.tools.callFunction. This approach is specially useful to\r
637                  * make DOM attribute function calls to JavaScript defined functions.\r
638                  * @param {Function} fn The function to be executed on call.\r
639                  * @param {Object} [scope] The object to have the context on "fn" execution.\r
640                  * @returns {Number} A unique reference to be used in conjuction with\r
641                  *              CKEDITOR.tools.callFunction.\r
642                  * @example\r
643                  * var ref = <b>CKEDITOR.tools.addFunction</b>(\r
644                  *     function()\r
645                  *     {\r
646                  *         alert( 'Hello!');\r
647                  *     });\r
648                  * CKEDITOR.tools.callFunction( ref );  // Hello!\r
649                  */\r
650                 addFunction : function( fn, scope )\r
651                 {\r
652                         return functions.push( function()\r
653                                 {\r
654                                         fn.apply( scope || this, arguments );\r
655                                 }) - 1;\r
656                 },\r
657 \r
658                 /**\r
659                  * Removes the function reference created with {@see CKEDITOR.tools.addFunction}.\r
660                  * @param {Number} ref The function reference created with\r
661                  *              CKEDITOR.tools.addFunction.\r
662                  */\r
663                 removeFunction : function( ref )\r
664                 {\r
665                         functions[ ref ] = null;\r
666                 },\r
667 \r
668                 /**\r
669                  * Executes a function based on the reference created with\r
670                  * CKEDITOR.tools.addFunction.\r
671                  * @param {Number} ref The function reference created with\r
672                  *              CKEDITOR.tools.addFunction.\r
673                  * @param {[Any,[Any,...]} params Any number of parameters to be passed\r
674                  *              to the executed function.\r
675                  * @returns {Any} The return value of the function.\r
676                  * @example\r
677                  * var ref = CKEDITOR.tools.addFunction(\r
678                  *     function()\r
679                  *     {\r
680                  *         alert( 'Hello!');\r
681                  *     });\r
682                  * <b>CKEDITOR.tools.callFunction( ref )</b>;  // Hello!\r
683                  */\r
684                 callFunction : function( ref )\r
685                 {\r
686                         var fn = functions[ ref ];\r
687                         return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
688                 },\r
689 \r
690                 /**\r
691                  * Append the 'px' length unit to the size if it's missing.\r
692                  * @param length\r
693                  */\r
694                 cssLength : (function()\r
695                 {\r
696                         var decimalRegex = /^\d+(?:\.\d+)?$/;\r
697                         return function( length )\r
698                         {\r
699                                 return length + ( decimalRegex.test( length ) ? 'px' : '' );\r
700                         };\r
701                 })(),\r
702 \r
703                 /**\r
704                  * String specified by {@param str} repeats {@param times} times.\r
705                  * @param str\r
706                  * @param times\r
707                  */\r
708                 repeat : function( str, times )\r
709                 {\r
710                         return new Array( times + 1 ).join( str );\r
711                 },\r
712 \r
713                 /**\r
714                  * Return the first successfully executed function's return value that\r
715                  * doesn't throw any exception.\r
716                  */\r
717                 tryThese : function()\r
718                 {\r
719                         var returnValue;\r
720                         for ( var i = 0, length = arguments.length; i < length; i++ )\r
721                         {\r
722                                 var lambda = arguments[i];\r
723                                 try\r
724                                 {\r
725                                         returnValue = lambda();\r
726                                         break;\r
727                                 }\r
728                                 catch (e) {}\r
729                         }\r
730                         return returnValue;\r
731                 },\r
732 \r
733                 /**\r
734                  * Generate a combined key from a series of params.\r
735                  * @param {String} subKey One or more string used as sub keys.\r
736                  * @example\r
737                  * var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' );\r
738                  * alert( key );                // "key1-key2-key3".\r
739                  */\r
740                 genKey : function()\r
741                 {\r
742                         return Array.prototype.slice.call( arguments ).join( '-' );\r
743                 }\r
744         };\r
745 })();\r
746 \r
747 // PACKAGER_RENAME( CKEDITOR.tools )\r