1 /*!
  2 * This file is part of Aloha Editor Project http://aloha-editor.org
  3 * Copyright © 2010-2011 Gentics Software GmbH, aloha@gentics.com
  4 * Contributors http://aloha-editor.org/contribution.php 
  5 * Licensed unter the terms of http://www.aloha-editor.org/license.html
  6 *//*
  7 * Aloha Editor is free software: you can redistribute it and/or modify
  8 * it under the terms of the GNU Affero General Public License as published by
  9 * the Free Software Foundation, either version 3 of the License, or
 10 * (at your option) any later version.*
 11 *
 12 * Aloha Editor is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 * GNU Affero General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Affero General Public License
 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20 
 21 // use specified jQuery or load jQuery
 22 
 23 define(
 24 [ 'aloha/jquery' ],
 25 function( jQuery ) {
 26 
 27 	//PATCH FOR A JQUERY BUG IN 1.6.1 & 1.6.2
 28 	//An additional sanity check was introduced to prevent IE from crashing when cache[id] does not exist
 29 	jQuery.data = ( function( jQuery ) {
 30 		return function( elem, name, data, pvt /* Internal Use Only */ ) {
 31 			if ( !jQuery.acceptData( elem ) ) {
 32 				return;
 33 			}
 34 			
 35 			var internalKey = jQuery.expando, getByName = typeof name === "string", thisCache,
 36 
 37 				// We have to handle DOM nodes and JS objects differently because IE6-7
 38 				// can't GC object references properly across the DOM-JS boundary
 39 				isNode = elem.nodeType,
 40 
 41 				// Only DOM nodes need the global jQuery cache; JS object data is
 42 				// attached directly to the object so GC can occur automatically
 43 				cache = isNode ? jQuery.cache : elem,
 44 
 45 				// Only defining an ID for JS objects if its cache already exists allows
 46 				// the code to shortcut on the same path as a DOM node with no cache
 47 				id = isNode ? elem[ jQuery.expando ] : elem[ jQuery.expando ] && jQuery.expando;
 48 
 49 			// Avoid doing any more work than we need to when trying to get data on an
 50 			// object that has no data at all
 51 			//if ( (!id || (pvt && id && !cache[ id ][ internalKey ])) && getByName && data === undefined ) {
 52 			if ( (!id || (pvt && id && (!cache[id] || !cache[ id ][ internalKey ]))) && getByName && data === undefined ) {
 53 				return;
 54 			}
 55 
 56 			if ( !id ) {
 57 				// Only DOM nodes need a new unique ID for each element since their data
 58 				// ends up in the global cache
 59 				if ( isNode ) {
 60 					elem[ jQuery.expando ] = id = ++jQuery.uuid;
 61 				} else {
 62 					id = jQuery.expando;
 63 				}
 64 			}
 65 
 66 			if ( !cache[ id ] ) {
 67 				cache[ id ] = {};
 68 
 69 				// TODO: This is a hack for 1.5 ONLY. Avoids exposing jQuery
 70 				// metadata on plain JS objects when the object is serialized using
 71 				// JSON.stringify
 72 				if ( !isNode ) {
 73 					cache[ id ].toJSON = jQuery.noop;
 74 				}
 75 			}
 76 
 77 			// An object can be passed to jQuery.data instead of a key/value pair; this gets
 78 			// shallow copied over onto the existing cache
 79 			if ( typeof name === "object" || typeof name === "function" ) {
 80 				if ( pvt ) {
 81 					cache[ id ][ internalKey ] = jQuery.extend(cache[ id ][ internalKey ], name);
 82 				} else {
 83 					cache[ id ] = jQuery.extend(cache[ id ], name);
 84 				}
 85 			}
 86 
 87 			thisCache = cache[ id ];
 88 
 89 			// Internal jQuery data is stored in a separate object inside the object's data
 90 			// cache in order to avoid key collisions between internal data and user-defined
 91 			// data
 92 			if ( pvt ) {
 93 				if ( !thisCache[ internalKey ] ) {
 94 					thisCache[ internalKey ] = {};
 95 				}
 96 
 97 				thisCache = thisCache[ internalKey ];
 98 			}
 99 
100 			if ( data !== undefined ) {
101 				thisCache[ jQuery.camelCase( name ) ] = data;
102 			}
103 
104 			// TODO: This is a hack for 1.5 ONLY. It will be removed in 1.6. Users should
105 			// not attempt to inspect the internal events object using jQuery.data, as this
106 			// internal data object is undocumented and subject to change.
107 			if ( name === "events" && !thisCache[name] ) {
108 				return thisCache[ internalKey ] && thisCache[ internalKey ].events;
109 			}
110 
111 			return getByName ? thisCache[ jQuery.camelCase( name ) ] : thisCache;
112 		};
113 	})( jQuery );
114 
115 });
116