1 /* 2 * CKEditor - The text editor for Internet - http://ckeditor.com 3 * Copyright (C) 2003-2008 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 */ 21 22 /** 23 * Utility functions. 24 * @namespace 25 * @example 26 */ 27 CKEDITOR.tools = 28 { 29 /** 30 * Evaluates a script in a window (global) scope. 31 * @param {String} script The script code to be evaluated. 32 * @param {Object} [win] The target window. Defaults to the current window. 33 * @type undefined 34 * @example 35 * var script = 'function sample() { alert( "Go!" ); }'; 36 * 37 * // Evaluates a script in the current window. 38 * CKEDITOR.tools.globalEval( script ); 39 * 40 * // Evaluates a script in the parent window. 41 * CKEDITOR.tools.globalEval( script, window.parent ); 42 * 43 * // Alerts "Go!" twice. 44 * window.sample(); 45 * window.parent.sample(); 46 */ 47 globalEval : function( script, win ) 48 { 49 if ( !win ) 50 win = window; 51 52 if ( win.execScript ) 53 win.execScript( script ); // IE 54 else if ( CKEDITOR.env.webkit ) 55 { 56 // win.eval in Safari executes in the current window environment, 57 // instead of win. The following should make it work. 58 var doc = win.document; 59 var scriptEl = doc.createElement('script'); 60 scriptEl.appendChild( doc.createTextNode( script ) ); 61 doc.documentElement.appendChild( scriptEl ); 62 } 63 else 64 eval.call( win, script ); 65 }, 66 67 /** 68 * Copy the properties from one object to another. By default, properties 69 * already present in the target object <strong>are not</strong> overwritten. 70 * @param {Object} target The object to be extended. 71 * @param {Object} source The object from which copy properties. 72 * @param {Boolean} [overwrite] Indicates that properties already present 73 * in the target object must be overwritten. 74 * @returns {Object} the extended object (target). 75 * @example 76 * // Create the sample object. 77 * var myObject = 78 * { 79 * prop1 : true 80 * }; 81 * 82 * // Extend the above object with two properties. 83 * CKEDITOR.tools.extend( myObject, 84 * { 85 * prop2 : true, 86 * prop3 : true 87 * } ); 88 * 89 * // Alert "prop1", "prop2" and "prop3". 90 * for ( var p in myObject ) 91 * alert( p ) ; 92 */ 93 extend : function( target, source, overwrite ) 94 { 95 this.each( source, function( value, propertyName ) 96 { 97 if ( overwrite || target[propertyName] == undefined ) 98 target[propertyName] = value; 99 } ); 100 return target; 101 }, 102 103 // Call a function for each element present in an object. 104 each : function( object, callback, scope ) 105 { 106 if ( !scope ) 107 scope = object; 108 109 var length = object.length; 110 111 if ( length == undefined ) 112 { 113 for ( var name in object ) 114 { 115 if ( callback.call( scope, object[ name ], name ) === false ) 116 break; 117 } 118 } 119 else 120 { 121 for ( var value, i = 0 ; 122 i < length && ( ( value = object[i] ) || 1) && callback.call( scope, value, i ) !== false ; i++ ) 123 {} 124 } 125 } 126 }; 127