2 Copyright (c) 2003-2009, 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
105 clone = new obj.constructor();
\r
107 for ( var propertyName in obj )
\r
109 var property = obj[ propertyName ];
\r
110 clone[ propertyName ] = this.clone( property );
\r
117 * Copy the properties from one object to another. By default, properties
\r
118 * already present in the target object <strong>are not</strong> overwritten.
\r
119 * @param {Object} target The object to be extended.
\r
120 * @param {Object} source[,souce(n)] The objects from which copy
\r
121 * properties. Any number of objects can be passed to this function.
\r
122 * @param {Boolean} [overwrite] If 'true' is specified it indicates that
\r
123 * properties already present in the target object could be
\r
124 * overwritten by subsequent objects.
\r
125 * @param {Object} [properties] Only properties within the specified names
\r
126 * list will be received from the source object.
\r
127 * @returns {Object} the extended object (target).
\r
129 * // Create the sample object.
\r
135 * // Extend the above object with two properties.
\r
136 * CKEDITOR.tools.extend( myObject,
\r
142 * // Alert "prop1", "prop2" and "prop3".
\r
143 * for ( var p in myObject )
\r
146 extend : function( target )
\r
148 var argsLength = arguments.length,
\r
149 overwrite, propertiesList;
\r
151 if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')
\r
153 else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )
\r
155 propertiesList = arguments [ argsLength -1 ];
\r
158 for ( var i = 1 ; i < argsLength ; i++ )
\r
160 var source = arguments[ i ];
\r
161 for ( var propertyName in source )
\r
163 // Only copy existed fields if in overwrite mode.
\r
164 if ( overwrite === true || target[ propertyName ] == undefined )
\r
166 // Only copy specified fields if list is provided.
\r
167 if ( !propertiesList || ( propertyName in propertiesList ) )
\r
168 target[ propertyName ] = source[ propertyName ];
\r
178 * Creates an object which is an instance of a class which prototype is a
\r
179 * predefined object. All properties defined in the source object are
\r
180 * automatically inherited by the resulting object, including future
\r
182 * @param {Object} source The source object to be used as the prototype for
\r
183 * the final object.
\r
184 * @returns {Object} The resulting copy.
\r
186 prototypedCopy : function( source )
\r
188 var copy = function()
\r
190 copy.prototype = source;
\r
195 * Checks if an object is an Array.
\r
196 * @param {Object} object The object to be checked.
\r
198 * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.
\r
200 * alert( CKEDITOR.tools.isArray( [] ) ); // "true"
\r
201 * alert( CKEDITOR.tools.isArray( 'Test' ) ); // "false"
\r
203 isArray : function( object )
\r
205 return ( !!object && object instanceof Array );
\r
208 isEmpty : function ( object )
\r
210 for ( var i in object )
\r
212 if ( object.hasOwnProperty( i ) )
\r
218 * Transforms a CSS property name to its relative DOM style name.
\r
219 * @param {String} cssName The CSS property name.
\r
220 * @returns {String} The transformed name.
\r
222 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) ); // "backgroundColor"
\r
223 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) ); // "cssFloat"
\r
225 cssStyleToDomStyle : ( function()
\r
227 var test = document.createElement( 'div' ).style;
\r
229 var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'
\r
230 : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'
\r
233 return function( cssName )
\r
235 if ( cssName == 'float' )
\r
239 return cssName.replace( /-./g, function( match )
\r
241 return match.substr( 1 ).toUpperCase();
\r
248 * Replace special HTML characters in a string with their relative HTML
\r
250 * @param {String} text The string to be encoded.
\r
251 * @returns {String} The encode string.
\r
253 * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) ); // "A &gt; B &amp; C &lt; D"
\r
255 htmlEncode : function( text )
\r
257 var standard = function( text )
\r
259 var span = new CKEDITOR.dom.element( 'span' );
\r
260 span.setText( text );
\r
261 return span.getHtml();
\r
264 var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?
\r
267 // #3874 IE and Safari encode line-break into <br>
\r
268 return standard( text ).replace( /<br>/gi, '\n' );
\r
272 var fix2 = ( standard( '>' ) == '>' ) ?
\r
275 // WebKit does't encode the ">" character, which makes sense, but
\r
276 // it's different than other browsers.
\r
277 return fix1( text ).replace( />/g, '>' );
\r
281 var fix3 = ( standard( ' ' ) == ' ' ) ?
\r
284 // #3785 IE8 changes spaces (>= 2) to
\r
285 return fix2( text ).replace( / /g, ' ' );
\r
289 this.htmlEncode = fix3;
\r
291 return this.htmlEncode( text );
\r
295 * Replace characters can't be represented through CSS Selectors string
\r
296 * by CSS Escape Notation where the character escape sequence consists
\r
297 * of a backslash character (\) followed by the orginal characters.
\r
298 * Ref: http://www.w3.org/TR/css3-selectors/#grammar
\r
299 * @param cssSelectText
\r
300 * @return the escaped selector text.
\r
302 escapeCssSelector : function( cssSelectText )
\r
304 return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' );
\r
308 * Gets a unique number for this CKEDITOR execution session. It returns
\r
309 * progressive numbers starting at 1.
\r
311 * @returns {Number} A unique number.
\r
313 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "1" (e.g.)
\r
314 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "2"
\r
316 getNextNumber : (function()
\r
326 * Creates a function override.
\r
327 * @param {Function} originalFunction The function to be overridden.
\r
328 * @param {Function} functionBuilder A function that returns the new
\r
329 * function. The original function reference will be passed to this
\r
331 * @returns {Function} The new function.
\r
335 * myFunction : function( name )
\r
337 * alert( 'Name: ' + name );
\r
341 * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )
\r
343 * return function( name )
\r
345 * alert( 'Override Name: ' + name );
\r
346 * myFunctionOriginal.call( this, name );
\r
350 override : function( originalFunction, functionBuilder )
\r
352 return functionBuilder( originalFunction );
\r
356 * Executes a function after specified delay.
\r
357 * @param {Function} func The function to be executed.
\r
358 * @param {Number} [milliseconds] The amount of time (millisecods) to wait
\r
359 * to fire the function execution. Defaults to zero.
\r
360 * @param {Object} [scope] The object to hold the function execution scope
\r
361 * (the "this" object). By default the "window" object.
\r
362 * @param {Object|Array} [args] A single object, or an array of objects, to
\r
363 * pass as arguments to the function.
\r
364 * @param {Object} [ownerWindow] The window that will be used to set the
\r
365 * timeout. By default the current "window".
\r
366 * @returns {Object} A value that can be used to cancel the function execution.
\r
368 * CKEDITOR.tools.<b>setTimeout(
\r
371 * alert( 'Executed after 2 seconds' );
\r
375 setTimeout : function( func, milliseconds, scope, args, ownerWindow )
\r
377 if ( !ownerWindow )
\r
378 ownerWindow = window;
\r
381 scope = ownerWindow;
\r
383 return ownerWindow.setTimeout(
\r
387 func.apply( scope, [].concat( args ) ) ;
\r
389 func.apply( scope ) ;
\r
391 milliseconds || 0 );
\r
395 * Remove spaces from the start and the end of a string. The following
\r
396 * characters are removed: space, tab, line break, line feed.
\r
398 * @param {String} str The text from which remove the spaces.
\r
399 * @returns {String} The modified string without the boundary spaces.
\r
401 * alert( CKEDITOR.tools.trim( ' example ' ); // "example"
\r
405 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
406 var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;
\r
407 return function( str )
\r
409 return str.replace( trimRegex, '' ) ;
\r
414 * Remove spaces from the start (left) of a string. The following
\r
415 * characters are removed: space, tab, line break, line feed.
\r
417 * @param {String} str The text from which remove the spaces.
\r
418 * @returns {String} The modified string excluding the removed spaces.
\r
420 * alert( CKEDITOR.tools.ltrim( ' example ' ); // "example "
\r
422 ltrim : (function()
\r
424 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
425 var trimRegex = /^[ \t\n\r]+/g;
\r
426 return function( str )
\r
428 return str.replace( trimRegex, '' ) ;
\r
433 * Remove spaces from the end (right) of a string. The following
\r
434 * characters are removed: space, tab, line break, line feed.
\r
436 * @param {String} str The text from which remove the spaces.
\r
437 * @returns {String} The modified string excluding the removed spaces.
\r
439 * alert( CKEDITOR.tools.ltrim( ' example ' ); // " example"
\r
441 rtrim : (function()
\r
443 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
444 var trimRegex = /[ \t\n\r]+$/g;
\r
445 return function( str )
\r
447 return str.replace( trimRegex, '' ) ;
\r
452 * Returns the index of an element in an array.
\r
453 * @param {Array} array The array to be searched.
\r
454 * @param {Object} entry The element to be found.
\r
455 * @returns {Number} The (zero based) index of the first entry that matches
\r
456 * the entry, or -1 if not found.
\r
458 * var letters = [ 'a', 'b', 0, 'c', false ];
\r
459 * alert( CKEDITOR.tools.indexOf( letters, '0' ) ); "-1" because 0 !== '0'
\r
460 * alert( CKEDITOR.tools.indexOf( letters, false ) ); "4" because 0 !== false
\r
463 // #2514: We should try to use Array.indexOf if it does exist.
\r
464 ( Array.prototype.indexOf ) ?
\r
465 function( array, entry )
\r
467 return array.indexOf( entry );
\r
470 function( array, entry )
\r
472 for ( var i = 0, len = array.length ; i < len ; i++ )
\r
474 if ( array[ i ] === entry )
\r
481 * Creates a function that will always execute in the context of a
\r
482 * specified object.
\r
483 * @param {Function} func The function to be executed.
\r
484 * @param {Object} obj The object to which bind the execution context.
\r
485 * @returns {Function} The function that can be used to execute the
\r
486 * "func" function in the context of "obj".
\r
488 * var obj = { text : 'My Object' };
\r
490 * function alertText()
\r
492 * alert( this.text );
\r
495 * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;
\r
496 * newFunc(); // Alerts "My Object".
\r
498 bind : function( func, obj )
\r
500 return function() { return func.apply( obj, arguments ); };
\r
504 * Class creation based on prototype inheritance, with supports of the
\r
505 * following features:
\r
507 * <li> Static fields </li>
\r
508 * <li> Private fields </li>
\r
509 * <li> Public (prototype) fields </li>
\r
510 * <li> Chainable base class constructor </li>
\r
512 * @param {Object} definiton The class definiton object.
\r
513 * @returns {Function} A class-like JavaScript function.
\r
515 createClass : function( definition )
\r
517 var $ = definition.$,
\r
518 baseClass = definition.base,
\r
519 privates = definition.privates || definition._,
\r
520 proto = definition.proto,
\r
521 statics = definition.statics;
\r
525 var originalConstructor = $;
\r
528 // Create (and get) the private namespace.
\r
529 var _ = this._ || ( this._ = {} );
\r
531 // Make some magic so "this" will refer to the main
\r
532 // instance when coding private functions.
\r
533 for ( var privateName in privates )
\r
535 var priv = privates[ privateName ];
\r
538 ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;
\r
541 originalConstructor.apply( this, arguments );
\r
547 $.prototype = this.prototypedCopy( baseClass.prototype );
\r
548 $.prototype.constructor = $;
\r
549 $.prototype.base = function()
\r
551 this.base = baseClass.prototype.base;
\r
552 baseClass.apply( this, arguments );
\r
553 this.base = arguments.callee;
\r
558 this.extend( $.prototype, proto, true );
\r
561 this.extend( $, statics, true );
\r
567 * Creates a function reference that can be called later using
\r
568 * CKEDITOR.tools.callFunction. This approach is specially useful to
\r
569 * make DOM attribute function calls to JavaScript defined functions.
\r
570 * @param {Function} fn The function to be executed on call.
\r
571 * @param {Object} [scope] The object to have the context on "fn" execution.
\r
572 * @returns {Number} A unique reference to be used in conjuction with
\r
573 * CKEDITOR.tools.callFunction.
\r
575 * var ref = <b>CKEDITOR.tools.addFunction</b>(
\r
578 * alert( 'Hello!');
\r
580 * CKEDITOR.tools.callFunction( ref ); // Hello!
\r
582 addFunction : function( fn, scope )
\r
584 return functions.push( function()
\r
586 fn.apply( scope || this, arguments );
\r
591 * Executes a function based on the reference created with
\r
592 * CKEDITOR.tools.addFunction.
\r
593 * @param {Number} ref The function reference created with
\r
594 * CKEDITOR.tools.addFunction.
\r
595 * @param {[Any,[Any,...]} params Any number of parameters to be passed
\r
596 * to the executed function.
\r
597 * @returns {Any} The return value of the function.
\r
599 * var ref = CKEDITOR.tools.addFunction(
\r
602 * alert( 'Hello!');
\r
604 * <b>CKEDITOR.tools.callFunction( ref )</b>; // Hello!
\r
606 callFunction : function( ref )
\r
608 var fn = functions[ ref ];
\r
609 return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );
\r
612 cssLength : (function()
\r
614 var decimalRegex = /^\d+(?:\.\d+)?$/;
\r
615 return function( length )
\r
617 return length + ( decimalRegex.test( length ) ? 'px' : '' );
\r
621 repeat : function( str, times )
\r
623 return new Array( times + 1 ).join( str );
\r
628 // PACKAGER_RENAME( CKEDITOR.tools )
\r