]> git.donarmstrong.com Git - dactyl.git/blob - common/modules/dom-e4x.jsm
Import r6948 from upstream hg supporting Firefox up to 24.*
[dactyl.git] / common / modules / dom-e4x.jsm
1 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
2 // Copyright (c) 2008-2012 Kris Maglione <maglione.k@gmail.com>
3 //
4 // This work is licensed for reuse under an MIT license. Details are
5 // given in the LICENSE.txt file included with this file.
6 /* use strict */
7
8 defineModule("dom", {
9     exports: ["fromXML"]
10 });
11
12 lazyRequire("highlight", ["highlight"]);
13
14 var XBL = Namespace("xbl", "http://www.mozilla.org/xbl");
15 var XHTML = Namespace("html", "http://www.w3.org/1999/xhtml");
16 var XUL = Namespace("xul", "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
17 var NS = Namespace("dactyl", "http://vimperator.org/namespaces/liberator");
18
19 function fromXML(node, doc, nodes) {
20     XML.ignoreWhitespace = XML.prettyPrinting = false;
21     if (typeof node === "string") // Sandboxes can't currently pass us XML objects.
22         node = XML(node);
23
24     if (node.length() != 1) {
25         let domnode = doc.createDocumentFragment();
26         for each (let child in node)
27             domnode.appendChild(fromXML(child, doc, nodes));
28         return domnode;
29     }
30
31     switch (node.nodeKind()) {
32     case "text":
33         return doc.createTextNode(String(node));
34     case "element":
35         let domnode = doc.createElementNS(node.namespace(), node.localName());
36
37         for each (let attr in node.@*::*)
38             if (attr.name() != "highlight")
39                 domnode.setAttributeNS(attr.namespace(), attr.localName(), String(attr));
40
41         for each (let child in node.*::*)
42             domnode.appendChild(fromXML(child, doc, nodes));
43         if (nodes && node.@key)
44             nodes[node.@key] = domnode;
45
46         if ("@highlight" in node)
47             highlight.highlightNode(domnode, String(node.@highlight), nodes || true);
48         return domnode;
49     default:
50         return null;
51     }
52 }
53
54 // vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: