annotate lib/js/urweb.js @ 1785:ffd7ed3bc0b7

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