diff options
-rw-r--r-- | lib/js/urweb.js | 51 | ||||
-rw-r--r-- | src/jscomp.sml | 12 | ||||
-rw-r--r-- | src/monoize.sml | 8 | ||||
-rw-r--r-- | tests/dlist.ur | 1 |
4 files changed, 59 insertions, 13 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](); +} + diff --git a/src/jscomp.sml b/src/jscomp.sml index 1b675abd..f61ec3f0 100644 --- a/src/jscomp.sml +++ b/src/jscomp.sml @@ -486,7 +486,6 @@ fun process file = maxName = #maxName st} val (e, st) = jsExp mode skip [] 0 (e, st) - val () = Print.prefaces "Pre-e" [("e", MonoPrint.p_exp MonoEnv.empty e)] val e = deStrcat 0 e val sc = "_n" ^ Int.toString n ^ "=" ^ e ^ ";\n" @@ -759,7 +758,11 @@ fun process file = end | EJavaScript (Source _, _, SOME _) => (e, st) - | EJavaScript (_, _, SOME e) => ((EFfiApp ("Basis", "jsifyString", [e]), loc), st) + | EJavaScript (_, _, SOME e) => + (strcat [str "\"cr(\"+ca(function(){return ", + e, + str "})+\")\""], + st) | EClosure _ => unsupported "EClosure" | EQuery _ => unsupported "Query" @@ -770,7 +773,10 @@ fun process file = let val (e, st) = jsE inner (e, st) in - ((EFfiApp ("Basis", "jsifyString", [e]), loc), st) + (strcat [str "\"cr(\"+ca(function(){return ", + e, + str "})+\")\""], + st) end | ESignalReturn e => diff --git a/src/monoize.sml b/src/monoize.sml index 993034e4..8d5ed36c 100644 --- a/src/monoize.sml +++ b/src/monoize.sml @@ -1910,9 +1910,9 @@ fun monoExp (env, st, fm) (all as (e, loc)) = e), _), _)] => (e, fm) | [("Signal", e, _)] => ((L'.EStrcat - ((L'.EPrim (Prim.String "<script>dyn("), loc), + ((L'.EPrim (Prim.String "<span><script type=\"text/javascript\">dyn("), loc), (L'.EStrcat ((L'.EJavaScript (L'.Script, e, NONE), loc), - (L'.EPrim (Prim.String ")</script>"), loc)), loc)), loc), + (L'.EPrim (Prim.String ")</script></span>"), loc)), loc)), loc), fm) | _ => raise Fail "Monoize: Bad dyn attributes") @@ -1932,7 +1932,7 @@ fun monoExp (env, st, fm) (all as (e, loc)) = loc)), loc), fm) end | SOME (_, src, _) => - (strcat [str "<script>inp(\"input\",", + (strcat [str "<script type=\"text/javascript\">inp(\"input\",", (L'.EJavaScript (L'.Script, src, NONE), loc), str ")</script>"], fm)) @@ -2002,7 +2002,7 @@ fun monoExp (env, st, fm) (all as (e, loc)) = loc), fm) end | SOME (_, src, _) => - (strcat [str "<script>inp(\"input\",", + (strcat [str "<script type=\"text/javascript\">inp(\"input\",", (L'.EJavaScript (L'.Script, src, NONE), loc), str ")</script>"], fm)) diff --git a/tests/dlist.ur b/tests/dlist.ur index 211291bc..dbf8c3c5 100644 --- a/tests/dlist.ur +++ b/tests/dlist.ur @@ -19,4 +19,5 @@ fun main () : transaction page = tl <- get s; s' <- source (Cons (hd, tl)); set s s'}/> + <button value="Reset" onclick={set s ns}/> </body></xml> |