Mercurial > hg > digilib-old
comparison webapp/src/main/webapp/jquery/svg/jquery.svgdom.js @ 1050:301ef9bf1965
update jquery.svg.js
author | hertzhaft |
---|---|
date | Mon, 26 Mar 2012 08:18:40 +0200 |
parents | 7779b37d1d05 |
children | 139ad480333b |
comparison
equal
deleted
inserted
replaced
1049:32096eff13c7 | 1050:301ef9bf1965 |
---|---|
1 /* http://keith-wood.name/svg.html | 1 /* http://keith-wood.name/svg.html |
2 SVG/jQuery DOM compatibility for jQuery v1.4.3. | 2 jQuery DOM compatibility for jQuery SVG v1.4.4. |
3 Written by Keith Wood (kbwood{at}iinet.com.au) April 2009. | 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 | 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. | 5 MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. |
6 Please attribute the author if you use it. */ | 6 Please attribute the author if you use it. */ |
7 | 7 |
10 /* Support adding class names to SVG nodes. */ | 10 /* Support adding class names to SVG nodes. */ |
11 $.fn.addClass = function(origAddClass) { | 11 $.fn.addClass = function(origAddClass) { |
12 return function(classNames) { | 12 return function(classNames) { |
13 classNames = classNames || ''; | 13 classNames = classNames || ''; |
14 return this.each(function() { | 14 return this.each(function() { |
15 if (isSVGElem(this)) { | 15 if ($.svg.isSVGElem(this)) { |
16 var node = this; | 16 var node = this; |
17 $.each(classNames.split(/\s+/), function(i, className) { | 17 $.each(classNames.split(/\s+/), function(i, className) { |
18 var classes = (node.className ? node.className.baseVal : node.getAttribute('class')); | 18 var classes = (node.className ? node.className.baseVal : node.getAttribute('class')); |
19 if ($.inArray(className, classes.split(/\s+/)) == -1) { | 19 if ($.inArray(className, classes.split(/\s+/)) == -1) { |
20 classes += (classes ? ' ' : '') + className; | 20 classes += (classes ? ' ' : '') + className; |
33 /* Support removing class names from SVG nodes. */ | 33 /* Support removing class names from SVG nodes. */ |
34 $.fn.removeClass = function(origRemoveClass) { | 34 $.fn.removeClass = function(origRemoveClass) { |
35 return function(classNames) { | 35 return function(classNames) { |
36 classNames = classNames || ''; | 36 classNames = classNames || ''; |
37 return this.each(function() { | 37 return this.each(function() { |
38 if (isSVGElem(this)) { | 38 if ($.svg.isSVGElem(this)) { |
39 var node = this; | 39 var node = this; |
40 $.each(classNames.split(/\s+/), function(i, className) { | 40 $.each(classNames.split(/\s+/), function(i, className) { |
41 var classes = (node.className ? node.className.baseVal : node.getAttribute('class')); | 41 var classes = (node.className ? node.className.baseVal : node.getAttribute('class')); |
42 classes = $.grep(classes.split(/\s+/), function(n, i) { return n != className; }). | 42 classes = $.grep(classes.split(/\s+/), function(n, i) { return n != className; }). |
43 join(' '); | 43 join(' '); |
54 | 54 |
55 /* Support toggling class names on SVG nodes. */ | 55 /* Support toggling class names on SVG nodes. */ |
56 $.fn.toggleClass = function(origToggleClass) { | 56 $.fn.toggleClass = function(origToggleClass) { |
57 return function(className, state) { | 57 return function(className, state) { |
58 return this.each(function() { | 58 return this.each(function() { |
59 if (isSVGElem(this)) { | 59 if ($.svg.isSVGElem(this)) { |
60 if (typeof state !== 'boolean') { | 60 if (typeof state !== 'boolean') { |
61 state = !$(this).hasClass(className); | 61 state = !$(this).hasClass(className); |
62 } | 62 } |
63 $(this)[(state ? 'add' : 'remove') + 'Class'](className); | 63 $(this)[(state ? 'add' : 'remove') + 'Class'](className); |
64 } | 64 } |
73 $.fn.hasClass = function(origHasClass) { | 73 $.fn.hasClass = function(origHasClass) { |
74 return function(className) { | 74 return function(className) { |
75 className = className || ''; | 75 className = className || ''; |
76 var found = false; | 76 var found = false; |
77 this.each(function() { | 77 this.each(function() { |
78 if (isSVGElem(this)) { | 78 if ($.svg.isSVGElem(this)) { |
79 var classes = (this.className ? this.className.baseVal : | 79 var classes = (this.className ? this.className.baseVal : |
80 this.getAttribute('class')).split(/\s+/); | 80 this.getAttribute('class')).split(/\s+/); |
81 found = ($.inArray(className, classes) > -1); | 81 found = ($.inArray(className, classes) > -1); |
82 } | 82 } |
83 else { | 83 else { |
92 /* Support attributes on SVG nodes. */ | 92 /* Support attributes on SVG nodes. */ |
93 $.fn.attr = function(origAttr) { | 93 $.fn.attr = function(origAttr) { |
94 return function(name, value, type) { | 94 return function(name, value, type) { |
95 if (typeof name === 'string' && value === undefined) { | 95 if (typeof name === 'string' && value === undefined) { |
96 var val = origAttr.apply(this, [name, value, type]); | 96 var val = origAttr.apply(this, [name, value, type]); |
97 if (val && val.baseVal && val.baseVal.numberOfItems != null) { // Transform | 97 if (val && val.baseVal && val.baseVal.numberOfItems != null) { // Multiple values |
98 value = ''; | 98 value = ''; |
99 val = val.baseVal; | 99 val = val.baseVal; |
100 for (var i = 0; i < val.numberOfItems; i++) { | 100 if (name == 'transform') { |
101 var item = val.getItem(i); | 101 for (var i = 0; i < val.numberOfItems; i++) { |
102 switch (item.type) { | 102 var item = val.getItem(i); |
103 case 1: value += ' matrix(' + item.matrix.a + ',' + item.matrix.b + ',' + | 103 switch (item.type) { |
104 item.matrix.c + ',' + item.matrix.d + ',' + | 104 case 1: value += ' matrix(' + item.matrix.a + ',' + item.matrix.b + ',' + |
105 item.matrix.e + ',' + item.matrix.f + ')'; | 105 item.matrix.c + ',' + item.matrix.d + ',' + |
106 break; | 106 item.matrix.e + ',' + item.matrix.f + ')'; |
107 case 2: value += ' translate(' + item.matrix.e + ',' + item.matrix.f + ')'; break; | 107 break; |
108 case 3: value += ' scale(' + item.matrix.a + ',' + item.matrix.d + ')'; break; | 108 case 2: value += ' translate(' + item.matrix.e + ',' + item.matrix.f + ')'; break; |
109 case 4: value += ' rotate(' + item.angle + ')'; break; // Doesn't handle new origin | 109 case 3: value += ' scale(' + item.matrix.a + ',' + item.matrix.d + ')'; break; |
110 case 5: value += ' skewX(' + item.angle + ')'; break; | 110 case 4: value += ' rotate(' + item.angle + ')'; break; // Doesn't handle new origin |
111 case 6: value += ' skewY(' + item.angle + ')'; break; | 111 case 5: value += ' skewX(' + item.angle + ')'; break; |
112 } | 112 case 6: value += ' skewY(' + item.angle + ')'; break; |
113 } | 113 } |
114 val = value.substring(1); | 114 } |
115 val = value.substring(1); | |
116 } | |
117 else { | |
118 val = val.getItem(0).valueAsString; | |
119 } | |
115 } | 120 } |
116 return (val && val.baseVal ? val.baseVal.valueAsString : val); | 121 return (val && val.baseVal ? val.baseVal.valueAsString : val); |
117 } | 122 } |
123 | |
118 var options = name; | 124 var options = name; |
119 if (typeof name === 'string') { | 125 if (typeof name === 'string') { |
120 options = {}; | 126 options = {}; |
121 options[name] = value; | 127 options[name] = value; |
122 } | 128 } |
123 return this.each(function() { | 129 return this.each(function() { |
124 if (isSVGElem(this)) { | 130 if ($.svg.isSVGElem(this)) { |
125 for (var n in options) { | 131 for (var n in options) { |
126 var val = ($.isFunction(options[n]) ? options[n]() : options[n]); | 132 var val = ($.isFunction(options[n]) ? options[n]() : options[n]); |
127 (type ? this.style[n] = val : this.setAttribute(n, val)); | 133 (type ? this.style[n] = val : this.setAttribute(n, val)); |
128 } | 134 } |
129 } | 135 } |
136 | 142 |
137 /* Support removing attributes on SVG nodes. */ | 143 /* Support removing attributes on SVG nodes. */ |
138 $.fn.removeAttr = function(origRemoveAttr) { | 144 $.fn.removeAttr = function(origRemoveAttr) { |
139 return function(name) { | 145 return function(name) { |
140 return this.each(function() { | 146 return this.each(function() { |
141 if (isSVGElem(this)) { | 147 if ($.svg.isSVGElem(this)) { |
142 (this[name] && this[name].baseVal ? this[name].baseVal.value = '' : | 148 (this[name] && this[name].baseVal ? this[name].baseVal.value = '' : |
143 this.setAttribute(name, '')); | 149 this.setAttribute(name, '')); |
144 } | 150 } |
145 else { | 151 else { |
146 origRemoveAttr.apply($(this), [name]); | 152 origRemoveAttr.apply($(this), [name]); |
147 } | 153 } |
148 }); | 154 }); |
149 }; | 155 }; |
150 }($.fn.removeAttr); | 156 }($.fn.removeAttr); |
151 | 157 |
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 | |
152 /* Determine if any nodes are SVG nodes. */ | 175 /* Determine if any nodes are SVG nodes. */ |
153 function anySVG(checkSet) { | 176 function anySVG(checkSet) { |
154 for (var i = 0; i < checkSet.length; i++) { | 177 for (var i = 0; i < checkSet.length; i++) { |
155 if (checkSet[i].nodeType == 1 && checkSet[i].namespaceURI == $.svg.svgNS) { | 178 if (checkSet[i].nodeType == 1 && checkSet[i].namespaceURI == $.svg.svgNS) { |
156 return true; | 179 return true; |
185 }; | 208 }; |
186 }($.expr.relative['~']); | 209 }($.expr.relative['~']); |
187 | 210 |
188 $.expr.find.ID = function(origFindId) { | 211 $.expr.find.ID = function(origFindId) { |
189 return function(match, context, isXML) { | 212 return function(match, context, isXML) { |
190 return (isSVGElem(context) ? | 213 return ($.svg.isSVGElem(context) ? |
191 [context.ownerDocument.getElementById(match[1])] : | 214 [context.ownerDocument.getElementById(match[1])] : |
192 origFindId(match, context, isXML)); | 215 origFindId(match, context, isXML)); |
193 }; | 216 }; |
194 }($.expr.find.ID); | 217 }($.expr.find.ID); |
195 | 218 |
225 catch (e) { | 248 catch (e) { |
226 // Ignore | 249 // Ignore |
227 } | 250 } |
228 } | 251 } |
229 if (elem) { | 252 if (elem) { |
230 var className = (!isSVGElem(elem) ? elem.className : | 253 var className = (!$.svg.isSVGElem(elem) ? elem.className : |
231 (elem.className ? elem.className.baseVal : '') || elem.getAttribute('class')); | 254 (elem.className ? elem.className.baseVal : '') || elem.getAttribute('class')); |
232 if (not ^ (className && (' ' + className + ' ').indexOf(match) > -1)) { | 255 if (not ^ (className && (' ' + className + ' ').indexOf(match) > -1)) { |
233 if (!inplace) | 256 if (!inplace) |
234 result.push(elem); | 257 result.push(elem); |
235 } | 258 } |
240 } | 263 } |
241 return false; | 264 return false; |
242 }; | 265 }; |
243 | 266 |
244 $.expr.filter.CLASS = function(elem, match) { | 267 $.expr.filter.CLASS = function(elem, match) { |
245 var className = (!isSVGElem(elem) ? elem.className : | 268 var className = (!$.svg.isSVGElem(elem) ? elem.className : |
246 (elem.className ? elem.className.baseVal : elem.getAttribute('class'))); | 269 (elem.className ? elem.className.baseVal : elem.getAttribute('class'))); |
247 return (' ' + className + ' ').indexOf(match) > -1; | 270 return (' ' + className + ' ').indexOf(match) > -1; |
248 }; | 271 }; |
249 | 272 |
250 $.expr.filter.ATTR = function(origFilterAttr) { | 273 $.expr.filter.ATTR = function(origFilterAttr) { |
251 return function(elem, match) { | 274 return function(elem, match) { |
252 var handler = null; | 275 var handler = null; |
253 if (isSVGElem(elem)) { | 276 if ($.svg.isSVGElem(elem)) { |
254 handler = match[1]; | 277 handler = match[1]; |
255 $.expr.attrHandle[handler] = function(elem){ | 278 $.expr.attrHandle[handler] = function(elem){ |
256 var attr = elem.getAttribute(handler); | 279 var attr = elem.getAttribute(handler); |
257 return attr && attr.baseVal || attr; | 280 return attr && attr.baseVal || attr; |
258 }; | 281 }; |
264 return filter; | 287 return filter; |
265 }; | 288 }; |
266 }($.expr.filter.ATTR); | 289 }($.expr.filter.ATTR); |
267 | 290 |
268 /* | 291 /* |
269 Change Sizzle initialisation (line 1425) in jQuery v1.3.2 base code... | 292 In the event.add function (line 2646, v1.6.2): |
270 | 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 | |
271 if ( toString.call(checkSet) === "[object Array]" ) { | 331 if ( toString.call(checkSet) === "[object Array]" ) { |
272 if ( !prune ) { | 332 if ( !prune ) { |
273 results.push.apply( results, checkSet ); | 333 results.push.apply( results, checkSet ); |
274 } else if ( context.nodeType === 1 ) { | 334 |
275 for ( var i = 0; checkSet[i] != null; i++ ) { | 335 } else if ( context && context.nodeType === 1 ) { |
276 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) { | 336 for ( i = 0; checkSet[i] != null; i++ ) { |
277 results.push( set[i] || set.item(i) ); // Here | 337 if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) { |
278 } | 338 results.push( set[i] || set.item(i) ); // SVG |
279 } | 339 } |
340 } | |
341 | |
280 } else { | 342 } else { |
281 for ( var i = 0; checkSet[i] != null; i++ ) { | 343 for ( i = 0; checkSet[i] != null; i++ ) { |
282 if ( checkSet[i] && checkSet[i].nodeType === 1 ) { | 344 if ( checkSet[i] && checkSet[i].nodeType === 1 ) { |
283 results.push( set[i] || set.item(i) ); // Here | 345 results.push( set[i] || set.item(i) ); // SVG |
284 } | 346 } |
285 } | 347 } |
286 } | 348 } |
287 } | 349 |
288 | 350 } else { |
289 Change fallback makeArray (line 2076) implementation in jQuery Sizzle... | 351 |
290 | 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 { | |
291 if ( typeof array.length === "number" ) { | 358 if ( typeof array.length === "number" ) { |
292 for ( var i = 0, l = array.length; i < l; i++ ) { | 359 for ( var l = array.length; i < l; i++ ) { |
293 ret.push( array[i] || array.item(i) ); // Here | 360 ret.push( array[i] || array.item(i) ); // SVG |
294 } | 361 } |
295 } | 362 |
363 } else { | |
364 for ( ; array[i]; i++ ) { | |
365 ret.push( array[i] ); | |
366 } | |
367 } | |
368 } | |
369 | |
370 In the jQuery.cleanData function (line 6220, v1.6.2) | |
371 | |
372 if ( deleteExpando ) { | |
373 delete elem[ jQuery.expando ]; | |
374 | |
375 } else { | |
376 try { // SVG | |
377 elem.removeAttribute( jQuery.expando ); | |
378 } catch (e) { | |
379 // Ignore | |
380 } | |
381 } | |
382 | |
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 | |
296 */ | 390 */ |
297 | 391 |
298 /* | |
299 Events management requires changes to jQuery v1.3.2 base code... | |
300 | |
301 In $.event.add (line 2437)... | |
302 | |
303 if ( !jQuery.event.special[type] || jQuery.event.special[type].setup.call(elem, data, namespaces) === false ) { | |
304 // Bind the global event handler to the element | |
305 try { // Here | |
306 elem.addEventListener(type, handle, false); | |
307 } | |
308 catch(e) { | |
309 if (elem.attachEvent) | |
310 elem.attachEvent("on" + type, handle); | |
311 } | |
312 } | |
313 | |
314 In $.event.remove (line 2521)... | |
315 | |
316 if ( !jQuery.event.special[type] || jQuery.event.special[type].teardown.call(elem, namespaces) === false ) { | |
317 try { // Here | |
318 elem.removeEventListener(type, jQuery.data(elem, "handle"), false); | |
319 } | |
320 catch (e) { | |
321 if (elem.detachEvent) | |
322 elem.detachEvent("on" + type, jQuery.data(elem, "handle")); | |
323 } | |
324 } | |
325 */ | |
326 | |
327 /* Does this node belong to SVG? */ | |
328 function isSVGElem(node) { | |
329 return (node.nodeType == 1 && node.namespaceURI == $.svg.svgNS); | |
330 } | |
331 | |
332 })(jQuery); | 392 })(jQuery); |