JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / core / tools.js
1 /*\r
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license\r
4 */\r
5 \r
6 /**\r
7  * @fileOverview Defines the {@link CKEDITOR.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                  * Gets a unique number for this CKEDITOR execution session. It returns\r
350                  * progressive numbers starting at 1.\r
351                  * @function\r
352                  * @returns {Number} A unique number.\r
353                  * @example\r
354                  * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "1" (e.g.)\r
355                  * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "2"\r
356                  */\r
357                 getNextNumber : (function()\r
358                 {\r
359                         var last = 0;\r
360                         return function()\r
361                         {\r
362                                 return ++last;\r
363                         };\r
364                 })(),\r
365 \r
366                 /**\r
367                  * Gets a unique ID for CKEditor's interface elements. It returns a\r
368                  * string with the "cke_" prefix and a progressive number.\r
369                  * @function\r
370                  * @returns {String} A unique ID.\r
371                  * @example\r
372                  * alert( CKEDITOR.tools.<b>getNextId()</b> );  // "cke_1" (e.g.)\r
373                  * alert( CKEDITOR.tools.<b>getNextId()</b> );  // "cke_2"\r
374                  */\r
375                 getNextId : function()\r
376                 {\r
377                         return 'cke_' + this.getNextNumber();\r
378                 },\r
379 \r
380                 /**\r
381                  * Creates a function override.\r
382                  * @param {Function} originalFunction The function to be overridden.\r
383                  * @param {Function} functionBuilder A function that returns the new\r
384                  *              function. The original function reference will be passed to this\r
385                  *              function.\r
386                  * @returns {Function} The new function.\r
387                  * @example\r
388                  * var example =\r
389                  * {\r
390                  *     myFunction : function( name )\r
391                  *     {\r
392                  *         alert( 'Name: ' + name );\r
393                  *     }\r
394                  * };\r
395                  *\r
396                  * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )\r
397                  *     {\r
398                  *         return function( name )\r
399                  *             {\r
400                  *                 alert( 'Override Name: ' + name );\r
401                  *                 myFunctionOriginal.call( this, name );\r
402                  *             };\r
403                  *     });\r
404                  */\r
405                 override : function( originalFunction, functionBuilder )\r
406                 {\r
407                         return functionBuilder( originalFunction );\r
408                 },\r
409 \r
410                 /**\r
411                  * Executes a function after specified delay.\r
412                  * @param {Function} func The function to be executed.\r
413                  * @param {Number} [milliseconds] The amount of time (millisecods) to wait\r
414                  *              to fire the function execution. Defaults to zero.\r
415                  * @param {Object} [scope] The object to hold the function execution scope\r
416                  *              (the "this" object). By default the "window" object.\r
417                  * @param {Object|Array} [args] A single object, or an array of objects, to\r
418                  *              pass as arguments to the function.\r
419                  * @param {Object} [ownerWindow] The window that will be used to set the\r
420                  *              timeout. By default the current "window".\r
421                  * @returns {Object} A value that can be used to cancel the function execution.\r
422                  * @example\r
423                  * CKEDITOR.tools.<b>setTimeout(\r
424                  *     function()\r
425                  *     {\r
426                  *         alert( 'Executed after 2 seconds' );\r
427                  *     },\r
428                  *     2000 )</b>;\r
429                  */\r
430                 setTimeout : function( func, milliseconds, scope, args, ownerWindow )\r
431                 {\r
432                         if ( !ownerWindow )\r
433                                 ownerWindow = window;\r
434 \r
435                         if ( !scope )\r
436                                 scope = ownerWindow;\r
437 \r
438                         return ownerWindow.setTimeout(\r
439                                 function()\r
440                                 {\r
441                                         if ( args )\r
442                                                 func.apply( scope, [].concat( args ) ) ;\r
443                                         else\r
444                                                 func.apply( scope ) ;\r
445                                 },\r
446                                 milliseconds || 0 );\r
447                 },\r
448 \r
449                 /**\r
450                  * Remove spaces from the start and the end of a string. The following\r
451                  * characters are removed: space, tab, line break, line feed.\r
452                  * @function\r
453                  * @param {String} str The text from which remove the spaces.\r
454                  * @returns {String} The modified string without the boundary spaces.\r
455                  * @example\r
456                  * alert( CKEDITOR.tools.trim( '  example ' );  // "example"\r
457                  */\r
458                 trim : (function()\r
459                 {\r
460                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
461                         var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;\r
462                         return function( str )\r
463                         {\r
464                                 return str.replace( trimRegex, '' ) ;\r
465                         };\r
466                 })(),\r
467 \r
468                 /**\r
469                  * Remove spaces from the start (left) of a string. The following\r
470                  * characters are removed: space, tab, line break, line feed.\r
471                  * @function\r
472                  * @param {String} str The text from which remove the spaces.\r
473                  * @returns {String} The modified string excluding the removed spaces.\r
474                  * @example\r
475                  * alert( CKEDITOR.tools.ltrim( '  example ' );  // "example "\r
476                  */\r
477                 ltrim : (function()\r
478                 {\r
479                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
480                         var trimRegex = /^[ \t\n\r]+/g;\r
481                         return function( str )\r
482                         {\r
483                                 return str.replace( trimRegex, '' ) ;\r
484                         };\r
485                 })(),\r
486 \r
487                 /**\r
488                  * Remove spaces from the end (right) of a string. The following\r
489                  * characters are removed: space, tab, line break, line feed.\r
490                  * @function\r
491                  * @param {String} str The text from which remove the spaces.\r
492                  * @returns {String} The modified string excluding the removed spaces.\r
493                  * @example\r
494                  * alert( CKEDITOR.tools.ltrim( '  example ' );  // "  example"\r
495                  */\r
496                 rtrim : (function()\r
497                 {\r
498                         // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
499                         var trimRegex = /[ \t\n\r]+$/g;\r
500                         return function( str )\r
501                         {\r
502                                 return str.replace( trimRegex, '' ) ;\r
503                         };\r
504                 })(),\r
505 \r
506                 /**\r
507                  * Returns the index of an element in an array.\r
508                  * @param {Array} array The array to be searched.\r
509                  * @param {Object} entry The element to be found.\r
510                  * @returns {Number} The (zero based) index of the first entry that matches\r
511                  *              the entry, or -1 if not found.\r
512                  * @example\r
513                  * var letters = [ 'a', 'b', 0, 'c', false ];\r
514                  * alert( CKEDITOR.tools.indexOf( letters, '0' ) );  "-1" because 0 !== '0'\r
515                  * alert( CKEDITOR.tools.indexOf( letters, false ) );  "4" because 0 !== false\r
516                  */\r
517                 indexOf :\r
518                         // #2514: We should try to use Array.indexOf if it does exist.\r
519                         ( Array.prototype.indexOf ) ?\r
520                                 function( array, entry )\r
521                                         {\r
522                                                 return array.indexOf( entry );\r
523                                         }\r
524                         :\r
525                                 function( array, entry )\r
526                                 {\r
527                                         for ( var i = 0, len = array.length ; i < len ; i++ )\r
528                                         {\r
529                                                 if ( array[ i ] === entry )\r
530                                                         return i;\r
531                                         }\r
532                                         return -1;\r
533                                 },\r
534 \r
535                 /**\r
536                  * Creates a function that will always execute in the context of a\r
537                  * specified object.\r
538                  * @param {Function} func The function to be executed.\r
539                  * @param {Object} obj The object to which bind the execution context.\r
540                  * @returns {Function} The function that can be used to execute the\r
541                  *              "func" function in the context of "obj".\r
542                  * @example\r
543                  * var obj = { text : 'My Object' };\r
544                  *\r
545                  * function alertText()\r
546                  * {\r
547                  *     alert( this.text );\r
548                  * }\r
549                  *\r
550                  * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;\r
551                  * newFunc();  // Alerts "My Object".\r
552                  */\r
553                 bind : function( func, obj )\r
554                 {\r
555                         return function() { return func.apply( obj, arguments ); };\r
556                 },\r
557 \r
558                 /**\r
559                  * Class creation based on prototype inheritance, with supports of the\r
560                  * following features:\r
561                  * <ul>\r
562                  * <li> Static fields </li>\r
563                  * <li> Private fields </li>\r
564                  * <li> Public (prototype) fields </li>\r
565                  * <li> Chainable base class constructor </li>\r
566                  * </ul>\r
567                  * @param {Object} definition The class definition object.\r
568                  * @returns {Function} A class-like JavaScript function.\r
569                  */\r
570                 createClass : function( definition )\r
571                 {\r
572                         var $ = definition.$,\r
573                                 baseClass = definition.base,\r
574                                 privates = definition.privates || definition._,\r
575                                 proto = definition.proto,\r
576                                 statics = definition.statics;\r
577 \r
578                         if ( privates )\r
579                         {\r
580                                 var originalConstructor = $;\r
581                                 $ = function()\r
582                                 {\r
583                                         // Create (and get) the private namespace.\r
584                                         var _ = this._ || ( this._ = {} );\r
585 \r
586                                         // Make some magic so "this" will refer to the main\r
587                                         // instance when coding private functions.\r
588                                         for ( var privateName in privates )\r
589                                         {\r
590                                                 var priv = privates[ privateName ];\r
591 \r
592                                                 _[ privateName ] =\r
593                                                         ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;\r
594                                         }\r
595 \r
596                                         originalConstructor.apply( this, arguments );\r
597                                 };\r
598                         }\r
599 \r
600                         if ( baseClass )\r
601                         {\r
602                                 $.prototype = this.prototypedCopy( baseClass.prototype );\r
603                                 $.prototype.constructor = $;\r
604                                 $.prototype.base = function()\r
605                                 {\r
606                                         this.base = baseClass.prototype.base;\r
607                                         baseClass.apply( this, arguments );\r
608                                         this.base = arguments.callee;\r
609                                 };\r
610                         }\r
611 \r
612                         if ( proto )\r
613                                 this.extend( $.prototype, proto, true );\r
614 \r
615                         if ( statics )\r
616                                 this.extend( $, statics, true );\r
617 \r
618                         return $;\r
619                 },\r
620 \r
621                 /**\r
622                  * Creates a function reference that can be called later using\r
623                  * CKEDITOR.tools.callFunction. This approach is specially useful to\r
624                  * make DOM attribute function calls to JavaScript defined functions.\r
625                  * @param {Function} fn The function to be executed on call.\r
626                  * @param {Object} [scope] The object to have the context on "fn" execution.\r
627                  * @returns {Number} A unique reference to be used in conjuction with\r
628                  *              CKEDITOR.tools.callFunction.\r
629                  * @example\r
630                  * var ref = <b>CKEDITOR.tools.addFunction</b>(\r
631                  *     function()\r
632                  *     {\r
633                  *         alert( 'Hello!');\r
634                  *     });\r
635                  * CKEDITOR.tools.callFunction( ref );  // Hello!\r
636                  */\r
637                 addFunction : function( fn, scope )\r
638                 {\r
639                         return functions.push( function()\r
640                                 {\r
641                                         return fn.apply( scope || this, arguments );\r
642                                 }) - 1;\r
643                 },\r
644 \r
645                 /**\r
646                  * Removes the function reference created with {@see CKEDITOR.tools.addFunction}.\r
647                  * @param {Number} ref The function reference created with\r
648                  *              CKEDITOR.tools.addFunction.\r
649                  */\r
650                 removeFunction : function( ref )\r
651                 {\r
652                         functions[ ref ] = null;\r
653                 },\r
654 \r
655                 /**\r
656                  * Executes a function based on the reference created with\r
657                  * CKEDITOR.tools.addFunction.\r
658                  * @param {Number} ref The function reference created with\r
659                  *              CKEDITOR.tools.addFunction.\r
660                  * @param {[Any,[Any,...]} params Any number of parameters to be passed\r
661                  *              to the executed function.\r
662                  * @returns {Any} The return value of the function.\r
663                  * @example\r
664                  * var ref = CKEDITOR.tools.addFunction(\r
665                  *     function()\r
666                  *     {\r
667                  *         alert( 'Hello!');\r
668                  *     });\r
669                  * <b>CKEDITOR.tools.callFunction( ref )</b>;  // Hello!\r
670                  */\r
671                 callFunction : function( ref )\r
672                 {\r
673                         var fn = functions[ ref ];\r
674                         return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
675                 },\r
676 \r
677                 /**\r
678                  * Append the 'px' length unit to the size if it's missing.\r
679                  * @param length\r
680                  */\r
681                 cssLength : (function()\r
682                 {\r
683                         return function( length )\r
684                         {\r
685                                 return length + ( !length || isNaN( Number( length ) ) ? '' : 'px' );\r
686                         };\r
687                 })(),\r
688 \r
689                 /**\r
690                  * Convert the specified CSS length value to the calculated pixel length inside this page.\r
691                  * <strong>Note:</strong> Percentage based value is left intact.\r
692                  * @param {String} cssLength CSS length value.\r
693                  */\r
694                 convertToPx : ( function ()\r
695                         {\r
696                                 var calculator;\r
697 \r
698                                 return function( cssLength )\r
699                                 {\r
700                                         if ( !calculator )\r
701                                         {\r
702                                                 calculator = CKEDITOR.dom.element.createFromHtml(\r
703                                                                 '<div style="position:absolute;left:-9999px;' +\r
704                                                                 'top:-9999px;margin:0px;padding:0px;border:0px;"' +\r
705                                                                 '></div>', CKEDITOR.document );\r
706                                                 CKEDITOR.document.getBody().append( calculator );\r
707                                         }\r
708 \r
709                                         if ( !(/%$/).test( cssLength ) )\r
710                                         {\r
711                                                 calculator.setStyle( 'width', cssLength );\r
712                                                 return calculator.$.clientWidth;\r
713                                         }\r
714 \r
715                                         return cssLength;\r
716                                 };\r
717                         } )(),\r
718 \r
719                 /**\r
720                  * String specified by {@param str} repeats {@param times} times.\r
721                  * @param str\r
722                  * @param times\r
723                  */\r
724                 repeat : function( str, times )\r
725                 {\r
726                         return new Array( times + 1 ).join( str );\r
727                 },\r
728 \r
729                 /**\r
730                  * Return the first successfully executed function's return value that\r
731                  * doesn't throw any exception.\r
732                  */\r
733                 tryThese : function()\r
734                 {\r
735                         var returnValue;\r
736                         for ( var i = 0, length = arguments.length; i < length; i++ )\r
737                         {\r
738                                 var lambda = arguments[i];\r
739                                 try\r
740                                 {\r
741                                         returnValue = lambda();\r
742                                         break;\r
743                                 }\r
744                                 catch (e) {}\r
745                         }\r
746                         return returnValue;\r
747                 },\r
748 \r
749                 /**\r
750                  * Generate a combined key from a series of params.\r
751                  * @param {String} subKey One or more string used as sub keys.\r
752                  * @example\r
753                  * var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' );\r
754                  * alert( key );                // "key1-key2-key3".\r
755                  */\r
756                 genKey : function()\r
757                 {\r
758                         return Array.prototype.slice.call( arguments ).join( '-' );\r
759                 }\r
760         };\r
761 })();\r
762 \r
763 // PACKAGER_RENAME( CKEDITOR.tools )\r