X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fcore%2Ftools.js;h=9407fd70f715f05092d723980d742a08f12ffe1a;hb=055b6b0792ce7dc53d47af606b367c04b927c2ab;hp=c9d9877dc1c1e5fb410427c1b9a9301a71800936;hpb=7cd80714081a8ffdf4a1a8d2c72f120ed5ef3d6d;p=ckeditor.git diff --git a/_source/core/tools.js b/_source/core/tools.js index c9d9877..9407fd7 100644 --- a/_source/core/tools.js +++ b/_source/core/tools.js @@ -1,5 +1,5 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ @@ -12,6 +12,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { var functions = []; + CKEDITOR.on( 'reset', function() + { + functions = []; + }); + /** * Utility functions. * @namespace @@ -96,7 +101,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license || ( obj instanceof String ) || ( obj instanceof Number ) || ( obj instanceof Boolean ) - || ( obj instanceof Date ) ) + || ( obj instanceof Date ) + || ( obj instanceof RegExp) ) { return obj; } @@ -114,6 +120,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** + * Turn the first letter of string to upper-case. + * @param {String} str + */ + capitalize: function( str ) + { + return str.charAt( 0 ).toUpperCase() + str.substring( 1 ).toLowerCase(); + }, + + /** * Copy the properties from one object to another. By default, properties * already present in the target object are not overwritten. * @param {Object} target The object to be extended. @@ -205,6 +220,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return ( !!object && object instanceof Array ); }, + /** + * Whether the object contains no properties of it's own. + * @param object + */ isEmpty : function ( object ) { for ( var i in object ) @@ -214,6 +233,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } return true; }, + /** * Transforms a CSS property name to its relative DOM style name. * @param {String} cssName The CSS property name. @@ -245,6 +265,27 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } )(), /** + * Build the HTML snippet of a set of <style>/<link>. + * @param css {String|Array} Each of which are url (absolute) of a CSS file or + * a trunk of style text. + */ + buildStyleHtml : function ( css ) + { + css = [].concat( css ); + var item, retval = []; + for ( var i = 0; i < css.length; i++ ) + { + item = css[ i ]; + // Is CSS style text ? + if ( /@import|[{}]/.test(item) ) + retval.push(''); + else + retval.push(''); + } + return retval.join( '' ); + }, + + /** * Replace special HTML characters in a string with their relative HTML * entity values. * @param {String} text The string to be encoded. @@ -292,6 +333,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** + * Replace special HTML characters in HTMLElement's attribute with their relative HTML entity values. + * @param {String} The attribute's value to be encoded. + * @returns {String} The encode value. + * @example + * element.setAttribute( 'title', '' ); + * alert( CKEDITOR.tools.htmlEncodeAttr( element.getAttribute( 'title' ) ); // ">a " b <" + */ + htmlEncodeAttr : function( text ) + { + return text.replace( /"/g, '"' ).replace( //g, '>' ); + }, + + /** * Replace characters can't be represented through CSS Selectors string * by CSS Escape Notation where the character escape sequence consists * of a backslash character (\) followed by the orginal characters. @@ -509,7 +563,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license *
  • Public (prototype) fields
  • *
  • Chainable base class constructor
  • * - * @param {Object} definiton The class definiton object. + * @param {Object} definition The class definition object. * @returns {Function} A class-like JavaScript function. */ createClass : function( definition ) @@ -588,6 +642,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, /** + * Removes the function reference created with {@see CKEDITOR.tools.addFunction}. + * @param {Number} ref The function reference created with + * CKEDITOR.tools.addFunction. + */ + removeFunction : function( ref ) + { + functions[ ref ] = null; + }, + + /** * Executes a function based on the reference created with * CKEDITOR.tools.addFunction. * @param {Number} ref The function reference created with @@ -609,6 +673,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license return fn && fn.apply( window, Array.prototype.slice.call( arguments, 1 ) ); }, + /** + * Append the 'px' length unit to the size if it's missing. + * @param length + */ cssLength : (function() { var decimalRegex = /^\d+(?:\.\d+)?$/; @@ -618,9 +686,46 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }; })(), + /** + * String specified by {@param str} repeats {@param times} times. + * @param str + * @param times + */ repeat : function( str, times ) { return new Array( times + 1 ).join( str ); + }, + + /** + * Return the first successfully executed function's return value that + * doesn't throw any exception. + */ + tryThese : function() + { + var returnValue; + for ( var i = 0, length = arguments.length; i < length; i++ ) + { + var lambda = arguments[i]; + try + { + returnValue = lambda(); + break; + } + catch (e) {} + } + return returnValue; + }, + + /** + * Generate a combined key from a series of params. + * @param {String} subKey One or more string used as sub keys. + * @example + * var key = CKEDITOR.tools.genKey( 'key1', 'key2', 'key3' ); + * alert( key ); // "key1-key2-key3". + */ + genKey : function() + { + return Array.prototype.slice.call( arguments ).join( '-' ); } }; })();