1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
this.EXPORTED_SYMBOLS = ["XPathGenerator"];
this.XPathGenerator = {
// these two hashes should be kept in sync
namespaceURIs: { "xhtml": "http://www.w3.org/1999/xhtml" },
namespacePrefixes: { "http://www.w3.org/1999/xhtml": "xhtml" },
/**
* Generates an approximate XPath query to an (X)HTML node
*/
generate: function sss_xph_generate(aNode) {
// have we reached the document node already?
if (!aNode.parentNode)
return "";
// Access localName, namespaceURI just once per node since it's expensive.
let nNamespaceURI = aNode.namespaceURI;
let nLocalName = aNode.localName;
let prefix = this.namespacePrefixes[nNamespaceURI] || null;
let tag = (prefix ? prefix + ":" : "") + this.escapeName(nLocalName);
// stop once we've found a tag with an ID
if (aNode.id)
return "//" + tag + "[@id=" + this.quoteArgument(aNode.id) + "]";
// count the number of previous sibling nodes of the same tag
// (and possible also the same name)
let count = 0;
let nName = aNode.name || null;
for (let n = aNode; (n = n.previousSibling); )
if (n.localName == nLocalName && n.namespaceURI == nNamespaceURI &&
(!nName || n.name == nName))
count++;
// recurse until hitting either the document node or an ID'd node
return this.generate(aNode.parentNode) + "/" + tag +
(nName ? "[@name=" + this.quoteArgument(nName) + "]" : "") +
(count ? "[" + (count + 1) + "]" : "");
},
/**
* Resolves an XPath query generated by XPathGenerator.generate
*/
resolve: function sss_xph_resolve(aDocument, aQuery) {
let xptype = Components.interfaces.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
return aDocument.evaluate(aQuery, aDocument, this.resolveNS, xptype, null).singleNodeValue;
},
/**
* Namespace resolver for the above XPath resolver
*/
resolveNS: function sss_xph_resolveNS(aPrefix) {
return XPathGenerator.namespaceURIs[aPrefix] || null;
},
/**
* @returns valid XPath for the given node (usually just the local name itself)
*/
escapeName: function sss_xph_escapeName(aName) {
// we can't just use the node's local name, if it contains
// special characters (cf. bug 485482)
return /^\w+$/.test(aName) ? aName :
"*[local-name()=" + this.quoteArgument(aName) + "]";
},
/**
* @returns a properly quoted string to insert into an XPath query
*/
quoteArgument: function sss_xph_quoteArgument(aArg) {
return !/'/.test(aArg) ? "'" + aArg + "'" :
!/"/.test(aArg) ? '"' + aArg + '"' :
"concat('" + aArg.replace(/'+/g, "',\"$&\",'") + "')";
},
/**
* @returns an XPath query to all savable form field nodes
*/
get restorableFormNodes() {
// for a comprehensive list of all available <INPUT> types see
// http://mxr.mozilla.org/mozilla-central/search?string=kInputTypeTable
let ignoreTypes = ["password", "hidden", "button", "image", "submit", "reset"];
// XXXzeniko work-around until lower-case has been implemented (bug 398389)
let toLowerCase = '"ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"';
let ignore = "not(translate(@type, " + toLowerCase + ")='" +
ignoreTypes.join("' or translate(@type, " + toLowerCase + ")='") + "')";
let formNodesXPath = "//textarea|//select|//xhtml:textarea|//xhtml:select|" +
"//input[" + ignore + "]|//xhtml:input[" + ignore + "]";
delete this.restorableFormNodes;
return (this.restorableFormNodes = formNodesXPath);
}
};
|