-/*\r
-Copyright (c) 2003-2011, 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
- CKEDITOR.on( 'reset', function()\r
- {\r
- functions = [];\r
- });\r
-\r
- /**\r
- * Utility functions.\r
- * @namespace\r
- * @example\r
- */\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
- 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
- || ( obj instanceof RegExp) )\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
- * 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
- * @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
- * 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
- {\r
- if ( object.hasOwnProperty( i ) )\r
- return false;\r
- }\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
- * @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()\r
- {\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
- 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
- * 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 &gt; B &amp; C &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, '>' );\r
- } :\r
- fix1;\r
-\r
- var fix3 = ( standard( ' ' ) == ' ' ) ?\r
- function( text )\r
- {\r
- // #3785 IE8 changes spaces (>= 2) to \r
- return fix2( text ).replace( / /g, ' ' );\r
- } :\r
- fix2;\r
-\r
- this.htmlEncode = fix3;\r
-\r
- return this.htmlEncode( text );\r
- },\r
-\r
- /**\r
- * Replace special HTML characters in HTMLElement's attribute with their relative HTML entity values.\r
- * @param {String} The attribute's value to be encoded.\r
- * @returns {String} The encode value.\r
- * @example\r
- * element.setAttribute( 'title', '<a " b >' );\r
- * alert( CKEDITOR.tools.htmlEncodeAttr( element.getAttribute( 'title' ) ); // ">a " b <"\r
- */\r
- htmlEncodeAttr : function( text )\r
- {\r
- return text.replace( /"/g, '"' ).replace( /</g, '<' ).replace( />/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
- * @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
- * 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
- * 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
- /**\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
- },\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
- * @param {Object} definition The class definition object.\r
- * @returns {Function} A class-like JavaScript function.\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
- /**\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
- {\r
- return fn.apply( scope || this, arguments );\r
- }) - 1;\r
- },\r
-\r
- /**\r
- * Removes the function reference created with {@see CKEDITOR.tools.addFunction}.\r
- * @param {Number} ref The function reference created with\r
- * CKEDITOR.tools.addFunction.\r
- */\r
- removeFunction : function( ref )\r
- {\r
- functions[ ref ] = null;\r
- },\r
-\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[ ref ];\r
- 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
- return function( length )\r
- {\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
- 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
- * 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
-\r
-// PACKAGER_RENAME( CKEDITOR.tools )\r