changeset 604:20c083327364

Add dynamic content under proper parents
author Adam Chlipala <adamc@hcoop.net>
date Sat, 17 Jan 2009 09:47:30 -0500
parents b1064de2b1f9
children 1a9171e31fd1
files lib/js/urweb.js tests/dlist.ur
diffstat 2 files changed, 25 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Fri Jan 16 15:49:10 2009 -0500
+++ b/lib/js/urweb.js	Sat Jan 17 09:47:30 2009 -0500
@@ -37,53 +37,54 @@
   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;
+var thisScript = null;
 
-function pushParent(node) {
-  parents = cons(node, parents);
+function addNode(node) {
+  if (thisScript) {
+    thisScript.parentNode.appendChild(node);
+    thisScript.parentNode.removeChild(thisScript);
+  } else
+    lastParent().appendChild(node);
 }
 
-function popParent() {
-  if (parents)
-    parents = parents.n;
-  else
-    alert("popParent: stack underflow");
-}
+function runScripts(node) {
+  var savedScript = thisScript;
 
-function curParent() {
-  return lastParent(parents ? parents.v : document);
+  var scripts = node.getElementsByTagName("script");
+  var len = scripts.length;
+  for (var i = 0; i < len; ++i) {
+    thisScript = scripts[i];
+    eval(thisScript.textContent);
+  }
+
+  thisScript = savedScript;
 }
 
 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();
-  }
+  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) };
 }
--- a/tests/dlist.ur	Fri Jan 16 15:49:10 2009 -0500
+++ b/tests/dlist.ur	Sat Jan 17 09:47:30 2009 -0500
@@ -3,7 +3,7 @@
 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>