diff lib/js/urweb.js @ 1538:ade93cd5bc59

Fix bug with <dyn> as first child of <table>
author Adam Chlipala <adam@chlipala.net>
date Sun, 14 Aug 2011 17:39:18 -0400
parents 883347f5c3c2
children 355a928871ff
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun Aug 14 16:46:36 2011 -0400
+++ b/lib/js/urweb.js	Sun Aug 14 17:39:18 2011 -0400
@@ -385,6 +385,37 @@
         freeClosure(ls.data);
 }
 
+// Sometimes we wind up with tables that contain <script>s outside the single <tbody>.
+// To avoid dealing with that case, we normalize by moving <script>s into <tbody>.
+function normalizeTable(table) {
+    var orig = table;
+
+    var script, next;
+
+    while (table.tagName != "TABLE")
+        table = table.parentNode;
+
+    for (var tbody = table.firstChild; tbody; tbody = tbody.nextSibling) {
+        if (tbody.tagName == "TBODY") {
+            for (script = table.firstChild; script && script != tbody; script = next) {
+                next = script.nextSibling;
+
+                tbody.insertBefore(script, tbody.firstChild);
+            }
+
+            return;
+        }
+    }
+
+    var tbody = document.createElement("tbody");
+    for (script = table.firstChild; script; script = next) {
+        next = script.nextSibling;
+        
+        tbody.insertBefore(script, tbody.firstChild);
+    }
+    table.appendChild(tbody);
+}
+
 function dyn(pnode, s) {
     var x = document.createElement("script");
     x.dead = false;
@@ -420,6 +451,8 @@
         x.closures = cls.v;
 
         if (pnode == "table") {
+            normalizeTable(x.parentNode);
+
             var dummy = document.createElement("body");
             dummy.innerHTML = "<table>" + html + "</table>";
             runScripts(dummy);