1 /**
  2  * ====================================================================
  3  * About
  4  * ====================================================================
  5  * Sarissa cross browser XML library - IE XPath Emulation 
  6  * @version 0.9.9.5
  7  * @author: Copyright 2004-2007 Emmanouil Batsis, mailto: mbatsis at users full stop sourceforge full stop net
  8  *
  9  * This script emulates Internet Explorer's selectNodes and selectSingleNode
 10  * for Mozilla. Associating namespace prefixes with URIs for your XPath queries
 11  * is easy with IE's setProperty. 
 12  * USers may also map a namespace prefix to a default (unprefixed) namespace in the
 13  * source document with Sarissa.setXpathNamespaces
 14  *
 15  * ====================================================================
 16  * Licence
 17  * ====================================================================
 18  * Sarissa is free software distributed under the GNU GPL version 2 (see <a href="gpl.txt">gpl.txt</a>) or higher, 
 19  * GNU LGPL version 2.1 (see <a href="lgpl.txt">lgpl.txt</a>) or higher and Apache Software License 2.0 or higher 
 20  * (see <a href="asl.txt">asl.txt</a>). This means you can choose one of the three and use that if you like. If 
 21  * you make modifications under the ASL, i would appreciate it if you submitted those.
 22  * In case your copy of Sarissa does not include the license texts, you may find
 23  * them online in various formats at <a href="http://www.gnu.org">http://www.gnu.org</a> and 
 24  * <a href="http://www.apache.org">http://www.apache.org</a>.
 25  *
 26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 
 27  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 28  * WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE 
 29  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
 30  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 31  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 32  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 33  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 34  */
 35 if(Sarissa._SARISSA_HAS_DOM_FEATURE && document.implementation.hasFeature("XPath", "3.0")){
 36     /**
 37      * <p>SarissaNodeList behaves as a NodeList but is only used as a result to <code>selectNodes</code>,
 38      * so it also has some properties IEs proprietery object features.</p>
 39      * @private
 40      * @constructor
 41      * @argument i the (initial) list size
 42      */
 43     SarissaNodeList = function (i){
 44         this.length = i;
 45     };
 46     /** 
 47      * <p>Set an Array as the prototype object</p> 
 48      * @private
 49      */
 50     SarissaNodeList.prototype = [];
 51     /** 
 52      * <p>Inherit the Array constructor </p> 
 53      * @private
 54      */
 55     SarissaNodeList.prototype.constructor = Array;
 56     /**
 57      * <p>Returns the node at the specified index or null if the given index
 58      * is greater than the list size or less than zero </p>
 59      * <p><b>Note</b> that in ECMAScript you can also use the square-bracket
 60      * array notation instead of calling <code>item</code>
 61      * @argument i the index of the member to return
 62      * @returns the member corresponding to the given index
 63      * @private
 64      */
 65     SarissaNodeList.prototype.item = function(i) {
 66         return (i < 0 || i >= this.length)?null:this[i];
 67     };
 68     /**
 69      * <p>Emulate IE's expr property
 70      * (Here the SarissaNodeList object is given as the result of selectNodes).</p>
 71      * @returns the XPath expression passed to selectNodes that resulted in
 72      *          this SarissaNodeList
 73      * @private
 74      */
 75     SarissaNodeList.prototype.expr = "";
 76     /** dummy, used to accept IE's stuff without throwing errors */
 77     if(window.XMLDocument && (!XMLDocument.prototype.setProperty)){
 78         XMLDocument.prototype.setProperty  = function(x,y){};
 79     }
 80     /**
 81     * <p>Programmatically control namespace URI/prefix mappings for XPath
 82     * queries.</p>
 83     * <p>This method comes especially handy when used to apply XPath queries
 84     * on XML documents with a default namespace, as there is no other way
 85     * of mapping that to a prefix.</p>
 86     * <p>Using no namespace prefix in DOM Level 3 XPath queries, implies you
 87     * are looking for elements in the null namespace. If you need to look
 88     * for nodes in the default namespace, you need to map a prefix to it
 89     * first like:</p>
 90     * <pre>Sarissa.setXpathNamespaces(oDoc, "xmlns:myprefix'http://mynsURI'");</pre>
 91     * <p><b>Note 1 </b>: Use this method only if the source document features
 92     * a default namespace (without a prefix), otherwise just use IE's setProperty
 93     * (moz will rezolve non-default namespaces by itself). You will need to map that
 94     * namespace to a prefix for queries to work.</p>
 95     * <p><b>Note 2 </b>: This method calls IE's setProperty method to set the
 96     * appropriate namespace-prefix mappings, so you dont have to do that.</p>
 97     * @param oDoc The target XMLDocument to set the namespace mappings for.
 98     * @param sNsSet A whilespace-seperated list of namespace declarations as
 99     *            those would appear in an XML document. E.g.:
100     *            <code>"xmlns:xhtml='http://www.w3.org/1999/xhtml'
101     * xmlns:'http://www.w3.org/1999/XSL/Transform'"</code>
102     * @throws An error if the format of the given namespace declarations is bad.
103     */
104     Sarissa.setXpathNamespaces = function(oDoc, sNsSet) {
105         //oDoc._sarissa_setXpathNamespaces(sNsSet);
106         oDoc._sarissa_useCustomResolver = true;
107         var namespaces = sNsSet.indexOf(" ")>-1?sNsSet.split(" "):[sNsSet];
108         oDoc._sarissa_xpathNamespaces = [];
109         for(var i=0;i < namespaces.length;i++){
110             var ns = namespaces[i];
111             var colonPos = ns.indexOf(":");
112             var assignPos = ns.indexOf("=");
113             if(colonPos > 0 && assignPos > colonPos+1){
114                 var prefix = ns.substring(colonPos+1, assignPos);
115                 var uri = ns.substring(assignPos+2, ns.length-1);
116                 oDoc._sarissa_xpathNamespaces[prefix] = uri;
117             }else{
118                 throw "Bad format on namespace declaration(s) given";
119             }
120         }
121     };
122     /**
123     * @private Flag to control whether a custom namespace resolver should
124     *          be used, set to true by Sarissa.setXpathNamespaces
125     */
126     XMLDocument.prototype._sarissa_useCustomResolver = false;
127     /** @private */
128     XMLDocument.prototype._sarissa_xpathNamespaces = [];
129     /**
130     * <p>Extends the XMLDocument to emulate IE's selectNodes.</p>
131     * @argument sExpr the XPath expression to use
132     * @argument contextNode this is for internal use only by the same
133     *           method when called on Elements
134     * @returns the result of the XPath search as a SarissaNodeList
135     * @throws An error if no namespace URI is found for the given prefix.
136     */
137     XMLDocument.prototype.selectNodes = function(sExpr, contextNode, returnSingle){
138         var nsDoc = this;
139         var nsresolver;
140         if(this._sarissa_useCustomResolver){
141             nsresolver = function(prefix){
142                 var s = nsDoc._sarissa_xpathNamespaces[prefix];
143                 if(s){
144                     return s;
145                 }
146                 else {
147                     throw "No namespace URI found for prefix: '" + prefix+"'";
148                 }
149             };
150         }
151         else{
152             nsresolver = this.createNSResolver(this.documentElement);
153         }
154         var result = null;
155         if(!returnSingle){
156             var oResult = this.evaluate(sExpr,
157                 (contextNode?contextNode:this),
158                 nsresolver,
159                 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
160             var nodeList = new SarissaNodeList(oResult.snapshotLength);
161             nodeList.expr = sExpr;
162             for(var i=0;i<nodeList.length;i++){
163                 nodeList[i] = oResult.snapshotItem(i);
164             }
165             result = nodeList;
166         }
167         else {
168             result = this.evaluate(sExpr,
169                 (contextNode?contextNode:this),
170                 nsresolver,
171                 XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
172         }
173         return result;      
174     };
175     /**
176     * <p>Extends the Element to emulate IE's selectNodes</p>
177     * @argument sExpr the XPath expression to use
178     * @returns the result of the XPath search as an (Sarissa)NodeList
179     * @throws An
180     *             error if invoked on an HTML Element as this is only be
181     *             available to XML Elements.
182     */
183     Element.prototype.selectNodes = function(sExpr){
184         var doc = this.ownerDocument;
185         if(doc.selectNodes){
186             return doc.selectNodes(sExpr, this);
187         }
188         else{
189             throw "Method selectNodes is only supported by XML Elements";
190         }
191     };
192     /**
193     * <p>Extends the XMLDocument to emulate IE's selectSingleNode.</p>
194     * @argument sExpr the XPath expression to use
195     * @argument contextNode this is for internal use only by the same
196     *           method when called on Elements
197     * @returns the result of the XPath search as an (Sarissa)NodeList
198     */
199     XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode){
200         var ctx = contextNode?contextNode:null;
201         return this.selectNodes(sExpr, ctx, true);
202     };
203     /**
204     * <p>Extends the Element to emulate IE's selectSingleNode.</p>
205     * @argument sExpr the XPath expression to use
206     * @returns the result of the XPath search as an (Sarissa)NodeList
207     * @throws An error if invoked on an HTML Element as this is only be
208     *             available to XML Elements.
209     */
210     Element.prototype.selectSingleNode = function(sExpr){
211         var doc = this.ownerDocument;
212         if(doc.selectSingleNode){
213             return doc.selectSingleNode(sExpr, this);
214         }
215         else{
216             throw "Method selectNodes is only supported by XML Elements";
217         }
218     };
219     Sarissa.IS_ENABLED_SELECT_NODES = true;
220 }