2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2007-2012 John J. Foerch
5 * Use, modification, and distribution are subject to the terms specified in the
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;
25 dom_generator.prototype = {
26 constructor: dom_generator,
27 element: function (tag, parent) {
28 var node = this.document.createElementNS(this.ns, tag);
30 if (parent != null && (parent instanceof Ci.nsIDOMNode)) {
31 parent.appendChild(node);
34 for (var nargs = arguments.length; i < nargs; i += 2)
35 node.setAttribute(arguments[i], arguments[i+1]);
38 text: function (str, parent) {
39 var node = this.document.createTextNode(str);
41 parent.appendChild(node);
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);
50 parent.appendChild(node);
53 add_stylesheet: function (url) {
54 var head = this.document.documentElement.firstChild;
55 this.stylesheet_link(url, head);
61 * dom_add_class adds a css class to the given dom node.
63 function dom_add_class (node, cssclass) {
65 var cs = node.className.split(" ");
66 if (cs.indexOf(cssclass) != -1)
69 node.className = cs.join(" ");
71 node.className = cssclass;
75 * dom_remove_class removes the given css class from the given dom node.
77 function dom_remove_class (node, cssclass) {
80 var classes = node.className.split(" ");
81 classes = classes.filter(function (x) { return x != cssclass; });
82 node.className = classes.join(" ");
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.
90 function dom_node_flash (node, cssclass) {
91 dom_add_class(node, cssclass);
94 dom_remove_class(node, cssclass);
101 * xpath_lookup_namespace is a namespace resolver that may be passed to
104 function xpath_lookup_namespace (prefix) {
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.
118 function xpath_find_node (doc, xpath) {
119 var r = doc.evaluate(xpath, doc, xpath_lookup_namespace,
120 Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE,
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.
130 function xpath_find_any (doc, xpath) {
131 return doc.evaluate(xpath, doc, xpath_lookup_namespace,
132 Ci.nsIDOMXPathResult.ANY_TYPE, null);