mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-02-11 18:55:48 +01:00
Death to E4X and stuff.
This commit is contained in:
@@ -144,7 +144,7 @@ defineModule("base", {
|
||||
"Set", "Struct", "StructBase", "Timer", "UTF8", "XPCOM", "XPCOMShim", "XPCOMUtils",
|
||||
"XPCSafeJSObjectWrapper", "array", "bind", "call", "callable", "ctypes", "curry",
|
||||
"debuggerProperties", "defineModule", "deprecated", "endModule", "forEach", "isArray",
|
||||
"isGenerator", "isinstance", "isObject", "isString", "isSubclass", "iter", "iterAll",
|
||||
"isGenerator", "isinstance", "isObject", "isString", "isSubclass", "isXML", "iter", "iterAll",
|
||||
"iterOwnProperties", "keys", "memoize", "octal", "properties", "require", "set", "update",
|
||||
"values", "withCallerGlobal"
|
||||
]
|
||||
@@ -482,6 +482,12 @@ function isinstance(object, interfaces) {
|
||||
*/
|
||||
function isObject(obj) typeof obj === "object" && obj != null || obj instanceof Ci.nsISupports;
|
||||
|
||||
/**
|
||||
* Returns true if obje is an E4X XML object.
|
||||
* @deprecated
|
||||
*/
|
||||
function isXML(obj) typeof obj === "xml";
|
||||
|
||||
/**
|
||||
* Returns true if and only if its sole argument is an
|
||||
* instance of the builtin Array type. The array may come from
|
||||
|
||||
@@ -55,6 +55,12 @@ var DOM = Class("DOM", {
|
||||
;
|
||||
else if (typeof val == "xml" && context instanceof Ci.nsIDOMDocument)
|
||||
this[length++] = DOM.fromXML(val, context, this.nodes);
|
||||
else if (DOM.isJSONXML(val)) {
|
||||
if (context instanceof Ci.nsIDOMDocument)
|
||||
this[length++] = DOM.fromJSON(val, context, this.nodes);
|
||||
else
|
||||
this[length++] = val;
|
||||
}
|
||||
else if (val instanceof Ci.nsIDOMNode || val instanceof Ci.nsIDOMWindow)
|
||||
this[length++] = val;
|
||||
else if ("__iterator__" in val || isinstance(val, ["Iterator", "Generator"]))
|
||||
@@ -1377,6 +1383,12 @@ var DOM = Class("DOM", {
|
||||
? function (elem, dir) services.dactyl.getScrollable(elem) & (dir ? services.dactyl["DIRECTION_" + dir.toUpperCase()] : ~0)
|
||||
: function (elem, dir) true),
|
||||
|
||||
isJSONXML: function isJSONXML(val) isArray(val) && isString(val[0]) || isObject(val) && "toDOM" in val,
|
||||
|
||||
DOMString: function (val) ({
|
||||
toDOM: function toDOM(doc) doc.createTextNode(val)
|
||||
}),
|
||||
|
||||
/**
|
||||
* The set of input element type attribute values that mark the element as
|
||||
* an editable field.
|
||||
@@ -1536,6 +1548,93 @@ var DOM = Class("DOM", {
|
||||
}
|
||||
},
|
||||
|
||||
fromJSON: update(function fromJSON(xml, doc, nodes, namespaces) {
|
||||
if (!doc)
|
||||
doc = document;
|
||||
|
||||
function tag(args, namespaces) {
|
||||
let _namespaces = namespaces;
|
||||
|
||||
let [name, attr] = args;
|
||||
|
||||
if (Array.isArray(name) || args.length == 0) {
|
||||
var frag = doc.createDocumentFragment();
|
||||
Array.forEach(args, function (arg) {
|
||||
if (!Array.isArray(arg[0]))
|
||||
frag.appendChild(tag(arg, namespaces));
|
||||
else
|
||||
arg.forEach(function (arg) {
|
||||
frag.appendChild(tag(arg, namespaces));
|
||||
});
|
||||
});
|
||||
return frag;
|
||||
}
|
||||
|
||||
if (isObject(name) && "toDOM" in name)
|
||||
return name.toDOM(doc, namespaces, nodes);
|
||||
|
||||
function parseNamespace(name) {
|
||||
var m = /^(?:(.*):)?(.*)$/.exec(name);
|
||||
return [namespaces[m[1]], m[2]];
|
||||
}
|
||||
|
||||
// FIXME: Surely we can do better.
|
||||
for (var key in attr) {
|
||||
if (/^xmlns(?:$|:)/.test(key)) {
|
||||
if (_namespaces === namespaces)
|
||||
namespaces = update({}, namespaces);
|
||||
|
||||
namespaces[key.substr(6)] = namespaces[attr[key]] || attr[key];
|
||||
}}
|
||||
|
||||
var args = Array.slice(args, 2);
|
||||
var vals = parseNamespace(name);
|
||||
var elem = doc.createElementNS(vals[0] || namespaces[""],
|
||||
vals[1]);
|
||||
|
||||
for (var key in attr)
|
||||
if (!/^xmlns(?:$|:)/.test(key)) {
|
||||
var val = attr[key];
|
||||
if (nodes && key == "key")
|
||||
nodes[val] = elem;
|
||||
|
||||
vals = parseNamespace(key);
|
||||
if (typeof val == "function")
|
||||
elem.addEventListener(key.replace(/^on/, ""), val, false);
|
||||
else if (vals[0])
|
||||
elem.setAttributeNS(vals[0], key, val);
|
||||
else if (key != "highlight")
|
||||
elem.setAttribute(key, val);
|
||||
}
|
||||
args.forEach(function(e) {
|
||||
elem.appendChild(typeof e == "object" ? tag(e, namespaces) :
|
||||
e instanceof Node ? e : doc.createTextNode(e));
|
||||
});
|
||||
|
||||
if ("highlight" in attr)
|
||||
highlight.highlightNode(elem, attr.highlight, nodes || true);
|
||||
return elem;
|
||||
}
|
||||
|
||||
if (namespaces)
|
||||
namespaces = update({}, fromJSON.namespaces, namespaces);
|
||||
else
|
||||
namespaces = fromJSON.namespaces;
|
||||
|
||||
return tag(xml, namespaces)
|
||||
}, {
|
||||
namespaces: {
|
||||
"": "http://www.w3.org/1999/xhtml",
|
||||
html: "http://www.w3.org/1999/xhtml",
|
||||
xul: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
|
||||
}
|
||||
}),
|
||||
|
||||
parseNamespace: function parseNamespace(name) {
|
||||
var m = /^(?:(.*):)?(.*)$/.exec(name);
|
||||
return [DOM.fromJSON.namespaces[m[1]], m[2]];
|
||||
},
|
||||
|
||||
/**
|
||||
* Evaluates an XPath expression in the current or provided
|
||||
* document. It provides the xhtml, xhtml2 and dactyl XML
|
||||
|
||||
@@ -215,32 +215,65 @@ var Overlay = Module("Overlay", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReferen
|
||||
|
||||
_loadOverlay: function _loadOverlay(window, obj) {
|
||||
let doc = window.document;
|
||||
let elems = this.getData(doc, "overlayElements");
|
||||
let attrs = this.getData(doc, "overlayAttributes");
|
||||
let savedElems = this.getData(doc, "overlayElements");
|
||||
let savedAttrs = this.getData(doc, "overlayAttributes");
|
||||
|
||||
function insert(key, fn) {
|
||||
if (obj[key]) {
|
||||
let iterator = Iterator(obj[key]);
|
||||
if (!isObject(obj[key]))
|
||||
iterator = ([elem.@id, elem.elements(), elem.@*::*.(function::name() != "id")] for each (elem in obj[key]));
|
||||
iterator = ([elem.@id, elem.elements(), elem.@*::*.(function::name() != "id")]
|
||||
for each (elem in obj[key]));
|
||||
else if (isArray(obj[key])) {
|
||||
iterator = ([elem[1].id, elem.slice(2), elem[1]]
|
||||
for each (elem in obj[key]))
|
||||
}
|
||||
|
||||
for (let [elem, xml, attr] in iterator) {
|
||||
for (let [elem, xml, attrs] in iterator) {
|
||||
if (elem = doc.getElementById(String(elem))) {
|
||||
let node = DOM.fromXML(xml, doc, obj.objects);
|
||||
// Urgh. Hack.
|
||||
let namespaces;
|
||||
if (attrs && !isXML(attrs))
|
||||
namespaces = iter([k.slice(6), DOM.fromJSON.namespaces[v] || v]
|
||||
for ([k, v] in Iterator(attrs))
|
||||
if (/^xmlns(?:$|:)/.test(k))).toObject();
|
||||
|
||||
let node;
|
||||
if (isXML(xml))
|
||||
node = DOM.fromXML(xml, doc, obj.objects);
|
||||
else
|
||||
node = DOM.fromJSON(xml, doc, obj.objects, namespaces);
|
||||
|
||||
if (!(node instanceof Ci.nsIDOMDocumentFragment))
|
||||
elems.push(node);
|
||||
savedElems.push(node);
|
||||
else
|
||||
for (let n in array.iterValues(node.childNodes))
|
||||
elems.push(n);
|
||||
savedElems.push(n);
|
||||
|
||||
fn(elem, node);
|
||||
for each (let attr in attr || []) {
|
||||
let ns = attr.namespace(), name = attr.localName();
|
||||
attrs.push([elem, ns, name, getAttr(elem, ns, name), String(attr)]);
|
||||
if (attr.name() != "highlight")
|
||||
elem.setAttributeNS(ns, name, String(attr));
|
||||
|
||||
if (isXML(attrs))
|
||||
// Evilness and such.
|
||||
let (oldAttrs = attrs) {
|
||||
attrs = (attr for each (attr in oldAttrs));
|
||||
}
|
||||
|
||||
for (let attr in attrs || []) {
|
||||
let ns, name, localName, val;
|
||||
if (isXML(attr))
|
||||
ns = attr.namespace(), localName = attr.localName(),
|
||||
name = attr.name(), val = String(attr);
|
||||
else
|
||||
highlight.highlightNode(elem, String(attr));
|
||||
[ns, localName] = DOM.parseNamespace(attr),
|
||||
name = attr, val = attrs[attr];
|
||||
|
||||
savedAttrs.push([elem, ns, name, getAttr(elem, ns, name), val]);
|
||||
if (name === "highlight")
|
||||
highlight.highlightNode(elem, val);
|
||||
else if (ns)
|
||||
elem.setAttributeNS(ns, name, val);
|
||||
else
|
||||
elem.setAttribute(name, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user