JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / core / tools.js
diff --git a/_source/core/tools.js b/_source/core/tools.js
new file mode 100644 (file)
index 0000000..29b34b4
--- /dev/null
@@ -0,0 +1,531 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @fileOverview Defines the {@link CKEDITOR.tools} object, which contains\r
+ *             utility functions.\r
+ */\r
+\r
+(function()\r
+{\r
+       var functions = [];\r
+\r
+       /**\r
+        * Utility functions.\r
+        * @namespace\r
+        * @example\r
+        */\r
+       CKEDITOR.tools =\r
+       {\r
+               arrayCompare : function( arrayA, arrayB )\r
+               {\r
+                       if ( !arrayA && !arrayB )\r
+                               return true;\r
+\r
+                       if ( !arrayA || !arrayB || arrayA.length != arrayB.length )\r
+                               return false;\r
+\r
+                       for ( var i = 0 ; i < arrayA.length ; i++ )\r
+                       {\r
+                               if ( arrayA[ i ] != arrayB[ i ] )\r
+                                       return false;\r
+                       }\r
+\r
+                       return true;\r
+               },\r
+\r
+               /**\r
+                * Creates a deep copy of an object.\r
+                * Attention: there is no support for recursive references.\r
+                * @param {Object} object The object to be cloned.\r
+                * @returns {Object} The object clone.\r
+                * @example\r
+                * var obj =\r
+                *     {\r
+                *         name : 'John',\r
+                *         cars :\r
+                *             {\r
+                *                 Mercedes : { color : 'blue' },\r
+                *                 Porsche : { color : 'red' }\r
+                *             }\r
+                *     };\r
+                * var clone = CKEDITOR.tools.clone( obj );\r
+                * clone.name = 'Paul';\r
+                * clone.cars.Porsche.color = 'silver';\r
+                * alert( obj.name );   // John\r
+                * alert( clone.name ); // Paul\r
+                * alert( obj.cars.Porsche.color );     // red\r
+                * alert( clone.cars.Porsche.color );   // silver\r
+                */\r
+               clone : function( obj )\r
+               {\r
+                       var clone;\r
+\r
+                       // Array.\r
+                       if ( obj && ( obj instanceof Array ) )\r
+                       {\r
+                               clone = [];\r
+\r
+                               for ( var i = 0 ; i < obj.length ; i++ )\r
+                                       clone[ i ] = this.clone( obj[ i ] );\r
+\r
+                               return clone;\r
+                       }\r
+\r
+                       // "Static" types.\r
+                       if ( obj === null\r
+                               || ( typeof( obj ) != 'object' )\r
+                               || ( obj instanceof String )\r
+                               || ( obj instanceof Number )\r
+                               || ( obj instanceof Boolean )\r
+                               || ( obj instanceof Date ) )\r
+                       {\r
+                               return obj;\r
+                       }\r
+\r
+                       // Objects.\r
+                       clone = new obj.constructor();\r
+\r
+                       for ( var propertyName in obj )\r
+                       {\r
+                               var property = obj[ propertyName ];\r
+                               clone[ propertyName ] = this.clone( property );\r
+                       }\r
+\r
+                       return clone;\r
+               },\r
+\r
+               /**\r
+                * Copy the properties from one object to another. By default, properties\r
+                * already present in the target object <strong>are not</strong> overwritten.\r
+                * @param {Object} target The object to be extended.\r
+                * @param {Object} source[,souce(n)] The objects from which copy\r
+                *              properties. Any number of objects can be passed to this function.\r
+                * @param {Boolean} [overwrite] If 'true' is specified it indicates that\r
+                *            properties already present in the target object could be\r
+                *            overwritten by subsequent objects.\r
+                * @param {Object} [properties] Only properties within the specified names\r
+                *            list will be received from the source object.\r
+                * @returns {Object} the extended object (target).\r
+                * @example\r
+                * // Create the sample object.\r
+                * var myObject =\r
+                * {\r
+                *     prop1 : true\r
+                * };\r
+                *\r
+                * // Extend the above object with two properties.\r
+                * CKEDITOR.tools.extend( myObject,\r
+                *     {\r
+                *         prop2 : true,\r
+                *         prop3 : true\r
+                *     } );\r
+                *\r
+                * // Alert "prop1", "prop2" and "prop3".\r
+                * for ( var p in myObject )\r
+                *     alert( p );\r
+                */\r
+               extend : function( target )\r
+               {\r
+                       var argsLength = arguments.length,\r
+                               overwrite, propertiesList;\r
+\r
+                       if ( typeof ( overwrite = arguments[ argsLength - 1 ] ) == 'boolean')\r
+                               argsLength--;\r
+                       else if ( typeof ( overwrite = arguments[ argsLength - 2 ] ) == 'boolean' )\r
+                       {\r
+                               propertiesList = arguments [ argsLength -1 ];\r
+                               argsLength-=2;\r
+                       }\r
+                       for ( var i = 1 ; i < argsLength ; i++ )\r
+                       {\r
+                               var source = arguments[ i ];\r
+                               for ( var propertyName in source )\r
+                               {\r
+                                       // Only copy existed fields if in overwrite mode.\r
+                                       if ( overwrite === true || target[ propertyName ] == undefined )\r
+                                       {\r
+                                               // Only copy  specified fields if list is provided.\r
+                                               if ( !propertiesList || ( propertyName in propertiesList ) )\r
+                                                       target[ propertyName ] = source[ propertyName ];\r
+\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       return target;\r
+               },\r
+\r
+               /**\r
+                * Creates an object which is an instance of a class which prototype is a\r
+                * predefined object. All properties defined in the source object are\r
+                * automatically inherited by the resulting object, including future\r
+                * changes to it.\r
+                * @param {Object} source The source object to be used as the prototype for\r
+                *              the final object.\r
+                * @returns {Object} The resulting copy.\r
+                */\r
+               prototypedCopy : function( source )\r
+               {\r
+                       var copy = function()\r
+                       {};\r
+                       copy.prototype = source;\r
+                       return new copy();\r
+               },\r
+\r
+               /**\r
+                * Checks if an object is an Array.\r
+                * @param {Object} object The object to be checked.\r
+                * @type Boolean\r
+                * @returns <i>true</i> if the object is an Array, otherwise <i>false</i>.\r
+                * @example\r
+                * alert( CKEDITOR.tools.isArray( [] ) );      // "true"\r
+                * alert( CKEDITOR.tools.isArray( 'Test' ) );  // "false"\r
+                */\r
+               isArray : function( object )\r
+               {\r
+                       return ( !!object && object instanceof Array );\r
+               },\r
+\r
+               /**\r
+                * Transforms a CSS property name to its relative DOM style name.\r
+                * @param {String} cssName The CSS property name.\r
+                * @returns {String} The transformed name.\r
+                * @example\r
+                * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) );  // "backgroundColor"\r
+                * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) );             // "cssFloat"\r
+                */\r
+               cssStyleToDomStyle : function( cssName )\r
+               {\r
+                       if ( cssName == 'float' )\r
+                               return 'cssFloat';\r
+                       else\r
+                       {\r
+                               return cssName.replace( /-./g, function( match )\r
+                                       {\r
+                                               return match.substr( 1 ).toUpperCase();\r
+                                       });\r
+                       }\r
+               },\r
+\r
+               /**\r
+                * Replace special HTML characters in a string with their relative HTML\r
+                * entity values.\r
+                * @param {String} text The string to be encoded.\r
+                * @returns {String} The encode string.\r
+                * @example\r
+                * alert( CKEDITOR.tools.htmlEncode( 'A > B & C < D' ) );  // "A &amp;gt; B &amp;amp; C &amp;lt; D"\r
+                */\r
+               htmlEncode : function( text )\r
+               {\r
+                       var standard = function( text )\r
+                       {\r
+                               var span = new CKEDITOR.dom.element( 'span' );\r
+                               span.setText( text );\r
+                               return span.getHtml();\r
+                       };\r
+\r
+                       var fix1 = ( standard( '\n' ).toLowerCase() == '<br>' ) ?\r
+                               function( text )\r
+                               {\r
+                                       // #3874 IE and Safari encode line-break into <br>\r
+                                       return standard( text ).replace( /<br>/gi, '\n' );\r
+                               } :\r
+                               standard;\r
+\r
+                       var fix2 = ( standard( '>' ) == '>' ) ?\r
+                               function( text )\r
+                               {\r
+                                       // WebKit does't encode the ">" character, which makes sense, but\r
+                                       // it's different than other browsers.\r
+                                       return fix1( text ).replace( />/g, '&gt;' );\r
+                               } :\r
+                               fix1;\r
+\r
+                       var fix3 = ( standard( '  ' ) == '&nbsp; ' ) ?\r
+                               function( text )\r
+                               {\r
+                                       // #3785 IE8 changes spaces (>= 2) to &nbsp;\r
+                                       return fix2( text ).replace( /&nbsp;/g, ' ' );\r
+                               } :\r
+                               fix2;\r
+\r
+                       this.htmlEncode = fix3;\r
+\r
+                       return this.htmlEncode( text );\r
+               },\r
+\r
+               /**\r
+                * Gets a unique number for this CKEDITOR execution session. It returns\r
+                * progressive numbers starting at 1.\r
+                * @function\r
+                * @returns {Number} A unique number.\r
+                * @example\r
+                * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "1" (e.g.)\r
+                * alert( CKEDITOR.tools.<b>getNextNumber()</b> );  // "2"\r
+                */\r
+               getNextNumber : (function()\r
+               {\r
+                       var last = 0;\r
+                       return function()\r
+                       {\r
+                               return ++last;\r
+                       };\r
+               })(),\r
+\r
+               /**\r
+                * Creates a function override.\r
+                * @param {Function} originalFunction The function to be overridden.\r
+                * @param {Function} functionBuilder A function that returns the new\r
+                *              function. The original function reference will be passed to this\r
+                *              function.\r
+                * @returns {Function} The new function.\r
+                * @example\r
+                * var example =\r
+                * {\r
+                *     myFunction : function( name )\r
+                *     {\r
+                *         alert( 'Name: ' + name );\r
+                *     }\r
+                * };\r
+                *\r
+                * example.myFunction = CKEDITOR.tools.override( example.myFunction, function( myFunctionOriginal )\r
+                *     {\r
+                *         return function( name )\r
+                *             {\r
+                *                 alert( 'Override Name: ' + name );\r
+                *                 myFunctionOriginal.call( this, name );\r
+                *             };\r
+                *     });\r
+                */\r
+               override : function( originalFunction, functionBuilder )\r
+               {\r
+                       return functionBuilder( originalFunction );\r
+               },\r
+\r
+               /**\r
+                * Executes a function after specified delay.\r
+                * @param {Function} func The function to be executed.\r
+                * @param {Number} [milliseconds] The amount of time (millisecods) to wait\r
+                *              to fire the function execution. Defaults to zero.\r
+                * @param {Object} [scope] The object to hold the function execution scope\r
+                *              (the "this" object). By default the "window" object.\r
+                * @param {Object|Array} [args] A single object, or an array of objects, to\r
+                *              pass as arguments to the function.\r
+                * @param {Object} [ownerWindow] The window that will be used to set the\r
+                *              timeout. By default the current "window".\r
+                * @returns {Object} A value that can be used to cancel the function execution.\r
+                * @example\r
+                * CKEDITOR.tools.<b>setTimeout(\r
+                *     function()\r
+                *     {\r
+                *         alert( 'Executed after 2 seconds' );\r
+                *     },\r
+                *     2000 )</b>;\r
+                */\r
+               setTimeout : function( func, milliseconds, scope, args, ownerWindow )\r
+               {\r
+                       if ( !ownerWindow )\r
+                               ownerWindow = window;\r
+\r
+                       if ( !scope )\r
+                               scope = ownerWindow;\r
+\r
+                       return ownerWindow.setTimeout(\r
+                               function()\r
+                               {\r
+                                       if ( args )\r
+                                               func.apply( scope, [].concat( args ) ) ;\r
+                                       else\r
+                                               func.apply( scope ) ;\r
+                               },\r
+                               milliseconds || 0 );\r
+               },\r
+\r
+               /**\r
+                * Remove spaces from the start and the end of a string. The following\r
+                * characters are removed: space, tab, line break, line feed.\r
+                * @function\r
+                * @param {String} str The text from which remove the spaces.\r
+                * @returns {String} The modified string without the boundary spaces.\r
+                * @example\r
+                * alert( CKEDITOR.tools.trim( '  example ' );  // "example"\r
+                */\r
+               trim : (function()\r
+               {\r
+                       // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
+                       var trimRegex = /(?:^[ \t\n\r]+)|(?:[ \t\n\r]+$)/g;\r
+                       return function( str )\r
+                       {\r
+                               return str.replace( trimRegex, '' ) ;\r
+                       };\r
+               })(),\r
+\r
+               /**\r
+                * Remove spaces from the start (left) of a string. The following\r
+                * characters are removed: space, tab, line break, line feed.\r
+                * @function\r
+                * @param {String} str The text from which remove the spaces.\r
+                * @returns {String} The modified string excluding the removed spaces.\r
+                * @example\r
+                * alert( CKEDITOR.tools.ltrim( '  example ' );  // "example "\r
+                */\r
+               ltrim : (function()\r
+               {\r
+                       // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
+                       var trimRegex = /^[ \t\n\r]+/g;\r
+                       return function( str )\r
+                       {\r
+                               return str.replace( trimRegex, '' ) ;\r
+                       };\r
+               })(),\r
+\r
+               /**\r
+                * Remove spaces from the end (right) of a string. The following\r
+                * characters are removed: space, tab, line break, line feed.\r
+                * @function\r
+                * @param {String} str The text from which remove the spaces.\r
+                * @returns {String} The modified string excluding the removed spaces.\r
+                * @example\r
+                * alert( CKEDITOR.tools.ltrim( '  example ' );  // "  example"\r
+                */\r
+               rtrim : (function()\r
+               {\r
+                       // We are not using \s because we don't want "non-breaking spaces" to be caught.\r
+                       var trimRegex = /[ \t\n\r]+$/g;\r
+                       return function( str )\r
+                       {\r
+                               return str.replace( trimRegex, '' ) ;\r
+                       };\r
+               })(),\r
+\r
+               /**\r
+                * Returns the index of an element in an array.\r
+                * @param {Array} array The array to be searched.\r
+                * @param {Object} entry The element to be found.\r
+                * @returns {Number} The (zero based) index of the first entry that matches\r
+                *              the entry, or -1 if not found.\r
+                * @example\r
+                * var letters = [ 'a', 'b', 0, 'c', false ];\r
+                * alert( CKEDITOR.tools.indexOf( letters, '0' ) );  "-1" because 0 !== '0'\r
+                * alert( CKEDITOR.tools.indexOf( letters, false ) );  "4" because 0 !== false\r
+                */\r
+               indexOf :\r
+                       // #2514: We should try to use Array.indexOf if it does exist.\r
+                       ( Array.prototype.indexOf ) ?\r
+                               function( array, entry )\r
+                                       {\r
+                                               return array.indexOf( entry );\r
+                                       }\r
+                       :\r
+                               function( array, entry )\r
+                               {\r
+                                       for ( var i = 0, len = array.length ; i < len ; i++ )\r
+                                       {\r
+                                               if ( array[ i ] === entry )\r
+                                                       return i;\r
+                                       }\r
+                                       return -1;\r
+                               },\r
+\r
+               bind : function( func, obj )\r
+               {\r
+                       return function() { return func.apply( obj, arguments ); };\r
+               },\r
+\r
+               /**\r
+                * Class creation based on prototype inheritance, with supports of the\r
+                * following features:\r
+                * <ul>\r
+                * <li> Static fields </li>\r
+                * <li> Private fields </li>\r
+                * <li> Public(prototype) fields </li>\r
+                * <li> Chainable base class constructor </li>\r
+                * </ul>\r
+                *\r
+                * @param {Object} definiton (Optional)The class definiton object.\r
+                */\r
+               createClass : function( definition )\r
+               {\r
+                       var $ = definition.$,\r
+                               baseClass = definition.base,\r
+                               privates = definition.privates || definition._,\r
+                               proto = definition.proto,\r
+                               statics = definition.statics;\r
+\r
+                       if ( privates )\r
+                       {\r
+                               var originalConstructor = $;\r
+                               $ = function()\r
+                               {\r
+                                       // Create (and get) the private namespace.\r
+                                       var _ = this._ || ( this._ = {} );\r
+\r
+                                       // Make some magic so "this" will refer to the main\r
+                                       // instance when coding private functions.\r
+                                       for ( var privateName in privates )\r
+                                       {\r
+                                               var priv = privates[ privateName ];\r
+\r
+                                               _[ privateName ] =\r
+                                                       ( typeof priv == 'function' ) ? CKEDITOR.tools.bind( priv, this ) : priv;\r
+                                       }\r
+\r
+                                       originalConstructor.apply( this, arguments );\r
+                               };\r
+                       }\r
+\r
+                       if ( baseClass )\r
+                       {\r
+                               $.prototype = this.prototypedCopy( baseClass.prototype );\r
+                               $.prototype.constructor = $;\r
+                               $.prototype.base = function()\r
+                               {\r
+                                       this.base = baseClass.prototype.base;\r
+                                       baseClass.apply( this, arguments );\r
+                                       this.base = arguments.callee;\r
+                               };\r
+                       }\r
+\r
+                       if ( proto )\r
+                               this.extend( $.prototype, proto, true );\r
+\r
+                       if ( statics )\r
+                               this.extend( $, statics, true );\r
+\r
+                       return $;\r
+               },\r
+\r
+               addFunction : function( fn, scope )\r
+               {\r
+                       return functions.push( function()\r
+                               {\r
+                                       fn.apply( scope || this, arguments );\r
+                               }) - 1;\r
+               },\r
+\r
+               callFunction : function( index )\r
+               {\r
+                       var fn = functions[ index ];\r
+                       return fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
+               },\r
+\r
+               cssLength : (function()\r
+               {\r
+                       var decimalRegex = /^\d+(?:\.\d+)?$/;\r
+                       return function( length )\r
+                       {\r
+                               return length + ( decimalRegex.test( length ) ? 'px' : '' );\r
+                       };\r
+               })(),\r
+\r
+               repeat : function( str, times )\r
+               {\r
+                       return new Array( times + 1 ).join( str );\r
+               }\r
+       };\r
+})();\r
+\r
+// PACKAGER_RENAME( CKEDITOR.tools )\r