1050
|
1 /* http://keith-wood.name/svg.html
|
|
2 jQuery DOM compatibility for jQuery SVG v1.4.4.
|
756
|
3 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.
|
|
6 Please attribute the author if you use it. */
|
|
7
|
|
8 (function($) { // Hide scope, no $ conflict
|
|
9
|
|
10 /* Support adding class names to SVG nodes. */
|
|
11 $.fn.addClass = function(origAddClass) {
|
|
12 return function(classNames) {
|
|
13 classNames = classNames || '';
|
|
14 return this.each(function() {
|
1050
|
15 if ($.svg.isSVGElem(this)) {
|
756
|
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 });
|
|
30 };
|
|
31 }($.fn.addClass);
|
|
32
|
|
33 /* Support removing class names from SVG nodes. */
|
|
34 $.fn.removeClass = function(origRemoveClass) {
|
|
35 return function(classNames) {
|
|
36 classNames = classNames || '';
|
|
37 return this.each(function() {
|
1050
|
38 if ($.svg.isSVGElem(this)) {
|
756
|
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 });
|
|
52 };
|
|
53 }($.fn.removeClass);
|
|
54
|
|
55 /* Support toggling class names on SVG nodes. */
|
|
56 $.fn.toggleClass = function(origToggleClass) {
|
|
57 return function(className, state) {
|
|
58 return this.each(function() {
|
1050
|
59 if ($.svg.isSVGElem(this)) {
|
756
|
60 if (typeof state !== 'boolean') {
|
|
61 state = !$(this).hasClass(className);
|
|
62 }
|
|
63 $(this)[(state ? 'add' : 'remove') + 'Class'](className);
|
|
64 }
|
|
65 else {
|
|
66 origToggleClass.apply($(this), [className, state]);
|
|
67 }
|
|
68 });
|
|
69 };
|
|
70 }($.fn.toggleClass);
|
|
71
|
|
72 /* Support checking class names on SVG nodes. */
|
|
73 $.fn.hasClass = function(origHasClass) {
|
|
74 return function(className) {
|
|
75 className = className || '';
|
|
76 var found = false;
|
|
77 this.each(function() {
|
1050
|
78 if ($.svg.isSVGElem(this)) {
|
756
|
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;
|
|
89 };
|
|
90 }($.fn.hasClass);
|
|
91
|
|
92 /* Support attributes on SVG nodes. */
|
|
93 $.fn.attr = function(origAttr) {
|
|
94 return function(name, value, type) {
|
|
95 if (typeof name === 'string' && value === undefined) {
|
|
96 var val = origAttr.apply(this, [name, value, type]);
|
1050
|
97 if (val && val.baseVal && val.baseVal.numberOfItems != null) { // Multiple values
|
756
|
98 value = '';
|
|
99 val = val.baseVal;
|
1050
|
100 if (name == 'transform') {
|
|
101 for (var i = 0; i < val.numberOfItems; i++) {
|
|
102 var item = val.getItem(i);
|
|
103 switch (item.type) {
|
|
104 case 1: value += ' matrix(' + item.matrix.a + ',' + item.matrix.b + ',' +
|
|
105 item.matrix.c + ',' + item.matrix.d + ',' +
|
|
106 item.matrix.e + ',' + item.matrix.f + ')';
|
|
107 break;
|
|
108 case 2: value += ' translate(' + item.matrix.e + ',' + item.matrix.f + ')'; break;
|
|
109 case 3: value += ' scale(' + item.matrix.a + ',' + item.matrix.d + ')'; break;
|
|
110 case 4: value += ' rotate(' + item.angle + ')'; break; // Doesn't handle new origin
|
|
111 case 5: value += ' skewX(' + item.angle + ')'; break;
|
|
112 case 6: value += ' skewY(' + item.angle + ')'; break;
|
|
113 }
|
756
|
114 }
|
1050
|
115 val = value.substring(1);
|
756
|
116 }
|
1050
|
117 else {
|
|
118 val = val.getItem(0).valueAsString;
|
|
119 }
|
756
|
120 }
|
|
121 return (val && val.baseVal ? val.baseVal.valueAsString : val);
|
|
122 }
|
1050
|
123
|
756
|
124 var options = name;
|
|
125 if (typeof name === 'string') {
|
|
126 options = {};
|
|
127 options[name] = value;
|
|
128 }
|
|
129 return this.each(function() {
|
1050
|
130 if ($.svg.isSVGElem(this)) {
|
756
|
131 for (var n in options) {
|
|
132 var val = ($.isFunction(options[n]) ? options[n]() : options[n]);
|
|
133 (type ? this.style[n] = val : this.setAttribute(n, val));
|
|
134 }
|
|
135 }
|
|
136 else {
|
|
137 origAttr.apply($(this), [name, value, type]);
|
|
138 }
|
|
139 });
|
|
140 };
|
|
141 }($.fn.attr);
|
|
142
|
|
143 /* Support removing attributes on SVG nodes. */
|
|
144 $.fn.removeAttr = function(origRemoveAttr) {
|
|
145 return function(name) {
|
|
146 return this.each(function() {
|
1050
|
147 if ($.svg.isSVGElem(this)) {
|
756
|
148 (this[name] && this[name].baseVal ? this[name].baseVal.value = '' :
|
|
149 this.setAttribute(name, ''));
|
|
150 }
|
|
151 else {
|
|
152 origRemoveAttr.apply($(this), [name]);
|
|
153 }
|
|
154 });
|
|
155 };
|
|
156 }($.fn.removeAttr);
|
|
157
|
1050
|
158 /* Add numeric only properties. */
|
|
159 $.extend($.cssNumber, {
|
|
160 'stopOpacity': true,
|
|
161 'strokeMitrelimit': true,
|
|
162 'strokeOpacity': true
|
|
163 });
|
|
164
|
|
165 /* Support retrieving CSS/attribute values on SVG nodes. */
|
|
166 if ($.cssProps) {
|
|
167 $.css = function(origCSS) {
|
|
168 return function(elem, name) {
|
|
169 var value = (name.match(/^svg.*/) ? $(elem).attr($.cssProps[name] || name) : '');
|
|
170 return value || origCSS(elem, name);
|
|
171 };
|
|
172 }($.css);
|
|
173 }
|
|
174
|
756
|
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 }
|
|
184
|
|
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) {
|
1050
|
213 return ($.svg.isSVGElem(context) ?
|
756
|
214 [context.ownerDocument.getElementById(match[1])] :
|
|
215 origFindId(match, context, isXML));
|
|
216 };
|
|
217 }($.expr.find.ID);
|
|
218
|
|
219 var div = document.createElement('div');
|
|
220 div.appendChild(document.createComment(''));
|
|
221 if (div.getElementsByTagName('*').length > 0) { // Make sure no comments are found
|
|
222 $.expr.find.TAG = function(match, context) {
|
|
223 var results = context.getElementsByTagName(match[1]);
|
|
224 if (match[1] === '*') { // Filter out possible comments
|
|
225 var tmp = [];
|
|
226 for (var i = 0; results[i] || results.item(i); i++) {
|
|
227 if ((results[i] || results.item(i)).nodeType === 1) {
|
|
228 tmp.push(results[i] || results.item(i));
|
|
229 }
|
|
230 }
|
|
231 results = tmp;
|
|
232 }
|
|
233 return results;
|
|
234 };
|
|
235 }
|
|
236
|
|
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) {
|
1050
|
253 var className = (!$.svg.isSVGElem(elem) ? elem.className :
|
756
|
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 };
|
|
266
|
|
267 $.expr.filter.CLASS = function(elem, match) {
|
1050
|
268 var className = (!$.svg.isSVGElem(elem) ? elem.className :
|
756
|
269 (elem.className ? elem.className.baseVal : elem.getAttribute('class')));
|
|
270 return (' ' + className + ' ').indexOf(match) > -1;
|
|
271 };
|
|
272
|
|
273 $.expr.filter.ATTR = function(origFilterAttr) {
|
|
274 return function(elem, match) {
|
|
275 var handler = null;
|
1050
|
276 if ($.svg.isSVGElem(elem)) {
|
756
|
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
|
|
291 /*
|
1050
|
292 In the event.add function (line 2646, v1.6.2):
|
|
293
|
|
294 if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
|
|
295 // Bind the global event handler to the element
|
|
296 try { // SVG
|
|
297 elem.addEventListener( type, eventHandle, false );
|
|
298
|
|
299 } catch(e) {
|
|
300 if (elem.attachEvent)
|
|
301 elem.attachEvent( "on" + type, eventHandle );
|
|
302 }
|
|
303 }
|
|
304
|
|
305 In the event.remove function (line 2776, v1.6.2):
|
|
306
|
|
307 if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) {
|
|
308 try { // SVG
|
|
309 elem.removeEventListener(type, elemData.handle, false);
|
|
310 }
|
|
311 catch (e) {
|
|
312 if (elem.detachEvent)
|
|
313 elem.detachEvent("on" + type, elemData.handle);
|
|
314 }
|
|
315 }
|
|
316
|
|
317 In the event.fix function (line 3036, v.1.6.2)
|
|
318
|
|
319 if (event.target.namespaceURI == 'http://www.w3.org/2000/svg') { // SVG
|
|
320 event.button = [1, 4, 2][event.button];
|
|
321 }
|
|
322
|
|
323 // Add which for click: 1 === left; 2 === middle; 3 === right
|
|
324 // Note: button is not normalized, so don't use it
|
|
325 if ( !event.which && event.button !== undefined ) {
|
|
326 event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
|
|
327 }
|
|
328
|
|
329 In the Sizzle function (line 3873, v1.6.2):
|
|
330
|
756
|
331 if ( toString.call(checkSet) === "[object Array]" ) {
|
|
332 if ( !prune ) {
|
|
333 results.push.apply( results, checkSet );
|
1050
|
334
|
|
335 } else if ( context && context.nodeType === 1 ) {
|
|
336 for ( i = 0; checkSet[i] != null; i++ ) {
|
|
337 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) {
|
|
338 results.push( set[i] || set.item(i) ); // SVG
|
756
|
339 }
|
|
340 }
|
1050
|
341
|
756
|
342 } else {
|
1050
|
343 for ( i = 0; checkSet[i] != null; i++ ) {
|
756
|
344 if ( checkSet[i] && checkSet[i].nodeType === 1 ) {
|
1050
|
345 results.push( set[i] || set.item(i) ); // SVG
|
756
|
346 }
|
|
347 }
|
|
348 }
|
1050
|
349
|
|
350 } else {
|
|
351
|
|
352 In the fallback for the Sizzle makeArray function (line 4617, v1.6.2):
|
|
353
|
|
354 if ( toString.call(array) === "[object Array]" ) {
|
|
355 Array.prototype.push.apply( ret, array );
|
|
356
|
|
357 } else {
|
756
|
358 if ( typeof array.length === "number" ) {
|
1050
|
359 for ( var l = array.length; i < l; i++ ) {
|
|
360 ret.push( array[i] || array.item(i) ); // SVG
|
|
361 }
|
|
362
|
|
363 } else {
|
|
364 for ( ; array[i]; i++ ) {
|
|
365 ret.push( array[i] );
|
756
|
366 }
|
|
367 }
|
1050
|
368 }
|
756
|
369
|
1050
|
370 In the jQuery.cleanData function (line 6220, v1.6.2)
|
756
|
371
|
1050
|
372 if ( deleteExpando ) {
|
|
373 delete elem[ jQuery.expando ];
|
|
374
|
|
375 } else {
|
|
376 try { // SVG
|
|
377 elem.removeAttribute( jQuery.expando );
|
|
378 } catch (e) {
|
|
379 // Ignore
|
756
|
380 }
|
|
381 }
|
|
382
|
1050
|
383 In the fallback getComputedStyle function (line 6509, v.1.6.2)
|
|
384
|
|
385 defaultView = (elem.ownerDocument ? elem.ownerDocument.defaultView : elem.defaultView); // SVG
|
|
386 if ( !defaultView ) {
|
|
387 return undefined;
|
|
388 }
|
|
389
|
756
|
390 */
|
|
391
|
|
392 })(jQuery);
|