add new repository link
[voodoo.git] / scripts / jquery.xmlattr.js
blob0e1d3fd488cce160a9bb095cdeeb30dd95865654
1 /* -------------------------------------------------------------------------
3 jquery.xmlattr.js
4 version 0.1
6 Dual licensed under the MIT and GPL licenses.
7 ----------------------------------------------------------------------------
8 Copyright (C) 2008 Malthe Borch <mborch@gmail.com>
9 ----------------------------------------------------------------------------
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
28 ------------------------------------------------------------------------- */
30 (function($) {
31 var known_namespaces = [];
32 var reverse_prefix_map = {};
34 $.fn.xmlattr = function(name) {
35 // parse name using James Clark notation, e.g. "{ns}name"
36 var params = name.substring(1).split('}');
37 if (params.length == 1) return $(this).attr(name);
38 var ns = params[0];
39 var name = params[1];
41 // for each namespace uri, we have to find out what the current
42 // prefix assignment is in the document; if the namespace is not
43 // known, traverse parent chain until we find the corresponding
44 // namespace prefix mapping attribute
45 if ($.inArray(ns, known_namespaces) == -1) {
46 var items = $(this).parents().andSelf();
47 for (var i=0; i<items.length; i++) {
48 var attributes = items[i].attributes;
49 for (var j=0; j<attributes.length; j++) {
50 var p = attributes[j];
51 var k = p.name.indexOf("xmlns:");
52 if (k == 0) {
53 var prefix = p.name.substring(6);
54 known_namespaces.push(p.value);
55 reverse_prefix_map[p.value] = prefix;
56 if (ns == p.value) {
57 return $(this).attr(prefix+":"+name);
62 } else {
63 var prefix = reverse_prefix_map[ns];
64 return $(this).attr(prefix+":"+name);
67 })(jQuery);