diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/js/urweb.js | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 0ee19992..689792f7 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -37,26 +37,53 @@ function sb(x,y) { return s; } -function myParent() { - var pos = document; - +function lastParent(pos) { 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"); +} + +function curParent() { + return lastParent(parents ? parents.v : document); +} + +function populate(node, html) { + node.innerHTML = html; + + 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(); + } +} + function dyn(s) { var x = document.createElement("span"); x.innerHTML = s.v; - myParent().appendChild(x); - s.h = cons(function() { x.innerHTML = s.v }, s.h); + curParent().appendChild(x); + s.h = cons(function() { populate(x, s.v) }, s.h); } function inp(t, s) { var x = document.createElement(t); x.value = s.v; - myParent().appendChild(x); + curParent().appendChild(x); s.h = cons(function() { x.value = s.v }, s.h); x.onkeyup = function() { sv(s, x.value) }; } @@ -70,3 +97,15 @@ function bs(b) { return (b ? "True" : "False") } function pf() { alert("Pattern match failure") } +var closures = []; + +function ca(f) { + var n = closures.length; + closures[n] = f; + return n; +} + +function cr(n) { + return closures[n](); +} + |