toolkit/modules/sessionstore/XPathGenerator.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial