scratch – Diff between revs 58 and 125

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 58 Rev 125
1 define( [ 1 define([
2 "../core", 2 "../core",
3 "../core/stripAndCollapse", 3 "../var/rnotwhite",
4 "../var/rnothtmlwhite", 4 "../var/strundefined",
5 "../data/var/dataPriv", 5 "../data/var/data_priv",
6 "../core/init" 6 "../core/init"
7 ], function( jQuery, stripAndCollapse, rnothtmlwhite, dataPriv ) { 7 ], function( jQuery, rnotwhite, strundefined, data_priv ) {
8   8  
9 "use strict"; -  
10   -  
11 function getClass( elem ) { -  
12 return elem.getAttribute && elem.getAttribute( "class" ) || ""; -  
13 } 9 var rclass = /[\t\r\n\f]/g;
14   10  
15 jQuery.fn.extend( { 11 jQuery.fn.extend({
-   12 addClass: function( value ) {
16 addClass: function( value ) { 13 var classes, elem, cur, clazz, j, finalValue,
-   14 proceed = typeof value === "string" && value,
17 var classes, elem, cur, curValue, clazz, j, finalValue, 15 i = 0,
18 i = 0; 16 len = this.length;
19   17  
20 if ( jQuery.isFunction( value ) ) { 18 if ( jQuery.isFunction( value ) ) {
21 return this.each( function( j ) { 19 return this.each(function( j ) {
22 jQuery( this ).addClass( value.call( this, j, getClass( this ) ) ); 20 jQuery( this ).addClass( value.call( this, j, this.className ) );
-   21 });
23 } ); 22 }
24 } 23  
25   24 if ( proceed ) {
26 if ( typeof value === "string" && value ) { 25 // The disjunction here is for better compressibility (see removeClass)
27 classes = value.match( rnothtmlwhite ) || []; 26 classes = ( value || "" ).match( rnotwhite ) || [];
-   27  
-   28 for ( ; i < len; i++ ) {
-   29 elem = this[ i ];
28   30 cur = elem.nodeType === 1 && ( elem.className ?
29 while ( ( elem = this[ i++ ] ) ) { 31 ( " " + elem.className + " " ).replace( rclass, " " ) :
30 curValue = getClass( elem ); 32 " "
31 cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " ); 33 );
32   34  
33 if ( cur ) { 35 if ( cur ) {
34 j = 0; 36 j = 0;
35 while ( ( clazz = classes[ j++ ] ) ) { 37 while ( (clazz = classes[j++]) ) {
36 if ( cur.indexOf( " " + clazz + " " ) < 0 ) { 38 if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
37 cur += clazz + " "; 39 cur += clazz + " ";
38 } 40 }
39 } 41 }
40   42  
41 // Only assign if different to avoid unneeded rendering. 43 // only assign if different to avoid unneeded rendering.
42 finalValue = stripAndCollapse( cur ); 44 finalValue = jQuery.trim( cur );
43 if ( curValue !== finalValue ) { 45 if ( elem.className !== finalValue ) {
44 elem.setAttribute( "class", finalValue ); 46 elem.className = finalValue;
45 } 47 }
46 } 48 }
47 } 49 }
48 } 50 }
49   51  
50 return this; 52 return this;
51 }, 53 },
52   54  
53 removeClass: function( value ) { 55 removeClass: function( value ) {
54 var classes, elem, cur, curValue, clazz, j, finalValue, 56 var classes, elem, cur, clazz, j, finalValue,
-   57 proceed = arguments.length === 0 || typeof value === "string" && value,
55 i = 0; 58 i = 0,
-   59 len = this.length;
56   60  
57 if ( jQuery.isFunction( value ) ) { 61 if ( jQuery.isFunction( value ) ) {
58 return this.each( function( j ) { 62 return this.each(function( j ) {
59 jQuery( this ).removeClass( value.call( this, j, getClass( this ) ) ); 63 jQuery( this ).removeClass( value.call( this, j, this.className ) );
60 } ); 64 });
-   65 }
-   66 if ( proceed ) {
61 } 67 classes = ( value || "" ).match( rnotwhite ) || [];
62   -  
63 if ( !arguments.length ) { -  
64 return this.attr( "class", "" ); -  
65 } -  
66   -  
67 if ( typeof value === "string" && value ) { -  
68 classes = value.match( rnothtmlwhite ) || []; 68  
69   -  
70 while ( ( elem = this[ i++ ] ) ) { -  
71 curValue = getClass( elem ); 69 for ( ; i < len; i++ ) {
72   70 elem = this[ i ];
-   71 // This expression is here for better compressibility (see addClass)
-   72 cur = elem.nodeType === 1 && ( elem.className ?
-   73 ( " " + elem.className + " " ).replace( rclass, " " ) :
73 // This expression is here for better compressibility (see addClass) 74 ""
74 cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " ); 75 );
75   76  
76 if ( cur ) { 77 if ( cur ) {
77 j = 0; 78 j = 0;
78 while ( ( clazz = classes[ j++ ] ) ) { 79 while ( (clazz = classes[j++]) ) {
79   -  
80 // Remove *all* instances 80 // Remove *all* instances
81 while ( cur.indexOf( " " + clazz + " " ) > -1 ) { 81 while ( cur.indexOf( " " + clazz + " " ) >= 0 ) {
82 cur = cur.replace( " " + clazz + " ", " " ); 82 cur = cur.replace( " " + clazz + " ", " " );
83 } 83 }
84 } 84 }
85   85  
86 // Only assign if different to avoid unneeded rendering. 86 // Only assign if different to avoid unneeded rendering.
87 finalValue = stripAndCollapse( cur ); 87 finalValue = value ? jQuery.trim( cur ) : "";
88 if ( curValue !== finalValue ) { 88 if ( elem.className !== finalValue ) {
89 elem.setAttribute( "class", finalValue ); 89 elem.className = finalValue;
90 } 90 }
91 } 91 }
92 } 92 }
93 } 93 }
94   94  
95 return this; 95 return this;
96 }, 96 },
97   97  
98 toggleClass: function( value, stateVal ) { 98 toggleClass: function( value, stateVal ) {
99 var type = typeof value; 99 var type = typeof value;
100   100  
101 if ( typeof stateVal === "boolean" && type === "string" ) { 101 if ( typeof stateVal === "boolean" && type === "string" ) {
102 return stateVal ? this.addClass( value ) : this.removeClass( value ); 102 return stateVal ? this.addClass( value ) : this.removeClass( value );
103 } 103 }
104   104  
105 if ( jQuery.isFunction( value ) ) { 105 if ( jQuery.isFunction( value ) ) {
106 return this.each( function( i ) { 106 return this.each(function( i ) {
107 jQuery( this ).toggleClass( -  
108 value.call( this, i, getClass( this ), stateVal ), 107 jQuery( this ).toggleClass( value.call(this, i, this.className, stateVal), stateVal );
109 stateVal -  
110 ); -  
111 } ); 108 });
112 } 109 }
113   110  
114 return this.each( function() { -  
115 var className, i, self, classNames; -  
116   111 return this.each(function() {
117 if ( type === "string" ) { -  
118   112 if ( type === "string" ) {
-   113 // Toggle individual class names
119 // Toggle individual class names 114 var className,
120 i = 0; 115 i = 0,
121 self = jQuery( this ); 116 self = jQuery( this ),
122 classNames = value.match( rnothtmlwhite ) || []; -  
123   -  
-   117 classNames = value.match( rnotwhite ) || [];
124 while ( ( className = classNames[ i++ ] ) ) { 118  
125   119 while ( (className = classNames[ i++ ]) ) {
126 // Check each className given, space separated list 120 // Check each className given, space separated list
127 if ( self.hasClass( className ) ) { 121 if ( self.hasClass( className ) ) {
128 self.removeClass( className ); 122 self.removeClass( className );
129 } else { 123 } else {
130 self.addClass( className ); 124 self.addClass( className );
131 } 125 }
132 } 126 }
133   127  
134 // Toggle whole class name 128 // Toggle whole class name
135 } else if ( value === undefined || type === "boolean" ) { 129 } else if ( type === strundefined || type === "boolean" ) {
136 className = getClass( this ); -  
137 if ( className ) { 130 if ( this.className ) {
138   -  
139 // Store className if set 131 // store className if set
140 dataPriv.set( this, "__className__", className ); 132 data_priv.set( this, "__className__", this.className );
141 } 133 }
142   134  
143 // If the element has a class name or if we're passed `false`, 135 // If the element has a class name or if we're passed `false`,
144 // then remove the whole classname (if there was one, the above saved it). 136 // then remove the whole classname (if there was one, the above saved it).
145 // Otherwise bring back whatever was previously saved (if anything), 137 // Otherwise bring back whatever was previously saved (if anything),
146 // falling back to the empty string if nothing was stored. 138 // falling back to the empty string if nothing was stored.
147 if ( this.setAttribute ) { -  
148 this.setAttribute( "class", -  
149 className || value === false ? -  
150 "" : -  
151 dataPriv.get( this, "__className__" ) || "" 139 this.className = this.className || value === false ? "" : data_priv.get( this, "__className__" ) || "";
152 ); -  
153 } -  
154 } 140 }
155 } ); 141 });
156 }, 142 },
157   143  
158 hasClass: function( selector ) { 144 hasClass: function( selector ) {
159 var className, elem, 145 var className = " " + selector + " ",
160 i = 0; 146 i = 0,
161   -  
162 className = " " + selector + " "; 147 l = this.length;
163 while ( ( elem = this[ i++ ] ) ) { 148 for ( ; i < l; i++ ) {
164 if ( elem.nodeType === 1 && -  
165 ( " " + stripAndCollapse( getClass( elem ) ) + " " ).indexOf( className ) > -1 ) { 149 if ( this[i].nodeType === 1 && (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) >= 0 ) {
166 return true; 150 return true;
167 } 151 }
168 } 152 }
169   153  
170 return false; 154 return false;
171 } 155 }
172 } ); 156 });
173   157  
174 } ); 158 });
175   159