11/* http://keith-wood.name/svg.html
22 jQuery DOM compatibility for jQuery SVG v1.4.5.
33 Written by Keith Wood (kbwood{at}iinet.com.au) April 2009.
4- Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
5- MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
4+ Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
5+ MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
66 Please attribute the author if you use it. */
77
88( function ( $ ) { // Hide scope, no $ conflict
99
10+ var rclass = / [ \t \r \n ] / g,
11+ rspace = / \s + / ,
12+ rwhitespace = "[\\x20\\t\\r\\n\\f]" ;
13+
1014/* Support adding class names to SVG nodes. */
1115$ . fn . addClass = function ( origAddClass ) {
12- return function ( classNames ) {
13- classNames = classNames || '' ;
14- return this . each ( function ( ) {
15- if ( $ . svg . isSVGElem ( this ) ) {
16- var node = this ;
17- $ . each ( classNames . split ( / \s + / ) , function ( i , className ) {
18- var classes = ( node . className ? node . className . baseVal : node . getAttribute ( 'class' ) ) ;
19- if ( $ . inArray ( className , classes . split ( / \s + / ) ) == - 1 ) {
20- classes += ( classes ? ' ' : '' ) + className ;
21- ( node . className ? node . className . baseVal = classes :
22- node . setAttribute ( 'class' , classes ) ) ;
23- }
24- } ) ;
25- }
26- else {
27- origAddClass . apply ( $ ( this ) , [ classNames ] ) ;
28- }
29- } ) ;
16+ return function ( value ) {
17+ var classNames , i , l , elem ,
18+ setClass , c , cl ;
19+
20+ if ( jQuery . isFunction ( value ) ) {
21+ return this . each ( function ( j ) {
22+ jQuery ( this ) . addClass ( value . call ( this , j , this . className ) ) ;
23+ } ) ;
24+ }
25+
26+ if ( value && typeof value === "string" ) {
27+ classNames = value . split ( rspace ) ;
28+
29+ for ( i = 0 , l = this . length ; i < l ; i ++ ) {
30+ elem = this [ i ] ;
31+
32+ if ( elem . nodeType === 1 ) {
33+ if ( ! ( elem . className && elem . getAttribute ( 'class' ) ) && classNames . length === 1 ) {
34+ if ( $ . svg . isSVGElem ( elem ) ) {
35+ ( elem . className ? elem . className . baseVal = value
36+ : elem . setAttribute ( 'class' , value ) ) ;
37+ } else {
38+ elem . className = value ;
39+ }
40+ } else {
41+ setClass = ! $ . svg . isSVGElem ( elem ) ? elem . className :
42+ elem . className ? elem . className . baseVal :
43+ elem . getAttribute ( 'class' ) ;
44+
45+ setClass = ( " " + setClass + " " ) ;
46+ for ( c = 0 , cl = classNames . length ; c < cl ; c ++ ) {
47+ if ( setClass . indexOf ( " " + classNames [ c ] + " " ) < 0 ) {
48+ setClass += classNames [ c ] + " " ;
49+ }
50+ }
51+
52+ setClass = jQuery . trim ( setClass ) ;
53+ if ( $ . svg . isSVGElem ( elem ) ) {
54+
55+ ( elem . className ? elem . className . baseVal = setClass
56+ : elem . setAttribute ( 'class' , setClass ) ) ;
57+ } else {
58+ elem . className = setClass ;
59+ }
60+ }
61+ }
62+ }
63+ }
64+
65+ return this ;
3066 } ;
3167} ( $ . fn . addClass ) ;
3268
3369/* Support removing class names from SVG nodes. */
3470$ . fn . removeClass = function ( origRemoveClass ) {
35- return function ( classNames ) {
36- classNames = classNames || '' ;
37- return this . each ( function ( ) {
38- if ( $ . svg . isSVGElem ( this ) ) {
39- var node = this ;
40- $ . each ( classNames . split ( / \s + / ) , function ( i , className ) {
41- var classes = ( node . className ? node . className . baseVal : node . getAttribute ( 'class' ) ) ;
42- classes = $ . grep ( classes . split ( / \s + / ) , function ( n , i ) { return n != className ; } ) .
43- join ( ' ' ) ;
44- ( node . className ? node . className . baseVal = classes :
45- node . setAttribute ( 'class' , classes ) ) ;
46- } ) ;
47- }
48- else {
49- origRemoveClass . apply ( $ ( this ) , [ classNames ] ) ;
50- }
51- } ) ;
71+ return function ( value ) {
72+ var classNames , i , l , elem , className , c , cl ;
73+
74+ if ( jQuery . isFunction ( value ) ) {
75+ return this . each ( function ( j ) {
76+ jQuery ( this ) . removeClass ( value . call ( this , j , this . className ) ) ;
77+ } ) ;
78+ }
79+
80+ if ( ( value && typeof value === "string" ) || value === undefined ) {
81+ classNames = ( value || "" ) . split ( rspace ) ;
82+
83+ for ( i = 0 , l = this . length ; i < l ; i ++ ) {
84+ elem = this [ i ] ;
85+
86+ if ( elem . nodeType === 1 && ( elem . className || elem . getAttribute ( 'class' ) ) ) {
87+ if ( value ) {
88+ className = ! $ . svg . isSVGElem ( elem ) ? elem . className :
89+ elem . className ? elem . className . baseVal :
90+ elem . getAttribute ( 'class' ) ;
91+
92+ className = ( " " + className + " " ) . replace ( rclass , " " ) ;
93+
94+ for ( c = 0 , cl = classNames . length ; c < cl ; c ++ ) {
95+ // Remove until there is nothing to remove,
96+ while ( className . indexOf ( " " + classNames [ c ] + " " ) >= 0 ) {
97+ className = className . replace ( " " + classNames [ c ] + " " , " " ) ;
98+ }
99+ }
100+
101+ className = jQuery . trim ( className ) ;
102+ } else {
103+ className = "" ;
104+ }
105+
106+ if ( $ . svg . isSVGElem ( elem ) ) {
107+ ( elem . className ? elem . className . baseVal = className
108+ : elem . setAttribute ( 'class' , className ) ) ;
109+ } else {
110+ elem . className = className ;
111+ }
112+ }
113+ }
114+ }
115+
116+ return this ;
52117 } ;
53118} ( $ . fn . removeClass ) ;
54119
@@ -71,29 +136,35 @@ $.fn.toggleClass = function(origToggleClass) {
71136
72137/* Support checking class names on SVG nodes. */
73138$ . fn . hasClass = function ( origHasClass ) {
74- return function ( className ) {
75- className = className || '' ;
76- var found = false ;
77- this . each ( function ( ) {
78- if ( $ . svg . isSVGElem ( this ) ) {
79- var classes = ( this . className ? this . className . baseVal :
80- this . getAttribute ( 'class' ) ) . split ( / \s + / ) ;
81- found = ( $ . inArray ( className , classes ) > - 1 ) ;
82- }
83- else {
84- found = ( origHasClass . apply ( $ ( this ) , [ className ] ) ) ;
85- }
86- return ! found ;
87- } ) ;
88- return found ;
139+ return function ( selector ) {
140+
141+ var className = " " + selector + " " ,
142+ i = 0 ,
143+ l = this . length ,
144+ elem , classes ;
145+
146+ for ( ; i < l ; i ++ ) {
147+ elem = this [ i ] ;
148+ if ( elem . nodeType === 1 ) {
149+ classes = ! $ . svg . isSVGElem ( elem ) ? elem . className :
150+ elem . className ? elem . className . baseVal :
151+ elem . getAttribute ( 'class' ) ;
152+ if ( ( " " + classes + " " ) . replace ( rclass , " " ) . indexOf ( className ) > - 1 ) {
153+ return true ;
154+ }
155+ }
156+ }
157+
158+ return false ;
89159 } ;
90160} ( $ . fn . hasClass ) ;
91161
92162/* Support attributes on SVG nodes. */
93163$ . fn . attr = function ( origAttr ) {
94164 return function ( name , value , type ) {
165+ var origArgs = arguments ;
95166 if ( typeof name === 'string' && value === undefined ) {
96- var val = origAttr . apply ( this , [ name ] ) ;
167+ var val = origAttr . apply ( this , origArgs ) ;
97168 if ( val && val . baseVal && val . baseVal . numberOfItems != null ) { // Multiple values
98169 value = '' ;
99170 val = val . baseVal ;
@@ -126,15 +197,15 @@ $.fn.attr = function(origAttr) {
126197 options = { } ;
127198 options [ name ] = value ;
128199 }
129- return this . each ( function ( ) {
200+ return $ ( this ) . each ( function ( ) {
130201 if ( $ . svg . isSVGElem ( this ) ) {
131202 for ( var n in options ) {
132203 var val = ( $ . isFunction ( options [ n ] ) ? options [ n ] ( ) : options [ n ] ) ;
133204 ( type ? this . style [ n ] = val : this . setAttribute ( n , val ) ) ;
134205 }
135206 }
136207 else {
137- origAttr . apply ( $ ( this ) , [ name , value , type ] ) ;
208+ origAttr . apply ( $ ( this ) , origArgs ) ;
138209 }
139210 } ) ;
140211 } ;
@@ -165,56 +236,18 @@ $.extend($.cssNumber, {
165236/* Support retrieving CSS/attribute values on SVG nodes. */
166237if ( $ . cssProps ) {
167238 $ . css = function ( origCSS ) {
168- return function ( elem , name , extra ) {
239+ return function ( elem , name , numeric , extra ) {
169240 var value = ( name . match ( / ^ s v g .* / ) ? $ ( elem ) . attr ( $ . cssProps [ name ] || name ) : '' ) ;
170- return value || origCSS ( elem , name , extra ) ;
241+ return value || origCSS ( elem , name , numeric , extra ) ;
171242 } ;
172243 } ( $ . css ) ;
173244}
174-
175- /* Determine if any nodes are SVG nodes. */
176- function anySVG ( checkSet ) {
177- for ( var i = 0 ; i < checkSet . length ; i ++ ) {
178- if ( checkSet [ i ] . nodeType == 1 && checkSet [ i ] . namespaceURI == $ . svg . svgNS ) {
179- return true ;
180- }
181- }
182- return false ;
183- }
184245
185- /* Update Sizzle selectors. */
186-
187- $ . expr . relative [ '+' ] = function ( origRelativeNext ) {
188- return function ( checkSet , part , isXML ) {
189- origRelativeNext ( checkSet , part , isXML || anySVG ( checkSet ) ) ;
190- } ;
191- } ( $ . expr . relative [ '+' ] ) ;
192-
193- $ . expr . relative [ '>' ] = function ( origRelativeChild ) {
194- return function ( checkSet , part , isXML ) {
195- origRelativeChild ( checkSet , part , isXML || anySVG ( checkSet ) ) ;
196- } ;
197- } ( $ . expr . relative [ '>' ] ) ;
198-
199- $ . expr . relative [ '' ] = function ( origRelativeDescendant ) {
200- return function ( checkSet , part , isXML ) {
201- origRelativeDescendant ( checkSet , part , isXML || anySVG ( checkSet ) ) ;
202- } ;
203- } ( $ . expr . relative [ '' ] ) ;
204-
205- $ . expr . relative [ '~' ] = function ( origRelativeSiblings ) {
206- return function ( checkSet , part , isXML ) {
207- origRelativeSiblings ( checkSet , part , isXML || anySVG ( checkSet ) ) ;
208- } ;
209- } ( $ . expr . relative [ '~' ] ) ;
210-
211- $ . expr . find . ID = function ( origFindId ) {
212- return function ( match , context , isXML ) {
213- return ( $ . svg . isSVGElem ( context ) ?
214- [ context . ownerDocument . getElementById ( match [ 1 ] ) ] :
215- origFindId ( match , context , isXML ) ) ;
216- } ;
217- } ( $ . expr . find . ID ) ;
246+ $ . find . isXML = function ( origIsXml ) {
247+ return function ( elem ) {
248+ return $ . svg . isSVGElem ( elem ) || origIsXml ( elem ) ;
249+ }
250+ } ( $ . find . isXML )
218251
219252var div = document . createElement ( 'div' ) ;
220253div . appendChild ( document . createComment ( '' ) ) ;
@@ -234,60 +267,16 @@ if (div.getElementsByTagName('*').length > 0) { // Make sure no comments are fou
234267 } ;
235268}
236269
237- $ . expr . preFilter . CLASS = function ( match , curLoop , inplace , result , not , isXML ) {
238- match = ' ' + match [ 1 ] . replace ( / \\ / g, '' ) + ' ' ;
239- if ( isXML ) {
240- return match ;
241- }
242- for ( var i = 0 , elem = { } ; elem != null ; i ++ ) {
243- elem = curLoop [ i ] ;
244- if ( ! elem ) {
245- try {
246- elem = curLoop . item ( i ) ;
247- }
248- catch ( e ) {
249- // Ignore
250- }
251- }
252- if ( elem ) {
253- var className = ( ! $ . svg . isSVGElem ( elem ) ? elem . className :
254- ( elem . className ? elem . className . baseVal : '' ) || elem . getAttribute ( 'class' ) ) ;
255- if ( not ^ ( className && ( ' ' + className + ' ' ) . indexOf ( match ) > - 1 ) ) {
256- if ( ! inplace )
257- result . push ( elem ) ;
258- }
259- else if ( inplace ) {
260- curLoop [ i ] = false ;
261- }
262- }
263- }
264- return false ;
265- } ;
270+ $ . expr . filter . CLASS = function ( className ) {
271+ var pattern = new RegExp ( "(^|" + rwhitespace + ")" + className + "(" + rwhitespace + "|$)" ) ;
272+ return function ( elem ) {
273+ var elemClass = ( ! $ . svg . isSVGElem ( elem ) ? elem . className || ( typeof elem . getAttribute !== "undefined" && elem . getAttribute ( "class" ) ) || "" :
274+ ( elem . className ? elem . className . baseVal : elem . getAttribute ( 'class' ) ) ) ;
266275
267- $ . expr . filter . CLASS = function ( elem , match ) {
268- var className = ( ! $ . svg . isSVGElem ( elem ) ? elem . className :
269- ( elem . className ? elem . className . baseVal : elem . getAttribute ( 'class' ) ) ) ;
270- return ( ' ' + className + ' ' ) . indexOf ( match ) > - 1 ;
276+ return pattern . test ( elemClass ) ;
277+ } ;
271278} ;
272279
273- $ . expr . filter . ATTR = function ( origFilterAttr ) {
274- return function ( elem , match ) {
275- var handler = null ;
276- if ( $ . svg . isSVGElem ( elem ) ) {
277- handler = match [ 1 ] ;
278- $ . expr . attrHandle [ handler ] = function ( elem ) {
279- var attr = elem . getAttribute ( handler ) ;
280- return attr && attr . baseVal || attr ;
281- } ;
282- }
283- var filter = origFilterAttr ( elem , match ) ;
284- if ( handler ) {
285- $ . expr . attrHandle [ handler ] = null ;
286- }
287- return filter ;
288- } ;
289- } ( $ . expr . filter . ATTR ) ;
290-
291280/*
292281 In the removeData function (line 1881, v1.7.2):
293282
@@ -403,4 +392,4 @@ $.expr.filter.ATTR = function(origFilterAttr) {
403392
404393*/
405394
406- } ) ( jQuery ) ;
395+ } ) ( jQuery ) ;
0 commit comments