scratch – Diff between revs 58 and 125
?pathlinks?
Rev 58 | Rev 125 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
1 | define( [ |
1 | define([ |
|
2 | "./core", |
2 | "./core", |
|
- | 3 | "./var/rnotwhite", |
||
3 | "./core/access", |
4 | "./core/access", |
|
4 | "./data/var/dataPriv", |
5 | "./data/var/data_priv", |
|
5 | "./data/var/dataUser" |
6 | "./data/var/data_user" |
|
6 | ], function( jQuery, access, dataPriv, dataUser ) { |
7 | ], function( jQuery, rnotwhite, access, data_priv, data_user ) { |
|
7 | |
- | ||
8 | "use strict"; |
- | ||
Line 9... | Line 8... | |||
9 | |
8 | |
|
10 | // Implementation Summary |
9 | // Implementation Summary |
|
11 | // |
10 | // |
|
12 | // 1. Enforce API surface and semantic compatibility with 1.9.x branch |
11 | // 1. Enforce API surface and semantic compatibility with 1.9.x branch |
|
Line 16... | Line 15... | |||
16 | // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData) |
15 | // 4. _Never_ expose "private" data to user code (TODO: Drop _data, _removeData) |
|
17 | // 5. Avoid exposing implementation details on user objects (eg. expando properties) |
16 | // 5. Avoid exposing implementation details on user objects (eg. expando properties) |
|
18 | // 6. Provide a clear path for implementation upgrade to WeakMap in 2014 |
17 | // 6. Provide a clear path for implementation upgrade to WeakMap in 2014 |
|
Line 19... | Line 18... | |||
19 | |
18 | |
|
20 | var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/, |
19 | var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/, |
|
21 | rmultiDash = /[A-Z]/g; |
- | ||
22 | |
- | ||
23 | function getData( data ) { |
- | ||
24 | if ( data === "true" ) { |
- | ||
25 | return true; |
- | ||
26 | } |
- | ||
27 | |
- | ||
28 | if ( data === "false" ) { |
- | ||
29 | return false; |
- | ||
30 | } |
- | ||
31 | |
- | ||
32 | if ( data === "null" ) { |
- | ||
33 | return null; |
- | ||
34 | } |
- | ||
35 | |
- | ||
36 | // Only convert to a number if it doesn't change the string |
- | ||
37 | if ( data === +data + "" ) { |
- | ||
38 | return +data; |
- | ||
39 | } |
- | ||
40 | |
- | ||
41 | if ( rbrace.test( data ) ) { |
- | ||
42 | return JSON.parse( data ); |
- | ||
43 | } |
- | ||
44 | |
- | ||
45 | return data; |
- | ||
Line 46... | Line 20... | |||
46 | } |
20 | rmultiDash = /([A-Z])/g; |
|
47 | |
21 | |
|
Line 48... | Line 22... | |||
48 | function dataAttr( elem, key, data ) { |
22 | function dataAttr( elem, key, data ) { |
|
49 | var name; |
23 | var name; |
|
50 | |
24 | |
|
51 | // If nothing was found internally, try to fetch any |
25 | // If nothing was found internally, try to fetch any |
|
52 | // data from the HTML5 data-* attribute |
26 | // data from the HTML5 data-* attribute |
|
Line 53... | Line 27... | |||
53 | if ( data === undefined && elem.nodeType === 1 ) { |
27 | if ( data === undefined && elem.nodeType === 1 ) { |
|
54 | name = "data-" + key.replace( rmultiDash, "-$&" ).toLowerCase(); |
28 | name = "data-" + key.replace( rmultiDash, "-$1" ).toLowerCase(); |
|
- | 29 | data = elem.getAttribute( name ); |
||
- | 30 | |
||
- | 31 | if ( typeof data === "string" ) { |
||
- | 32 | try { |
||
55 | data = elem.getAttribute( name ); |
33 | data = data === "true" ? true : |
|
- | 34 | data === "false" ? false : |
||
- | 35 | data === "null" ? null : |
||
56 | |
36 | // Only convert to a number if it doesn't change the string |
|
Line 57... | Line 37... | |||
57 | if ( typeof data === "string" ) { |
37 | +data + "" === data ? +data : |
|
58 | try { |
38 | rbrace.test( data ) ? jQuery.parseJSON( data ) : |
|
59 | data = getData( data ); |
39 | data; |
|
60 | } catch ( e ) {} |
40 | } catch( e ) {} |
|
61 | |
41 | |
|
62 | // Make sure we set the data so it isn't changed later |
42 | // Make sure we set the data so it isn't changed later |
|
63 | dataUser.set( elem, key, data ); |
43 | data_user.set( elem, key, data ); |
|
64 | } else { |
44 | } else { |
|
Line 65... | Line 45... | |||
65 | data = undefined; |
45 | data = undefined; |
|
66 | } |
46 | } |
|
67 | } |
47 | } |
|
68 | return data; |
48 | return data; |
|
Line 69... | Line 49... | |||
69 | } |
49 | } |
|
70 | |
50 | |
|
71 | jQuery.extend( { |
51 | jQuery.extend({ |
|
Line 72... | Line 52... | |||
72 | hasData: function( elem ) { |
52 | hasData: function( elem ) { |
|
73 | return dataUser.hasData( elem ) || dataPriv.hasData( elem ); |
53 | return data_user.hasData( elem ) || data_priv.hasData( elem ); |
|
74 | }, |
54 | }, |
|
Line 75... | Line 55... | |||
75 | |
55 | |
|
76 | data: function( elem, name, data ) { |
56 | data: function( elem, name, data ) { |
|
77 | return dataUser.access( elem, name, data ); |
57 | return data_user.access( elem, name, data ); |
|
78 | }, |
58 | }, |
|
79 | |
59 | |
|
Line 80... | Line 60... | |||
80 | removeData: function( elem, name ) { |
60 | removeData: function( elem, name ) { |
|
81 | dataUser.remove( elem, name ); |
61 | data_user.remove( elem, name ); |
|
82 | }, |
62 | }, |
|
83 | |
63 | |
|
Line 84... | Line 64... | |||
84 | // TODO: Now that all calls to _data and _removeData have been replaced |
64 | // TODO: Now that all calls to _data and _removeData have been replaced |
|
85 | // with direct calls to dataPriv methods, these can be deprecated. |
65 | // with direct calls to data_priv methods, these can be deprecated. |
|
86 | _data: function( elem, name, data ) { |
66 | _data: function( elem, name, data ) { |
|
87 | return dataPriv.access( elem, name, data ); |
67 | return data_priv.access( elem, name, data ); |
|
88 | }, |
68 | }, |
|
Line 89... | Line 69... | |||
89 | |
69 | |
|
90 | _removeData: function( elem, name ) { |
70 | _removeData: function( elem, name ) { |
|
91 | dataPriv.remove( elem, name ); |
71 | data_priv.remove( elem, name ); |
|
92 | } |
72 | } |
|
Line 93... | Line 73... | |||
93 | } ); |
73 | }); |
|
94 | |
74 | |
|
95 | jQuery.fn.extend( { |
75 | jQuery.fn.extend({ |
|
Line 96... | Line 76... | |||
96 | data: function( key, value ) { |
76 | data: function( key, value ) { |
|
97 | var i, name, data, |
77 | var i, name, data, |
|
98 | elem = this[ 0 ], |
78 | elem = this[ 0 ], |
|
99 | attrs = elem && elem.attributes; |
79 | attrs = elem && elem.attributes; |
|
100 | |
80 | |
|
101 | // Gets all values |
81 | // Gets all values |
|
102 | if ( key === undefined ) { |
82 | if ( key === undefined ) { |
|
103 | if ( this.length ) { |
83 | if ( this.length ) { |
|
104 | data = dataUser.get( elem ); |
84 | data = data_user.get( elem ); |
|
105 | |
85 | |
|
106 | if ( elem.nodeType === 1 && !dataPriv.get( elem, "hasDataAttrs" ) ) { |
86 | if ( elem.nodeType === 1 && !data_priv.get( elem, "hasDataAttrs" ) ) { |
|
107 | i = attrs.length; |
87 | i = attrs.length; |
|
108 | while ( i-- ) { |
88 | while ( i-- ) { |
|
Line 109... | Line 89... | |||
109 | |
89 | |
|
110 | // Support: IE 11 only |
90 | // Support: IE11+ |
|
Line 111... | Line 91... | |||
111 | // The attrs elements can be null (#14894) |
91 | // The attrs elements can be null (#14894) |
|
112 | if ( attrs[ i ] ) { |
92 | if ( attrs[ i ] ) { |
|
113 | name = attrs[ i ].name; |
93 | name = attrs[ i ].name; |
|
114 | if ( name.indexOf( "data-" ) === 0 ) { |
94 | if ( name.indexOf( "data-" ) === 0 ) { |
|
115 | name = jQuery.camelCase( name.slice( 5 ) ); |
95 | name = jQuery.camelCase( name.slice(5) ); |
|
116 | dataAttr( elem, name, data[ name ] ); |
96 | dataAttr( elem, name, data[ name ] ); |
|
Line 117... | Line 97... | |||
117 | } |
97 | } |
|
118 | } |
98 | } |
|
- | 99 | } |
||
Line 119... | Line 100... | |||
119 | } |
100 | data_priv.set( elem, "hasDataAttrs", true ); |
|
120 | dataPriv.set( elem, "hasDataAttrs", true ); |
101 | } |
|
121 | } |
102 | } |
|
122 | } |
103 | |
|
123 | |
104 | return data; |
|
124 | return data; |
105 | } |
|
- | 106 | |
||
- | 107 | // Sets multiple values |
||
- | 108 | if ( typeof key === "object" ) { |
||
- | 109 | return this.each(function() { |
||
- | 110 | data_user.set( this, key ); |
||
- | 111 | }); |
||
Line 125... | Line 112... | |||
125 | } |
112 | } |
|
126 | |
113 | |
|
127 | // Sets multiple values |
114 | return access( this, function( value ) { |
|
128 | if ( typeof key === "object" ) { |
115 | var data, |
|
129 | return this.each( function() { |
116 | camelKey = jQuery.camelCase( key ); |
|
130 | dataUser.set( this, key ); |
117 | |
|
Line 131... | Line 118... | |||
131 | } ); |
118 | // The calling jQuery object (element matches) is not empty |
|
132 | } |
119 | // (and therefore has an element appears at this[ 0 ]) and the |
|
133 | |
120 | // `value` parameter was not undefined. An empty jQuery object |
|
134 | return access( this, function( value ) { |
121 | // will result in `undefined` for elem = this[ 0 ] which will |
|
135 | var data; |
122 | // throw an exception if an attempt to read a data cache is made. |
|
136 | |
123 | if ( elem && value === undefined ) { |
|
Line 137... | Line 124... | |||
137 | // The calling jQuery object (element matches) is not empty |
124 | // Attempt to get data from the cache |
|
138 | // (and therefore has an element appears at this[ 0 ]) and the |
125 | // with the key as-is |
|
139 | // `value` parameter was not undefined. An empty jQuery object |
126 | data = data_user.get( elem, key ); |
|
Line 140... | Line 127... | |||
140 | // will result in `undefined` for elem = this[ 0 ] which will |
127 | if ( data !== undefined ) { |
|
141 | // throw an exception if an attempt to read a data cache is made. |
128 | return data; |
|
- | 129 | } |
||
- | 130 | |
||
- | 131 | // Attempt to get data from the cache |
||
142 | if ( elem && value === undefined ) { |
132 | // with the key camelized |
|
- | 133 | data = data_user.get( elem, camelKey ); |
||
143 | |
134 | if ( data !== undefined ) { |
|
- | 135 | return data; |
||
- | 136 | } |
||
- | 137 | |
||
- | 138 | // Attempt to "discover" the data in |
||
- | 139 | // HTML5 custom data-* attrs |
||
- | 140 | data = dataAttr( elem, camelKey, undefined ); |
||
- | 141 | if ( data !== undefined ) { |
||
144 | // Attempt to get data from the cache |
142 | return data; |
|
- | 143 | } |
||
145 | // The key will always be camelCased in Data |
144 | |
|
146 | data = dataUser.get( elem, key ); |
145 | // We tried really hard, but the data doesn't exist. |
|
147 | if ( data !== undefined ) { |
146 | return; |
|
Line 148... | Line 147... | |||
148 | return data; |
147 | } |
|
149 | } |
148 | |
|
150 | |
149 | // Set the data... |
|
151 | // Attempt to "discover" the data in |
150 | this.each(function() { |
|
152 | // HTML5 custom data-* attrs |
151 | // First, attempt to store a copy or reference of any |
|
153 | data = dataAttr( elem, key ); |
152 | // data that might've been store with a camelCased key. |
|
Line 154... | Line 153... | |||
154 | if ( data !== undefined ) { |
153 | var data = data_user.get( this, camelKey ); |
|
155 | return data; |
154 | |