JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / core / tools.js
index fc82904..148c588 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -12,6 +12,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 {\r
        var functions = [];\r
 \r
+       CKEDITOR.on( 'reset', function()\r
+               {\r
+                       functions = [];\r
+               });\r
+\r
        /**\r
         * Utility functions.\r
         * @namespace\r
@@ -215,6 +220,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return ( !!object && object instanceof Array );\r
                },\r
 \r
+               /**\r
+                * Whether the object contains no properties of it's own.\r
+                * @param object\r
+                */\r
                isEmpty : function ( object )\r
                {\r
                        for ( var i in object )\r
@@ -224,6 +233,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                        return true;\r
                },\r
+\r
                /**\r
                 * Transforms a CSS property name to its relative DOM style name.\r
                 * @param {String} cssName The CSS property name.\r
@@ -255,7 +265,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                } )(),\r
 \r
                /**\r
-                * Build the HTML snippet of a set of <style>/<link>.\r
+                * Build the HTML snippet of a set of &lt;style>/&lt;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
@@ -332,20 +342,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 */\r
                htmlEncodeAttr : function( text )\r
                {\r
-                       return text.replace( /"/g, '&quot;' ).replace( /</g, '&lt;' ).replace( />/, '&gt;' );\r
-               },\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
+                       return text.replace( /"/g, '&quot;' ).replace( /</g, '&lt;' ).replace( />/g, '&gt;' );\r
                },\r
 \r
                /**\r
@@ -367,6 +364,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                })(),\r
 \r
                /**\r
+                * Gets a unique ID for CKEditor's interface elements. It returns a\r
+                * string with the "cke_" prefix and a progressive number.\r
+                * @function\r
+                * @returns {String} A unique ID.\r
+                * @example\r
+                * alert( CKEDITOR.tools.<b>getNextId()</b> );  // "cke_1" (e.g.)\r
+                * alert( CKEDITOR.tools.<b>getNextId()</b> );  // "cke_2"\r
+                */\r
+               getNextId : function()\r
+               {\r
+                       return 'cke_' + this.getNextNumber();\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
@@ -627,7 +638,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                {\r
                        return functions.push( function()\r
                                {\r
-                                       fn.apply( scope || this, arguments );\r
+                                       return fn.apply( scope || this, arguments );\r
                                }) - 1;\r
                },\r
 \r
@@ -663,20 +674,62 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) );\r
                },\r
 \r
+               /**\r
+                * Append the 'px' length unit to the size if it's missing.\r
+                * @param length\r
+                */\r
                cssLength : (function()\r
                {\r
-                       var decimalRegex = /^\d+(?:\.\d+)?$/;\r
                        return function( length )\r
                        {\r
-                               return length + ( decimalRegex.test( length ) ? 'px' : '' );\r
+                               return length + ( !length || isNaN( Number( length ) ) ? '' : 'px' );\r
                        };\r
                })(),\r
 \r
+               /**\r
+                * Convert the specified CSS length value to the calculated pixel length inside this page.\r
+                * <strong>Note:</strong> Percentage based value is left intact.\r
+                * @param {String} cssLength CSS length value.\r
+                */\r
+               convertToPx : ( function ()\r
+                       {\r
+                               var calculator;\r
+\r
+                               return function( cssLength )\r
+                               {\r
+                                       if ( !calculator )\r
+                                       {\r
+                                               calculator = CKEDITOR.dom.element.createFromHtml(\r
+                                                               '<div style="position:absolute;left:-9999px;' +\r
+                                                               'top:-9999px;margin:0px;padding:0px;border:0px;"' +\r
+                                                               '></div>', CKEDITOR.document );\r
+                                               CKEDITOR.document.getBody().append( calculator );\r
+                                       }\r
+\r
+                                       if ( !(/%$/).test( cssLength ) )\r
+                                       {\r
+                                               calculator.setStyle( 'width', cssLength );\r
+                                               return calculator.$.clientWidth;\r
+                                       }\r
+\r
+                                       return cssLength;\r
+                               };\r
+                       } )(),\r
+\r
+               /**\r
+                * String specified by {@param str} repeats {@param times} times.\r
+                * @param str\r
+                * @param times\r
+                */\r
                repeat : function( str, times )\r
                {\r
                        return new Array( times + 1 ).join( str );\r
                },\r
 \r
+               /**\r
+                * Return the first successfully executed function's return value that\r
+                * doesn't throw any exception.\r
+                */\r
                tryThese : function()\r
                {\r
                        var returnValue;\r
@@ -691,6 +744,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                catch (e) {}\r
                        }\r
                        return returnValue;\r
+               },\r
+\r
+               /**\r
+                * Generate a combined key from a series of params.\r
+                * @param {String} subKey One or more string used as sub keys.\r
+                * @example\r
+                * var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' );\r
+                * alert( key );                // "key1-key2-key3".\r
+                */\r
+               genKey : function()\r
+               {\r
+                       return Array.prototype.slice.call( arguments ).join( '-' );\r
                }\r
        };\r
 })();\r