| 1 |
/* http://keith-wood.name/svg.html |
| 2 |
jQuery DOM compatibility for jQuery SVG v1.4.5. |
| 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() { |
| 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 |
}); |
| 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() { |
| 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 |
}); |
| 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() { |
| 59 |
if ($.svg.isSVGElem(this)) { |
| 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() { |
| 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; |
| 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]); |
| 97 |
if (val && val.baseVal && val.baseVal.numberOfItems != null) { // Multiple values |
| 98 |
value = ''; |
| 99 |
val = val.baseVal; |
| 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 |
} |
| 114 |
} |
| 115 |
val = value.substring(1); |
| 116 |
} |
| 117 |
else { |
| 118 |
val = val.getItem(0).valueAsString; |
| 119 |
} |
| 120 |
} |
| 121 |
return (val && val.baseVal ? val.baseVal.valueAsString : val); |
| 122 |
} |
| 123 |
|
| 124 |
var options = name; |
| 125 |
if (typeof name === 'string') { |
| 126 |
options = {}; |
| 127 |
options[name] = value; |
| 128 |
} |
| 129 |
return this.each(function() { |
| 130 |
if ($.svg.isSVGElem(this)) { |
| 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() { |
| 147 |
if ($.svg.isSVGElem(this)) { |
| 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 |
|
| 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, extra) { |
| 169 |
var value = (name.match(/^svg.*/) ? $(elem).attr($.cssProps[name] || name) : ''); |
| 170 |
return value || origCSS(elem, name, extra); |
| 171 |
}; |
| 172 |
}($.css); |
| 173 |
} |
| 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 |
} |
| 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) { |
| 213 |
return ($.svg.isSVGElem(context) ? |
| 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) { |
| 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 |
}; |
| 266 |
|
| 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; |
| 271 |
}; |
| 272 |
|
| 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 |
|
| 291 |
/* |
| 292 |
In the removeData function (line 1881, v1.7.2): |
| 293 |
|
| 294 |
if ( jQuery.support.deleteExpando ) { |
| 295 |
delete elem[ internalKey ]; |
| 296 |
} else { |
| 297 |
try { // SVG |
| 298 |
elem.removeAttribute( internalKey ); |
| 299 |
} catch (e) { |
| 300 |
elem[ internalKey ] = null; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
In the event.add function (line 2985, v1.7.2): |
| 305 |
|
| 306 |
if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) { |
| 307 |
// Bind the global event handler to the element |
| 308 |
try { // SVG |
| 309 |
elem.addEventListener( type, eventHandle, false ); |
| 310 |
} catch(e) { |
| 311 |
if ( elem.attachEvent ) { |
| 312 |
elem.attachEvent( "on" + type, eventHandle ); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
In the event.remove function (line 3074, v1.7.2): |
| 318 |
|
| 319 |
if ( !special.teardown || special.teardown.call( elem, namespaces ) === false ) { |
| 320 |
try { // SVG |
| 321 |
elem.removeEventListener(type, elemData.handle, false); |
| 322 |
} |
| 323 |
catch (e) { |
| 324 |
if (elem.detachEvent) { |
| 325 |
elem.detachEvent("on" + type, elemData.handle); |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
In the event.fix function (line 3394, v1.7.2): |
| 331 |
|
| 332 |
if (event.target.namespaceURI == 'http://www.w3.org/2000/svg') { // SVG |
| 333 |
event.button = [1, 4, 2][event.button]; |
| 334 |
} |
| 335 |
|
| 336 |
// Add which for click: 1 === left; 2 === middle; 3 === right |
| 337 |
// Note: button is not normalized, so don't use it |
| 338 |
if ( !event.which && button !== undefined ) { |
| 339 |
event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) ); |
| 340 |
} |
| 341 |
|
| 342 |
In the Sizzle function (line 4083, v1.7.2): |
| 343 |
|
| 344 |
if ( toString.call(checkSet) === "[object Array]" ) { |
| 345 |
if ( !prune ) { |
| 346 |
results.push.apply( results, checkSet ); |
| 347 |
|
| 348 |
} else if ( context && context.nodeType === 1 ) { |
| 349 |
for ( i = 0; checkSet[i] != null; i++ ) { |
| 350 |
if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && Sizzle.contains(context, checkSet[i])) ) { |
| 351 |
results.push( set[i] || set.item(i) ); // SVG |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
} else { |
| 356 |
for ( i = 0; checkSet[i] != null; i++ ) { |
| 357 |
if ( checkSet[i] && checkSet[i].nodeType === 1 ) { |
| 358 |
results.push( set[i] || set.item(i) ); // SVG |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
} else {... |
| 363 |
|
| 364 |
In the fallback for the Sizzle makeArray function (line 4877, v1.7.2): |
| 365 |
|
| 366 |
if ( toString.call(array) === "[object Array]" ) { |
| 367 |
Array.prototype.push.apply( ret, array ); |
| 368 |
|
| 369 |
} else { |
| 370 |
if ( typeof array.length === "number" ) { |
| 371 |
for ( var l = array.length; i < l; i++ ) { |
| 372 |
ret.push( array[i] || array.item(i) ); // SVG |
| 373 |
} |
| 374 |
|
| 375 |
} else { |
| 376 |
for ( ; array[i]; i++ ) { |
| 377 |
ret.push( array[i] ); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
In the jQuery.cleandata function (line 6538, v1.7.2): |
| 383 |
|
| 384 |
if ( deleteExpando ) { |
| 385 |
delete elem[ jQuery.expando ]; |
| 386 |
|
| 387 |
} else { |
| 388 |
try { // SVG |
| 389 |
elem.removeAttribute( jQuery.expando ); |
| 390 |
} catch (e) { |
| 391 |
// Ignore |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
In the fallback getComputedStyle function (line 6727, v1.7.2): |
| 396 |
|
| 397 |
defaultView = (elem.ownerDocument ? elem.ownerDocument.defaultView : elem.defaultView); // SVG |
| 398 |
if ( defaultView && |
| 399 |
(computedStyle = defaultView.getComputedStyle( elem, null )) ) { |
| 400 |
|
| 401 |
ret = computedStyle.getPropertyValue( name ); |
| 402 |
... |
| 403 |
|
| 404 |
*/ |
| 405 |
|
| 406 |
})(jQuery); |