# HG changeset patch # User Adam Chlipala # Date 1232203650 18000 # Node ID 20c0833273645808fda51461ecc57d8a70e10780 # Parent b1064de2b1f9418dc40c2ca29bee05dce781f337 Add dynamic content under proper parents diff -r b1064de2b1f9 -r 20c083327364 lib/js/urweb.js --- 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) }; } diff -r b1064de2b1f9 -r 20c083327364 tests/dlist.ur --- 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 => [] - | Cons (x, s) => {[x]} :: {delistSource s} + | Cons (x, s) => {[x]} :: ({delistSource s}) and delistSource s =