isearch-backspace, isearch-done: docstrings
[conkeror.git] / modules / dom.js
blob6c76b66bb81a1e1442983a8b2da20c642fc5c02d
1 /**
2  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2007-2012 John J. Foerch
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 const XHTML_NS = "http://www.w3.org/1999/xhtml";
10 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
11 const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
12 const XLINK_NS = "http://www.w3.org/1999/xlink";
13 const SVG_NS = "http://www.w3.org/2000/svg";
16 function create_XUL (window, tag_name) {
17     return window.document.createElementNS(XUL_NS, tag_name);
21 function dom_generator (document, ns) {
22     this.document = document;
23     this.ns = ns;
25 dom_generator.prototype = {
26     constructor: dom_generator,
27     element: function (tag, parent) {
28         var node = this.document.createElementNS(this.ns, tag);
29         var i = 1;
30         if (parent != null && (parent instanceof Ci.nsIDOMNode)) {
31             parent.appendChild(node);
32             i = 2;
33         }
34         for (var nargs = arguments.length; i < nargs; i += 2)
35             node.setAttribute(arguments[i], arguments[i+1]);
36         return node;
37     },
38     text: function (str, parent) {
39         var node = this.document.createTextNode(str);
40         if (parent)
41             parent.appendChild(node);
42         return node;
43     },
44     stylesheet_link: function (href, parent) {
45         var node = this.element("link");
46         node.setAttribute("rel", "stylesheet");
47         node.setAttribute("type", "text/css");
48         node.setAttribute("href", href);
49         if (parent)
50             parent.appendChild(node);
51         return node;
52     },
53     add_stylesheet: function (url) {
54         var head = this.document.documentElement.firstChild;
55         this.stylesheet_link(url, head);
56     }
60 /**
61  * dom_add_class adds a css class to the given dom node.
62  */
63 function dom_add_class (node, cssclass) {
64     if (node.className) {
65         var cs = node.className.split(" ");
66         if (cs.indexOf(cssclass) != -1)
67             return;
68         cs.push(cssclass);
69         node.className = cs.join(" ");
70     } else
71         node.className = cssclass;
74 /**
75  * dom_remove_class removes the given css class from the given dom node.
76  */
77 function dom_remove_class (node, cssclass) {
78     if (! node.className)
79         return;
80     var classes = node.className.split(" ");
81     classes = classes.filter(function (x) { return x != cssclass; });
82     node.className = classes.join(" ");
86 /**
87  * dom_node_flash adds the given cssclass to the node for a brief interval.
88  * this class can be styled, to create a flashing effect.
89  */
90 function dom_node_flash (node, cssclass) {
91     dom_add_class(node, cssclass);
92     call_after_timeout(
93         function () {
94             dom_remove_class(node, cssclass);
95         },
96         400);
101  * xpath_lookup_namespace is a namespace resolver that may be passed to
102  * document.evaluate.
103  */
104 function xpath_lookup_namespace (prefix) {
105     return {
106         xhtml: XHTML_NS,
107         m: MATHML_NS,
108         xul: XUL_NS,
109         svg: SVG_NS
110     }[prefix] || null;
114  * xpath_find_node takes a document and a string xpath expression,
115  * performs a FIRST_ORDERED_NODE_TYPE lookup with the expression, and
116  * returns the single node in the result set, if any.
117  */
118 function xpath_find_node (doc, xpath) {
119     var r = doc.evaluate(xpath, doc, xpath_lookup_namespace,
120                          Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE,
121                          null);
122     return r.singleNodeValue;
126  * xpath_find_any takes a document and a string xpath expression, performs
127  * an ANY_TYPE lookup with the expression, and returns an XPathResult
128  * object that gives the result set.
129  */
130 function xpath_find_any (doc, xpath) {
131     return doc.evaluate(xpath, doc, xpath_lookup_namespace,
132                         Ci.nsIDOMXPathResult.ANY_TYPE, null);
135 provide("dom");