annotate lib/js/urweb.js @ 2058:235b33d62275

A new test for <button>
author Adam Chlipala <adam@chlipala.net>
date Sun, 17 Aug 2014 15:56:43 -0400
parents 7c2229aa22fc
children e1879ded8095
rev   line source
adamc@1036 1 // Detect browser quirks that we should be aware of.
adamc@1036 2
adamc@1036 3 function needsDynPrefix() {
adamc@1260 4 var span = document.createElement("span");
adamc@1260 5 span.innerHTML = "<script>alert('test');</script>";
adamc@1260 6 var scripts = span.getElementsByTagName("script");
adamc@1260 7 return scripts.length == 0;
adamc@1036 8 }
adamc@1036 9
adamc@1036 10 var dynPrefix = needsDynPrefix() ? "<span style=\"display:none\">A</span>" : "";
adamc@1036 11
adamc@970 12 // Function versions of operators
adamc@970 13
adamc@970 14 function not(x) { return !x; }
adamc@970 15 function neg(x) { return -x; }
adamc@970 16
adamc@970 17 function eq(x, y) { return x == y; }
adamc@970 18 function plus(x, y) { return x + y; }
adamc@970 19 function minus(x, y) { return x - y; }
adamc@970 20 function times(x, y) { return x * y; }
adamc@970 21 function div(x, y) { return x / y; }
adam@1619 22 function divInt(x, y) { if (y == 0) er("Division by zero"); var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
adamc@970 23 function mod(x, y) { return x % y; }
adam@1619 24 function modInt(x, y) { if (y == 0) er("Division by zero"); var n = x % y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
adamc@970 25 function lt(x, y) { return x < y; }
adamc@970 26 function le(x, y) { return x <= y; }
adamc@970 27
adamc@1061 28 // Characters
adamc@1061 29
adamc@1061 30 function isLower(c) { return c >= 'a' && c <= 'z'; }
adamc@1061 31 function isUpper(c) { return c >= 'A' && c <= 'Z'; }
adamc@1061 32 function isAlpha(c) { return isLower(c) || isUpper(c); }
adamc@1061 33 function isDigit(c) { return c >= '0' && c <= '9'; }
adamc@1061 34 function isAlnum(c) { return isAlpha(c) || isDigit(c); }
adamc@1061 35 function isBlank(c) { return c == ' ' || c == '\t'; }
adamc@1061 36 function isSpace(c) { return isBlank(c) || c == '\r' || c == '\n'; }
adamc@1061 37 function isXdigit(c) { return isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); }
adam@1927 38 function ord(c) { return c.charCodeAt(0); }
adam@1927 39 function isPrint(c) { return ord(c) > 31 && ord(c) < 127; }
adamc@1177 40 function toLower(c) { return c.toLowerCase(); }
adamc@1177 41 function toUpper(c) { return c.toUpperCase(); }
adamc@1061 42
adamc@692 43 // Lists
adamc@692 44
adamc@580 45 function cons(v, ls) {
adamc@1260 46 return { next : ls, data : v };
adamc@690 47 }
adamc@969 48 function rev(ls) {
adamc@1260 49 var acc = null;
adamc@1260 50 for (; ls; ls = ls.next)
adam@1643 51 acc = cons(ls.data, acc);
adamc@1260 52 return acc;
adamc@969 53 }
adamc@690 54 function concat(ls1, ls2) {
adamc@1260 55 var acc = ls2;
adamc@1260 56 ls1 = rev(ls1);
adamc@1260 57 for (; ls1; ls1 = ls1.next)
adamc@1260 58 acc = cons(ls1.data, acc);
adamc@1260 59 return acc;
adamc@690 60 }
adamc@690 61 function member(x, ls) {
adamc@1260 62 for (; ls; ls = ls.next)
adamc@1260 63 if (ls.data == x)
adamc@1260 64 return true;
adamc@1260 65 return false;
adamc@690 66 }
adamc@690 67 function remove(x, ls) {
adamc@1260 68 var acc = null;
adamc@969 69
adamc@1260 70 for (; ls; ls = ls.next)
adamc@1260 71 if (ls.data == x)
adamc@1260 72 return concat(acc, ls.next);
adamc@1260 73 else
adamc@1260 74 acc = cons(ls.data, acc);
adamc@969 75
adamc@1260 76 return ls;
adamc@690 77 }
adamc@690 78 function union(ls1, ls2) {
adamc@1260 79 var acc = ls2;
adamc@969 80
adamc@1260 81 for (; ls1; ls1 = ls1.next)
adamc@1260 82 if (!member(ls1.data, ls2))
adamc@1260 83 acc = cons(ls1.data, acc);
adamc@969 84
adamc@1260 85 return acc;
adamc@580 86 }
adamc@703 87 function length(ls) {
adamc@1260 88 var acc = 0;
adamc@969 89
adamc@1260 90 for (; ls; ls = ls.next)
adamc@1260 91 ++acc;
adamc@969 92
adamc@1260 93 return acc;
adamc@703 94 }
adamc@670 95
adamc@690 96
adam@1571 97 // Floats
adam@1571 98
adam@1571 99 function float(n) {
adam@1571 100 return n;
adam@1571 101 }
adam@1571 102
adam@1571 103 function trunc(n) {
adam@1571 104 return ~~n;
adam@1571 105 }
adam@1571 106
adam@1571 107 function ceil(n) {
adam@1571 108 return Math.ceil(n);
adam@1571 109 }
adam@1571 110
adam@1571 111 function round(n) {
adam@1571 112 return Math.round(n);
adam@1571 113 }
adam@1571 114
adam@1571 115
adam@1599 116 // Time, represented as counts of microseconds since the epoch
adam@1487 117
adam@1629 118 var time_format = "%c";
adam@1629 119
adam@1487 120 function showTime(tm) {
adam@1629 121 return strftime(time_format, tm);
adam@1629 122 }
adam@1629 123
adam@1629 124 function showTimeHtml(tm) {
adam@1629 125 return eh(showTime(tm));
adam@1487 126 }
adam@1487 127
adam@1487 128 function now() {
adam@1487 129 return (new Date()).getTime() * 1000;
adam@1487 130 }
adam@1487 131
adam@1514 132 function diffInSeconds(tm1, tm2) {
adam@1514 133 return Math.round((tm2 - tm1) / 1000000);
adam@1514 134 }
adam@1514 135
adam@1685 136 function diffInMilliseconds(tm1, tm2) {
adam@1685 137 return Math.round((tm2 - tm1) / 1000);
adam@1685 138 }
adam@1685 139
adam@1514 140 function toSeconds(tm) {
adam@1514 141 return Math.round(tm / 1000000);
adam@1514 142 }
adam@1514 143
adam@1685 144 function toMilliseconds(tm) {
adam@1685 145 return Math.round(tm / 1000);
adam@1685 146 }
adam@1685 147
adam@1518 148 function addSeconds(tm, n) {
adam@1518 149 return tm + n * 1000000;
adam@1518 150 }
adam@1518 151
adam@1599 152 function stringToTime_error(string) {
adam@1599 153 var t = Date.parse(string);
adam@1599 154 if (isNaN(t))
adam@1632 155 er("Invalid date string: " + string);
adam@1599 156 else
adam@1599 157 return t * 1000;
adam@1599 158 }
adam@1599 159
adam@1599 160 function stringToTime(string) {
adam@1599 161 try {
adam@1599 162 var t = Date.parse(string);
adam@1599 163 if (isNaN(t))
adam@1599 164 return null;
adam@1599 165 else
adam@1599 166 return t * 1000;
adam@1599 167 } catch (e) {
adam@1599 168 return null;
adam@1599 169 }
rmbruijn@1597 170 }
rmbruijn@1597 171
adam@1609 172 /*
adam@1615 173 strftime() implementation from:
adam@1615 174 YUI 3.4.1 (build 4118)
adam@1615 175 Copyright 2011 Yahoo! Inc. All rights reserved.
adam@1615 176 Licensed under the BSD License.
adam@1615 177 http://yuilibrary.com/license/
adam@1615 178 */
adam@1609 179
adam@1615 180 var xPad=function (x, pad, r)
adam@1615 181 {
adam@1615 182 if(typeof r === "undefined")
adam@1615 183 {
adam@1615 184 r=10;
adam@1615 185 }
adam@1615 186 pad = pad.toString();
adam@1615 187 for( ; parseInt(x, 10)<r && r>1; r/=10) {
adam@1615 188 x = pad + x;
adam@1615 189 }
adam@1615 190 return x.toString();
adam@1615 191 };
adam@1609 192
adam@1615 193 var YDateEn = {
adam@1615 194 a: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
adam@1615 195 A: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
adam@1615 196 b: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
adam@1615 197 B: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
adam@1615 198 c: "%a %d %b %Y %T %Z",
adam@1615 199 p: ["AM", "PM"],
adam@1615 200 P: ["am", "pm"],
adam@1615 201 r: "%I:%M:%S %p",
adam@1615 202 x: "%d/%m/%y",
adam@1615 203 X: "%T"
adam@1615 204 };
adam@1609 205
adam@1615 206 var Dt = {
adam@1615 207 formats: {
adam@1615 208 a: function (d, l) { return l.a[d.getDay()]; },
adam@1615 209 A: function (d, l) { return l.A[d.getDay()]; },
adam@1615 210 b: function (d, l) { return l.b[d.getMonth()]; },
adam@1615 211 B: function (d, l) { return l.B[d.getMonth()]; },
adam@1615 212 C: function (d) { return xPad(parseInt(d.getFullYear()/100, 10), 0); },
adam@1615 213 d: ["getDate", "0"],
adam@1615 214 e: ["getDate", " "],
adam@1615 215 g: function (d) { return xPad(parseInt(Dt.formats.G(d)%100, 10), 0); },
adam@1615 216 G: function (d) {
adam@1615 217 var y = d.getFullYear();
adam@1615 218 var V = parseInt(Dt.formats.V(d), 10);
adam@1615 219 var W = parseInt(Dt.formats.W(d), 10);
phurst@1986 220
adam@1615 221 if(W > V) {
adam@1615 222 y++;
adam@1615 223 } else if(W===0 && V>=52) {
adam@1615 224 y--;
adam@1615 225 }
phurst@1986 226
adam@1615 227 return y;
adam@1615 228 },
adam@1615 229 H: ["getHours", "0"],
adam@1615 230 I: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, 0); },
adam@1615 231 j: function (d) {
adam@1615 232 var gmd_1 = new Date("" + d.getFullYear() + "/1/1 GMT");
adam@1615 233 var gmdate = new Date("" + d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate() + " GMT");
adam@1615 234 var ms = gmdate - gmd_1;
adam@1615 235 var doy = parseInt(ms/60000/60/24, 10)+1;
adam@1615 236 return xPad(doy, 0, 100);
adam@1615 237 },
adam@1615 238 k: ["getHours", " "],
adam@1615 239 l: function (d) { var I=d.getHours()%12; return xPad(I===0?12:I, " "); },
adam@1615 240 m: function (d) { return xPad(d.getMonth()+1, 0); },
adam@1615 241 M: ["getMinutes", "0"],
adam@1615 242 p: function (d, l) { return l.p[d.getHours() >= 12 ? 1 : 0 ]; },
adam@1615 243 P: function (d, l) { return l.P[d.getHours() >= 12 ? 1 : 0 ]; },
adam@1615 244 s: function (d, l) { return parseInt(d.getTime()/1000, 10); },
adam@1615 245 S: ["getSeconds", "0"],
adam@1615 246 u: function (d) { var dow = d.getDay(); return dow===0?7:dow; },
adam@1615 247 U: function (d) {
adam@1615 248 var doy = parseInt(Dt.formats.j(d), 10);
adam@1615 249 var rdow = 6-d.getDay();
adam@1615 250 var woy = parseInt((doy+rdow)/7, 10);
adam@1615 251 return xPad(woy, 0);
adam@1615 252 },
adam@1615 253 V: function (d) {
adam@1615 254 var woy = parseInt(Dt.formats.W(d), 10);
adam@1615 255 var dow1_1 = (new Date("" + d.getFullYear() + "/1/1")).getDay();
adam@1615 256 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
adam@1615 257 if(idow === 53 && (new Date("" + d.getFullYear() + "/12/31")).getDay() < 4)
adam@1615 258 {
adam@1615 259 idow = 1;
adam@1615 260 }
adam@1615 261 else if(idow === 0)
adam@1615 262 {
adam@1615 263 idow = Dt.formats.V(new Date("" + (d.getFullYear()-1) + "/12/31"));
adam@1615 264 }
phurst@1986 265
adam@1615 266 return xPad(idow, 0);
adam@1615 267 },
adam@1615 268 w: "getDay",
adam@1615 269 W: function (d) {
adam@1615 270 var doy = parseInt(Dt.formats.j(d), 10);
adam@1615 271 var rdow = 7-Dt.formats.u(d);
adam@1615 272 var woy = parseInt((doy+rdow)/7, 10);
adam@1615 273 return xPad(woy, 0, 10);
adam@1615 274 },
adam@1615 275 y: function (d) { return xPad(d.getFullYear()%100, 0); },
adam@1615 276 Y: "getFullYear",
adam@1615 277 z: function (d) {
adam@1615 278 var o = d.getTimezoneOffset();
adam@1615 279 var H = xPad(parseInt(Math.abs(o/60), 10), 0);
adam@1615 280 var M = xPad(Math.abs(o%60), 0);
adam@1615 281 return (o>0?"-":"+") + H + M;
adam@1615 282 },
adam@1615 283 Z: function (d) {
adam@1615 284 var tz = d.toString().replace(/^.*:\d\d( GMT[+-]\d+)? \(?([A-Za-z ]+)\)?\d*$/, "$2").replace(/[a-z ]/g, "");
adam@1615 285 if(tz.length > 4) {
adam@1615 286 tz = Dt.formats.z(d);
adam@1615 287 }
adam@1615 288 return tz;
adam@1615 289 },
adam@1615 290 "%": function (d) { return "%"; }
adam@1615 291 },
adam@1615 292
adam@1615 293 aggregates: {
adam@1615 294 c: "locale",
adam@1615 295 D: "%m/%d/%y",
adam@1615 296 F: "%Y-%m-%d",
adam@1615 297 h: "%b",
adam@1615 298 n: "\n",
adam@1615 299 r: "%I:%M:%S %p",
adam@1615 300 R: "%H:%M",
adam@1615 301 t: "\t",
adam@1615 302 T: "%H:%M:%S",
adam@1615 303 x: "locale",
adam@1615 304 X: "locale"
adam@1615 305 },
adam@1615 306
adam@1615 307 format : function (oDate, format) {
adam@1615 308 var replace_aggs = function (m0, m1) {
adam@1615 309 var f = Dt.aggregates[m1];
adam@1615 310 return (f === "locale" ? YDateEn[m1] : f);
adam@1615 311 };
adam@1615 312
adam@1615 313 var replace_formats = function (m0, m1) {
adam@1615 314 var f = Dt.formats[m1];
adam@1615 315 switch(typeof f) {
adam@1615 316 case "string":
adam@1615 317 return oDate[f]();
adam@1615 318 case "function":
adam@1615 319 return f.call(oDate, oDate, YDateEn);
adam@1615 320 case "array":
adam@1615 321 case "object":
adam@1615 322 if(typeof(f[0]) === "string")
adam@1615 323 return xPad(oDate[f[0]](), f[1]);
adam@1615 324 default:
adam@1615 325 return m1;
adam@1615 326 }
adam@1615 327 };
adam@1615 328
adam@1615 329 while(format.match(/%[cDFhnrRtTxX]/)) {
adam@1615 330 format = format.replace(/%([cDFhnrRtTxX])/g, replace_aggs);
adam@1609 331 }
adam@1615 332
adam@1615 333 var str = format.replace(/%([aAbBCdegGHIjklmMpPsSuUVwWyYzZ%])/g, replace_formats);
adam@1615 334
adam@1615 335 replace_aggs = replace_formats = undefined;
adam@1615 336
adam@1615 337 return str;
adam@1615 338 }
adam@1609 339 };
adam@1609 340
adam@1615 341 // End of YUI code
adam@1609 342
adam@1609 343 function strftime(fmt, thisTime)
adam@1609 344 {
adam@1609 345 var thisDate = new Date();
adam@1622 346 thisDate.setTime(Math.floor(thisTime / 1000));
adam@1615 347 return Dt.format(thisDate, fmt);
phurst@1986 348 };
phurst@1986 349
phurst@1986 350 function fromDatetime(year, month, date, hour, minute, second) {
phurst@1986 351 return (new Date(year, month, date, hour, minute, second)).getTime() * 1000;
phurst@1986 352 };
phurst@1986 353
phurst@1986 354 function datetimeYear(t) {
phurst@1986 355 return (new Date(t / 1000)).getYear() + 1900;
phurst@1986 356 };
phurst@1986 357
phurst@1986 358 function datetimeMonth(t) {
phurst@1986 359 return (new Date(t / 1000)).getMonth();
phurst@1986 360 };
phurst@1986 361
phurst@1986 362 function datetimeDay(t) {
phurst@1986 363 return (new Date(t / 1000)).getDate();
phurst@1986 364 };
phurst@1986 365
phurst@1986 366 function datetimeHour(t) {
phurst@1986 367 return (new Date(t / 1000)).getHours();
phurst@1986 368 };
phurst@1986 369
phurst@1986 370 function datetimeMinute(t) {
phurst@1986 371 return (new Date(t / 1000)).getMinutes();
phurst@1986 372 };
phurst@1986 373
phurst@1986 374 function datetimeSecond(t) {
phurst@1986 375 return (new Date(t / 1000)).getSeconds();
phurst@1986 376 };
phurst@1986 377
phurst@1986 378 function datetimeDayOfWeek(t) {
phurst@1986 379 return (new Date(t / 1000)).getDay();
phurst@1986 380 };
adam@1609 381
adam@1487 382
adamc@729 383 // Error handling
adamc@729 384
adam@1625 385 function uw_debug(msg) {
adam@1625 386 try {
adam@1625 387 console.debug(msg);
adam@1625 388 } catch (e) {
adam@1625 389 alert("DEBUG: " + msg);
adam@1625 390 }
adam@1625 391
adam@1625 392 return 0;
adam@1625 393 }
adam@1625 394
adamc@729 395 function whine(msg) {
adamc@1260 396 alert(msg);
adamc@1260 397 throw msg;
adamc@729 398 }
adamc@729 399
adamc@810 400 function pf(loc) {
adamc@1260 401 throw ("Pattern match failure (" + loc + ")");
adamc@729 402 }
adamc@729 403
adam@1553 404 var lameDuck = false;
adam@1553 405
adamc@798 406 function runHandlers(kind, ls, arg) {
adam@1553 407 if (!lameDuck) {
adam@1553 408 if (ls == null)
adam@1553 409 alert(kind + ": " + arg);
adam@1553 410 for (; ls; ls = ls.next)
adam@1553 411 try {
adam@1890 412 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
adam@1553 413 } catch (v) { }
adam@1553 414 }
adamc@729 415 }
adamc@729 416
adamc@729 417 var errorHandlers = null;
adamc@729 418
adam@1890 419 function flift0(v) {
adam@1890 420 return {c:"c", v:v};
adam@1890 421 }
adam@1890 422
adamc@729 423 function onError(f) {
adam@1890 424 errorHandlers = cons(flift0(f), errorHandlers);
adamc@729 425 }
adamc@729 426
adamc@729 427 function er(s) {
adamc@1260 428 runHandlers("Error", errorHandlers, s);
adamc@1260 429 throw {uw_error: s};
adamc@729 430 }
adamc@729 431
adamc@729 432 var failHandlers = null;
adamc@729 433
adamc@729 434 function onFail(f) {
adam@1890 435 failHandlers = cons(flift0(f), failHandlers);
adamc@729 436 }
adamc@729 437
adamc@729 438 function doExn(v) {
adamc@1260 439 if (v == null || v.uw_error == null) {
adamc@1260 440 var s = (v == null ? "null" : v.message ? v.message : v.toString());
adamc@1260 441 if (v != null && v.fileName && v.lineNumber)
adamc@1260 442 s += " (" + v.fileName + ":" + v.lineNumber + ")";
adamc@1260 443 runHandlers("Fail", failHandlers, s);
adamc@1260 444 }
adamc@729 445 }
adamc@729 446
adamc@729 447 var disconnectHandlers = null;
adamc@729 448
adamc@976 449 function flift(f) {
kkallio@1484 450 return {c: "c", v:{env:cons(f,null), body:{c:"v", n:1}}};
adamc@976 451 }
adamc@976 452
adamc@729 453 function onDisconnect(f) {
adamc@1260 454 disconnectHandlers = cons(flift(f), disconnectHandlers);
adamc@729 455 }
adamc@729 456
adamc@729 457 function discon() {
adamc@1260 458 runHandlers("Disconnect", disconnectHandlers, null);
adamc@729 459 }
adamc@729 460
adamc@729 461 var connectHandlers = null;
adamc@729 462
adamc@729 463 function onConnectFail(f) {
adamc@1260 464 connectHandlers = cons(flift(f), connectHandlers);
adamc@729 465 }
adamc@729 466
adamc@729 467 function conn() {
adamc@1260 468 runHandlers("Connect", connectHandlers, null);
adamc@729 469 }
adamc@729 470
adamc@729 471 var serverHandlers = null;
adamc@729 472
adamc@729 473 function onServerError(f) {
adam@1890 474 serverHandlers = cons(flift0(f), serverHandlers);
adamc@729 475 }
adamc@729 476
adamc@729 477 function servErr(s) {
adamc@1260 478 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
adamc@729 479 }
adamc@729 480
adam@1783 481 // Key and mouse events
adam@1555 482
adam@1555 483 var uw_event = null;
adam@1555 484
adam@1783 485 function uw_getEvent() {
adam@1783 486 return window.event ? window.event : uw_event;
adam@1555 487 }
adam@1555 488
adam@1783 489 function firstGood(x, y) {
adam@1783 490 if (x == undefined || x == 0)
adam@1783 491 return y;
adam@1783 492 else
adam@1783 493 return x;
adam@1783 494 }
adam@1783 495
adam@1783 496 function uw_mouseEvent() {
adam@1783 497 var ev = uw_getEvent();
adam@1783 498
adam@1783 499 return {_ScreenX : firstGood(ev.screenX, 0),
adam@1783 500 _ScreenY : firstGood(ev.screenY, 0),
adam@1783 501 _ClientX : firstGood(ev.clientX, 0),
adam@1783 502 _ClientY : firstGood(ev.clientY, 0),
adam@1783 503 _CtrlKey : firstGood(ev.ctrlKey, false),
adam@1783 504 _ShiftKey : firstGood(ev.shiftKey, false),
adam@1783 505 _AltKey : firstGood(ev.altKey, false),
adam@1783 506 _MetaKey : firstGood(ev.metaKey, false),
adam@1783 507 _Button : ev.button == 2 ? "Right" : ev.button == 1 ? "Middle" : "Left"};
adam@1783 508 }
adam@1783 509
adam@1783 510 function uw_keyEvent() {
adam@1783 511 var ev = uw_getEvent();
adam@1783 512
adam@1783 513 return {_KeyCode : firstGood(ev.keyCode, ev.which),
adam@1783 514 _CtrlKey : firstGood(ev.ctrlKey, false),
adam@1783 515 _ShiftKey : firstGood(ev.shiftKey, false),
adam@1783 516 _AltKey : firstGood(ev.altKey, false),
adam@1783 517 _MetaKey : firstGood(ev.metaKey, false)};
adam@1783 518 }
adam@1783 519
adam@1783 520
adam@1783 521
adam@1555 522 // Document events
adam@1555 523
adam@1555 524 function uw_handler(name, f) {
adam@1555 525 var old = document[name];
adam@1555 526 if (old == undefined)
adam@1783 527 document[name] = function(event) { uw_event = event; execF(execF(f, uw_mouseEvent())); };
adam@1555 528 else
adam@1783 529 document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_mouseEvent())); };
adam@1555 530 }
adam@1555 531
adam@1555 532 function uw_onClick(f) {
adam@1555 533 uw_handler("onclick", f);
adam@1555 534 }
adam@1555 535
adam@1555 536 function uw_onDblclick(f) {
adam@1555 537 uw_handler("ondblclick", f);
adam@1555 538 }
adam@1555 539
adam@1555 540 function uw_onMousedown(f) {
adam@1555 541 uw_handler("onmousedown", f);
adam@1555 542 }
adam@1555 543
adam@1791 544 function uw_onMousemove(f) {
adam@1791 545 uw_handler("onmousemove", f);
adam@1791 546 }
adam@1791 547
adam@1791 548 function uw_onMouseout(f) {
adam@1791 549 uw_handler("onmouseout", f);
adam@1791 550 }
adam@1791 551
adam@1791 552 function uw_onMouseover(f) {
adam@1791 553 uw_handler("onmouseover", f);
adam@1791 554 }
adam@1791 555
adam@1555 556 function uw_onMouseup(f) {
adam@1555 557 uw_handler("onmouseup", f);
adam@1555 558 }
adam@1555 559
adam@1555 560 function uw_keyHandler(name, f) {
adam@1555 561 var old = document[name];
adam@1555 562 if (old == undefined)
adam@1783 563 document[name] = function(event) { uw_event = event; execF(execF(f, uw_keyEvent())); };
adam@1555 564 else
adam@1783 565 document[name] = function(event) { uw_event = event; old(); execF(execF(f, uw_keyEvent())); };
adam@1555 566 }
adam@1555 567
adam@1555 568 function uw_onKeydown(f) {
adam@1555 569 uw_keyHandler("onkeydown", f);
adam@1555 570 }
adam@1555 571
adam@1555 572 function uw_onKeypress(f) {
adam@1555 573 uw_keyHandler("onkeypress", f);
adam@1555 574 }
adam@1555 575
adam@1555 576 function uw_onKeyup(f) {
adam@1555 577 uw_keyHandler("onkeyup", f);
adam@1555 578 }
adam@1555 579
adam@1559 580 // Cancelling of further event processing
adam@1559 581
adam@1559 582 function uw_preventDefault() {
adam@1559 583 var e = window.event ? window.event : uw_event;
adam@1559 584 e.returnValue = false;
adam@1559 585 if (e.preventDefault) e.preventDefault();
adam@1559 586 }
adam@1559 587
adam@1559 588 function uw_stopPropagation() {
adam@1559 589 var e = window.event ? window.event : uw_event;
adam@1559 590 e.cancelBubble = true;
adam@1559 591 if (e.stopPropagation) e.stopPropagation();
adam@1559 592 }
adamc@729 593
adamc@692 594 // Embedding closures in XML strings
adamc@692 595
adamc@693 596 function cs(f) {
adamc@1260 597 return {closure: f};
adamc@693 598 }
adamc@693 599
adamc@693 600 function isWeird(v) {
adamc@1260 601 return v.closure != null || v.cat1 != null;
adamc@693 602 }
adamc@693 603
adamc@692 604 function cat(s1, s2) {
adamc@1260 605 if (isWeird(s1) || isWeird(s2))
adamc@1260 606 return {cat1: s1, cat2: s2};
adamc@1260 607 else
adamc@1260 608 return s1 + s2;
adamc@692 609 }
adamc@692 610
adamc@692 611 var closures = [];
adamc@703 612 var freeClosures = null;
adamc@692 613
adamc@692 614 function newClosure(f) {
adamc@1260 615 var n;
adamc@1260 616 if (freeClosures == null) {
adamc@1260 617 n = closures.length;
adamc@1260 618 } else {
adamc@1260 619 n = freeClosures.data;
adamc@1260 620 freeClosures = freeClosures.next;
adamc@1260 621 }
adamc@1260 622 closures[n] = f;
adamc@1260 623 return n;
adamc@692 624 }
adamc@692 625
adamc@703 626 function freeClosure(n) {
adamc@1260 627 closures[n] = null;
adamc@1260 628 freeClosures = cons(n, freeClosures);
adamc@703 629 }
adamc@703 630
adamc@692 631 function cr(n) {
adamc@1260 632 return closures[n];
adamc@692 633 }
adamc@692 634
vshabanoff@1813 635 function flattenAcc(a, cls, tr) {
vshabanoff@1813 636 if (tr.cat1 != null) {
vshabanoff@1813 637 flattenAcc(a, cls, tr.cat1);
vshabanoff@1813 638 flattenAcc(a, cls, tr.cat2);
vshabanoff@1813 639 } else if (tr.closure != null) {
adamc@1260 640 var cl = newClosure(tr.closure);
adamc@1260 641 cls.v = cons(cl, cls.v);
vshabanoff@1813 642 a.push("cr(", cl.toString(), ")");
adamc@1260 643 } else
vshabanoff@1813 644 a.push(tr);
vshabanoff@1813 645 }
vshabanoff@1813 646
vshabanoff@1813 647 function flatten(cls, tr) {
vshabanoff@1813 648 var a = [];
vshabanoff@1813 649 flattenAcc(a, cls, tr);
vshabanoff@1813 650 return a.join("");
adamc@692 651 }
adamc@692 652
adamc@728 653 function flattenLocal(s) {
adamc@1260 654 var cls = {v : null};
adamc@1260 655 var r = flatten(cls, s);
adamc@1260 656 for (cl = cls.v; cl != null; cl = cl.next)
adamc@1260 657 freeClosure(cl.data);
adamc@1260 658 return r;
adamc@728 659 }
adamc@728 660
adamc@692 661
adamc@692 662 // Dynamic tree management
adamc@692 663
adamc@690 664 function populate(node) {
adamc@1260 665 var s = node.signal;
adamc@1260 666 var oldSources = node.sources;
adamc@1260 667 try {
adamc@1260 668 var sr = execF(s, null);
adamc@1260 669 var newSources = sr._sources;
adamc@690 670
adamc@1260 671 for (var sp = oldSources; sp; sp = sp.next)
adamc@1260 672 if (!member(sp.data, newSources))
adamc@1260 673 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 674
adamc@1260 675 for (var sp = newSources; sp; sp = sp.next)
adamc@1260 676 if (!member(sp.data, oldSources))
adamc@1260 677 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 678
adamc@1260 679 node.sources = newSources;
adamc@1260 680 node.recreate(sr._data);
adamc@1260 681 } catch (v) {
adamc@1260 682 doExn(v);
adamc@1260 683 }
adamc@579 684 }
adamc@574 685
adamc@580 686 function sc(v) {
adamc@1260 687 return {data : v, dyns : null};
adamc@580 688 }
adamc@580 689 function sv(s, v) {
adam@1491 690 if (s.data != v) {
adam@1491 691 s.data = v;
adamc@967 692
adam@1491 693 for (var ls = s.dyns; ls; ls = ls.next)
adam@1491 694 if (!ls.dead)
adam@1491 695 populate(ls.data);
adam@1491 696 }
adamc@580 697 }
adamc@601 698 function sg(s) {
adamc@1260 699 return s.data;
adamc@601 700 }
adamc@579 701
adamc@580 702 function ss(s) {
adamc@1260 703 return {env:cons(s, null), body:{c:"r", l:
adamc@1260 704 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
adamc@1260 705 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
adamc@580 706 }
adamc@580 707 function sr(v) {
adamc@1260 708 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
adamc@580 709 }
adamc@580 710 function sb(x,y) {
adamc@1260 711 return {env:cons(y,cons(x,null)),
adamc@1260 712 body:{c:"=",
adamc@1260 713 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
adamc@1260 714 e2:{c:"=",
adamc@1260 715 e1:{c:"a",
adamc@1260 716 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
adamc@1260 717 x:{c:"c", v:null}},
adamc@1260 718 e2:{c:"r", l:cons(
adamc@1260 719 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
adamc@1260 720 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
adamc@1260 721 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
adamc@580 722 }
adamc@841 723 function scur(s) {
adamc@1260 724 return execF(s, null)._data;
adamc@841 725 }
adamc@571 726
adamc@604 727 function lastParent() {
adamc@1260 728 var pos = document.body;
adamc@604 729
adamc@1260 730 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@1260 731 pos = pos.lastChild;
adamc@600 732
adamc@1260 733 pos = pos.parentNode;
adamc@1039 734
adamc@1260 735 return pos;
adamc@600 736 }
adamc@600 737
adam@1803 738 var thisScript = null;
adam@1803 739
adamc@604 740 function addNode(node) {
adam@1678 741 if (thisScript) {
adam@1678 742 if (thisScript.parentNode)
adam@1678 743 thisScript.parentNode.replaceChild(node, thisScript);
adam@1678 744 } else
adamc@1260 745 lastParent().appendChild(node);
adamc@603 746 }
adamc@603 747
adam@1803 748 function runScripts(node) {
adam@1803 749 if (node.tagName == "SCRIPT") {
adam@1803 750 var savedScript = thisScript;
adam@1803 751 thisScript = node;
phurst@1986 752
adam@1803 753 try {
adam@1803 754 eval(thisScript.text);
adam@1803 755 } catch (v) {
adam@1803 756 doExn(v);
adam@1803 757 }
adam@1803 758 if (thisScript.parentNode)
adam@1803 759 thisScript.parentNode.removeChild(thisScript);
adamc@690 760
adam@1803 761 thisScript = savedScript;
adam@1803 762 } else if (node.getElementsByTagName) {
adamc@1260 763 var savedScript = thisScript;
adamc@603 764
adamc@1260 765 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@1260 766 var len = scripts.length;
adamc@1260 767 for (var i = 0; i < len; ++i)
adamc@1260 768 scriptsCopy[i] = scripts[i];
adamc@1260 769 for (var i = 0; i < len; ++i) {
adamc@1260 770 thisScript = scriptsCopy[i];
adamc@1039 771
adamc@1260 772 try {
adamc@1260 773 eval(thisScript.text);
adamc@1260 774 } catch (v) {
adamc@1260 775 doExn(v);
adamc@1260 776 }
adamc@1260 777 if (thisScript.parentNode)
adamc@1260 778 thisScript.parentNode.removeChild(thisScript);
adamc@1260 779 }
adamc@1260 780
adamc@1260 781 thisScript = savedScript;
adamc@729 782 }
adamc@603 783 }
adamc@603 784
adamc@603 785
adamc@692 786 // Dynamic tree entry points
adamc@692 787
adamc@967 788 function killScript(scr) {
adamc@1260 789 scr.dead = true;
adamc@1260 790 for (var ls = scr.sources; ls; ls = ls.next)
adamc@1260 791 ls.data.dyns = remove(scr, ls.data.dyns);
adamc@1260 792 for (var ls = scr.closures; ls; ls = ls.next)
adamc@1260 793 freeClosure(ls.data);
adamc@967 794 }
adamc@967 795
adam@1538 796 // Sometimes we wind up with tables that contain <script>s outside the single <tbody>.
adam@1538 797 // To avoid dealing with that case, we normalize by moving <script>s into <tbody>.
adam@1538 798 function normalizeTable(table) {
adam@1538 799 var orig = table;
adam@1538 800
adam@1538 801 var script, next;
adam@1538 802
adam@1803 803 while (table && table.tagName != "TABLE")
adam@1538 804 table = table.parentNode;
adam@1538 805
adam@1538 806 for (var tbody = table.firstChild; tbody; tbody = tbody.nextSibling) {
adam@1538 807 if (tbody.tagName == "TBODY") {
adam@1681 808 var firstChild = tbody.firstChild;
adam@1681 809
adam@1538 810 for (script = table.firstChild; script && script != tbody; script = next) {
adam@1538 811 next = script.nextSibling;
adam@1538 812
adam@1681 813 if (firstChild)
adam@1681 814 tbody.insertBefore(script, firstChild);
adam@1681 815 else
adam@1681 816 tbody.appendChild(script);
adam@1538 817 }
adam@1538 818
adam@1538 819 return;
adam@1538 820 }
adam@1538 821 }
adam@1538 822
adam@1538 823 var tbody = document.createElement("tbody");
adam@1538 824 for (script = table.firstChild; script; script = next) {
adam@1538 825 next = script.nextSibling;
rmbruijn@1597 826
adam@1681 827 tbody.appendChild(script);
adam@1538 828 }
adam@1538 829 table.appendChild(tbody);
adam@1538 830 }
adam@1538 831
adam@1803 832 var suspendScripts = false;
adam@1803 833
adamc@901 834 function dyn(pnode, s) {
adam@1803 835 if (suspendScripts)
adam@1803 836 return;
adam@1803 837
adamc@1260 838 var x = document.createElement("script");
adamc@1260 839 x.dead = false;
adamc@1260 840 x.signal = s;
adamc@1260 841 x.sources = null;
adamc@1260 842 x.closures = null;
adamc@899 843
adamc@1260 844 var firstChild = null;
adamc@899 845
adamc@1260 846 x.recreate = function(v) {
adamc@1260 847 for (var ls = x.closures; ls; ls = ls.next)
adamc@1260 848 freeClosure(ls.data);
adamc@945 849
adamc@946 850 var next;
adamc@1260 851 for (var child = firstChild; child && child != x; child = next) {
adamc@1260 852 next = child.nextSibling;
adamc@1260 853
adamc@1260 854 killScript(child);
adamc@1260 855 if (child.getElementsByTagName) {
adamc@1260 856 var arr = child.getElementsByTagName("script");
adamc@1260 857 for (var i = 0; i < arr.length; ++i)
adamc@1260 858 killScript(arr[i]);
adamc@1260 859 }
adamc@1260 860
adamc@1260 861 if (child.parentNode)
adamc@1260 862 child.parentNode.removeChild(child);
adamc@946 863 }
adamc@903 864
adamc@1260 865 var cls = {v : null};
adamc@1260 866 var html = flatten(cls, v);
adamc@1260 867 if (pnode != "table" && pnode != "tr")
adamc@1260 868 html = dynPrefix + html;
adamc@1260 869 x.closures = cls.v;
adamc@946 870
adamc@1260 871 if (pnode == "table") {
adam@1538 872 normalizeTable(x.parentNode);
adam@1538 873
adamc@1260 874 var dummy = document.createElement("body");
adam@1803 875 suspendScripts = true;
adam@1803 876 try {
adam@1803 877 dummy.innerHTML = "<table>" + html + "</table>";
adam@1803 878 } catch (e) {
adam@1803 879 suspendScripts = false;
adam@1803 880 throw e;
adam@1803 881 }
adam@1803 882
adamc@1260 883 var table = x.parentNode;
adamc@1260 884
adamc@1260 885 if (table) {
adamc@1260 886 firstChild = null;
adamc@1260 887 var tbody;
adamc@1260 888
adamc@1260 889 var arr = dummy.getElementsByTagName("tbody");
adamc@1260 890
adamc@1260 891 var tbody;
adamc@1260 892 if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) {
adamc@1260 893 tbody = arr[0];
adamc@1260 894 var next;
adamc@1260 895 for (var node = dummy.firstChild.firstChild; node; node = next) {
adamc@1260 896 next = node.nextSibling;
adamc@1260 897
adamc@1260 898 if (node.tagName != "TBODY")
adamc@1260 899 tbody.appendChild(node);
adamc@1260 900 }
adamc@1260 901 } else
adamc@1260 902 tbody = dummy.firstChild;
adamc@1260 903
adamc@1260 904 var next;
adamc@1260 905 firstChild = document.createElement("script");
adamc@1260 906 table.insertBefore(firstChild, x);
adamc@1260 907 for (var node = tbody.firstChild; node; node = next) {
adamc@1260 908 next = node.nextSibling;
adamc@1260 909 table.insertBefore(node, x);
adam@1803 910 suspendScripts = false;
adam@1803 911 runScripts(node);
adam@1803 912 suspendScripts = true;
adamc@1260 913 }
adamc@1260 914 }
adam@1803 915
adam@1803 916 suspendScripts = false;
adamc@1260 917 } else if (pnode == "tr") {
adamc@1260 918 var dummy = document.createElement("body");
adam@1803 919 suspendScripts = true;
adam@1803 920 try {
adam@1803 921 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adam@1803 922 } catch (e) {
adam@1803 923 suspendScripts = false;
adam@1803 924 throw e;
adam@1803 925 }
adam@1803 926
adamc@1260 927 var table = x.parentNode;
adamc@1260 928
adamc@1260 929 if (table) {
adamc@1260 930 var arr = dummy.getElementsByTagName("tr");
adamc@1260 931 firstChild = null;
adamc@1260 932 var tr;
adamc@1260 933 if (arr.length > 0 && table != null)
adamc@1260 934 tr = arr[0];
adamc@1260 935 else
adamc@1260 936 tr = dummy.firstChild;
adamc@1260 937
adamc@1260 938 var next;
adamc@1260 939 firstChild = document.createElement("script");
adamc@1260 940 table.insertBefore(firstChild, x);
adamc@1260 941 for (var node = tr.firstChild; node; node = next) {
adamc@1260 942 next = node.nextSibling;
adamc@1260 943 table.insertBefore(node, x);
adam@1803 944 suspendScripts = false;
adam@1803 945 runScripts(node);
adam@1803 946 suspendScripts = true;
adamc@1260 947 }
adam@1803 948 };
adam@1803 949
adam@1803 950 suspendScripts = false;
adamc@1260 951 } else {
adamc@1260 952 firstChild = document.createElement("span");
adam@1803 953
adam@1803 954 suspendScripts = true;
adam@1803 955 try {
adam@1803 956 firstChild.innerHTML = html;
adam@1803 957 if (x.parentNode)
adam@1803 958 x.parentNode.insertBefore(firstChild, x);
adam@1803 959 } catch (e) {
adam@1803 960 suspendScripts = false;
adam@1803 961 throw e;
adam@1803 962 }
adam@1803 963 suspendScripts = false;
adam@1802 964 runScripts(firstChild);
adamc@903 965 }
adamc@1260 966 };
adamc@899 967
adamc@1260 968 addNode(x);
adamc@1260 969 populate(x);
adamc@571 970 }
adamc@582 971
adam@1702 972 function setInnerHTML(node, html) {
adam@1702 973 var x;
adam@1702 974
adam@1702 975 if (node.previousSibling && node.previousSibling.closures != undefined) {
adam@1702 976 x = node.previousSibling;
adam@1702 977
adam@1702 978 for (var ls = x.closures; ls; ls = ls.next)
adam@1702 979 freeClosure(ls.data);
adam@1702 980
adam@1702 981 if (node.getElementsByTagName) {
adam@1702 982 var arr = node.getElementsByTagName("script");
adam@1702 983 for (var i = 0; i < arr.length; ++i)
adam@1702 984 killScript(arr[i]);
adam@1702 985 }
adam@1702 986 } else {
adam@1702 987 x = document.createElement("script");
adam@1702 988 x.dead = false;
adam@1702 989 x.sources = null;
adam@1702 990
adam@1702 991 if (node.parentNode)
adam@1702 992 node.parentNode.insertBefore(x, node);
adam@1702 993 else
adam@1702 994 whine("setInnerHTML: node is not already in the DOM tree");
adam@1702 995 }
adam@1702 996
adam@1702 997 var cls = {v : null};
adam@1702 998 var html = flatten(cls, html);
adam@1702 999 x.closures = cls.v;
adam@1803 1000 suspendScripts = true;
adam@1702 1001 node.innerHTML = html;
adam@1803 1002 suspendScripts = false;
adam@1702 1003 runScripts(node);
adam@1702 1004 }
adam@1702 1005
adam@1792 1006 var maySuspend = true;
adam@1792 1007
adam@1786 1008 function active(s) {
adam@1803 1009 if (suspendScripts)
adam@1803 1010 return;
adam@1803 1011
adam@1792 1012 var ms = maySuspend;
adam@1792 1013 maySuspend = false;
adam@1792 1014 try {
adam@1792 1015 var html = execF(s);
adam@1792 1016 } catch (e) {
adam@1792 1017 maySuspend = ms;
adam@1792 1018 throw e;
adam@1792 1019 }
adam@1792 1020 maySuspend = ms;
adam@2002 1021 if (html != "") {
adam@2002 1022 var span = document.createElement("span");
adam@2002 1023 addNode(span);
adam@2002 1024 setInnerHTML(span, html);
adam@2002 1025 }
adam@1786 1026 }
adam@1786 1027
adam@1463 1028 function input(x, s, recreate, type, name) {
adam@1463 1029 if (name) x.name = name;
adamc@1260 1030 if (type) x.type = type;
adamc@1260 1031 x.dead = false;
adamc@1260 1032 x.signal = ss(s);
adamc@1260 1033 x.sources = null;
adamc@1260 1034 x.recreate = recreate(x);
adamc@1260 1035 addNode(x);
adamc@1260 1036 populate(x);
adamc@816 1037
adamc@1260 1038 return x;
adamc@816 1039 }
adamc@816 1040
adam@1463 1041 function inp(s, name) {
adam@1803 1042 if (suspendScripts)
adam@1803 1043 return;
adam@1803 1044
adamc@1260 1045 var x = input(document.createElement("input"), s,
adam@1835 1046 function(x) { return function(v) { if (x.value != v) x.value = v; }; }, "text", name);
adamc@1260 1047 x.value = s.data;
adam@1491 1048 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
adamc@816 1049
adamc@1260 1050 return x;
adamc@816 1051 }
adamc@816 1052
ziv@2055 1053 function password(s, name) {
ziv@2055 1054 if (suspendScripts)
ziv@2055 1055 return;
ziv@2055 1056
ziv@2055 1057 var x = input(document.createElement("input"), s,
ziv@2055 1058 function(x) { return function(v) { if (x.value != v) x.value = v; }; }, "password", name);
ziv@2055 1059 x.value = s.data;
ziv@2055 1060 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
ziv@2055 1061
ziv@2055 1062 return x;
ziv@2055 1063 }
ziv@2055 1064
adam@1479 1065 function selectValue(x) {
adam@1479 1066 if (x.options.length == 0)
adam@1479 1067 return "";
adam@1479 1068 else
adam@1479 1069 return x.options[x.selectedIndex].value;
adam@1479 1070 }
adam@1479 1071
adam@1479 1072 function setSelectValue(x, v) {
adam@1479 1073 for (var i = 0; i < x.options.length; ++i) {
adam@1479 1074 if(x.options[i].value == v) {
adam@1479 1075 x.selectedIndex = i;
adam@1479 1076 return;
adam@1479 1077 }
adam@1479 1078 }
adam@1479 1079 }
adam@1479 1080
adamc@816 1081 function sel(s, content) {
adam@1803 1082 if (suspendScripts)
adam@1803 1083 return;
adam@1803 1084
adamc@1260 1085 var dummy = document.createElement("span");
adamc@1260 1086 dummy.innerHTML = "<select>" + content + "</select>";
adam@1479 1087 var x = input(dummy.firstChild, s, function(x) { return function(v) { if (selectValue(x) != v) setSelectValue(x, v); }; });
adam@1479 1088
adam@1479 1089 for (var i = 0; i < x.options.length; ++i) {
adam@1479 1090 if (x.options[i].value == "")
adam@1479 1091 x.options[i].value = x.options[i].text;
adam@1479 1092 else
adam@1479 1093 x.options[i].value = x.options[i].value.substring(1);
adam@1479 1094 }
adam@1479 1095
adam@1479 1096 setSelectValue(x, s.data);
adam@1479 1097 if (selectValue(x) != s.data)
adam@1479 1098 sv(s, selectValue(x));
adam@1479 1099 x.onchange = function() { sv(s, selectValue(x)) };
adamc@797 1100
adamc@1260 1101 return x;
adamc@598 1102 }
adamc@598 1103
adamc@817 1104 function chk(s) {
adam@1803 1105 if (suspendScripts)
adam@1803 1106 return;
adam@1803 1107
adamc@1260 1108 var x = input(document.createElement("input"), s,
adamc@1260 1109 function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
adamc@1260 1110 x.defaultChecked = x.checked = s.data;
adam@1610 1111 x.onclick = x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.checked) };
adamc@817 1112
adamc@1260 1113 return x;
adamc@817 1114 }
adamc@817 1115
adamc@1099 1116 function tbx(s) {
adam@1803 1117 if (suspendScripts)
adam@1803 1118 return;
adam@1803 1119
adamc@1260 1120 var x = input(document.createElement("textarea"), s,
adam@1781 1121 function(x) { return function(v) { if (x.value != v) x.value = v; }; });
adamc@1260 1122 x.innerHTML = s.data;
adam@1491 1123 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
adamc@1099 1124
adamc@1260 1125 return x;
adamc@1099 1126 }
adamc@1099 1127
adam@2012 1128 function dynClass(pnode, html, s_class, s_style) {
adam@1803 1129 if (suspendScripts)
adam@1803 1130 return;
adam@1803 1131
adam@1643 1132 var htmlCls = {v : null};
adam@1643 1133 html = flatten(htmlCls, html);
adam@1643 1134 htmlCls = htmlCls.v;
adam@1643 1135
adam@2012 1136 var dummy = document.createElement(pnode);
adam@1803 1137 suspendScripts = true;
adam@1643 1138 dummy.innerHTML = html;
adam@1803 1139 suspendScripts = false;
adam@1643 1140 var html = dummy.firstChild;
adam@1643 1141 dummy.removeChild(html);
adam@1643 1142 addNode(html);
adam@1792 1143 runScripts(html);
adam@1643 1144
adam@1751 1145 if (s_class) {
adam@1751 1146 var x = document.createElement("script");
adam@1751 1147 x.dead = false;
adam@1751 1148 x.signal = s_class;
adam@1751 1149 x.sources = null;
adam@1751 1150 x.closures = htmlCls;
phurst@1986 1151
adam@1751 1152 x.recreate = function(v) {
adam@1751 1153 for (var ls = x.closures; ls != htmlCls; ls = ls.next)
adam@1751 1154 freeClosure(ls.data);
adam@1643 1155
adam@1751 1156 var cls = {v : null};
adam@1751 1157 html.className = flatten(cls, v);
adam@1751 1158 x.closures = concat(cls.v, htmlCls);
adam@1751 1159 }
adam@1643 1160
vshabanoff@1811 1161 html.appendChild(x);
adam@1751 1162 populate(x);
adam@1751 1163 }
adam@1751 1164
adam@1751 1165 if (s_style) {
vshabanoff@1811 1166 var htmlCls2 = s_class ? null : htmlCls;
adam@2012 1167 var y = document.createElement("script");
adam@2012 1168 y.dead = false;
adam@2012 1169 y.signal = s_style;
adam@2012 1170 y.sources = null;
adam@2012 1171 y.closures = htmlCls2;
phurst@1986 1172
adam@2012 1173 y.recreate = function(v) {
adam@2012 1174 for (var ls = y.closures; ls != htmlCls2; ls = ls.next)
adam@1751 1175 freeClosure(ls.data);
adam@1751 1176
adam@1751 1177 var cls = {v : null};
adam@1751 1178 html.style.cssText = flatten(cls, v);
adam@2012 1179 y.closures = concat(cls.v, htmlCls2);
adam@1751 1180 }
adam@1751 1181
adam@2012 1182 html.appendChild(y);
adam@2012 1183 populate(y);
adam@1751 1184 }
adam@1643 1185 }
adam@1643 1186
adamc@800 1187 function addOnChange(x, f) {
adamc@1260 1188 var old = x.onchange;
adam@1290 1189 if (old == null)
adam@1290 1190 x.onchange = f;
adam@1290 1191 else
adam@1290 1192 x.onchange = function() { old(); f(); };
adam@1290 1193 }
adam@1290 1194
adam@1290 1195 function addOnKeyUp(x, f) {
adam@1290 1196 var old = x.onkeyup;
adam@1290 1197 if (old == null)
adam@1290 1198 x.onkeyup = f;
adam@1290 1199 else
adam@1290 1200 x.onkeyup = function(x) { old(x); f(x); };
adamc@800 1201 }
adamc@800 1202
adamc@692 1203
adamc@692 1204 // Basic string operations
adamc@692 1205
adamc@597 1206 function eh(x) {
adamc@1260 1207 if (x == null)
adamc@1260 1208 return "NULL";
adamc@1260 1209 else
adamc@1260 1210 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 1211 }
adamc@597 1212
adamc@582 1213 function ts(x) { return x.toString() }
adamc@586 1214 function bs(b) { return (b ? "True" : "False") }
adamc@586 1215
adamc@1057 1216 function id(x) { return x; }
adamc@1038 1217 function sub(s, i) { return s.charAt(i); }
adamc@828 1218 function suf(s, i) { return s.substring(i); }
adamc@828 1219 function slen(s) { return s.length; }
adamc@829 1220 function sidx(s, ch) {
adamc@1260 1221 var r = s.indexOf(ch);
adamc@1260 1222 if (r == -1)
adamc@1260 1223 return null;
adamc@1260 1224 else
adamc@1260 1225 return r;
adamc@829 1226 }
dukhovni@2045 1227 function ssidx(h, n) {
dukhovni@2045 1228 var r = h.indexOf(n);
dukhovni@2045 1229 if (r == -1)
dukhovni@2045 1230 return null;
dukhovni@2045 1231 else
dukhovni@2045 1232 return r;
dukhovni@2045 1233 }
adamc@831 1234 function sspn(s, chs) {
adamc@1260 1235 for (var i = 0; i < s.length; ++i)
adamc@1260 1236 if (chs.indexOf(s.charAt(i)) != -1)
adamc@1260 1237 return i;
adamc@831 1238
adam@1793 1239 return s.length;
adamc@831 1240 }
adamc@829 1241 function schr(s, ch) {
adamc@1260 1242 var r = s.indexOf(ch);
adamc@1260 1243 if (r == -1)
adamc@1260 1244 return null;
adamc@1260 1245 else
adamc@1260 1246 return s.substring(r);
adamc@829 1247 }
adamc@829 1248 function ssub(s, start, len) {
adamc@1260 1249 return s.substring(start, start+len);
adamc@829 1250 }
adam@1624 1251 function strlenGe(s, len) {
adam@1624 1252 return s.length >= len;
adam@1624 1253 }
adamc@821 1254
adam@1621 1255 function trimZeroes(s) {
adam@1621 1256 for (var i = 0; i < s.length; ++i)
adam@1632 1257 if (s.charAt(i) != '0') {
adam@1621 1258 if (i > 0)
adam@1621 1259 return s.substring(i);
adam@1621 1260 else
adam@1621 1261 return s;
adam@1621 1262 }
adam@1621 1263
adam@1631 1264 if (s.length == 0)
adam@1621 1265 return s;
adam@1621 1266 else
adam@1631 1267 return "0";
adam@1621 1268 }
adam@1621 1269
adamc@649 1270 function pi(s) {
adam@1631 1271 var st = trimZeroes(s);
adam@1631 1272 var r = parseInt(st);
adam@1631 1273 if (r.toString() == st)
adamc@1260 1274 return r;
adamc@1260 1275 else
adamc@1260 1276 er("Can't parse int: " + s);
adamc@649 1277 }
adamc@649 1278
adamc@649 1279 function pfl(s) {
adamc@1260 1280 var r = parseFloat(s);
adamc@1260 1281 if (r.toString() == s)
adamc@1260 1282 return r;
adamc@1260 1283 else
adamc@1260 1284 er("Can't parse float: " + s);
adamc@649 1285 }
adamc@649 1286
adamc@838 1287 function pio(s) {
adam@1631 1288 var st = trimZeroes(s);
adam@1631 1289 var r = parseInt(st);
adam@1631 1290 if (r.toString() == st)
adamc@1260 1291 return r;
adamc@1260 1292 else
adamc@1260 1293 return null;
adamc@838 1294 }
adamc@838 1295
adamc@838 1296 function pflo(s) {
adamc@1260 1297 var r = parseFloat(s);
adamc@1260 1298 if (r.toString() == s)
adamc@1260 1299 return r;
adamc@1260 1300 else
adamc@1260 1301 return null;
adamc@838 1302 }
adamc@838 1303
adam@1620 1304 function parseSource(s1, s2) {
adam@1620 1305 return eval("s" + s1 + "_" + s2);
adam@1620 1306 }
adam@1620 1307
adamc@692 1308 function uf(s) {
adamc@1260 1309 if (s.length == 0)
adamc@1260 1310 return "_";
vshabanoff@1813 1311 s = s.replace(/\./g, ".2E");
vshabanoff@1813 1312 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(/%/g, ".");
adamc@691 1313 }
adamc@691 1314
adamc@692 1315 function uu(s) {
adamc@1260 1316 if (s.length > 0 && s.charAt(0) == '_') {
adamc@1260 1317 s = s.substring(1);
adamc@1260 1318 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.')
adamc@1260 1319 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
adamc@1260 1320 s = s.substring(3);
vshabanoff@1813 1321 s = s.replace(/\+/g, " ");
vshabanoff@1813 1322 s = s.replace(/\./g, "%");
adamc@1260 1323 return decodeURIComponent(s);
adamc@905 1324 }
adamc@905 1325
adamc@1108 1326 function atr(s) {
vshabanoff@1813 1327 return s.replace(/\"/g, "&quot;").replace(/&/g, "&amp;")
vshabanoff@1813 1328 }
adamc@1108 1329
adamc@912 1330 function ub(b) {
adamc@1260 1331 return b ? "1" : "0";
adamc@912 1332 }
adamc@912 1333
adamc@905 1334 function uul(getToken, getData) {
adamc@1260 1335 var tok = getToken();
adamc@1260 1336 if (tok == "Nil") {
adamc@1260 1337 return null;
adamc@1260 1338 } else if (tok == "Cons") {
adamc@1260 1339 var d = getData();
adamc@1260 1340 var l = uul(getToken, getData);
adamc@1260 1341 return {_1:d, _2:l};
adamc@1260 1342 } else
adamc@1260 1343 whine("Can't unmarshal list (" + tok + ")");
adamc@692 1344 }
adamc@692 1345
adamc@961 1346 function strcmp(str1, str2) {
adamc@1260 1347 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 1348 }
adamc@692 1349
adamc@603 1350
adamc@692 1351 // Remote calls
adamc@609 1352
adamc@703 1353 var client_id = null;
adamc@668 1354 var client_pass = 0;
adamc@668 1355 var url_prefix = "/";
adamc@673 1356 var timeout = 60;
adam@1552 1357 var isPost = false;
adamc@668 1358
adamc@668 1359 function getXHR(uri)
adamc@609 1360 {
adamc@609 1361 try {
adamc@1260 1362 return new XMLHttpRequest();
adamc@609 1363 } catch (e) {
adamc@1260 1364 try {
adamc@1260 1365 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@1260 1366 } catch (e) {
adamc@1260 1367 try {
adamc@1260 1368 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@1260 1369 } catch (e) {
adamc@1260 1370 whine("Your browser doesn't seem to support AJAX.");
adamc@1260 1371 }
adamc@1260 1372 }
adamc@609 1373 }
adamc@609 1374 }
adamc@609 1375
adamc@736 1376 var sig = null;
adamc@736 1377
adamc@1042 1378 var unloading = false, inFlight = null;
adamc@1042 1379
adamc@1042 1380 function unload() {
adamc@1260 1381 for (; inFlight; inFlight = inFlight.next) {
adamc@1260 1382 inFlight.data.abort();
adamc@1260 1383 }
adamc@1042 1384 }
adamc@1042 1385
adam@1691 1386 function requestUri(xhr, uri, needsSig, isRpc) {
adam@1691 1387 var extraData = null;
adam@1691 1388
adam@1691 1389 if (isRpc && uri.length > 2000) {
adam@1691 1390 extraData = uri.substring(2000);
adam@1691 1391 uri = uri.substring(0, 2000);
adam@1691 1392 }
adam@1691 1393
vshabanoff@1763 1394 xhr.open("POST", uri, !unloading);
adamc@1260 1395 xhr.setRequestHeader("Content-type", "text/plain");
adamc@668 1396
adamc@1260 1397 if (client_id != null) {
adamc@1260 1398 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@1260 1399 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@1260 1400 }
adamc@668 1401
adamc@1260 1402 if (needsSig) {
adamc@1260 1403 if (sig == null)
adamc@1260 1404 whine("Missing cookie signature!");
adamc@736 1405
adamc@1260 1406 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@1260 1407 }
adamc@736 1408
adamc@1260 1409 inFlight = cons(xhr, inFlight);
adam@1691 1410 xhr.send(extraData);
adamc@668 1411 }
adamc@668 1412
adamc@1042 1413 function xhrFinished(xhr) {
adamc@1260 1414 xhr.abort();
adamc@1260 1415 inFlight = remove(xhr, inFlight);
adamc@1042 1416 }
adamc@1042 1417
adamc@1111 1418 function unurlify(parse, s) {
adamc@1260 1419 return parse(s);
adamc@1111 1420 }
adamc@1111 1421
adam@1385 1422 function redirect(s) {
adam@1385 1423 window.location = s;
adam@1385 1424 }
adam@1385 1425
adam@1848 1426 function makeSome(isN, v) {
adam@1848 1427 if (isN)
adam@1848 1428 return {v: v};
adam@1848 1429 else
adam@1848 1430 return v;
adam@1848 1431 }
adam@1848 1432
adam@1848 1433 function rc(prefix, uri, parse, k, needsSig, isN) {
adam@1792 1434 if (!maySuspend)
adam@1794 1435 er("May not 'rpc' in main thread of 'code' for <active>");
adam@1792 1436
adamc@1260 1437 uri = cat(prefix, uri);
adamc@1260 1438 uri = flattenLocal(uri);
adamc@1260 1439 var xhr = getXHR();
adamc@609 1440
adamc@1260 1441 xhr.onreadystatechange = function() {
adamc@1260 1442 if (xhr.readyState == 4) {
adamc@1260 1443 var isok = false;
adamc@612 1444
adamc@1260 1445 try {
adamc@1260 1446 if (xhr.status == 200)
adamc@1260 1447 isok = true;
adamc@1260 1448 } catch (e) { }
adamc@612 1449
adamc@1260 1450 if (isok) {
adam@1446 1451 var lines = xhr.responseText.split("\n");
adam@1848 1452 if (lines.length != 2) {
adam@1848 1453 if (isN == null)
adam@1848 1454 whine("Bad RPC response lines");
adam@1848 1455 else
adam@1848 1456 k(null);
adam@1848 1457 } else {
adam@1848 1458 eval(lines[0]);
adam@1446 1459
adam@1848 1460 try {
adam@1848 1461 var v = parse(lines[1]);
adam@1848 1462 try {
adam@1848 1463 k(makeSome(isN, v));
adam@1848 1464 } catch (v) {
adam@1848 1465 doExn(v);
adam@1848 1466 }
adam@1848 1467 } catch (v) {
adam@1848 1468 k(null);
adam@1848 1469 }
adamc@1260 1470 }
adamc@1260 1471 } else {
adam@1848 1472 if (isN == null)
adam@1848 1473 conn();
adam@1848 1474 else
adam@1848 1475 k(null);
adamc@1260 1476 }
adamc@1260 1477
adamc@1260 1478 xhrFinished(xhr);
adamc@729 1479 }
adamc@1260 1480 };
adamc@1042 1481
adam@1691 1482 requestUri(xhr, uri, needsSig, true);
adamc@609 1483 }
adamc@667 1484
adamc@667 1485 function path_join(s1, s2) {
adamc@1260 1486 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
adamc@1260 1487 return s1 + s2;
adamc@1260 1488 else
adamc@1260 1489 return s1 + "/" + s2;
adamc@667 1490 }
adamc@667 1491
adamc@670 1492 var channels = [];
adamc@670 1493
adamc@670 1494 function newQueue() {
adamc@1260 1495 return { front : null, back : null };
adamc@670 1496 }
adamc@670 1497 function enqueue(q, v) {
adamc@1260 1498 if (q.front == null) {
adamc@1260 1499 q.front = cons(v, null);
adamc@1260 1500 q.back = q.front;
adamc@1260 1501 } else {
adamc@1260 1502 var node = cons(v, null);
adamc@1260 1503 q.back.next = node;
adamc@1260 1504 q.back = node;
adamc@1260 1505 }
adamc@670 1506 }
adamc@670 1507 function dequeue(q) {
adamc@670 1508 if (q.front == null)
adamc@1260 1509 return null;
adamc@1260 1510 else {
adamc@1260 1511 var r = q.front.data;
adamc@1260 1512 q.front = q.front.next;
adamc@1260 1513 if (q.front == null)
adamc@1260 1514 q.back = null;
adamc@1260 1515 return r;
adamc@1260 1516 }
adamc@670 1517 }
adamc@670 1518
adamc@670 1519 function newChannel() {
adamc@1260 1520 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 1521 }
adamc@670 1522
adamc@667 1523 function listener() {
adamc@1260 1524 var uri = path_join(url_prefix, ".msgs");
adamc@1260 1525 var xhr = getXHR();
adamc@1260 1526 var tid, orsc, onTimeout;
adamc@673 1527
adamc@1260 1528 var connect = function () {
adamc@1260 1529 xhr.onreadystatechange = orsc;
adamc@1260 1530 tid = window.setTimeout(onTimeout, timeout * 500);
adam@1691 1531 requestUri(xhr, uri, false, false);
adamc@1260 1532 }
adamc@673 1533
adamc@1260 1534 orsc = function() {
adamc@1260 1535 if (xhr.readyState == 4) {
adamc@1260 1536 window.clearTimeout(tid);
adamc@673 1537
adamc@1260 1538 var isok = false;
adamc@667 1539
adamc@1260 1540 try {
adamc@1260 1541 if (xhr.status == 200)
adamc@1260 1542 isok = true;
adamc@1260 1543 } catch (e) { }
adamc@667 1544
adamc@1260 1545 if (isok) {
adam@1447 1546 var text = xhr.responseText;
adamc@1260 1547 if (text == "")
adamc@1260 1548 return;
adamc@1260 1549 var lines = text.split("\n");
adamc@889 1550
adam@1552 1551 if (lines.length == 1 && lines[0] == "R") {
adam@1553 1552 lameDuck = true;
adam@1553 1553
adam@1552 1554 if (isPost)
adam@1552 1555 history.back();
adam@1552 1556 else
adam@1552 1557 location.reload();
adam@1552 1558
adam@1552 1559 return;
adam@1552 1560 }
adam@1552 1561
adamc@1260 1562 if (lines.length < 2) {
adamc@1260 1563 discon();
adamc@1260 1564 return;
adamc@1260 1565 }
adamc@1260 1566
adam@1447 1567 var messageReader = function(i) {
adam@1447 1568 if (i+1 >= lines.length) {
adam@1447 1569 xhrFinished(xhr);
adam@1447 1570 connect();
adam@1447 1571 }
adam@1447 1572 else {
adam@1447 1573 var chn = lines[i];
adam@1447 1574 var msg = lines[i+1];
adamc@1260 1575
adam@1447 1576 if (chn == "E") {
adam@1447 1577 eval(msg);
adam@1447 1578 window.setTimeout(function() { messageReader(i+2); }, 0);
adam@1447 1579 } else {
adam@1447 1580 if (chn < 0)
adam@1447 1581 whine("Out-of-bounds channel in message from remote server");
adamc@1260 1582
adam@1447 1583 var ch;
adamc@1260 1584
adam@1447 1585 if (chn >= channels.length || channels[chn] == null) {
adam@1447 1586 ch = newChannel();
adam@1447 1587 channels[chn] = ch;
adam@1447 1588 } else
adam@1447 1589 ch = channels[chn];
adamc@1260 1590
adam@1447 1591 var listener = dequeue(ch.listeners);
adam@1447 1592 if (listener == null) {
adam@1447 1593 enqueue(ch.msgs, msg);
adam@1447 1594 } else {
adam@1447 1595 try {
adam@1447 1596 listener(msg);
adam@1447 1597 } catch (v) {
adam@1447 1598 doExn(v);
adam@1447 1599 }
adam@1447 1600 }
adam@1447 1601
adam@1447 1602 messageReader(i+2);
adamc@1260 1603 }
adamc@1260 1604 }
adamc@1260 1605 }
adamc@1260 1606
adam@1447 1607 messageReader(0);
adamc@1260 1608 }
adamc@1260 1609 else {
adamc@1260 1610 try {
adamc@1260 1611 if (xhr.status != 0)
adamc@1260 1612 servErr("Error querying remote server for messages: " + xhr.status);
adamc@1260 1613 } catch (e) { }
adamc@1260 1614 }
adamc@729 1615 }
adamc@1260 1616 };
adamc@669 1617
adamc@1260 1618 onTimeout = function() {
adamc@1260 1619 xhrFinished(xhr);
adamc@1260 1620 connect();
adamc@1260 1621 };
adamc@670 1622
adamc@673 1623 connect();
adamc@667 1624 }
adamc@670 1625
adamc@670 1626 function rv(chn, parse, k) {
adam@1792 1627 if (!maySuspend)
adam@1794 1628 er("May not 'recv' in main thread of 'code' for <active>");
adam@1792 1629
adamc@1260 1630 if (chn == null)
adam@1942 1631 er("Client-side code tried to recv() from a channel belonging to a different page view.");
adamc@682 1632
adamc@1260 1633 if (chn < 0)
adamc@1260 1634 whine("Out-of-bounds channel receive");
adamc@670 1635
adamc@1260 1636 var ch;
adamc@670 1637
adamc@1260 1638 if (chn >= channels.length || channels[chn] == null) {
adamc@1260 1639 ch = newChannel();
adamc@1260 1640 channels[chn] = ch;
adamc@1260 1641 } else
adamc@1260 1642 ch = channels[chn];
adamc@670 1643
adamc@1260 1644 var msg = dequeue(ch.msgs);
adamc@1260 1645 if (msg == null) {
adamc@1260 1646 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
adamc@1260 1647 } else {
adamc@1260 1648 try {
adamc@1260 1649 k(parse(msg));
adamc@1260 1650 } catch (v) {
adamc@1260 1651 doExn(v);
adamc@1260 1652 }
adamc@728 1653 }
adamc@670 1654 }
adamc@693 1655
adamc@978 1656 function sl(ms, k) {
adam@1792 1657 if (!maySuspend)
adam@1794 1658 er("May not 'sleep' in main thread of 'code' for <active>");
adam@1792 1659
adamc@1260 1660 window.setTimeout(function() { k(null); }, ms);
adamc@1021 1661 }
adamc@1021 1662
adamc@1021 1663 function sp(e) {
adam@1794 1664 window.setTimeout(function() { execF(e); }, 0);
adamc@978 1665 }
adamc@978 1666
adamc@693 1667
adamc@970 1668 // The Ur interpreter
adamc@970 1669
adamc@970 1670 var urfuncs = [];
adamc@970 1671
adamc@970 1672 function lookup(env, n) {
adamc@1260 1673 while (env != null) {
adamc@1260 1674 if (n == 0)
adamc@1260 1675 return env.data;
adamc@1260 1676 else {
adamc@1260 1677 --n;
adamc@1260 1678 env = env.next;
adamc@1260 1679 }
adamc@970 1680 }
adamc@970 1681
adamc@1260 1682 whine("Out-of-bounds Ur variable reference");
adamc@970 1683 }
adamc@970 1684
adamc@974 1685 function execP(env, p, v) {
adamc@1260 1686 switch (p.c) {
adamc@1260 1687 case "w":
adamc@1260 1688 return env;
adamc@1260 1689 case "v":
adamc@1260 1690 return cons(v, env);
adamc@1260 1691 case "c":
adamc@1260 1692 if (v == p.v)
adamc@1260 1693 return env;
adamc@1260 1694 else
adamc@1260 1695 return false;
adamc@1260 1696 case "s":
adamc@1260 1697 if (v == null)
adamc@1260 1698 return false;
adamc@1260 1699 else
adamc@1260 1700 return execP(env, p.p, p.n ? v.v : v);
adamc@1260 1701 case "1":
adamc@1260 1702 if (v.n != p.n)
adamc@1260 1703 return false;
adamc@1260 1704 else
adamc@1260 1705 return execP(env, p.p, v.v);
adamc@1260 1706 case "r":
adamc@1260 1707 for (var fs = p.l; fs != null; fs = fs.next) {
adamc@1260 1708 env = execP(env, fs.data.p, v["_" + fs.data.n]);
adamc@1260 1709 if (env == false)
adamc@1260 1710 return false;
adamc@1260 1711 }
adamc@1260 1712 return env;
adamc@1260 1713 default:
adamc@1260 1714 whine("Unknown Ur pattern kind" + p.c);
adamc@974 1715 }
adamc@974 1716 }
adamc@974 1717
adamc@970 1718 function exec0(env, e) {
adamc@1260 1719 return exec1(env, null, e);
adamc@1020 1720 }
adamc@1020 1721
adamc@1020 1722 function exec1(env, stack, e) {
adamc@1260 1723 var stack, usedK = false;
adamc@970 1724
adamc@1260 1725 var saveEnv = function() {
adamc@1260 1726 if (stack.next != null && stack.next.data.c != "<")
adamc@1260 1727 stack = cons({c: "<", env: env}, stack.next);
adamc@1260 1728 else
adamc@1260 1729 stack = stack.next;
adamc@1260 1730 };
adamc@983 1731
adamc@1260 1732 while (true) {
adamc@1260 1733 switch (e.c) {
adamc@1260 1734 case "c":
adamc@1260 1735 var v = e.v;
adamc@1260 1736 if (stack == null)
adamc@1260 1737 return v;
adamc@1260 1738 var fr = stack.data;
adamc@970 1739
adamc@1260 1740 switch (fr.c) {
adamc@1260 1741 case "s":
adamc@1260 1742 e = {c: "c", v: {v: v}};
adamc@1260 1743 stack = stack.next;
adamc@1260 1744 break;
adamc@1260 1745 case "1":
adamc@1260 1746 e = {c: "c", v: {n: fr.n, v: v}};
adamc@1260 1747 stack = stack.next;
adamc@1260 1748 break;
adamc@1260 1749 case "f":
adamc@1260 1750 fr.args[fr.pos++] = v;
adamc@1260 1751 if (fr.a == null) {
adamc@1260 1752 var res;
adamc@1260 1753 stack = stack.next;
adamc@1035 1754
adamc@1260 1755 if (fr.f.apply)
adamc@1260 1756 res = fr.f.apply(null, fr.args);
adamc@1260 1757 else if (fr.args.length == 0)
adamc@1260 1758 res = fr.f();
adamc@1260 1759 else if (fr.args.length == 1)
adamc@1260 1760 res = fr.f(fr.args[0]);
adamc@1260 1761 else if (fr.args.length == 2)
adamc@1260 1762 res = fr.f(fr.args[0], fr.args[1]);
adamc@1260 1763 else if (fr.args.length == 3)
adamc@1260 1764 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
adamc@1260 1765 else if (fr.args.length == 4)
adamc@1260 1766 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
adamc@1260 1767 else if (fr.args.length == 5)
adamc@1260 1768 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
adamc@1260 1769 else
adamc@1260 1770 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
adamc@1035 1771
adamc@1260 1772 e = {c: "c", v: res};
adamc@1260 1773 if (usedK) return null;
adamc@1260 1774 } else {
adamc@1260 1775 e = fr.a.data;
adamc@1260 1776 fr.a = fr.a.next;
adamc@1260 1777 }
adamc@1260 1778 break;
adamc@1260 1779 case "a1":
adamc@1260 1780 e = fr.x;
adamc@1260 1781 stack = cons({c: "a2", f: v}, stack.next);
adamc@1260 1782 break;
adamc@1260 1783 case "a2":
adamc@1260 1784 if (fr.f == null)
adamc@1260 1785 whine("Ur: applying null function");
adamc@1260 1786 else if (fr.f.body) {
adamc@1260 1787 saveEnv();
adamc@1260 1788 env = cons(v, fr.f.env);
adamc@1260 1789 e = fr.f.body;
adamc@1260 1790 } else {
adamc@1260 1791 e = {c: "c", v: fr.f(v)};
adamc@1260 1792 stack = stack.next;
adamc@1260 1793 }
adamc@1260 1794 break;
adamc@1260 1795 case "<":
adamc@1260 1796 env = fr.env;
adamc@1260 1797 stack = stack.next;
adamc@1260 1798 break;
adamc@1260 1799 case "r":
adamc@1260 1800 fr.fs["_" + fr.n] = v;
adamc@1260 1801 if (fr.l == null) {
adamc@1260 1802 e = {c: "c", v: fr.fs};
adamc@1260 1803 stack = stack.next;
adamc@1260 1804 } else {
adamc@1260 1805 fr.n = fr.l.data.n;
adamc@1260 1806 e = fr.l.data.v;
adamc@1260 1807 fr.l = fr.l.next;
adamc@1260 1808 }
adamc@1260 1809 break;
adamc@1260 1810 case ".":
adamc@1260 1811 e = {c: "c", v: v["_" + fr.f]};
adamc@1260 1812 stack = stack.next;
adamc@1260 1813 break;
adamc@1260 1814 case ";":
adamc@1260 1815 e = fr.e2;
adamc@1260 1816 stack = stack.next;
adamc@1260 1817 break;
adamc@1260 1818 case "=":
adamc@1260 1819 saveEnv();
adamc@1260 1820 env = cons(v, env);
adamc@1260 1821 e = fr.e2;
adamc@1260 1822 break;
adamc@1260 1823 case "m":
adamc@1260 1824 var ps;
adamc@1260 1825 for (ps = fr.p; ps != null; ps = ps.next) {
adamc@1260 1826 var r = execP(env, ps.data.p, v);
adamc@1260 1827 if (r != false) {
adamc@1260 1828 saveEnv();
adamc@1260 1829 env = r;
adamc@1260 1830 e = ps.data.b;
adamc@1260 1831 break;
adamc@1260 1832 }
adamc@1260 1833 }
adamc@1260 1834 if (ps == null)
adamc@1260 1835 whine("Match failure in Ur interpretation");
adamc@1260 1836 break;
adamc@1260 1837 default:
adamc@1260 1838 whine("Unknown Ur continuation kind " + fr.c);
adamc@1260 1839 }
adamc@1260 1840
adamc@1260 1841 break;
adamc@1260 1842 case "v":
adamc@1260 1843 e = {c: "c", v: lookup(env, e.n)};
adamc@1260 1844 break;
adamc@1260 1845 case "n":
adamc@1260 1846 var idx = e.n;
adamc@1260 1847 e = urfuncs[idx];
adamc@1260 1848 if (e.c == "t")
adamc@1262 1849 e = urfuncs[idx] = eval("(" + e.f + ")");
adamc@1260 1850 break;
adamc@1260 1851 case "s":
adamc@1260 1852 stack = cons({c: "s"}, stack);
adamc@1260 1853 e = e.v;
adamc@1260 1854 break;
adamc@1260 1855 case "1":
adamc@1260 1856 stack = cons({c: "1", n: e.n}, stack);
adamc@1260 1857 e = e.v;
adamc@1260 1858 break;
adamc@1260 1859 case "f":
adamc@1260 1860 if (e.a == null)
adam@1798 1861 e = {c: "c", v: e.f()};
adamc@1260 1862 else {
adamc@1260 1863 var args = [];
adam@1798 1864 stack = cons({c: "f", f: e.f, args: args, pos: 0, a: e.a.next}, stack);
adamc@1260 1865 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
adamc@1260 1866 e = e.a.data;
adamc@1260 1867 }
adamc@1260 1868 break;
adamc@1260 1869 case "l":
adamc@1260 1870 e = {c: "c", v: {env: env, body: e.b}};
adamc@1260 1871 break;
adamc@1260 1872 case "a":
adamc@1260 1873 stack = cons({c: "a1", x: e.x}, stack);
adamc@1260 1874 e = e.f;
adamc@1260 1875 break;
adamc@1260 1876 case "r":
adamc@1260 1877 if (e.l == null)
adamc@1260 1878 whine("Empty Ur record in interpretation");
adamc@1260 1879 var fs = {};
adamc@1260 1880 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
adamc@1260 1881 e = e.l.data.v;
adamc@1260 1882 break;
adamc@1260 1883 case ".":
adamc@1260 1884 stack = cons({c: ".", f: e.f}, stack);
adamc@1260 1885 e = e.r;
adamc@1260 1886 break;
adamc@1260 1887 case ";":
adamc@1260 1888 stack = cons({c: ";", e2: e.e2}, stack);
adamc@1260 1889 e = e.e1;
adamc@1260 1890 break;
adamc@1260 1891 case "=":
adamc@1260 1892 stack = cons({c: "=", e2: e.e2}, stack);
adamc@1260 1893 e = e.e1;
adamc@1260 1894 break;
adamc@1260 1895 case "m":
adamc@1260 1896 stack = cons({c: "m", p: e.p}, stack);
adamc@1260 1897 e = e.e;
adamc@1260 1898 break;
adamc@1260 1899 case "e":
adamc@1260 1900 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
adamc@1260 1901 break;
adamc@1260 1902 case "wc":
adamc@1260 1903 env = e.env;
adamc@1260 1904 e = e.body;
adamc@1260 1905 break;
adamc@1260 1906 case "K":
adamc@1260 1907 { var savedStack = stack.next, savedEnv = env;
adamc@1260 1908 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
adamc@1260 1909 usedK = true;
rmbruijn@1597 1910 break;
adamc@1260 1911 default:
adamc@1260 1912 whine("Unknown Ur expression kind " + e.c);
adamc@970 1913 }
adamc@970 1914 }
adamc@970 1915 }
adamc@970 1916
adamc@976 1917 function execD(e) {
adamc@1260 1918 return exec0(null, e);
adamc@976 1919 }
adamc@976 1920
adamc@970 1921 function exec(e) {
adamc@1260 1922 var r = exec0(null, e);
adamc@970 1923
adamc@1260 1924 if (r != null && r.body != null)
adamc@1260 1925 return function(v) { return exec0(cons(v, r.env), r.body); };
adamc@1260 1926 else
adamc@1260 1927 return r;
adamc@895 1928 }
adamc@895 1929
adamc@977 1930 function execF(f, x) {
adamc@1260 1931 return exec0(cons(x, f.env), f.body);
adamc@977 1932 }
adamc@977 1933
adamc@895 1934
adam@1290 1935 // Wrappers
adam@1290 1936
adam@1290 1937 function confrm(s) {
adam@1290 1938 return confirm(s) ? true : false;
adam@1290 1939 }
adam@1290 1940
adam@1290 1941
adamc@1323 1942 // URL blessing
adamc@1323 1943
adamc@1323 1944 var urlRules = null;
adamc@1323 1945
adamc@1323 1946 function checkUrl(s) {
adamc@1323 1947 for (var r = urlRules; r; r = r.next) {
adamc@1323 1948 var ru = r.data;
adamc@1323 1949 if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern)
adamc@1323 1950 return ru.allow ? s : null;
adamc@1323 1951 }
adamc@1323 1952
adamc@1323 1953 return null;
adamc@1323 1954 }
adamc@1323 1955
adamc@1323 1956 function bless(s) {
adamc@1323 1957 u = checkUrl(s);
adamc@1323 1958 if (u == null)
adamc@1323 1959 er("Disallowed URL: " + s);
adamc@1323 1960 return u;
adamc@1323 1961 }
adamc@1323 1962
adam@1556 1963
adam@2008 1964 // Attribute name blessing
adam@2008 1965
adam@2008 1966 function blessData(s) {
adam@2008 1967 for (var i = 0; i < s.length; ++i) {
adam@2008 1968 var c = s[i];
adam@2008 1969 if (!isAlnum(c) && c != '-' && c != '_')
adam@2008 1970 er("Disallowed character in data-* attribute name");
adam@2008 1971 }
adam@2008 1972
adam@2008 1973 return s;
adam@2008 1974 }
adam@2008 1975
adam@2008 1976
adam@1755 1977 // CSS validation
adam@1755 1978
adam@1755 1979 function atom(s) {
adam@1755 1980 for (var i = 0; i < s.length; ++i) {
adam@1755 1981 var c = s[i];
adam@1755 1982 if (!isAlnum(c) && c != '+' && c != '-' && c != '.' && c != '%' && c != '#')
adam@1755 1983 er("Disallowed character in CSS atom");
adam@1755 1984 }
adam@1755 1985
adam@1755 1986 return s;
adam@1755 1987 }
adam@1755 1988
adam@1755 1989 function css_url(s) {
adam@1755 1990 for (var i = 0; i < s.length; ++i) {
adam@1755 1991 var c = s[i];
adam@1755 1992 if (!isAlnum(c) && c != ':' && c != '/' && c != '.' && c != '_' && c != '+'
adam@1755 1993 && c != '-' && c != '%' && c != '?' && c != '&' && c != '=' && c != '#')
adam@1755 1994 er("Disallowed character in CSS URL");
adam@1755 1995 }
adam@1755 1996
adam@1755 1997 return s;
adam@1755 1998 }
adam@1755 1999
adam@1755 2000 function property(s) {
adam@1755 2001 if (s.length <= 0)
adam@1755 2002 er("Empty CSS property");
adam@1755 2003
adam@1755 2004 if (!isLower(s[0]) && s[0] != '_')
adam@1755 2005 er("Bad initial character in CSS property");
adam@1755 2006
adam@1755 2007 for (var i = 0; i < s.length; ++i) {
adam@1755 2008 var c = s[i];
adam@1755 2009 if (!isLower(c) && !isDigit(c) && c != '_' && c != '-')
adam@1755 2010 er("Disallowed character in CSS property");
adam@1755 2011 }
adam@1755 2012
adam@1755 2013 return s;
adam@1755 2014 }
adam@1755 2015
adam@1755 2016
adam@1556 2017 // ID generation
adam@1556 2018
adam@1556 2019 var nextId = 0;
adam@1556 2020
adam@1556 2021 function fresh() {
adam@1703 2022 return "uw" + (--nextId);
adam@1556 2023 }
adam@1556 2024
adam@1785 2025 function giveFocus(id) {
adam@1803 2026 var node = document.getElementById(id);
adam@1785 2027
adam@1803 2028 if (node)
adam@1803 2029 node.focus();
adam@1803 2030 else
adam@1803 2031 er("Tried to give focus to ID not used in document: " + id);
adam@1785 2032 }
adam@1785 2033
rmbruijn@1597 2034
adamc@693 2035 // App-specific code