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
22 arrayCompare : function( arrayA, arrayB )
\r
24 if ( !arrayA && !arrayB )
\r
27 if ( !arrayA || !arrayB || arrayA.length != arrayB.length )
\r
30 for ( var i = 0 ; i < arrayA.length ; i++ )
\r
32 if ( arrayA[ i ] != arrayB[ i ] )
\r
40 * Creates a deep copy of an object.
\r
41 * Attention: there is no support for recursive references.
\r
42 * @param {Object} object The object to be cloned.
\r
43 * @returns {Object} The object clone.
\r
50 * Mercedes : { color : 'blue' },
\r
51 * Porsche : { color : 'red' }
\r
54 * var clone = CKEDITOR.tools.clone( obj );
\r
55 * clone.name = 'Paul';
\r
56 * clone.cars.Porsche.color = 'silver';
\r
57 * alert( obj.name ); // John
\r
58 * alert( clone.name ); // Paul
\r
59 * alert( obj.cars.Porsche.color ); // red
\r
60 * alert( clone.cars.Porsche.color ); // silver
\r
62 clone : function( obj )
\r
67 if ( obj && ( obj instanceof Array ) )
\r
71 for ( var i = 0 ; i < obj.length ; i++ )
\r
72 clone[ i ] = this.clone( obj[ i ] );
\r
79 || ( typeof( obj ) != 'object' )
\r
80 || ( obj instanceof String )
\r
81 || ( obj instanceof Number )
\r
82 || ( obj instanceof Boolean )
\r
83 || ( obj instanceof Date ) )
\r
89 clone = new obj.constructor();
\r
91 for ( var propertyName in obj )
\r
93 var property = obj[ propertyName ];
\r
94 clone[ propertyName ] = this.clone( property );
\r
101 * Copy the properties from one object to another. By default, properties
\r
102 * already present in the target object <strong>are not</strong> overwritten.
\r
103 * @param {Object} target The object to be extended.
\r
104 * @param {Object} source[,souce(n)] The objects from which copy
\r
105 * properties. Any number of objects can be passed to this function.
\r
106 * @param {Boolean} [overwrite] If 'true' is specified it indicates that
\r
107 * properties already present in the target object could be
\r
108 * overwritten by subsequent objects.
\r
109 * @param {Object} [properties] Only properties within the specified names
\r
110 * list will be received from the source object.
\r
111 * @returns {Object} the extended object (target).
\r
113 * // Create the sample object.
\r
119 * // Extend the above object with two properties.
\r
120 * CKEDITOR.tools.extend( myObject,
\r
126 * // Alert "prop1", "prop2" and "prop3".
\r
127 * for ( var p in myObject )
\r
130 extend : function( target )
\r
132 var argsLength = arguments.length,
\r
133 overwrite, propertiesList;
\r
135 if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')
\r
137 else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )
\r
139 propertiesList = arguments [ argsLength -1 ];
\r
142 for ( var i = 1 ; i < argsLength ; i++ )
\r
144 var source = arguments[ i ];
\r
145 for ( var propertyName in source )
\r
147 // Only copy existed fields if in overwrite mode.
\r
148 if ( overwrite === true || target[ propertyName ] == undefined )
\r
150 // Only copy specified fields if list is provided.
\r
151 if ( !propertiesList || ( propertyName in propertiesList ) )
\r
152 target[ propertyName ] = source[ propertyName ];
\r
162 * Creates an object which is an instance of a class which prototype is a
\r
163 * predefined object. All properties defined in the source object are
\r
164 * automatically inherited by the resulting object, including future
\r
166 * @param {Object} source The source object to be used as the prototype for
\r
167 * the final object.
\r
168 * @returns {Object} The resulting copy.
\r
170 prototypedCopy : function( source )
\r
172 var copy = function()
\r
174 copy.prototype = source;
\r
179 * Checks if an object is an Array.
\r
180 * @param {Object} object The object to be checked.
\r
182 * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.
\r
184 * alert( CKEDITOR.tools.isArray( [] ) ); // "true"
\r
185 * alert( CKEDITOR.tools.isArray( 'Test' ) ); // "false"
\r
187 isArray : function( object )
\r
189 return ( !!object && object instanceof Array );
\r
193 * Transforms a CSS property name to its relative DOM style name.
\r
194 * @param {String} cssName The CSS property name.
\r
195 * @returns {String} The transformed name.
\r
197 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) ); // "backgroundColor"
\r
198 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) ); // "cssFloat"
\r
200 cssStyleToDomStyle : ( function()
\r
202 var test = document.createElement( 'div' ).style;
\r
204 var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'
\r
205 : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'
\r
208 return function( cssName )
\r
210 if ( cssName == 'float' )
\r
214 return cssName.replace( /-./g, function( match )
\r
216 return match.substr( 1 ).toUpperCase();
\r
223 * Replace special HTML characters in a string with their relative HTML
\r
225 * @param {String} text The string to be encoded.
\r
226 * @returns {String} The encode string.
\r
228 * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) ); // "A &gt; B &amp; C &lt; D"
\r
230 htmlEncode : function( text )
\r
232 var standard = function( text )
\r
234 var span = new CKEDITOR.dom.element( 'span' );
\r
235 span.setText( text );
\r
236 return span.getHtml();
\r
239 var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?
\r
242 // #3874 IE and Safari encode line-break into <br>
\r
243 return standard( text ).replace( /<br>/gi, '\n' );
\r
247 var fix2 = ( standard( '>' ) == '>' ) ?
\r
250 // WebKit does't encode the ">" character, which makes sense, but
\r
251 // it's different than other browsers.
\r
252 return fix1( text ).replace( />/g, '>' );
\r
256 var fix3 = ( standard( ' ' ) == ' ' ) ?
\r
259 // #3785 IE8 changes spaces (>= 2) to
\r
260 return fix2( text ).replace( / /g, ' ' );
\r
264 this.htmlEncode = fix3;
\r
266 return this.htmlEncode( text );
\r
270 * Gets a unique number for this CKEDITOR execution session. It returns
\r
271 * progressive numbers starting at 1.
\r
273 * @returns {Number} A unique number.
\r
275 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "1" (e.g.)
\r
276 * alert( CKEDITOR.tools.<b>getNextNumber()</b> ); // "2"
\r
278 getNextNumber : (function()
\r
288 * Creates a function override.
\r
289 * @param {Function} originalFunction The function to be overridden.
\r
290 * @param {Function} functionBuilder A function that returns the new
\r
291 * function. The original function reference will be passed to this
\r
293 * @returns {Function} The new function.
\r
297 * myFunction : function( name )
\r
299 * alert( 'Name: ' + name );
\r
303 * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )
\r
305 * return function( name )
\r
307 * alert( 'Override Name: ' + name );
\r
308 * myFunctionOriginal.call( this, name );
\r
312 override : function( originalFunction, functionBuilder )
\r
314 return functionBuilder( originalFunction );
\r
318 * Executes a function after specified delay.
\r
319 * @param {Function} func The function to be executed.
\r
320 * @param {Number} [milliseconds] The amount of time (millisecods) to wait
\r
321 * to fire the function execution. Defaults to zero.
\r
322 * @param {Object} [scope] The object to hold the function execution scope
\r
323 * (the "this" object). By default the "window" object.
\r
324 * @param {Object|Array} [args] A single object, or an array of objects, to
\r
325 * pass as arguments to the function.
\r
326 * @param {Object} [ownerWindow] The window that will be used to set the
\r
327 * timeout. By default the current "window".
\r
328 * @returns {Object} A value that can be used to cancel the function execution.
\r
330 * CKEDITOR.tools.<b>setTimeout(
\r
333 * alert( 'Executed after 2 seconds' );
\r
337 setTimeout : function( func, milliseconds, scope, args, ownerWindow )
\r
339 if ( !ownerWindow )
\r
340 ownerWindow = window;
\r
343 scope = ownerWindow;
\r
345 return ownerWindow.setTimeout(
\r
349 func.apply( scope, [].concat( args ) ) ;
\r
351 func.apply( scope ) ;
\r
353 milliseconds || 0 );
\r
357 * Remove spaces from the start and the end of a string. The following
\r
358 * characters are removed: space, tab, line break, line feed.
\r
360 * @param {String} str The text from which remove the spaces.
\r
361 * @returns {String} The modified string without the boundary spaces.
\r
363 * alert( CKEDITOR.tools.trim( ' example ' ); // "example"
\r
367 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
368 var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;
\r
369 return function( str )
\r
371 return str.replace( trimRegex, '' ) ;
\r
376 * Remove spaces from the start (left) of a string. The following
\r
377 * characters are removed: space, tab, line break, line feed.
\r
379 * @param {String} str The text from which remove the spaces.
\r
380 * @returns {String} The modified string excluding the removed spaces.
\r
382 * alert( CKEDITOR.tools.ltrim( ' example ' ); // "example "
\r
384 ltrim : (function()
\r
386 // We are not using \s because we don't want "non-breaking spaces" to be caught.
\r
387 var trimRegex = /^[ \t\n\r]+/g;
\r
388 return function( str )
\r
390 return str.replace( trimRegex, '' ) ;
\r
395 * Remove spaces from the end (right) 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 excluding the removed spaces.
\r
401 * alert( CKEDITOR.tools.ltrim( ' example ' ); // " example"
\r
403 rtrim : (function()
\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]+$/g;
\r
407 return function( str )
\r
409 return str.replace( trimRegex, '' ) ;
\r
414 * Returns the index of an element in an array.
\r
415 * @param {Array} array The array to be searched.
\r
416 * @param {Object} entry The element to be found.
\r
417 * @returns {Number} The (zero based) index of the first entry that matches
\r
418 * the entry, or -1 if not found.
\r
420 * var letters = [ 'a', 'b', 0, 'c', false ];
\r
421 * alert( CKEDITOR.tools.indexOf( letters, '0' ) ); "-1" because 0 !== '0'
\r
422 * alert( CKEDITOR.tools.indexOf( letters, false ) ); "4" because 0 !== false
\r
425 // #2514: We should try to use Array.indexOf if it does exist.
\r
426 ( Array.prototype.indexOf ) ?
\r
427 function( array, entry )
\r
429 return array.indexOf( entry );
\r
432 function( array, entry )
\r
434 for ( var i = 0, len = array.length ; i < len ; i++ )
\r
436 if ( array[ i ] === entry )
\r
442 bind : function( func, obj )
\r
444 return function() { return func.apply( obj, arguments ); };
\r
448 * Class creation based on prototype inheritance, with supports of the
\r
449 * following features:
\r
451 * <li> Static fields </li>
\r
452 * <li> Private fields </li>
\r
453 * <li> Public(prototype) fields </li>
\r
454 * <li> Chainable base class constructor </li>
\r
457 * @param {Object} definiton (Optional)The class definiton object.
\r
459 createClass : function( definition )
\r
461 var $ = definition.$,
\r
462 baseClass = definition.base,
\r
463 privates = definition.privates || definition._,
\r
464 proto = definition.proto,
\r
465 statics = definition.statics;
\r
469 var originalConstructor = $;
\r
472 // Create (and get) the private namespace.
\r
473 var _ = this._ || ( this._ = {} );
\r
475 // Make some magic so "this" will refer to the main
\r
476 // instance when coding private functions.
\r
477 for ( var privateName in privates )
\r
479 var priv = privates[ privateName ];
\r
482 ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;
\r
485 originalConstructor.apply( this, arguments );
\r
491 $.prototype = this.prototypedCopy( baseClass.prototype );
\r
492 $.prototype.constructor = $;
\r
493 $.prototype.base = function()
\r
495 this.base = baseClass.prototype.base;
\r
496 baseClass.apply( this, arguments );
\r
497 this.base = arguments.callee;
\r
502 this.extend( $.prototype, proto, true );
\r
505 this.extend( $, statics, true );
\r
510 addFunction : function( fn, scope )
\r
512 return functions.push( function()
\r
514 fn.apply( scope || this, arguments );
\r
518 callFunction : function( index )
\r
520 var fn = functions[ index ];
\r
521 return fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );
\r
524 cssLength : (function()
\r
526 var decimalRegex = /^\d+(?:\.\d+)?$/;
\r
527 return function( length )
\r
529 return length + ( decimalRegex.test( length ) ? 'px' : '' );
\r
533 repeat : function( str, times )
\r
535 return new Array( times + 1 ).join( str );
\r
540 // PACKAGER_RENAME( CKEDITOR.tools )
\r