Update ckeditor to version 3.2.1
[gopost.git] / ckeditor / _source / core / tools.js
blobfc82904bb4d43248c6c3c668888535bce31681cf
1 /*
2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 /**
7 * @fileOverview Defines the {@link CKEDITOR.tools} object, which contains
8 * utility functions.
9 */
11 (function()
13 var functions = [];
15 /**
16 * Utility functions.
17 * @namespace
18 * @example
20 CKEDITOR.tools =
22 /**
23 * Compare the elements of two arrays.
24 * @param {Array} arrayA An array to be compared.
25 * @param {Array} arrayB The other array to be compared.
26 * @returns {Boolean} "true" is the arrays have the same lenght and
27 * their elements match.
28 * @example
29 * var a = [ 1, 'a', 3 ];
30 * var b = [ 1, 3, 'a' ];
31 * var c = [ 1, 'a', 3 ];
32 * var d = [ 1, 'a', 3, 4 ];
34 * alert( CKEDITOR.tools.arrayCompare( a, b ) ); // false
35 * alert( CKEDITOR.tools.arrayCompare( a, c ) ); // true
36 * alert( CKEDITOR.tools.arrayCompare( a, d ) ); // false
38 arrayCompare : function( arrayA, arrayB )
40 if ( !arrayA && !arrayB )
41 return true;
43 if ( !arrayA || !arrayB || arrayA.length != arrayB.length )
44 return false;
46 for ( var i = 0 ; i < arrayA.length ; i++ )
48 if ( arrayA[ i ] != arrayB[ i ] )
49 return false;
52 return true;
55 /**
56 * Creates a deep copy of an object.
57 * Attention: there is no support for recursive references.
58 * @param {Object} object The object to be cloned.
59 * @returns {Object} The object clone.
60 * @example
61 * var obj =
62 * {
63 * name : 'John',
64 * cars :
65 * {
66 * Mercedes : { color : 'blue' },
67 * Porsche : { color : 'red' }
68 * }
69 * };
70 * var clone = CKEDITOR.tools.clone( obj );
71 * clone.name = 'Paul';
72 * clone.cars.Porsche.color = 'silver';
73 * alert( obj.name ); // John
74 * alert( clone.name ); // Paul
75 * alert( obj.cars.Porsche.color ); // red
76 * alert( clone.cars.Porsche.color ); // silver
78 clone : function( obj )
80 var clone;
82 // Array.
83 if ( obj && ( obj instanceof Array ) )
85 clone = [];
87 for ( var i = 0 ; i < obj.length ; i++ )
88 clone[ i ] = this.clone( obj[ i ] );
90 return clone;
93 // "Static" types.
94 if ( obj === null
95 || ( typeof( obj ) != 'object' )
96 || ( obj instanceof String )
97 || ( obj instanceof Number )
98 || ( obj instanceof Boolean )
99 || ( obj instanceof Date )
100 || ( obj instanceof RegExp) )
102 return obj;
105 // Objects.
106 clone = new obj.constructor();
108 for ( var propertyName in obj )
110 var property = obj[ propertyName ];
111 clone[ propertyName ] = this.clone( property );
114 return clone;
118 * Turn the first letter of string to upper-case.
119 * @param {String} str
121 capitalize: function( str )
123 return str.charAt( 0 ).toUpperCase() + str.substring( 1 ).toLowerCase();
127 * Copy the properties from one object to another. By default, properties
128 * already present in the target object <strong>are not</strong> overwritten.
129 * @param {Object} target The object to be extended.
130 * @param {Object} source[,souce(n)] The objects from which copy
131 * properties. Any number of objects can be passed to this function.
132 * @param {Boolean} [overwrite] If 'true' is specified it indicates that
133 * properties already present in the target object could be
134 * overwritten by subsequent objects.
135 * @param {Object} [properties] Only properties within the specified names
136 * list will be received from the source object.
137 * @returns {Object} the extended object (target).
138 * @example
139 * // Create the sample object.
140 * var myObject =
142 * prop1 : true
143 * };
145 * // Extend the above object with two properties.
146 * CKEDITOR.tools.extend( myObject,
148 * prop2 : true,
149 * prop3 : true
150 * } );
152 * // Alert "prop1", "prop2" and "prop3".
153 * for ( var p in myObject )
154 * alert( p );
156 extend : function( target )
158 var argsLength = arguments.length,
159 overwrite, propertiesList;
161 if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')
162 argsLength--;
163 else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )
165 propertiesList = arguments [ argsLength -1 ];
166 argsLength-=2;
168 for ( var i = 1 ; i < argsLength ; i++ )
170 var source = arguments[ i ];
171 for ( var propertyName in source )
173 // Only copy existed fields if in overwrite mode.
174 if ( overwrite === true || target[ propertyName ] == undefined )
176 // Only copy specified fields if list is provided.
177 if ( !propertiesList || ( propertyName in propertiesList ) )
178 target[ propertyName ] = source[ propertyName ];
184 return target;
188 * Creates an object which is an instance of a class which prototype is a
189 * predefined object. All properties defined in the source object are
190 * automatically inherited by the resulting object, including future
191 * changes to it.
192 * @param {Object} source The source object to be used as the prototype for
193 * the final object.
194 * @returns {Object} The resulting copy.
196 prototypedCopy : function( source )
198 var copy = function()
200 copy.prototype = source;
201 return new copy();
205 * Checks if an object is an Array.
206 * @param {Object} object The object to be checked.
207 * @type Boolean
208 * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.
209 * @example
210 * alert( CKEDITOR.tools.isArray( [] ) ); // "true"
211 * alert( CKEDITOR.tools.isArray( 'Test' ) ); // "false"
213 isArray : function( object )
215 return ( !!object && object instanceof Array );
218 isEmpty : function ( object )
220 for ( var i in object )
222 if ( object.hasOwnProperty( i ) )
223 return false;
225 return true;
228 * Transforms a CSS property name to its relative DOM style name.
229 * @param {String} cssName The CSS property name.
230 * @returns {String} The transformed name.
231 * @example
232 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) ); // "backgroundColor"
233 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) ); // "cssFloat"
235 cssStyleToDomStyle : ( function()
237 var test = document.createElement( 'div' ).style;
239 var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'
240 : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'
241 : 'float';
243 return function( cssName )
245 if ( cssName == 'float' )
246 return cssFloat;
247 else
249 return cssName.replace( /-./g, function( match )
251 return match.substr( 1 ).toUpperCase();
255 } )(),
258 * Build the HTML snippet of a set of <style>/<link>.
259 * @param css {String|Array} Each of which are url (absolute) of a CSS file or
260 * a trunk of style text.
262 buildStyleHtml : function ( css )
264 css = [].concat( css );
265 var item, retval = [];
266 for ( var i = 0; i < css.length; i++ )
268 item = css[ i ];
269 // Is CSS style text ?
270 if ( /@import|[{}]/.test(item) )
271 retval.push('<style>' + item + '</style>');
272 else
273 retval.push('<link type="text/css" rel=stylesheet href="' + item + '">');
275 return retval.join( '' );
279 * Replace special HTML characters in a string with their relative HTML
280 * entity values.
281 * @param {String} text The string to be encoded.
282 * @returns {String} The encode string.
283 * @example
284 * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) ); // "A &amp;gt; B &amp;amp; C &amp;lt; D"
286 htmlEncode : function( text )
288 var standard = function( text )
290 var span = new CKEDITOR.dom.element( 'span' );
291 span.setText( text );
292 return span.getHtml();
295 var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?
296 function( text )
298 // #3874 IE and Safari encode line-break into <br>
299 return standard( text ).replace( /<br>/gi, '\n' );
301 standard;
303 var fix2 = ( standard( '>' ) == '>' ) ?
304 function( text )
306 // WebKit does't encode the ">" character, which makes sense, but
307 // it's different than other browsers.
308 return fix1( text ).replace( />/g, '&gt;' );
310 fix1;
312 var fix3 = ( standard( ' ' ) == '&nbsp; ' ) ?
313 function( text )
315 // #3785 IE8 changes spaces (>= 2) to &nbsp;
316 return fix2( text ).replace( /&nbsp;/g, ' ' );
318 fix2;
320 this.htmlEncode = fix3;
322 return this.htmlEncode( text );
326 * Replace special HTML characters in HTMLElement's attribute with their relative HTML entity values.
327 * @param {String} The attribute's value to be encoded.
328 * @returns {String} The encode value.
329 * @example
330 * element.setAttribute( 'title', '<a " b >' );
331 * alert( CKEDITOR.tools.htmlEncodeAttr( element.getAttribute( 'title' ) ); // "&gt;a &quot; b &lt;"
333 htmlEncodeAttr : function( text )
335 return text.replace( /"/g, '&quot;' ).replace( /</g, '&lt;' ).replace( />/, '&gt;' );
339 * Replace characters can't be represented through CSS Selectors string
340 * by CSS Escape Notation where the character escape sequence consists
341 * of a backslash character (\) followed by the orginal characters.
342 * Ref: http://www.w3.org/TR/css3-selectors/#grammar
343 * @param cssSelectText
344 * @return the escaped selector text.
346 escapeCssSelector : function( cssSelectText )
348 return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' );
352 * Gets a unique number for this CKEDITOR execution session. It returns
353 * progressive numbers starting at 1.
354 * @function
355 * @returns {Number} A unique number.
356 * @example
357 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "1" (e.g.)
358 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "2"
360 getNextNumber : (function()
362 var last = 0;
363 return function()
365 return ++last;
367 })(),
370 * Creates a function override.
371 * @param {Function} originalFunction The function to be overridden.
372 * @param {Function} functionBuilder A function that returns the new
373 * function. The original function reference will be passed to this
374 * function.
375 * @returns {Function} The new function.
376 * @example
377 * var example =
379 * myFunction : function( name )
381 * alert( 'Name: ' + name );
383 * };
385 * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )
387 * return function( name )
389 * alert( 'Override Name: ' + name );
390 * myFunctionOriginal.call( this, name );
391 * };
392 * });
394 override : function( originalFunction, functionBuilder )
396 return functionBuilder( originalFunction );
400 * Executes a function after specified delay.
401 * @param {Function} func The function to be executed.
402 * @param {Number} [milliseconds] The amount of time (millisecods) to wait
403 * to fire the function execution. Defaults to zero.
404 * @param {Object} [scope] The object to hold the function execution scope
405 * (the "this" object). By default the "window" object.
406 * @param {Object|Array} [args] A single object, or an array of objects, to
407 * pass as arguments to the function.
408 * @param {Object} [ownerWindow] The window that will be used to set the
409 * timeout. By default the current "window".
410 * @returns {Object} A value that can be used to cancel the function execution.
411 * @example
412 * CKEDITOR.tools.<b>setTimeout(
413 * function()
415 * alert( 'Executed after 2 seconds' );
416 * },
417 * 2000 )</b>;
419 setTimeout : function( func, milliseconds, scope, args, ownerWindow )
421 if ( !ownerWindow )
422 ownerWindow = window;
424 if ( !scope )
425 scope = ownerWindow;
427 return ownerWindow.setTimeout(
428 function()
430 if ( args )
431 func.apply( scope, [].concat( args ) ) ;
432 else
433 func.apply( scope ) ;
435 milliseconds || 0 );
439 * Remove spaces from the start and the end of a string. The following
440 * characters are removed: space, tab, line break, line feed.
441 * @function
442 * @param {String} str The text from which remove the spaces.
443 * @returns {String} The modified string without the boundary spaces.
444 * @example
445 * alert( CKEDITOR.tools.trim( ' example ' ); // "example"
447 trim : (function()
449 // We are not using \s because we don't want "non-breaking spaces" to be caught.
450 var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;
451 return function( str )
453 return str.replace( trimRegex, '' ) ;
455 })(),
458 * Remove spaces from the start (left) of a string. The following
459 * characters are removed: space, tab, line break, line feed.
460 * @function
461 * @param {String} str The text from which remove the spaces.
462 * @returns {String} The modified string excluding the removed spaces.
463 * @example
464 * alert( CKEDITOR.tools.ltrim( ' example ' ); // "example "
466 ltrim : (function()
468 // We are not using \s because we don't want "non-breaking spaces" to be caught.
469 var trimRegex = /^[ \t\n\r]+/g;
470 return function( str )
472 return str.replace( trimRegex, '' ) ;
474 })(),
477 * Remove spaces from the end (right) of a string. The following
478 * characters are removed: space, tab, line break, line feed.
479 * @function
480 * @param {String} str The text from which remove the spaces.
481 * @returns {String} The modified string excluding the removed spaces.
482 * @example
483 * alert( CKEDITOR.tools.ltrim( ' example ' ); // " example"
485 rtrim : (function()
487 // We are not using \s because we don't want "non-breaking spaces" to be caught.
488 var trimRegex = /[ \t\n\r]+$/g;
489 return function( str )
491 return str.replace( trimRegex, '' ) ;
493 })(),
496 * Returns the index of an element in an array.
497 * @param {Array} array The array to be searched.
498 * @param {Object} entry The element to be found.
499 * @returns {Number} The (zero based) index of the first entry that matches
500 * the entry, or -1 if not found.
501 * @example
502 * var letters = [ 'a', 'b', 0, 'c', false ];
503 * alert( CKEDITOR.tools.indexOf( letters, '0' ) ); "-1" because 0 !== '0'
504 * alert( CKEDITOR.tools.indexOf( letters, false ) ); "4" because 0 !== false
506 indexOf :
507 // #2514: We should try to use Array.indexOf if it does exist.
508 ( Array.prototype.indexOf ) ?
509 function( array, entry )
511 return array.indexOf( entry );
514 function( array, entry )
516 for ( var i = 0, len = array.length ; i < len ; i++ )
518 if ( array[ i ] === entry )
519 return i;
521 return -1;
525 * Creates a function that will always execute in the context of a
526 * specified object.
527 * @param {Function} func The function to be executed.
528 * @param {Object} obj The object to which bind the execution context.
529 * @returns {Function} The function that can be used to execute the
530 * "func" function in the context of "obj".
531 * @example
532 * var obj = { text : 'My Object' };
534 * function alertText()
536 * alert( this.text );
539 * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;
540 * newFunc(); // Alerts "My Object".
542 bind : function( func, obj )
544 return function() { return func.apply( obj, arguments ); };
548 * Class creation based on prototype inheritance, with supports of the
549 * following features:
550 * <ul>
551 * <li> Static fields </li>
552 * <li> Private fields </li>
553 * <li> Public (prototype) fields </li>
554 * <li> Chainable base class constructor </li>
555 * </ul>
556 * @param {Object} definition The class definition object.
557 * @returns {Function} A class-like JavaScript function.
559 createClass : function( definition )
561 var $ = definition.$,
562 baseClass = definition.base,
563 privates = definition.privates || definition._,
564 proto = definition.proto,
565 statics = definition.statics;
567 if ( privates )
569 var originalConstructor = $;
570 $ = function()
572 // Create (and get) the private namespace.
573 var _ = this._ || ( this._ = {} );
575 // Make some magic so "this" will refer to the main
576 // instance when coding private functions.
577 for ( var privateName in privates )
579 var priv = privates[ privateName ];
581 _[ privateName ] =
582 ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;
585 originalConstructor.apply( this, arguments );
589 if ( baseClass )
591 $.prototype = this.prototypedCopy( baseClass.prototype );
592 $.prototype.constructor = $;
593 $.prototype.base = function()
595 this.base = baseClass.prototype.base;
596 baseClass.apply( this, arguments );
597 this.base = arguments.callee;
601 if ( proto )
602 this.extend( $.prototype, proto, true );
604 if ( statics )
605 this.extend( $, statics, true );
607 return $;
611 * Creates a function reference that can be called later using
612 * CKEDITOR.tools.callFunction. This approach is specially useful to
613 * make DOM attribute function calls to JavaScript defined functions.
614 * @param {Function} fn The function to be executed on call.
615 * @param {Object} [scope] The object to have the context on "fn" execution.
616 * @returns {Number} A unique reference to be used in conjuction with
617 * CKEDITOR.tools.callFunction.
618 * @example
619 * var ref = <b>CKEDITOR.tools.addFunction</b>(
620 * function()
622 * alert( 'Hello!');
623 * });
624 * CKEDITOR.tools.callFunction( ref ); // Hello!
626 addFunction : function( fn, scope )
628 return functions.push( function()
630 fn.apply( scope || this, arguments );
631 }) - 1;
635 * Removes the function reference created with {@see CKEDITOR.tools.addFunction}.
636 * @param {Number} ref The function reference created with
637 * CKEDITOR.tools.addFunction.
639 removeFunction : function( ref )
641 functions[ ref ] = null;
645 * Executes a function based on the reference created with
646 * CKEDITOR.tools.addFunction.
647 * @param {Number} ref The function reference created with
648 * CKEDITOR.tools.addFunction.
649 * @param {[Any,[Any,...]} params Any number of parameters to be passed
650 * to the executed function.
651 * @returns {Any} The return value of the function.
652 * @example
653 * var ref = CKEDITOR.tools.addFunction(
654 * function()
656 * alert( 'Hello!');
657 * });
658 * <b>CKEDITOR.tools.callFunction( ref )</b>; // Hello!
660 callFunction : function( ref )
662 var fn = functions[ ref ];
663 return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );
666 cssLength : (function()
668 var decimalRegex = /^\d+(?:\.\d+)?$/;
669 return function( length )
671 return length + ( decimalRegex.test( length ) ? 'px' : '' );
673 })(),
675 repeat : function( str, times )
677 return new Array( times + 1 ).join( str );
680 tryThese : function()
682 var returnValue;
683 for ( var i = 0, length = arguments.length; i < length; i++ )
685 var lambda = arguments[i];
688 returnValue = lambda();
689 break;
691 catch (e) {}
693 return returnValue;
696 })();
698 // PACKAGER_RENAME( CKEDITOR.tools )