2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.tools} object, which contains
\r
16 * Utility functions.
\r
23 * Compare the elements of two arrays.
\r
24 * @param {Array} arrayA An array to be compared.
\r
25 * @param {Array} arrayB The other array to be compared.
\r
26 * @returns {Boolean} "true" is the arrays have the same lenght and
\r
27 * their elements match.
\r
29 * var a = [ 1, 'a', 3 ];
\r
30 * var b = [ 1, 3, 'a' ];
\r
31 * var c = [ 1, 'a', 3 ];
\r
32 * var d = [ 1, 'a', 3, 4 ];
\r
34 * alert( CKEDITOR.tools.arrayCompare( a, b ) ); // false
\r
35 * alert( CKEDITOR.tools.arrayCompare( a, c ) ); // true
\r
36 * alert( CKEDITOR.tools.arrayCompare( a, d ) ); // false
\r
38 arrayCompare : function( arrayA, arrayB )
\r
40 if ( !arrayA && !arrayB )
\r
43 if ( !arrayA || !arrayB || arrayA.length != arrayB.length )
\r
46 for ( var i = 0 ; i < arrayA.length ; i++ )
\r
48 if ( arrayA[ i ] != arrayB[ i ] )
\r
56 * Creates a deep copy of an object.
\r
57 * Attention: there is no support for recursive references.
\r
58 * @param {Object} object The object to be cloned.
\r
59 * @returns {Object} The object clone.
\r
66 * Mercedes : { color : 'blue' },
\r
67 * Porsche : { color : 'red' }
\r
70 * var clone = CKEDITOR.tools.clone( obj );
\r
71 * clone.name = 'Paul';
\r
72 * clone.cars.Porsche.color = 'silver';
\r
73 * alert( obj.name ); // John
\r
74 * alert( clone.name ); // Paul
\r
75 * alert( obj.cars.Porsche.color ); // red
\r
76 * alert( clone.cars.Porsche.color ); // silver
\r
78 clone : function( obj )
\r
83 if ( obj && ( obj instanceof Array ) )
\r
87 for ( var i = 0 ; i < obj.length ; i++ )
\r
88 clone[ i ] = this.clone( obj[ i ] );
\r
95 || ( typeof( obj ) != 'object' )
\r
96 || ( obj instanceof String )
\r
97 || ( obj instanceof Number )
\r
98 || ( obj instanceof Boolean )
\r
99 || ( obj instanceof Date )
\r
100 || ( obj instanceof RegExp) )
\r
106 clone = new obj.constructor();
\r
108 for ( var propertyName in obj )
\r
110 var property = obj[ propertyName ];
\r
111 clone[ propertyName ] = this.clone( property );
\r
118 * Turn the first letter of string to upper-case.
\r
119 * @param {String} str
\r
121 capitalize: function( str )
\r
123 return str.charAt( 0 ).toUpperCase() + str.substring( 1 ).toLowerCase();
\r
127 * Copy the properties from one object to another. By default, properties
\r
128 * already present in the target object <strong>are not</strong> overwritten.
\r
129 * @param {Object} target The object to be extended.
\r
130 * @param {Object} source[,souce(n)] The objects from which copy
\r
131 * properties. Any number of objects can be passed to this function.
\r
132 * @param {Boolean} [overwrite] If 'true' is specified it indicates that
\r
133 * properties already present in the target object could be
\r
134 * overwritten by subsequent objects.
\r
135 * @param {Object} [properties] Only properties within the specified names
\r
136 * list will be received from the source object.
\r
137 * @returns {Object} the extended object (target).
\r
139 * // Create the sample object.
\r
145 * // Extend the above object with two properties.
\r
146 * CKEDITOR.tools.extend( myObject,
\r
152 * // Alert "prop1", "prop2" and "prop3".
\r
153 * for ( var p in myObject )
\r
156 extend : function( target )
\r
158 var argsLength = arguments.length,
\r
159 overwrite, propertiesList;
\r
161 if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')
\r
163 else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )
\r
165 propertiesList = arguments [ argsLength -1 ];
\r
168 for ( var i = 1 ; i < argsLength ; i++ )
\r
170 var source = arguments[ i ];
\r
171 for ( var propertyName in source )
\r
173 // Only copy existed fields if in overwrite mode.
\r
174 if ( overwrite === true || target[ propertyName ] == undefined )
\r
176 // Only copy specified fields if list is provided.
\r
177 if ( !propertiesList || ( propertyName in propertiesList ) )
\r
178 target[ propertyName ] = source[ propertyName ];
\r
188 * Creates an object which is an instance of a class which prototype is a
\r
189 * predefined object. All properties defined in the source object are
\r
190 * automatically inherited by the resulting object, including future
\r
192 * @param {Object} source The source object to be used as the prototype for
\r
193 * the final object.
\r
194 * @returns {Object} The resulting copy.
\r
196 prototypedCopy : function( source )
\r
198 var copy = function()
\r
200 copy.prototype = source;
\r
205 * Checks if an object is an Array.
\r
206 * @param {Object} object The object to be checked.
\r
208 * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.
\r
210 * alert( CKEDITOR.tools.isArray( [] ) ); // "true"
\r
211 * alert( CKEDITOR.tools.isArray( 'Test' ) ); // "false"
\r
213 isArray : function( object )
\r
215 return ( !!object && object instanceof Array );
\r
218 isEmpty : function ( object )
\r
220 for ( var i in object )
\r
222 if ( object.hasOwnProperty( i ) )
\r
228 * Transforms a CSS property name to its relative DOM style name.
\r
229 * @param {String} cssName The CSS property name.
\r
230 * @returns {String} The transformed name.
\r
232 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) ); // "backgroundColor"
\r
233 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) ); // "cssFloat"
\r
235 cssStyleToDomStyle : ( function()
\r
237 var test = document.createElement( 'div' ).style;
\r
239 var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'
\r
240 : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'
\r
243 return function( cssName )
\r
245 if ( cssName == 'float' )
\r
249 return cssName.replace( /-./g, function( match )
\r
251 return match.substr( 1 ).toUpperCase();
\r
258 * Build the HTML snippet of a set of <style>/<link>.
\r
259 * @param css {String|Array} Each of which are url (absolute) of a CSS file or
\r
260 * a trunk of style text.
\r
262 buildStyleHtml : function ( css )
\r
264 css = [].concat( css );
\r
265 var item, retval = [];
\r
266 for ( var i = 0; i < css.length; i++ )
\r
269 // Is CSS style text ?
\r
270 if ( /@import|[{}]/.test(item) )
\r
271 retval.push('<style>' + item + '</style>');
\r
273 retval.push('<link type="text/css" rel=stylesheet href="' + item + '">');
\r
275 return retval.join( '' );
\r
279 * Replace special HTML characters in a string with their relative HTML
\r
281 * @param {String} text The string to be encoded.
\r
282 * @returns {String} The encode string.
\r
284 * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) ); // "A &gt; B &amp; C &lt; D"
\r
286 htmlEncode : function( text )
\r
288 var standard = function( text )
\r
290 var span = new CKEDITOR.dom.element( 'span' );
\r
291 span.setText( text );
\r
292 return span.getHtml();
\r
295 var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?
\r
298 // #3874 IE and Safari encode line-break into <br>
\r
299 return standard( text ).replace( /<br>/gi, '\n' );
\r
303 var fix2 = ( standard( '>' ) == '>' ) ?
\r
306 // WebKit does't encode the ">" character, which makes sense, but
\r
307 // it's different than other browsers.
\r
308 return fix1( text ).replace( />/g, '>' );
\r
312 var fix3 = ( standard( ' ' ) == ' ' ) ?
\r
315 // #3785 IE8 changes spaces (>= 2) to
\r
316 return fix2( text ).replace( / /g, ' ' );
\r
320 this.htmlEncode = fix3;
\r
322 return this.htmlEncode( text );
\r
326 * Replace characters can't be represented through CSS Selectors string
\r
327 * by CSS Escape Notation where the character escape sequence consists
\r
328 * of a backslash character (\) followed by the orginal characters.
\r
329 * Ref: http://www.w3.org/TR/css3-selectors/#grammar
\r
330 * @param cssSelectText
\r
331 * @return the escaped selector text.
\r
333 escapeCssSelector : function( cssSelectText )
\r
335 return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' );
\r
339 * Gets a unique number for this CKEDITOR execution session. It returns
\r
340 * progressive numbers starting at 1.
\r
342 * @returns {Number} A unique number.
\r
344 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "1" (e.g.)
\r
345 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "2"
\r
347 getNextNumber : (function()
\r
357 * Creates a function override.
\r
358 * @param {Function} originalFunction The function to be overridden.
\r
359 * @param {Function} functionBuilder A function that returns the new
\r
360 * function. The original function reference will be passed to this
\r
362 * @returns {Function} The new function.
\r
366 * myFunction : function( name )
\r
368 * alert( 'Name: ' + name );
\r
372 * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )
\r
374 * return function( name )
\r
376 * alert( 'Override Name: ' + name );
\r
377 * myFunctionOriginal.call( this, name );
\r
381 override : function( originalFunction, functionBuilder )
\r
383 return functionBuilder( originalFunction );
\r
387 * Executes a function after specified delay.
\r
388 * @param {Function} func The function to be executed.
\r
389 * @param {Number} [milliseconds] The amount of time (millisecods) to wait
\r
390 * to fire the function execution. Defaults to zero.
\r
391 * @param {Object} [scope] The object to hold the function execution scope
\r
392 * (the "this" object). By default the "window" object.
\r
393 * @param {Object|Array} [args] A single object, or an array of objects, to
\r
394 * pass as arguments to the function.
\r
395 * @param {Object} [ownerWindow] The window that will be used to set the
\r
396 * timeout. By default the current "window".
\r
397 * @returns {Object} A value that can be used to cancel the function execution.
\r
399 * CKEDITOR.tools.<b>setTimeout(
\r
402 * alert( 'Executed after 2 seconds' );
\r
406 setTimeout : function( func, milliseconds, scope, args, ownerWindow )
\r
408 if ( !ownerWindow )
\r
409 ownerWindow = window;
\r
412 scope = ownerWindow;
\r
414 return ownerWindow.setTimeout(
\r
418 func.apply( scope, [].concat( args ) ) ;
\r
420 func.apply( scope ) ;
\r
422 milliseconds || 0 );
\r
426 * Remove spaces from the start and the end of a string. The following
\r
427 * characters are removed: space, tab, line break, line feed.
\r
429 * @param {String} str The text from which remove the spaces.
\r
430 * @returns {String} The modified string without the boundary spaces.
\r
432 * alert( CKEDITOR.tools.trim( ' example ' ); // "example"
\r
436 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
437 var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;
\r
438 return function( str )
\r
440 return str.replace( trimRegex, '' ) ;
\r
445 * Remove spaces from the start (left) of a string. The following
\r
446 * characters are removed: space, tab, line break, line feed.
\r
448 * @param {String} str The text from which remove the spaces.
\r
449 * @returns {String} The modified string excluding the removed spaces.
\r
451 * alert( CKEDITOR.tools.ltrim( ' example ' ); // "example "
\r
453 ltrim : (function()
\r
455 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
456 var trimRegex = /^[ \t\n\r]+/g;
\r
457 return function( str )
\r
459 return str.replace( trimRegex, '' ) ;
\r
464 * Remove spaces from the end (right) of a string. The following
\r
465 * characters are removed: space, tab, line break, line feed.
\r
467 * @param {String} str The text from which remove the spaces.
\r
468 * @returns {String} The modified string excluding the removed spaces.
\r
470 * alert( CKEDITOR.tools.ltrim( ' example ' ); // " example"
\r
472 rtrim : (function()
\r
474 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
475 var trimRegex = /[ \t\n\r]+$/g;
\r
476 return function( str )
\r
478 return str.replace( trimRegex, '' ) ;
\r
483 * Returns the index of an element in an array.
\r
484 * @param {Array} array The array to be searched.
\r
485 * @param {Object} entry The element to be found.
\r
486 * @returns {Number} The (zero based) index of the first entry that matches
\r
487 * the entry, or -1 if not found.
\r
489 * var letters = [ 'a', 'b', 0, 'c', false ];
\r
490 * alert( CKEDITOR.tools.indexOf( letters, '0' ) ); "-1" because 0 !== '0'
\r
491 * alert( CKEDITOR.tools.indexOf( letters, false ) ); "4" because 0 !== false
\r
494 // #2514: We should try to use Array.indexOf if it does exist.
\r
495 ( Array.prototype.indexOf ) ?
\r
496 function( array, entry )
\r
498 return array.indexOf( entry );
\r
501 function( array, entry )
\r
503 for ( var i = 0, len = array.length ; i < len ; i++ )
\r
505 if ( array[ i ] === entry )
\r
512 * Creates a function that will always execute in the context of a
\r
513 * specified object.
\r
514 * @param {Function} func The function to be executed.
\r
515 * @param {Object} obj The object to which bind the execution context.
\r
516 * @returns {Function} The function that can be used to execute the
\r
517 * "func" function in the context of "obj".
\r
519 * var obj = { text : 'My Object' };
\r
521 * function alertText()
\r
523 * alert( this.text );
\r
526 * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;
\r
527 * newFunc(); // Alerts "My Object".
\r
529 bind : function( func, obj )
\r
531 return function() { return func.apply( obj, arguments ); };
\r
535 * Class creation based on prototype inheritance, with supports of the
\r
536 * following features:
\r
538 * <li> Static fields </li>
\r
539 * <li> Private fields </li>
\r
540 * <li> Public (prototype) fields </li>
\r
541 * <li> Chainable base class constructor </li>
\r
543 * @param {Object} definiton The class definiton object.
\r
544 * @returns {Function} A class-like JavaScript function.
\r
546 createClass : function( definition )
\r
548 var $ = definition.$,
\r
549 baseClass = definition.base,
\r
550 privates = definition.privates || definition._,
\r
551 proto = definition.proto,
\r
552 statics = definition.statics;
\r
556 var originalConstructor = $;
\r
559 // Create (and get) the private namespace.
\r
560 var _ = this._ || ( this._ = {} );
\r
562 // Make some magic so "this" will refer to the main
\r
563 // instance when coding private functions.
\r
564 for ( var privateName in privates )
\r
566 var priv = privates[ privateName ];
\r
569 ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;
\r
572 originalConstructor.apply( this, arguments );
\r
578 $.prototype = this.prototypedCopy( baseClass.prototype );
\r
579 $.prototype.constructor = $;
\r
580 $.prototype.base = function()
\r
582 this.base = baseClass.prototype.base;
\r
583 baseClass.apply( this, arguments );
\r
584 this.base = arguments.callee;
\r
589 this.extend( $.prototype, proto, true );
\r
592 this.extend( $, statics, true );
\r
598 * Creates a function reference that can be called later using
\r
599 * CKEDITOR.tools.callFunction. This approach is specially useful to
\r
600 * make DOM attribute function calls to JavaScript defined functions.
\r
601 * @param {Function} fn The function to be executed on call.
\r
602 * @param {Object} [scope] The object to have the context on "fn" execution.
\r
603 * @returns {Number} A unique reference to be used in conjuction with
\r
604 * CKEDITOR.tools.callFunction.
\r
606 * var ref = <b>CKEDITOR.tools.addFunction</b>(
\r
609 * alert( 'Hello!');
\r
611 * CKEDITOR.tools.callFunction( ref ); // Hello!
\r
613 addFunction : function( fn, scope )
\r
615 return functions.push( function()
\r
617 fn.apply( scope || this, arguments );
\r
622 * Executes a function based on the reference created with
\r
623 * CKEDITOR.tools.addFunction.
\r
624 * @param {Number} ref The function reference created with
\r
625 * CKEDITOR.tools.addFunction.
\r
626 * @param {[Any,[Any,...]} params Any number of parameters to be passed
\r
627 * to the executed function.
\r
628 * @returns {Any} The return value of the function.
\r
630 * var ref = CKEDITOR.tools.addFunction(
\r
633 * alert( 'Hello!');
\r
635 * <b>CKEDITOR.tools.callFunction( ref )</b>; // Hello!
\r
637 callFunction : function( ref )
\r
639 var fn = functions[ ref ];
\r
640 return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );
\r
643 cssLength : (function()
\r
645 var decimalRegex = /^\d+(?:\.\d+)?$/;
\r
646 return function( length )
\r
648 return length + ( decimalRegex.test( length ) ? 'px' : '' );
\r
652 repeat : function( str, times )
\r
654 return new Array( times + 1 ).join( str );
\r
657 tryThese : function()
\r
660 for ( var i = 0, length = arguments.length; i < length; i++ )
\r
662 var lambda = arguments[i];
\r
665 returnValue = lambda();
\r
670 return returnValue;
\r
675 // PACKAGER_RENAME( CKEDITOR.tools )
\r