Fixups for minibuffer text handling change
[conkeror.git] / content / dom-utils.js
blob6d2a4a6bc3492b739c60527b3d11b9f17f088fb3
2 ///
3 /// XUL DOM Procedures
4 ///
5 /// (layer: xuldom)
7 function xul_createappendchild (node, childtype, attributes)
9     const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
10     var child = document.createElementNS (XUL_NS, childtype);
11     var i, j;
12     for (i = 0, j = 1; j < attributes.length; i+=2, j+=2)
13     {
14         if (attributes[j])
15             child.setAttribute (attributes[i], attributes[j]);
16     }
17     node.appendChild (child);
18     return child;
21 ///
22 /// XUL DOM Menu Procedures
23 ///
24 /// (layer:   xuldommenu
25 ///  depends: xuldom)
27 function add_submenu (menu, id, label, key)
29     return xul_createappendchild (menu, "menu", ["id", id, "label", label, "accesskey", key]);
32 function add_menupopup (menu)
34     return xul_createappendchild (menu, "menupopup", []);
37 function add_menuitem (menu, id, label, key, oncommand)
39     return xul_createappendchild (menu, "menuitem", ["id", id, "label", label, "accesskey", key, "oncommand", oncommand]);
42 function add_menuseparator (menu, id)
44     return xul_createappendchild (menu, "menuseparator", ["id", id]);
57 function node_mathml_p (node)
59     const NS_MathML = "http://www.w3.org/1998/Math/MathML";
60     if ((node.nodeType == Node.TEXT_NODE &&  // JJF: where is Node.TEXT_NODE defined?  outer space?
61          node.parentNode.namespaceURI == NS_MathML)
62         || (node.namespaceURI == NS_MathML))
63         return true;
64     return false;
68 function text_selected_p () {
69     return (document.commandDispatcher.focusedWindow.getSelection().toString().length > 0);
73 function  content_selected_p () {
74     return !document.commandDispatcher.focusedWindow.getSelection().isCollapsed;
79 function image_node_p (node)
81     if (node instanceof Components.interfaces.nsIImageLoadingContent && node.currentURI) 
82         return true;
83     return false;
86 function image_node_loaded_p (node)
88     var request = node.getRequest (Components.interfaces.nsIImageLoadingContent.CURRENT_REQUEST);
89     if (request && (request.imageStatus & request.STATUS_SIZE_AVAILABLE))
90         return true;
91     return false;
94 function image_node_url_spec (node)
96     return node.currentURI.spec;
99 function image_standalone_p (node)
101     if (node.ownerDocument instanceof ImageDocument)
102         return true;
103     return false;
106 function html_input_node_p (node)
108     return node instanceof HTMLInputElement;
112 function html_textarea_node_p (node)
114     return node instanceof HTMLTextAreaElement;
118 function html_html_node_p (node)
120     return node instanceof HTMLHtmlElement;
124 function textbox_node_p (node)
126     // JJF: I'm not quite sure why password inputs count as textboxes, but
127     // this is right out of firefox.
128     if (node instanceof HTMLInputElement)
129         return (node.type == "text" || node.type == "password");
131     return (node instanceof HTMLTextAreaElement);
135 function keyword_field_node_p (node)
137     // JJF: this is out of firefox.. not quite sure yet what it's useful for.
138     var form = node.form;
139     if (!form)
140         return false;
141     var method = form.method.toUpperCase();
143     // These are the following types of forms we can create keywords for:
144     //
145     // method   encoding type       can create keyword
146     // GET      *                                 YES
147     //          *                                 YES
148     // POST                                       YES
149     // POST     application/x-www-form-urlencoded YES
150     // POST     text/plain                        NO (a little tricky to do)
151     // POST     multipart/form-data               NO
152     // POST     everything else                   YES
153     return (method == "GET" || method == "") ||
154         (form.enctype != "text/plain") && (form.enctype != "multipart/form-data");
158 function xul_node_p (node) {
159     const xulNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
160     return (node.namespaceURI == xulNS);
164 function url_saveable_p (url_s) {
165     var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
166     var uri;
167     try { uri = ioService.newURI (url_s, null, null); }
168     catch (ex) { return false; }
169     var protocol = uri.scheme; // can be |undefined|
170     return protocol && !(protocol == "mailto"     ||
171                          protocol == "javascript" ||
172                          protocol == "news"       ||
173                          protocol == "snews"      );
177 function metadata_item_p (node) {
178     // a metadata_item is an item for which, in firefox, you can view the
179     // Properties window.
180     return image_node_p (node) ||
181         ( (node instanceof HTMLAnchorElement && node.href) ||
182           node instanceof HTMLAreaElement ||
183           node instanceof HTMLLinkElement ||
184           node.getAttributeNS( "http://www.w3.org/1999/xlink", "type") == "simple" ) ||
185         ( ( node instanceof HTMLQuoteElement && node.cite)    ||
186           ( node instanceof HTMLTableElement && node.summary) ||
187           ( node instanceof HTMLModElement &&
188             ( node.cite || node.dateTime ) )                ||
189           ( node instanceof HTMLElement &&
190             ( node.title || node.lang ) )                   ||
191           node.getAttributeNS(XMLNS, "lang") );
195 // Returns a "url"-type computed style attribute value, with the url() stripped.
196 function get_computed_url (elem, prop) {
197     var url = elem.ownerDocument.defaultView.getComputedStyle (elem, '').getPropertyCSSValue (prop);
198     return (url.primitiveType == CSSPrimitiveValue.CSS_URI) ? url.getStringValue () : null;
202 // Generate fully qualified URL for clicked-on link.
203 function get_link_url (node) {
204     var href = node.href;
205         
206     if (href) {
207         return href;
208     }
210     var href = node.getAttributeNS("http://www.w3.org/1999/xlink",
211                                    "href");
213     if (!href || !href.match(/\S/)) {
214         throw "Empty href"; // Without this we try to save as the current doc, for example, HTML case also throws if empty
215     }
216     href = makeURLAbsolute (node.baseURI, href);
217     return href;
221 function get_link_uri (linkURL) {
222     var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
223     try {
224         return ioService.newURI(linkURL, null, null);
225     } catch (ex) {
226         // e.g. empty URL string
227         return null;
228     }