JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.1
[ckeditor.git] / _source / core / tools.js
index 29b34b4..6fb417b 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -19,6 +19,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
         */\r
        CKEDITOR.tools =\r
        {\r
+               /**\r
+                * Compare the elements of two arrays.\r
+                * @param {Array} arrayA An array to be compared.\r
+                * @param {Array} arrayB The other array to be compared.\r
+                * @returns {Boolean} "true" is the arrays have the same lenght and\r
+                *              their elements match.\r
+                * @example\r
+                * var a = [ 1, 'a', 3 ];\r
+                * var b = [ 1, 3, 'a' ];\r
+                * var c = [ 1, 'a', 3 ];\r
+                * var d = [ 1, 'a', 3, 4 ];\r
+                *\r
+                * alert( CKEDITOR.tools.arrayCompare( a, b ) );  // false\r
+                * alert( CKEDITOR.tools.arrayCompare( a, c ) );  // true\r
+                * alert( CKEDITOR.tools.arrayCompare( a, d ) );  // false\r
+                */\r
                arrayCompare : function( arrayA, arrayB )\r
                {\r
                        if ( !arrayA && !arrayB )\r
@@ -80,7 +96,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                || ( obj instanceof String )\r
                                || ( obj instanceof Number )\r
                                || ( obj instanceof Boolean )\r
-                               || ( obj instanceof Date ) )\r
+                               || ( obj instanceof Date )\r
+                               || ( obj instanceof RegExp) )\r
                        {\r
                                return obj;\r
                        }\r
@@ -98,6 +115,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
+                * Turn the first letter of string to upper-case.\r
+                * @param {String} str\r
+                */\r
+               capitalize: function( str )\r
+               {\r
+                       return str.charAt( 0 ).toUpperCase() + str.substring( 1 ).toLowerCase();\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
@@ -189,6 +215,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return ( !!object && object instanceof Array );\r
                },\r
 \r
+               isEmpty : function ( object )\r
+               {\r
+                       for ( var i in object )\r
+                       {\r
+                               if ( object.hasOwnProperty( i ) )\r
+                                       return false;\r
+                       }\r
+                       return true;\r
+               },\r
                /**\r
                 * Transforms a CSS property name to its relative DOM style name.\r
                 * @param {String} cssName The CSS property name.\r
@@ -197,17 +232,47 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'background-color' ) );  // "backgroundColor"\r
                 * alert( CKEDITOR.tools.cssStyleToDomStyle( 'float' ) );             // "cssFloat"\r
                 */\r
-               cssStyleToDomStyle : function( cssName )\r
+               cssStyleToDomStyle : ( function()\r
                {\r
-                       if ( cssName == 'float' )\r
-                               return 'cssFloat';\r
-                       else\r
+                       var test = document.createElement( 'div' ).style;\r
+\r
+                       var cssFloat = ( typeof test.cssFloat != 'undefined' ) ? 'cssFloat'\r
+                               : ( typeof test.styleFloat != 'undefined' ) ? 'styleFloat'\r
+                               : 'float';\r
+\r
+                       return function( cssName )\r
                        {\r
-                               return cssName.replace( /-./g, function( match )\r
-                                       {\r
-                                               return match.substr( 1 ).toUpperCase();\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
+               /**\r
+                * Build the HTML snippet of a set of <style>/<link>.\r
+                * @param css {String|Array} Each of which are url (absolute) of a CSS file or\r
+                * a trunk of style text.\r
+                */\r
+               buildStyleHtml : function ( css )\r
+               {\r
+                       css = [].concat( css );\r
+                       var item, retval = [];\r
+                       for ( var i = 0; i < css.length; i++ )\r
+                       {\r
+                               item = css[ i ];\r
+                               // Is CSS style text ?\r
+                               if ( /@import|[{}]/.test(item) )\r
+                                       retval.push('<style>' + item + '</style>');\r
+                               else\r
+                                       retval.push('<link type="text/css" rel=stylesheet href="' + item + '">');\r
                        }\r
+                       return retval.join( '' );\r
                },\r
 \r
                /**\r
@@ -258,6 +323,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
+                * Replace characters can't be represented through CSS Selectors string\r
+                * by CSS Escape Notation where the character escape sequence consists\r
+                * of a backslash character (\) followed by the orginal characters.\r
+                * Ref: http://www.w3.org/TR/css3-selectors/#grammar\r
+                * @param cssSelectText\r
+                * @return the escaped selector text.\r
+                */\r
+               escapeCssSelector : function( cssSelectText )\r
+               {\r
+                       return cssSelectText.replace( /[\s#:.,$*^\[\]()~=+>]/g, '\\$&' );\r
+               },\r
+\r
+               /**\r
                 * Gets a unique number for this CKEDITOR execution session. It returns\r
                 * progressive numbers starting at 1.\r
                 * @function\r
@@ -430,6 +508,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        return -1;\r
                                },\r
 \r
+               /**\r
+                * Creates a function that will always execute in the context of a\r
+                * specified object.\r
+                * @param {Function} func The function to be executed.\r
+                * @param {Object} obj The object to which bind the execution context.\r
+                * @returns {Function} The function that can be used to execute the\r
+                *              "func" function in the context of "obj".\r
+                * @example\r
+                * var obj = { text : 'My Object' };\r
+                *\r
+                * function alertText()\r
+                * {\r
+                *     alert( this.text );\r
+                * }\r
+                *\r
+                * var newFunc = <b>CKEDITOR.tools.bind( alertText, obj )</b>;\r
+                * newFunc();  // Alerts "My Object".\r
+                */\r
                bind : function( func, obj )\r
                {\r
                        return function() { return func.apply( obj, arguments ); };\r
@@ -441,11 +537,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 * <ul>\r
                 * <li> Static fields </li>\r
                 * <li> Private fields </li>\r
-                * <li> Public(prototype) 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
+                * @param {Object} definiton The class definiton object.\r
+                * @returns {Function} A class-like JavaScript function.\r
                 */\r
                createClass : function( definition )\r
                {\r
@@ -498,6 +594,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return $;\r
                },\r
 \r
+               /**\r
+                * Creates a function reference that can be called later using\r
+                * CKEDITOR.tools.callFunction. This approach is specially useful to\r
+                * make DOM attribute function calls to JavaScript defined functions.\r
+                * @param {Function} fn The function to be executed on call.\r
+                * @param {Object} [scope] The object to have the context on "fn" execution.\r
+                * @returns {Number} A unique reference to be used in conjuction with\r
+                *              CKEDITOR.tools.callFunction.\r
+                * @example\r
+                * var ref = <b>CKEDITOR.tools.addFunction</b>(\r
+                *     function()\r
+                *     {\r
+                *         alert( 'Hello!');\r
+                *     });\r
+                * CKEDITOR.tools.callFunction( ref );  // Hello!\r
+                */\r
                addFunction : function( fn, scope )\r
                {\r
                        return functions.push( function()\r
@@ -506,10 +618,26 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }) - 1;\r
                },\r
 \r
-               callFunction : function( index )\r
+               /**\r
+                * Executes a function based on the reference created with\r
+                * CKEDITOR.tools.addFunction.\r
+                * @param {Number} ref The function reference created with\r
+                *              CKEDITOR.tools.addFunction.\r
+                * @param {[Any,[Any,...]} params Any number of parameters to be passed\r
+                *              to the executed function.\r
+                * @returns {Any} The return value of the function.\r
+                * @example\r
+                * var ref = CKEDITOR.tools.addFunction(\r
+                *     function()\r
+                *     {\r
+                *         alert( 'Hello!');\r
+                *     });\r
+                * <b>CKEDITOR.tools.callFunction( ref )</b>;  // Hello!\r
+                */\r
+               callFunction : function( ref )\r
                {\r
-                       var fn = functions[ index ];\r
-                       return fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
+                       var fn = functions[ ref ];\r
+                       return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
                },\r
 \r
                cssLength : (function()\r
@@ -524,6 +652,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                repeat : function( str, times )\r
                {\r
                        return new Array( times + 1 ).join( str );\r
+               },\r
+\r
+               tryThese : function()\r
+               {\r
+                       var returnValue;\r
+                       for ( var i = 0, length = arguments.length; i < length; i++ )\r
+                       {\r
+                               var lambda = arguments[i];\r
+                               try\r
+                               {\r
+                                       returnValue = lambda();\r
+                                       break;\r
+                               }\r
+                               catch (e) {}\r
+                       }\r
+                       return returnValue;\r
                }\r
        };\r
 })();\r