Use nested-arrays (sexpr style) instead of hashes for DOM fragments
This commit is contained in:
parent
8fde23d187
commit
f7baa65a2d
|
@ -19,7 +19,7 @@ $(document).ready(function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [pub(["DOM", "#clicker-holder", "clicker",
|
}, [pub(["DOM", "#clicker-holder", "clicker",
|
||||||
["button", ["span", {"style": "font-style: italic"}, "Click me!"]]]),
|
["button", ["span", [["style", "font-style: italic"]], "Click me!"]]]),
|
||||||
pub("bump_count"),
|
pub("bump_count"),
|
||||||
sub(["jQuery", "button.clicker", "click", __])]);
|
sub(["jQuery", "button.clicker", "click", __])]);
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,10 @@ DOMFragment.prototype.handleEvent = function (e) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isAttributes(x) {
|
||||||
|
return (x instanceof Array) && ((x.length === 0) || (x[0] instanceof Array));
|
||||||
|
}
|
||||||
|
|
||||||
DOMFragment.prototype.interpretSpec = function (spec) {
|
DOMFragment.prototype.interpretSpec = function (spec) {
|
||||||
// Fragment specs are roughly JSON-equivalents of SXML.
|
// Fragment specs are roughly JSON-equivalents of SXML.
|
||||||
// spec ::== ["tag", {"attr": "value", ...}, spec, spec, ...]
|
// spec ::== ["tag", {"attr": "value", ...}, spec, spec, ...]
|
||||||
|
@ -63,16 +67,14 @@ DOMFragment.prototype.interpretSpec = function (spec) {
|
||||||
return document.createTextNode(spec);
|
return document.createTextNode(spec);
|
||||||
} else if ($.isArray(spec)) {
|
} else if ($.isArray(spec)) {
|
||||||
var tagName = spec[0];
|
var tagName = spec[0];
|
||||||
var hasAttrs = $.isPlainObject(spec[1]);
|
var hasAttrs = isAttributes(spec[1]);
|
||||||
var attrs = hasAttrs ? spec[1] : {};
|
var attrs = hasAttrs ? spec[1] : {};
|
||||||
var kidIndex = hasAttrs ? 2 : 1;
|
var kidIndex = hasAttrs ? 2 : 1;
|
||||||
|
|
||||||
// Wow! Such XSS! Many hacks! So vulnerability! Amaze!
|
// Wow! Such XSS! Many hacks! So vulnerability! Amaze!
|
||||||
var n = document.createElement(tagName);
|
var n = document.createElement(tagName);
|
||||||
for (var attr in attrs) {
|
for (var i = 0; i < attrs.length; i++) {
|
||||||
if (attrs.hasOwnProperty(attr)) {
|
n.setAttribute(attrs[i][0], attrs[i][1]);
|
||||||
n.setAttribute(attr, attrs[attr]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (var i = kidIndex; i < spec.length; i++) {
|
for (var i = kidIndex; i < spec.length; i++) {
|
||||||
n.appendChild(this.interpretSpec(spec[i]));
|
n.appendChild(this.interpretSpec(spec[i]));
|
||||||
|
|
Loading…
Reference in New Issue