diff options
author | Adam Chlipala <adamc@hcoop.net> | 2009-01-17 09:47:30 -0500 |
---|---|---|
committer | Adam Chlipala <adamc@hcoop.net> | 2009-01-17 09:47:30 -0500 |
commit | 6ee23b4ea530ff85a780cc8401cdcd078bed4ada (patch) | |
tree | 730db370cf9cd008c5cef8a958cc816c8926f231 | |
parent | ed56c462bf4131b7e179c72bfafb4f6967bc27dc (diff) |
Add dynamic content under proper parents
-rw-r--r-- | lib/js/urweb.js | 47 | ||||
-rw-r--r-- | tests/dlist.ur | 2 |
2 files changed, 25 insertions, 24 deletions
diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 689792f7..7bb6849e 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -37,53 +37,54 @@ function sb(x,y) { return s; } -function lastParent(pos) { +function lastParent() { + var pos = document; + while (pos.lastChild && pos.lastChild.nodeType == 1) pos = pos.lastChild; return pos.parentNode; } -var parents = null; - -function pushParent(node) { - parents = cons(node, parents); -} - -function popParent() { - if (parents) - parents = parents.n; - else - alert("popParent: stack underflow"); -} +var thisScript = null; -function curParent() { - return lastParent(parents ? parents.v : document); +function addNode(node) { + if (thisScript) { + thisScript.parentNode.appendChild(node); + thisScript.parentNode.removeChild(thisScript); + } else + lastParent().appendChild(node); } -function populate(node, html) { - node.innerHTML = html; +function runScripts(node) { + var savedScript = thisScript; var scripts = node.getElementsByTagName("script"); var len = scripts.length; for (var i = 0; i < len; ++i) { - pushParent(scripts[i].parentNode); - eval(scripts[i].textContent); - popParent(); + thisScript = scripts[i]; + eval(thisScript.textContent); } + + thisScript = savedScript; +} + +function populate(node, html) { + node.innerHTML = html; + runScripts(node); } function dyn(s) { var x = document.createElement("span"); - x.innerHTML = s.v; - curParent().appendChild(x); + populate(x, s.v); + addNode(x); s.h = cons(function() { populate(x, s.v) }, s.h); } function inp(t, s) { var x = document.createElement(t); x.value = s.v; - curParent().appendChild(x); + addNode(x); s.h = cons(function() { x.value = s.v }, s.h); x.onkeyup = function() { sv(s, x.value) }; } diff --git a/tests/dlist.ur b/tests/dlist.ur index dbf8c3c5..0733a0db 100644 --- a/tests/dlist.ur +++ b/tests/dlist.ur @@ -3,7 +3,7 @@ datatype dlist = Nil | Cons of string * source dlist fun delist dl = case dl of Nil => <xml>[]</xml> - | Cons (x, s) => <xml>{[x]} :: {delistSource s}</xml> + | Cons (x, s) => <xml>{[x]} :: ({delistSource s})</xml> and delistSource s = <xml><dyn signal={dl <- signal s; return (delist dl)}/></xml> |