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. |
|
Line 99... | Line 79... | |||
99 | attrs = elem && elem.attributes; |
79 | attrs = elem && elem.attributes; |
|
Line 100... | Line 80... | |||
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 ) { |
|
Line 104... | Line 84... | |||
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" ) ) { |
|
Line 107... | Line 87... | |||
107 | i = attrs.length; |
87 | i = attrs.length; |
|
108 | while ( i-- ) { |
88 | while ( i-- ) { |
|
109 | |
89 | |
|
110 | // Support: IE 11 only |
90 | // Support: IE11+ |
|
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 ] ); |
|
117 | } |
97 | } |
|
118 | } |
98 | } |
|
119 | } |
99 | } |
|
Line 120... | Line 100... | |||
120 | dataPriv.set( elem, "hasDataAttrs", true ); |
100 | data_priv.set( elem, "hasDataAttrs", true ); |
|
121 | } |
101 | } |
|
Line 122... | Line 102... | |||
122 | } |
102 | } |
|
123 | |
103 | |
|
124 | return data; |
104 | return data; |
|
125 | } |
105 | } |
|
126 | |
106 | |
|
127 | // Sets multiple values |
107 | // Sets multiple values |
|
Line 128... | Line 108... | |||
128 | if ( typeof key === "object" ) { |
108 | if ( typeof key === "object" ) { |
|
129 | return this.each( function() { |
109 | return this.each(function() { |
|
- | 110 | data_user.set( this, key ); |
||
Line 130... | Line 111... | |||
130 | dataUser.set( this, key ); |
111 | }); |
|
131 | } ); |
112 | } |
|
132 | } |
113 | |
|
133 | |
114 | return access( this, function( value ) { |
|
134 | return access( this, function( value ) { |
115 | var data, |
|
135 | var data; |
116 | camelKey = jQuery.camelCase( key ); |
|
- | 117 | |
||
- | 118 | // The calling jQuery object (element matches) is not empty |
||
- | 119 | // (and therefore has an element appears at this[ 0 ]) and the |
||
- | 120 | // `value` parameter was not undefined. An empty jQuery object |
||
- | 121 | // will result in `undefined` for elem = this[ 0 ] which will |
||
- | 122 | // throw an exception if an attempt to read a data cache is made. |
||
Line 136... | Line 123... | |||
136 | |
123 | if ( elem && value === undefined ) { |
|
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 ); |
|
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; |
|
Line 142... | Line 129... | |||
142 | if ( elem && value === undefined ) { |
129 | } |
|
143 | |
130 | |
|
144 | // Attempt to get data from the cache |
131 | // Attempt to get data from the cache |
|
145 | // The key will always be camelCased in Data |
132 | // with the key camelized |
|
146 | data = dataUser.get( elem, key ); |
133 | data = data_user.get( elem, camelKey ); |
|
147 | if ( data !== undefined ) { |
134 | if ( data !== undefined ) { |
|
Line 148... | Line 135... | |||
148 | return data; |
135 | return data; |
|
149 | } |
136 | } |
|
150 | |
137 | |
|
Line 151... | Line 138... | |||
151 | // Attempt to "discover" the data in |
138 | // Attempt to "discover" the data in |
|
152 | // HTML5 custom data-* attrs |
139 | // HTML5 custom data-* attrs |
|
- | 140 | data = dataAttr( elem, camelKey, undefined ); |
||
- | 141 | if ( data !== undefined ) { |
||
- | 142 | return data; |
||
153 | data = dataAttr( elem, key ); |
143 | } |
|
- | 144 | |
||
154 | if ( data !== undefined ) { |
145 | // We tried really hard, but the data doesn't exist. |
|
- | 146 | return; |
||
- | 147 | } |
||
- | 148 | |
||
- | 149 | // Set the data... |
||
- | 150 | this.each(function() { |
||
- | 151 | // First, attempt to store a copy or reference of any |
||
- | 152 | // data that might've been store with a camelCased key. |
||
155 | return data; |
153 | var data = data_user.get( this, camelKey ); |
|
- | 154 | |
||
156 | } |
155 | // For HTML5 data-* attribute interop, we have to |
|
157 | |
156 | // store property names with dashes in a camelCase form. |
|
158 | // We tried really hard, but the data doesn't exist. |
157 | // This might not apply to all properties...* |
|
Line 159... | Line 158... | |||
159 | return; |
158 | data_user.set( this, camelKey, value ); |
|
160 | } |
159 | |
|
161 | |
160 | // *... In the case of properties that might _actually_ |
|
162 | // Set the data... |
161 | // have dashes, we need to also store a copy of that |
|
163 | this.each( function() { |
162 | // unchanged property. |
|
164 | |
163 | if ( key.indexOf("-") !== -1 && data !== undefined ) { |
|
Line 165... | Line 164... | |||
165 | // We always store the camelCased key |
164 | data_user.set( this, key, value ); |