From f7baa65a2de1b4ad170ba1ce45b3b948dd7f7645 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 24 Jul 2014 16:21:07 -0700 Subject: [PATCH] Use nested-arrays (sexpr style) instead of hashes for DOM fragments --- examples/dom/index.js | 2 +- src/dom-driver.js | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/dom/index.js b/examples/dom/index.js index 94c6040..a797a34 100644 --- a/examples/dom/index.js +++ b/examples/dom/index.js @@ -19,7 +19,7 @@ $(document).ready(function () { } } }, [pub(["DOM", "#clicker-holder", "clicker", - ["button", ["span", {"style": "font-style: italic"}, "Click me!"]]]), + ["button", ["span", [["style", "font-style: italic"]], "Click me!"]]]), pub("bump_count"), sub(["jQuery", "button.clicker", "click", __])]); diff --git a/src/dom-driver.js b/src/dom-driver.js index 500c131..ef9e5d7 100644 --- a/src/dom-driver.js +++ b/src/dom-driver.js @@ -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) { // Fragment specs are roughly JSON-equivalents of SXML. // spec ::== ["tag", {"attr": "value", ...}, spec, spec, ...] @@ -63,16 +67,14 @@ DOMFragment.prototype.interpretSpec = function (spec) { return document.createTextNode(spec); } else if ($.isArray(spec)) { var tagName = spec[0]; - var hasAttrs = $.isPlainObject(spec[1]); + var hasAttrs = isAttributes(spec[1]); var attrs = hasAttrs ? spec[1] : {}; var kidIndex = hasAttrs ? 2 : 1; // Wow! Such XSS! Many hacks! So vulnerability! Amaze! var n = document.createElement(tagName); - for (var attr in attrs) { - if (attrs.hasOwnProperty(attr)) { - n.setAttribute(attr, attrs[attr]); - } + for (var i = 0; i < attrs.length; i++) { + n.setAttribute(attrs[i][0], attrs[i][1]); } for (var i = kidIndex; i < spec.length; i++) { n.appendChild(this.interpretSpec(spec[i]));