JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.2.2
[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                 isEmpty : function ( object )\r
224                 {\r
225                         for ( var i in object )\r
226                         {\r
227                                 if ( object.hasOwnProperty( i ) )\r
228                                         return false;\r
229                         }\r
230                         return true;\r
231                 },\r
232                 /**\r
233                  * Transforms a CSS property name to its relative DOM style name.\r
234                  * @param {String} cssName The CSS property name.\r
235                  * @returns {String} The transformed name.\r
236                  * @example\r
237                  * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) );  // "backgroundColor"\r
238                  * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) );             // "cssFloat"\r
239                  */\r
240                 cssStyleToDomStyle : ( function()\r
241                 {\r
242                         var test = document.createElement( 'div' ).style;\r
243 \r
244                         var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'\r
245                                 : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'\r
246                                 : 'float';\r
247 \r
248                         return function( cssName )\r
249                         {\r
250                                 if ( cssName == 'float' )\r
251                                         return cssFloat;\r
252                                 else\r
253                                 {\r
254                                         return cssName.replace( /-./g, function( match )\r
255                                                 {\r
256                                                         return match.substr( 1 ).toUpperCase();\r
257                                                 });\r
258                                 }\r
259                         };\r
260                 } )(),\r
261 \r
262                 /**\r
263                  * Build the HTML snippet of a set of &lt;style>/&lt;link>.\r
264                  * @param css {String|Array} Each of which are url (absolute) of a CSS file or\r
265                  * a trunk of style text.\r
266                  */\r
267                 buildStyleHtml : function ( css )\r
268                 {\r
269                         css = [].concat( css );\r
270                         var item, retval = [];\r
271                         for ( var i = 0; i < css.length; i++ )\r
272                         {\r
273                                 item = css[ i ];\r
274                                 // Is CSS style text ?\r
275                                 if ( /@import|[{}]/.test(item) )\r
276                                         retval.push('<style>' + item + '</style>');\r
277                                 else\r
278                                         retval.push('<link type="text/css" rel=stylesheet href="' + item + '">');\r
279                         }\r
280                         return retval.join( '' );\r
281                 },\r
282 \r
283                 /**\r
284                  * Replace special HTML characters in a string with their relative HTML\r
285                  * entity values.\r
286                  * @param {String} text The string to be encoded.\r
287                  * @returns {String} The encode string.\r
288                  * @example\r
289                  * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) );  // "A &amp;gt; B &amp;amp; C &amp;lt; D"\r
290                  */\r
291                 htmlEncode : function( text )\r
292                 {\r
293                         var standard = function( text )\r
294                         {\r
295                                 var span = new CKEDITOR.dom.element( 'span' );\r
296                                 span.setText( text );\r
297                                 return span.getHtml();\r
298                         };\r
299 \r
300                         var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?\r
301                                 function( text )\r
302                                 {\r
303                                         // #3874 IE and Safari encode line-break into <br>\r
304                                         return standard( text ).replace( /<br>/gi, '\n' );\r
305                                 } :\r
306                                 standard;\r
307 \r
308                         var fix2 = ( standard( '>' ) == '>' ) ?\r
309                                 function( text )\r
310                                 {\r
311                                         // WebKit does't encode the ">" character, which makes sense, but\r
312                                         // it's different than other browsers.\r
313                                         return fix1( text ).replace( />/g, '&gt;' );\r
314                                 } :\r
315                                 fix1;\r
316 \r
317                         var fix3 = ( standard( '  ' ) == '&nbsp; ' ) ?\r
318                                 function( text )\r
319                                 {\r
320                                         // #3785 IE8 changes spaces (>= 2) to &nbsp;\r
321                                         return fix2( text ).replace( /&nbsp;/g, ' ' );\r
322                                 } :\r
323                                 fix2;\r
324 \r
325                         this.htmlEncode = fix3;\r
326 \r
327                         return this.htmlEncode( text );\r
328                 },\r
329 \r
330                 /**\r
331                  * Replace special HTML characters in HTMLElement's attribute with their relative HTML entity values.\r
332                  * @param {String} The attribute's value to be encoded.\r
333                  * @returns {String} The encode value.\r
334                  * @example\r
335                  * element.setAttribute( 'title', '<a " b >' );\r
336                  * alert( CKEDITOR.tools.htmlEncodeAttr( element.getAttribute( 'title' ) );  // "&gt;a &quot; b &lt;"\r
337                  */\r
338                 htmlEncodeAttr : function( text )\r
339                 {\r
340                         return text.replace( /"/g, '&quot;' ).replace( /</g, '&lt;' ).replace( />/, '&gt;' );\r
341                 },\r
342 \r
343                 /**\r
344                  * Replace characters can't be represented through CSS Selectors string\r
345                  * by CSS Escape Notation where the character escape sequence consists\r
346                  * of a backslash character (\) followed by the orginal characters.\r
347                  * Ref: http://www.w3.org/TR/css3-selectors/#grammar\r
348                  * @param cssSelectText\r
349                  * @return the escaped selector text.\r
350                  */\r
351                 escapeCssSelector : function( cssSelectText )\r
352                 {\r
353                         return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' );\r
354                 },\r
355 \r
356                 /**\r
357                  * Gets a unique number for this CKEDITOR execution session. It returns\r
358                  * progressive numbers starting at 1.\r
359                  * @function\r
360                  * @returns {Number} A unique number.\r
361                  * @example\r
362                  * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "1" (e.g.)\r
363                  * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "2"\r
364                  */\r
365                 getNextNumber : (function()\r
366                 {\r
367                         var last = 0;\r
368                         return function()\r
369                         {\r
370                                 return ++last;\r
371                         };\r
372                 })(),\r
373 \r
374                 /**\r
375                  * Creates a function override.\r
376                  * @param {Function} originalFunction The function to be overridden.\r
377                  * @param {Function} functionBuilder A function that returns the new\r
378                  *              function. The original function reference will be passed to this\r
379                  *              function.\r
380                  * @returns {Function} The new function.\r
381                  * @example\r
382                  * var example =\r
383                  * {\r
384                  *     myFunction : function( name )\r
385                  *     {\r
386                  *         alert( 'Name: ' + name );\r
387                  *     }\r
388                  * };\r
389                  *\r
390                  * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )\r
391                  *     {\r
392                  *         return function( name )\r
393                  *             {\r
394                  *                 alert( 'Override Name: ' + name );\r
395                  *                 myFunctionOriginal.call( this, name );\r
396                  *             };\r
397                  *     });\r
398                  */\r
399                 override : function( originalFunction, functionBuilder )\r
400                 {\r
401                         return functionBuilder( originalFunction );\r
402                 },\r
403 \r
404                 /**\r
405                  * Executes a function after specified delay.\r
406                  * @param {Function} func The function to be executed.\r
407                  * @param {Number} [milliseconds] The amount of time (millisecods) to wait\r
408                  *              to fire the function execution. Defaults to zero.\r
409                  * @param {Object} [scope] The object to hold the function execution scope\r
410                  *              (the "this" object). By default the "window" object.\r
411                  * @param {Object|Array} [args] A single object, or an array of objects, to\r
412                  *              pass as arguments to the function.\r
413                  * @param {Object} [ownerWindow] The window that will be used to set the\r
414                  *              timeout. By default the current "window".\r
415                  * @returns {Object} A value that can be used to cancel the function execution.\r
416                  * @example\r
417                  * CKEDITOR.tools.<b>setTimeout(\r
418                  *     function()\r
419                  *     {\r
420                  *         alert( 'Executed after 2 seconds' );\r
421                  *     },\r
422                  *     2000 )</b>;\r
423                  */\r
424                 setTimeout : function( func, milliseconds, scope, args, ownerWindow )\r
425                 {\r
426                         if ( !ownerWindow )\r
427                                 ownerWindow = window;\r
428 \r
429                         if ( !scope )\r
430                                 scope = ownerWindow;\r
431 \r
432                         return ownerWindow.setTimeout(\r
433                                 function()\r
434                                 {\r
435                                         if ( args )\r
436                                                 func.apply( scope, [].concat( args ) ) ;\r
437                                         else\r
438                                                 func.apply( scope ) ;\r
439                                 },\r
440                                 milliseconds || 0 );\r
441                 },\r
442 \r
443                 /**\r
444                  * Remove spaces from the start and the end of a string. The following\r
445                  * characters are removed: space, tab, line break, line feed.\r
446                  * @function\r
447                  * @param {String} str The text from which remove the spaces.\r
448                  * @returns {String} The modified string without the boundary spaces.\r
449                  * @example\r
450                  * alert( CKEDITOR.tools.trim( '  example ' );  // "example"\r
451                  */\r
452                 trim : (function()\r
453                 {\r
454                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
455                         var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;\r
456                         return function( str )\r
457                         {\r
458                                 return str.replace( trimRegex, '' ) ;\r
459                         };\r
460                 })(),\r
461 \r
462                 /**\r
463                  * Remove spaces from the start (left) 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 excluding the removed spaces.\r
468                  * @example\r
469                  * alert( CKEDITOR.tools.ltrim( '  example ' );  // "example "\r
470                  */\r
471                 ltrim : (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]+/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 end (right) 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                 rtrim : (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                  * Returns the index of an element in an array.\r
502                  * @param {Array} array The array to be searched.\r
503                  * @param {Object} entry The element to be found.\r
504                  * @returns {Number} The (zero based) index of the first entry that matches\r
505                  *              the entry, or -1 if not found.\r
506                  * @example\r
507                  * var letters = [ 'a', 'b', 0, 'c', false ];\r
508                  * alert( CKEDITOR.tools.indexOf( letters, '0' ) );  "-1" because 0 !== '0'\r
509                  * alert( CKEDITOR.tools.indexOf( letters, false ) );  "4" because 0 !== false\r
510                  */\r
511                 indexOf :\r
512                         // #2514: We should try to use Array.indexOf if it does exist.\r
513                         ( Array.prototype.indexOf ) ?\r
514                                 function( array, entry )\r
515                                         {\r
516                                                 return array.indexOf( entry );\r
517                                         }\r
518                         :\r
519                                 function( array, entry )\r
520                                 {\r
521                                         for ( var i = 0, len = array.length ; i < len ; i++ )\r
522                                         {\r
523                                                 if ( array[ i ] === entry )\r
524                                                         return i;\r
525                                         }\r
526                                         return -1;\r
527                                 },\r
528 \r
529                 /**\r
530                  * Creates a function that will always execute in the context of a\r
531                  * specified object.\r
532                  * @param {Function} func The function to be executed.\r
533                  * @param {Object} obj The object to which bind the execution context.\r
534                  * @returns {Function} The function that can be used to execute the\r
535                  *              "func" function in the context of "obj".\r
536                  * @example\r
537                  * var obj = { text : 'My Object' };\r
538                  *\r
539                  * function alertText()\r
540                  * {\r
541                  *     alert( this.text );\r
542                  * }\r
543                  *\r
544                  * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;\r
545                  * newFunc();  // Alerts "My Object".\r
546                  */\r
547                 bind : function( func, obj )\r
548                 {\r
549                         return function() { return func.apply( obj, arguments ); };\r
550                 },\r
551 \r
552                 /**\r
553                  * Class creation based on prototype inheritance, with supports of the\r
554                  * following features:\r
555                  * <ul>\r
556                  * <li> Static fields </li>\r
557                  * <li> Private fields </li>\r
558                  * <li> Public (prototype) fields </li>\r
559                  * <li> Chainable base class constructor </li>\r
560                  * </ul>\r
561                  * @param {Object} definition The class definition object.\r
562                  * @returns {Function} A class-like JavaScript function.\r
563                  */\r
564                 createClass : function( definition )\r
565                 {\r
566                         var $ = definition.$,\r
567                                 baseClass = definition.base,\r
568                                 privates = definition.privates || definition._,\r
569                                 proto = definition.proto,\r
570                                 statics = definition.statics;\r
571 \r
572                         if ( privates )\r
573                         {\r
574                                 var originalConstructor = $;\r
575                                 $ = function()\r
576                                 {\r
577                                         // Create (and get) the private namespace.\r
578                                         var _ = this._ || ( this._ = {} );\r
579 \r
580                                         // Make some magic so "this" will refer to the main\r
581                                         // instance when coding private functions.\r
582                                         for ( var privateName in privates )\r
583                                         {\r
584                                                 var priv = privates[ privateName ];\r
585 \r
586                                                 _[ privateName ] =\r
587                                                         ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;\r
588                                         }\r
589 \r
590                                         originalConstructor.apply( this, arguments );\r
591                                 };\r
592                         }\r
593 \r
594                         if ( baseClass )\r
595                         {\r
596                                 $.prototype = this.prototypedCopy( baseClass.prototype );\r
597                                 $.prototype.constructor = $;\r
598                                 $.prototype.base = function()\r
599                                 {\r
600                                         this.base = baseClass.prototype.base;\r
601                                         baseClass.apply( this, arguments );\r
602                                         this.base = arguments.callee;\r
603                                 };\r
604                         }\r
605 \r
606                         if ( proto )\r
607                                 this.extend( $.prototype, proto, true );\r
608 \r
609                         if ( statics )\r
610                                 this.extend( $, statics, true );\r
611 \r
612                         return $;\r
613                 },\r
614 \r
615                 /**\r
616                  * Creates a function reference that can be called later using\r
617                  * CKEDITOR.tools.callFunction. This approach is specially useful to\r
618                  * make DOM attribute function calls to JavaScript defined functions.\r
619                  * @param {Function} fn The function to be executed on call.\r
620                  * @param {Object} [scope] The object to have the context on "fn" execution.\r
621                  * @returns {Number} A unique reference to be used in conjuction with\r
622                  *              CKEDITOR.tools.callFunction.\r
623                  * @example\r
624                  * var ref = <b>CKEDITOR.tools.addFunction</b>(\r
625                  *     function()\r
626                  *     {\r
627                  *         alert( 'Hello!');\r
628                  *     });\r
629                  * CKEDITOR.tools.callFunction( ref );  // Hello!\r
630                  */\r
631                 addFunction : function( fn, scope )\r
632                 {\r
633                         return functions.push( function()\r
634                                 {\r
635                                         fn.apply( scope || this, arguments );\r
636                                 }) - 1;\r
637                 },\r
638 \r
639                 /**\r
640                  * Removes the function reference created with {@see CKEDITOR.tools.addFunction}.\r
641                  * @param {Number} ref The function reference created with\r
642                  *              CKEDITOR.tools.addFunction.\r
643                  */\r
644                 removeFunction : function( ref )\r
645                 {\r
646                         functions[ ref ] = null;\r
647                 },\r
648 \r
649                 /**\r
650                  * Executes a function based on the reference created with\r
651                  * CKEDITOR.tools.addFunction.\r
652                  * @param {Number} ref The function reference created with\r
653                  *              CKEDITOR.tools.addFunction.\r
654                  * @param {[Any,[Any,...]} params Any number of parameters to be passed\r
655                  *              to the executed function.\r
656                  * @returns {Any} The return value of the function.\r
657                  * @example\r
658                  * var ref = CKEDITOR.tools.addFunction(\r
659                  *     function()\r
660                  *     {\r
661                  *         alert( 'Hello!');\r
662                  *     });\r
663                  * <b>CKEDITOR.tools.callFunction( ref )</b>;  // Hello!\r
664                  */\r
665                 callFunction : function( ref )\r
666                 {\r
667                         var fn = functions[ ref ];\r
668                         return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
669                 },\r
670 \r
671                 cssLength : (function()\r
672                 {\r
673                         var decimalRegex = /^\d+(?:\.\d+)?$/;\r
674                         return function( length )\r
675                         {\r
676                                 return length + ( decimalRegex.test( length ) ? 'px' : '' );\r
677                         };\r
678                 })(),\r
679 \r
680                 repeat : function( str, times )\r
681                 {\r
682                         return new Array( times + 1 ).join( str );\r
683                 },\r
684 \r
685                 tryThese : function()\r
686                 {\r
687                         var returnValue;\r
688                         for ( var i = 0, length = arguments.length; i < length; i++ )\r
689                         {\r
690                                 var lambda = arguments[i];\r
691                                 try\r
692                                 {\r
693                                         returnValue = lambda();\r
694                                         break;\r
695                                 }\r
696                                 catch (e) {}\r
697                         }\r
698                         return returnValue;\r
699                 },\r
700 \r
701                 /**\r
702                  * Generate a combined key from a series of params.\r
703                  * @param {String} subKey One or more string used as sub keys.\r
704                  * @example\r
705                  * var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' );\r
706                  * alert( key );                // "key1-key2-key3".\r
707                  */\r
708                 genKey : function()\r
709                 {\r
710                         return Array.prototype.slice.call( arguments ).join( '-' );\r
711                 }\r
712         };\r
713 })();\r
714 \r
715 // PACKAGER_RENAME( CKEDITOR.tools )\r