Annotation of kupu/common/sarissa_ieemu_xpath.js, revision 1.1.1.1
1.1 dwinter 1: /*****************************************************************************
2: *
3: * Sarissa XML library version 0.9.6
4: * Copyright (c) 2003 Manos Batsis,
5: * mailto: mbatsis at users full stop sourceforge full stop net
6: * This software is distributed under the Kupu License. See
7: * LICENSE.txt for license text. See the Sarissa homepage at
8: * http://sarissa.sourceforge.net for more information.
9: *
10: *****************************************************************************
11:
12: * ====================================================================
13: * About
14: * ====================================================================
15: * Sarissa cross browser XML library - IE XPath Emulation
16: * @version 0.9.6
17: * @author: Manos Batsis, mailto: mbatsis at users full stop sourceforge full stop net
18: *
19: * This script emulates Internet Explorer's selectNodes and selectSingleNode
20: * for Mozilla. Associating namespace prefixes with URIs for your XPath queries
21: * is easy with IE's setProperty.
22: * USers may also map a namespace prefix to a default (unprefixed) namespace in the
23: * source document with Sarissa.setXpathNamespaces
24: *
25: */
26: if(_SARISSA_HAS_DOM_FEATURE && document.implementation.hasFeature("XPath", "3.0")){
27: /**
28: * <p>SarissaNodeList behaves as a NodeList but is only used as a result to <code>selectNodes</code>,
29: * so it also has some properties IEs proprietery object features.</p>
30: * @private
31: * @constructor
32: * @argument i the (initial) list size
33: */
34: function SarissaNodeList(i){
35: this.length = i;
36: };
37: /** <p>Set an Array as the prototype object</p> */
38: SarissaNodeList.prototype = new Array(0);
39: /** <p>Inherit the Array constructor </p> */
40: SarissaNodeList.prototype.constructor = Array;
41: /**
42: * <p>Returns the node at the specified index or null if the given index
43: * is greater than the list size or less than zero </p>
44: * <p><b>Note</b> that in ECMAScript you can also use the square-bracket
45: * array notation instead of calling <code>item</code>
46: * @argument i the index of the member to return
47: * @returns the member corresponding to the given index
48: */
49: SarissaNodeList.prototype.item = function(i) {
50: return (i < 0 || i >= this.length)?null:this[i];
51: };
52: /**
53: * <p>Emulate IE's expr property
54: * (Here the SarissaNodeList object is given as the result of selectNodes).</p>
55: * @returns the XPath expression passed to selectNodes that resulted in
56: * this SarissaNodeList
57: */
58: SarissaNodeList.prototype.expr = "";
59: /** dummy, used to accept IE's stuff without throwing errors */
60: XMLDocument.prototype.setProperty = function(x,y){};
61: /**
62: * <p>Programmatically control namespace URI/prefix mappings for XPath
63: * queries.</p>
64: * <p>This method comes especially handy when used to apply XPath queries
65: * on XML documents with a default namespace, as there is no other way
66: * of mapping that to a prefix.</p>
67: * <p>Using no namespace prefix in DOM Level 3 XPath queries, implies you
68: * are looking for elements in the null namespace. If you need to look
69: * for nodes in the default namespace, you need to map a prefix to it
70: * first like:</p>
71: * <pre>Sarissa.setXpathNamespaces(oDoc, "xmlns:myprefix=&aposhttp://mynsURI&apos");</pre>
72: * <p><b>Note 1 </b>: Use this method only if the source document features
73: * a default namespace (without a prefix), otherwise just use IE's setProperty
74: * (moz will rezolve non-default namespaces by itself). You will need to map that
75: * namespace to a prefix for queries to work.</p>
76: * <p><b>Note 2 </b>: This method calls IE's setProperty method to set the
77: * appropriate namespace-prefix mappings, so you dont have to do that.</p>
78: * @param oDoc The target XMLDocument to set the namespace mappings for.
79: * @param sNsSet A whilespace-seperated list of namespace declarations as
80: * those would appear in an XML document. E.g.:
81: * <code>"xmlns:xhtml='http://www.w3.org/1999/xhtml'
82: * xmlns:'http://www.w3.org/1999/XSL/Transform'"</code>
83: * @throws An error if the format of the given namespace declarations is bad.
84: */
85: Sarissa.setXpathNamespaces = function(oDoc, sNsSet) {
86: //oDoc._sarissa_setXpathNamespaces(sNsSet);
87: oDoc._sarissa_useCustomResolver = true;
88: var namespaces = sNsSet.indexOf(" ")>-1?sNsSet.split(" "):new Array(sNsSet);
89: oDoc._sarissa_xpathNamespaces = new Array(namespaces.length);
90: for(var i=0;i < namespaces.length;i++){
91: var ns = namespaces[i];
92: var colonPos = ns.indexOf(":");
93: var assignPos = ns.indexOf("=");
94: if(colonPos == 5 && assignPos > colonPos+2){
95: var prefix = ns.substring(colonPos+1, assignPos);
96: var uri = ns.substring(assignPos+2, ns.length-1);
97: oDoc._sarissa_xpathNamespaces[prefix] = uri;
98: }else{
99: throw "Bad format on namespace declaration(s) given";
100: };
101: };
102: };
103: /**
104: * @private Flag to control whether a custom namespace resolver should
105: * be used, set to true by Sarissa.setXpathNamespaces
106: */
107: XMLDocument.prototype._sarissa_useCustomResolver = false;
108: /** @private */
109: XMLDocument.prototype._sarissa_xpathNamespaces = new Array();
110: /**
111: * <p>Extends the XMLDocument to emulate IE's selectNodes.</p>
112: * @argument sExpr the XPath expression to use
113: * @argument contextNode this is for internal use only by the same
114: * method when called on Elements
115: * @returns the result of the XPath search as a SarissaNodeList
116: * @throws An error if no namespace URI is found for the given prefix.
117: */
118: XMLDocument.prototype.selectNodes = function(sExpr, contextNode){
119: var nsDoc = this;
120: var nsresolver = this._sarissa_useCustomResolver
121: ? function(prefix){
122: var s = nsDoc._sarissa_xpathNamespaces[prefix];
123: if(s)return s;
124: else throw "No namespace URI found for prefix: '" + prefix+"'";
125: }
126: : this.createNSResolver(this.documentElement);
127: var oResult = this.evaluate(sExpr,
128: (contextNode?contextNode:this),
129: nsresolver,
130: XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
131: var nodeList = new SarissaNodeList(oResult.snapshotLength);
132: nodeList.expr = sExpr;
133: for(var i=0;i<nodeList.length;i++)
134: nodeList[i] = oResult.snapshotItem(i);
135: return nodeList;
136: };
137: /**
138: * <p>Extends the Element to emulate IE's selectNodes</p>
139: * @argument sExpr the XPath expression to use
140: * @returns the result of the XPath search as an (Sarissa)NodeList
141: * @throws An
142: * error if invoked on an HTML Element as this is only be
143: * available to XML Elements.
144: */
145: Element.prototype.selectNodes = function(sExpr){
146: var doc = this.ownerDocument;
147: if(doc.selectNodes)
148: return doc.selectNodes(sExpr, this);
149: else
150: throw "Method selectNodes is only supported by XML Elements";
151: };
152: /**
153: * <p>Extends the XMLDocument to emulate IE's selectSingleNodes.</p>
154: * @argument sExpr the XPath expression to use
155: * @argument contextNode this is for internal use only by the same
156: * method when called on Elements
157: * @returns the result of the XPath search as an (Sarissa)NodeList
158: */
159: XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode){
160: var ctx = contextNode?contextNode:null;
161: sExpr = "("+sExpr+")[1]";
162: var nodeList = this.selectNodes(sExpr, ctx);
163: if(nodeList.length > 0)
164: return nodeList.item(0);
165: else
166: return null;
167: };
168: /**
169: * <p>Extends the Element to emulate IE's selectNodes.</p>
170: * @argument sExpr the XPath expression to use
171: * @returns the result of the XPath search as an (Sarissa)NodeList
172: * @throws An error if invoked on an HTML Element as this is only be
173: * available to XML Elements.
174: */
175: Element.prototype.selectSingleNode = function(sExpr){
176: var doc = this.ownerDocument;
177: if(doc.selectSingleNode)
178: return doc.selectSingleNode(sExpr, this);
179: else
180: throw "Method selectNodes is only supported by XML Elements";
181: };
182: Sarissa.IS_ENABLED_SELECT_NODES = true;
183: };
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>