1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/sessionstore/XPathGenerator.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +this.EXPORTED_SYMBOLS = ["XPathGenerator"]; 1.11 + 1.12 +this.XPathGenerator = { 1.13 + // these two hashes should be kept in sync 1.14 + namespaceURIs: { 1.15 + "xhtml": "http://www.w3.org/1999/xhtml", 1.16 + "xul": "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 1.17 + }, 1.18 + namespacePrefixes: { 1.19 + "http://www.w3.org/1999/xhtml": "xhtml", 1.20 + "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul": "xul" 1.21 + }, 1.22 + 1.23 + /** 1.24 + * Generates an approximate XPath query to an (X)HTML node 1.25 + */ 1.26 + generate: function sss_xph_generate(aNode) { 1.27 + // have we reached the document node already? 1.28 + if (!aNode.parentNode) 1.29 + return ""; 1.30 + 1.31 + // Access localName, namespaceURI just once per node since it's expensive. 1.32 + let nNamespaceURI = aNode.namespaceURI; 1.33 + let nLocalName = aNode.localName; 1.34 + 1.35 + let prefix = this.namespacePrefixes[nNamespaceURI] || null; 1.36 + let tag = (prefix ? prefix + ":" : "") + this.escapeName(nLocalName); 1.37 + 1.38 + // stop once we've found a tag with an ID 1.39 + if (aNode.id) 1.40 + return "//" + tag + "[@id=" + this.quoteArgument(aNode.id) + "]"; 1.41 + 1.42 + // count the number of previous sibling nodes of the same tag 1.43 + // (and possible also the same name) 1.44 + let count = 0; 1.45 + let nName = aNode.name || null; 1.46 + for (let n = aNode; (n = n.previousSibling); ) 1.47 + if (n.localName == nLocalName && n.namespaceURI == nNamespaceURI && 1.48 + (!nName || n.name == nName)) 1.49 + count++; 1.50 + 1.51 + // recurse until hitting either the document node or an ID'd node 1.52 + return this.generate(aNode.parentNode) + "/" + tag + 1.53 + (nName ? "[@name=" + this.quoteArgument(nName) + "]" : "") + 1.54 + (count ? "[" + (count + 1) + "]" : ""); 1.55 + }, 1.56 + 1.57 + /** 1.58 + * Resolves an XPath query generated by XPathGenerator.generate 1.59 + */ 1.60 + resolve: function sss_xph_resolve(aDocument, aQuery) { 1.61 + let xptype = Components.interfaces.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE; 1.62 + return aDocument.evaluate(aQuery, aDocument, this.resolveNS, xptype, null).singleNodeValue; 1.63 + }, 1.64 + 1.65 + /** 1.66 + * Namespace resolver for the above XPath resolver 1.67 + */ 1.68 + resolveNS: function sss_xph_resolveNS(aPrefix) { 1.69 + return XPathGenerator.namespaceURIs[aPrefix] || null; 1.70 + }, 1.71 + 1.72 + /** 1.73 + * @returns valid XPath for the given node (usually just the local name itself) 1.74 + */ 1.75 + escapeName: function sss_xph_escapeName(aName) { 1.76 + // we can't just use the node's local name, if it contains 1.77 + // special characters (cf. bug 485482) 1.78 + return /^\w+$/.test(aName) ? aName : 1.79 + "*[local-name()=" + this.quoteArgument(aName) + "]"; 1.80 + }, 1.81 + 1.82 + /** 1.83 + * @returns a properly quoted string to insert into an XPath query 1.84 + */ 1.85 + quoteArgument: function sss_xph_quoteArgument(aArg) { 1.86 + return !/'/.test(aArg) ? "'" + aArg + "'" : 1.87 + !/"/.test(aArg) ? '"' + aArg + '"' : 1.88 + "concat('" + aArg.replace(/'+/g, "',\"$&\",'") + "')"; 1.89 + }, 1.90 + 1.91 + /** 1.92 + * @returns an XPath query to all savable form field nodes 1.93 + */ 1.94 + get restorableFormNodes() { 1.95 + // for a comprehensive list of all available <INPUT> types see 1.96 + // http://mxr.mozilla.org/mozilla-central/search?string=kInputTypeTable 1.97 + let ignoreTypes = ["password", "hidden", "button", "image", "submit", "reset"]; 1.98 + // XXXzeniko work-around until lower-case has been implemented (bug 398389) 1.99 + let toLowerCase = '"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"'; 1.100 + let ignore = "not(translate(@type, " + toLowerCase + ")='" + 1.101 + ignoreTypes.join("' or translate(@type, " + toLowerCase + ")='") + "')"; 1.102 + let formNodesXPath = "//textarea|//select|//xhtml:textarea|//xhtml:select|" + 1.103 + "//input[" + ignore + "]|//xhtml:input[" + ignore + "]"; 1.104 + 1.105 + // Special case for about:config's search field. 1.106 + formNodesXPath += '|/xul:window[@id="config"]//xul:textbox[@id="textbox"]'; 1.107 + 1.108 + delete this.restorableFormNodes; 1.109 + return (this.restorableFormNodes = formNodesXPath); 1.110 + } 1.111 +};