]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.webapp/src/main/webapp/source/class/org/argeo/slc/web/util/Element.js
Huge refactoring
[gpl/argeo-slc.git] / org.argeo.slc.webapp / src / main / webapp / source / class / org / argeo / slc / web / util / Element.js
1 /* ************************************************************************
2
3 Overrides the Qooxdoo qx.xml.Element to handle the namespace prefixes!!
4
5 ************************************************************************ */
6
7 /**
8 * Cross browser XML Element API
9 *
10 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/81f3de54-3b79-46dc-8e01-73ca2d94cdb5.asp
11 * http://developer.mozilla.org/en/docs/Parsing_and_serializing_XML
12 */
13 qx.Class.define("org.argeo.slc.web.util.Element",
14 {
15
16 statics :
17 {
18 /**
19 * Selects the first XmlNode that matches the XPath expression.
20 *
21 * @param element {Element | Document} root element for the search
22 * @param query {String} XPath query
23 * @param NSMap (Object) A map matching namespace prefixes to namespace URIS;
24 * @return {Element} first matching element
25 * @signature function(element, query)
26 */
27 selectSingleNode : qx.core.Variant.select("qx.client",
28 {
29 "mshtml|opera": function(element, query, NSMap) {
30 if(NSMap){
31 var namespaces = [];
32 var i=0;
33 for(var prefix in NSMap){
34 namespaces[i] = 'xmlns:'+prefix+'="'+NSMap[prefix]+'"';
35 i++;
36 }
37 var doc = element.ownerDocument || element;
38 doc.setProperty('SelectionNamespaces', namespaces.join(" "));
39 }
40 try{
41 return element.selectSingleNode(query);
42 }catch(err){}
43 },
44
45 "default": function(element, query, NSMap)
46 {
47 if(!this.__xpe) {
48 this.__xpe = new XPathEvaluator();
49 }
50
51 var xpe = this.__xpe;
52
53 try {
54 var resolver;
55 if(NSMap){
56 resolver = function(prefix){
57 return NSMap[prefix] || null;
58 }
59 }else{
60 resolver = xpe.createNSResolver(element);
61 }
62 //return xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
63 return xpe.evaluate(query, element, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
64 } catch(err) {
65 throw new Error("selectSingleNode: query: " + query + ", element: " + element + ", error: " + err);
66 }
67 }
68 }),
69
70
71 /**
72 * Selects a list of nodes matching the XPath expression.
73 *
74 * @param element {Element | Document} root element for the search
75 * @param query {String} XPath query
76 * @param NSMap {Map} Mapping between namespaces prefixes and URI.
77 * @return {Element[]} List of matching elements
78 * @signature function(element, query)
79 */
80 selectNodes : qx.core.Variant.select("qx.client",
81 {
82 "mshtml|opera": function(element, query, NSMap) {
83 if(NSMap){
84 var namespaces = [];
85 var i=0;
86 for(var prefix in NSMap){
87 namespaces[i] = 'xmlns:'+prefix+'="'+NSMap[prefix]+'"';
88 i++;
89 }
90 var doc = element.ownerDocument || element;
91 doc.setProperty('SelectionNamespaces', namespaces.join(" "));
92 }
93 return element.selectNodes(query);
94 },
95
96 "default": function(element, query, NSMap)
97 {
98 var xpe = this.__xpe;
99
100 if(!xpe) {
101 this.__xpe = xpe = new XPathEvaluator();
102 }
103
104 try {
105 var resolver;
106 if(NSMap){
107 resolver = function(prefix){
108 return NSMap[prefix] || null;
109 }
110 }else{
111 resolver = xpe.createNSResolver(element);
112 }
113 //var result = xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
114 var result = xpe.evaluate(query, element, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
115 } catch(err) {
116 throw new Error("selectNodes: query: " + query + ", element: " + element + ", error: " + err);
117 }
118
119 var nodes = [];
120 for (var i=0; i<result.snapshotLength; i++) {
121 nodes[i] = result.snapshotItem(i);
122 }
123
124 return nodes;
125 }
126 }),
127
128
129 /**
130 * Returns a list of elements with the given tag name belonging to the given namespace (http://developer.mozilla.org/en/docs/DOM:element.getElementsByTagNameNS).
131 *
132 * @param element {Element | Document} the element from where the search should start.
133 * Note that only the descendants of this element are included in the search, not the node itself.
134 * @param namespaceURI {var} is the namespace URI of elements to look for . For example, if you need to look
135 * for XHTML elements, use the XHTML namespace URI, <tt>http://www.w3.org/1999/xhtml</tt>.
136 * @param tagname {String} the tagname to look for
137 * @return {Element[]} a list of found elements in the order they appear in the tree.
138 * @signature function(element, namespaceURI, tagname)
139 */
140 getElementsByTagNameNS : qx.core.Variant.select("qx.client",
141 {
142 "mshtml": function(element, namespaceURI, tagname)
143 {
144 var doc = element.ownerDocument || element;
145
146 doc.setProperty("SelectionLanguage", "XPath");
147 doc.setProperty("SelectionNamespaces", "xmlns:ns='" + namespaceURI + "'");
148
149 return qx.xml.Element.selectNodes(element, 'descendant-or-self::ns:' + tagname);
150 },
151
152 "default": function(element, namespaceURI, tagname) {
153 return element.getElementsByTagNameNS(namespaceURI, tagname);
154 }
155 }),
156
157
158 /**
159 * Selects the first XmlNode that matches the XPath expression and returns the text content of the element
160 *
161 * @param element {Element|Document} root element for the search
162 * @param query {String} XPath query
163 * @param NSMap {Object} Mapping between NS prefix / uri
164 * @return {String} the joined text content of the found element or null if not appropriate.
165 * @signature function(element, query)
166 */
167 getSingleNodeText : function(element, query, NSMap)
168 {
169 var node = this.selectSingleNode(element, query, NSMap);
170 return qx.dom.Node.getText(node);
171 }
172 }
173 });