sp-codes.de/bower_components/jquery/src/data/Data.js

162 lines
3.9 KiB
JavaScript
Raw Normal View History

2016-10-28 17:33:25 +00:00
define( [
"../core",
2016-11-22 23:07:57 +00:00
"../var/rnothtmlwhite",
2016-10-28 17:33:25 +00:00
"./var/acceptData"
2016-11-22 23:07:57 +00:00
], function( jQuery, rnothtmlwhite, acceptData ) {
"use strict";
2016-10-28 17:33:25 +00:00
function Data() {
this.expando = jQuery.expando + Data.uid++;
}
Data.uid = 1;
Data.prototype = {
cache: function( owner ) {
// Check if the owner object already has a cache
var value = owner[ this.expando ];
// If not, create one
if ( !value ) {
value = {};
// We can accept data for non-element nodes in modern browsers,
// but we should not, see #8335.
// Always return an empty object.
if ( acceptData( owner ) ) {
// If it is a node unlikely to be stringify-ed or looped over
// use plain assignment
if ( owner.nodeType ) {
owner[ this.expando ] = value;
// Otherwise secure it in a non-enumerable property
// configurable must be true to allow the property to be
// deleted when data is removed
} else {
Object.defineProperty( owner, this.expando, {
value: value,
configurable: true
} );
}
}
}
return value;
},
set: function( owner, data, value ) {
var prop,
cache = this.cache( owner );
// Handle: [ owner, key, value ] args
2016-11-22 23:07:57 +00:00
// Always use camelCase key (gh-2257)
2016-10-28 17:33:25 +00:00
if ( typeof data === "string" ) {
2016-11-22 23:07:57 +00:00
cache[ jQuery.camelCase( data ) ] = value;
2016-10-28 17:33:25 +00:00
// Handle: [ owner, { properties } ] args
} else {
// Copy the properties one-by-one to the cache object
for ( prop in data ) {
2016-11-22 23:07:57 +00:00
cache[ jQuery.camelCase( prop ) ] = data[ prop ];
2016-10-28 17:33:25 +00:00
}
}
return cache;
},
get: function( owner, key ) {
return key === undefined ?
this.cache( owner ) :
2016-11-22 23:07:57 +00:00
// Always use camelCase key (gh-2257)
owner[ this.expando ] && owner[ this.expando ][ jQuery.camelCase( key ) ];
2016-10-28 17:33:25 +00:00
},
access: function( owner, key, value ) {
// In cases where either:
//
// 1. No key was specified
// 2. A string key was specified, but no value provided
//
// Take the "read" path and allow the get method to determine
// which value to return, respectively either:
//
// 1. The entire cache object
// 2. The data stored at the key
//
if ( key === undefined ||
( ( key && typeof key === "string" ) && value === undefined ) ) {
2016-11-22 23:07:57 +00:00
return this.get( owner, key );
2016-10-28 17:33:25 +00:00
}
// When the key is not a string, or both a key and value
// are specified, set or extend (existing objects) with either:
//
// 1. An object of properties
// 2. A key and value
//
this.set( owner, key, value );
// Since the "set" path can have two possible entry points
// return the expected data based on which path was taken[*]
return value !== undefined ? value : key;
},
remove: function( owner, key ) {
2016-11-22 23:07:57 +00:00
var i,
2016-10-28 17:33:25 +00:00
cache = owner[ this.expando ];
if ( cache === undefined ) {
return;
}
2016-11-22 23:07:57 +00:00
if ( key !== undefined ) {
2016-10-28 17:33:25 +00:00
// Support array or space separated string of keys
if ( jQuery.isArray( key ) ) {
2016-11-22 23:07:57 +00:00
// If key is an array of keys...
// We always set camelCase keys, so remove that.
key = key.map( jQuery.camelCase );
2016-10-28 17:33:25 +00:00
} else {
2016-11-22 23:07:57 +00:00
key = jQuery.camelCase( key );
2016-10-28 17:33:25 +00:00
2016-11-22 23:07:57 +00:00
// If a key with the spaces exists, use it.
// Otherwise, create an array by matching non-whitespace
key = key in cache ?
[ key ] :
( key.match( rnothtmlwhite ) || [] );
2016-10-28 17:33:25 +00:00
}
2016-11-22 23:07:57 +00:00
i = key.length;
2016-10-28 17:33:25 +00:00
while ( i-- ) {
2016-11-22 23:07:57 +00:00
delete cache[ key[ i ] ];
2016-10-28 17:33:25 +00:00
}
}
// Remove the expando if there's no more data
if ( key === undefined || jQuery.isEmptyObject( cache ) ) {
2016-11-22 23:07:57 +00:00
// Support: Chrome <=35 - 45
2016-10-28 17:33:25 +00:00
// Webkit & Blink performance suffers when deleting properties
// from DOM nodes, so set to undefined instead
2016-11-22 23:07:57 +00:00
// https://bugs.chromium.org/p/chromium/issues/detail?id=378607 (bug restricted)
2016-10-28 17:33:25 +00:00
if ( owner.nodeType ) {
owner[ this.expando ] = undefined;
} else {
delete owner[ this.expando ];
}
}
},
hasData: function( owner ) {
var cache = owner[ this.expando ];
return cache !== undefined && !jQuery.isEmptyObject( cache );
}
};
return Data;
} );