annotate lib/js/urweb.js @ 1610:37f5a23446b2

Catch more <ccheckbox> events
author Adam Chlipala <adam@chlipala.net>
date Thu, 24 Nov 2011 11:41:52 -0500
parents c6cc104a47ac
children 7d459f223ac2
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@1360 22 function divInt(x, y) { var n = x / y; return n < 0 ? Math.ceil(n) : Math.floor(n); }
adamc@970 23 function mod(x, y) { return x % y; }
adam@1360 24 function modInt(x, y) { 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)
adamc@1260 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@1487 117 function showTime(tm) {
adam@1487 118 var newDate = new Date();
adam@1487 119 newDate.setTime(tm / 1000);
adam@1599 120 var r = newDate.toUTCString();
adam@1599 121 return r;
adam@1487 122 }
adam@1487 123
adam@1487 124 function now() {
adam@1487 125 return (new Date()).getTime() * 1000;
adam@1487 126 }
adam@1487 127
adam@1514 128 function diffInSeconds(tm1, tm2) {
adam@1514 129 return Math.round((tm2 - tm1) / 1000000);
adam@1514 130 }
adam@1514 131
adam@1514 132 function toSeconds(tm) {
adam@1514 133 return Math.round(tm / 1000000);
adam@1514 134 }
adam@1514 135
adam@1518 136 function addSeconds(tm, n) {
adam@1518 137 return tm + n * 1000000;
adam@1518 138 }
adam@1518 139
adam@1599 140 function stringToTime_error(string) {
adam@1599 141 var t = Date.parse(string);
adam@1599 142 if (isNaN(t))
adam@1599 143 onFail("Invalid date string: " + string);
adam@1599 144 else
adam@1599 145 return t * 1000;
adam@1599 146 }
adam@1599 147
adam@1599 148 function stringToTime(string) {
adam@1599 149 try {
adam@1599 150 var t = Date.parse(string);
adam@1599 151 if (isNaN(t))
adam@1599 152 return null;
adam@1599 153 else
adam@1599 154 return t * 1000;
adam@1599 155 } catch (e) {
adam@1599 156 return null;
adam@1599 157 }
rmbruijn@1597 158 }
rmbruijn@1597 159
adam@1609 160 /*
adam@1609 161 strftime for Javascript
adam@1609 162 Copyright (c) 2008, Philip S Tellis <philip@bluesmoon.info>
adam@1609 163 All rights reserved.
adam@1609 164
adam@1609 165 This code is distributed under the terms of the BSD licence
adam@1609 166
adam@1609 167 Redistribution and use of this software in source and binary forms, with or without modification,
adam@1609 168 are permitted provided that the following conditions are met:
adam@1609 169
adam@1609 170 * Redistributions of source code must retain the above copyright notice, this list of conditions
adam@1609 171 and the following disclaimer.
adam@1609 172 * Redistributions in binary form must reproduce the above copyright notice, this list of
adam@1609 173 conditions and the following disclaimer in the documentation and/or other materials provided
adam@1609 174 with the distribution.
adam@1609 175 * The names of the contributors to this file may not be used to endorse or promote products
adam@1609 176 derived from this software without specific prior written permission.
adam@1609 177
adam@1609 178 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
adam@1609 179 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
adam@1609 180 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
adam@1609 181 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adam@1609 182 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adam@1609 183 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
adam@1609 184 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
adam@1609 185 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adam@1609 186 */
adam@1609 187
adam@1609 188 Date.ext = {};
adam@1609 189 Date.ext.util = {};
adam@1609 190 Date.ext.util.xPad=function(x, pad, r)
adam@1609 191 {
adam@1609 192 if(typeof(r) == 'undefined')
adam@1609 193 {
adam@1609 194 r=10;
adam@1609 195 }
adam@1609 196 for( ; parseInt(x, 10)<r && r>1; r/=10)
adam@1609 197 x = pad.toString() + x;
adam@1609 198 return x.toString();
adam@1609 199 };
adam@1609 200 Date.prototype.locale = 'en-US';
adam@1609 201 if(document.getElementsByTagName('html') && document.getElementsByTagName('html')[0].lang)
adam@1609 202 {
adam@1609 203 Date.prototype.locale = document.getElementsByTagName('html')[0].lang;
adam@1609 204 }
adam@1609 205 Date.ext.locales = { };
adam@1609 206 Date.ext.locales.en = {
adam@1609 207 a: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
adam@1609 208 A: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
adam@1609 209 b: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
adam@1609 210 B: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
adam@1609 211 c: '%a %d %b %Y %T %Z',
adam@1609 212 p: ['AM', 'PM'],
adam@1609 213 P: ['am', 'pm'],
adam@1609 214 x: '%d/%m/%y',
adam@1609 215 X: '%T'
adam@1609 216 };
adam@1609 217 Date.ext.locales['en-US'] = Date.ext.locales.en;
adam@1609 218 Date.ext.locales['en-US'].c = '%a %d %b %Y %r %Z';
adam@1609 219 Date.ext.locales['en-US'].x = '%D';
adam@1609 220 Date.ext.locales['en-US'].X = '%r';
adam@1609 221 Date.ext.locales['en-GB'] = Date.ext.locales.en;
adam@1609 222 Date.ext.locales['en-AU'] = Date.ext.locales['en-GB'];
adam@1609 223 Date.ext.formats = {
adam@1609 224 a: function(d) { return Date.ext.locales[d.locale].a[d.getDay()]; },
adam@1609 225 A: function(d) { return Date.ext.locales[d.locale].A[d.getDay()]; },
adam@1609 226 b: function(d) { return Date.ext.locales[d.locale].b[d.getMonth()]; },
adam@1609 227 B: function(d) { return Date.ext.locales[d.locale].B[d.getMonth()]; },
adam@1609 228 c: 'toLocaleString',
adam@1609 229 C: function(d) { return Date.ext.util.xPad(parseInt(d.getFullYear()/100, 10), 0); },
adam@1609 230 d: ['getDate', '0'],
adam@1609 231 e: ['getDate', ' '],
adam@1609 232 g: function(d) { return Date.ext.util.xPad(parseInt(Date.ext.util.G(d)/100, 10), 0); },
adam@1609 233 G: function(d) {
adam@1609 234 var y = d.getFullYear();
adam@1609 235 var V = parseInt(Date.ext.formats.V(d), 10);
adam@1609 236 var W = parseInt(Date.ext.formats.W(d), 10);
adam@1609 237
adam@1609 238 if(W > V) {
adam@1609 239 y++;
adam@1609 240 } else if(W===0 && V>=52) {
adam@1609 241 y--;
adam@1609 242 }
adam@1609 243
adam@1609 244 return y;
adam@1609 245 },
adam@1609 246 H: ['getHours', '0'],
adam@1609 247 I: function(d) { var I=d.getHours()%12; return Date.ext.util.xPad(I===0?12:I, 0); },
adam@1609 248 j: function(d) {
adam@1609 249 var ms = d - new Date('' + d.getFullYear() + '/1/1 GMT');
adam@1609 250 ms += d.getTimezoneOffset()*60000;
adam@1609 251 var doy = parseInt(ms/60000/60/24, 10)+1;
adam@1609 252 return Date.ext.util.xPad(doy, 0, 100);
adam@1609 253 },
adam@1609 254 m: function(d) { return Date.ext.util.xPad(d.getMonth()+1, 0); },
adam@1609 255 M: ['getMinutes', '0'],
adam@1609 256 p: function(d) { return Date.ext.locales[d.locale].p[d.getHours() >= 12 ? 1 : 0 ]; },
adam@1609 257 P: function(d) { return Date.ext.locales[d.locale].P[d.getHours() >= 12 ? 1 : 0 ]; },
adam@1609 258 S: ['getSeconds', '0'],
adam@1609 259 u: function(d) { var dow = d.getDay(); return dow===0?7:dow; },
adam@1609 260 U: function(d) {
adam@1609 261 var doy = parseInt(Date.ext.formats.j(d), 10);
adam@1609 262 var rdow = 6-d.getDay();
adam@1609 263 var woy = parseInt((doy+rdow)/7, 10);
adam@1609 264 return Date.ext.util.xPad(woy, 0);
adam@1609 265 },
adam@1609 266 V: function(d) {
adam@1609 267 var woy = parseInt(Date.ext.formats.W(d), 10);
adam@1609 268 var dow1_1 = (new Date('' + d.getFullYear() + '/1/1')).getDay();
adam@1609 269 var idow = woy + (dow1_1 > 4 || dow1_1 <= 1 ? 0 : 1);
adam@1609 270 if(idow == 53 && (new Date('' + d.getFullYear() + '/12/31')).getDay() < 4)
adam@1609 271 {
adam@1609 272 idow = 1;
adam@1609 273 }
adam@1609 274 else if(idow === 0)
adam@1609 275 {
adam@1609 276 idow = Date.ext.formats.V(new Date('' + (d.getFullYear()-1) + '/12/31'));
adam@1609 277 }
adam@1609 278
adam@1609 279 return Date.ext.util.xPad(idow, 0);
adam@1609 280 },
adam@1609 281 w: 'getDay',
adam@1609 282 W: function(d) {
adam@1609 283 var doy = parseInt(Date.ext.formats.j(d), 10);
adam@1609 284 var rdow = 7-Date.ext.formats.u(d);
adam@1609 285 var woy = parseInt((doy+rdow)/7, 10);
adam@1609 286 return Date.ext.util.xPad(woy, 0, 10);
adam@1609 287 },
adam@1609 288 y: function(d) { return Date.ext.util.xPad(d.getFullYear()%100, 0); },
adam@1609 289 Y: 'getFullYear',
adam@1609 290 z: function(d) {
adam@1609 291 var o = d.getTimezoneOffset();
adam@1609 292 var H = Date.ext.util.xPad(parseInt(Math.abs(o/60), 10), 0);
adam@1609 293 var M = Date.ext.util.xPad(o%60, 0);
adam@1609 294 return (o>0?'-':'+') + H + M;
adam@1609 295 },
adam@1609 296 Z: function(d) { return d.toString().replace(/^.*\(([^)]+)\)$/, '$1'); },
adam@1609 297 '%': function(d) { return '%'; }
adam@1609 298 };
adam@1609 299 Date.ext.aggregates = {
adam@1609 300 c: 'locale',
adam@1609 301 D: '%m/%d/%y',
adam@1609 302 h: '%b',
adam@1609 303 n: '\n',
adam@1609 304 r: '%I:%M:%S %p',
adam@1609 305 R: '%H:%M',
adam@1609 306 t: '\t',
adam@1609 307 T: '%H:%M:%S',
adam@1609 308 x: 'locale',
adam@1609 309 X: 'locale'
adam@1609 310 };
adam@1609 311 Date.ext.aggregates.z = Date.ext.formats.z(new Date());
adam@1609 312 Date.ext.aggregates.Z = Date.ext.formats.Z(new Date());
adam@1609 313 Date.ext.unsupported = { };
adam@1609 314 function strftime(fmt, thisTime)
adam@1609 315 {
adam@1609 316 var thisDate = new Date();
adam@1609 317 thisDate.setTime(thisTime / 1000);
adam@1609 318
adam@1609 319 if(!(thisDate.locale in Date.ext.locales))
adam@1609 320 {
adam@1609 321 if(thisDate.locale.replace(/-[a-zA-Z]+$/, '') in Date.ext.locales)
adam@1609 322 {
adam@1609 323 thisDate.locale = thisDate.locale.replace(/-[a-zA-Z]+$/, '');
adam@1609 324 }
adam@1609 325 else
adam@1609 326 {
adam@1609 327 thisDate.locale = 'en-US';
adam@1609 328 }
adam@1609 329 }
adam@1609 330
adam@1609 331 var d = thisDate;
adam@1609 332 while(fmt.match(/%[cDhnrRtTxXzZ]/))
adam@1609 333 {
adam@1609 334 fmt = fmt.replace(/%([cDhnrRtTxXzZ])/g, function(m0, m1)
adam@1609 335 {
adam@1609 336 var f = Date.ext.aggregates[m1];
adam@1609 337 return (f == 'locale' ? Date.ext.locales[d.locale][m1] : f);
adam@1609 338 });
adam@1609 339 }
adam@1609 340
adam@1609 341 var str = fmt.replace(/%([aAbBCdegGHIjmMpPSuUVwWyY%])/g, function(m0, m1)
adam@1609 342 {
adam@1609 343 var f = Date.ext.formats[m1];
adam@1609 344 if(typeof(f) == 'string') {
adam@1609 345 return d[f]();
adam@1609 346 } else if(typeof(f) == 'function') {
adam@1609 347 return f.call(d, d);
adam@1609 348 } else if(typeof(f) == 'object' && typeof(f[0]) == 'string') {
adam@1609 349 return Date.ext.util.xPad(d[f[0]](), f[1]);
adam@1609 350 } else {
adam@1609 351 return m1;
adam@1609 352 }
adam@1609 353 });
adam@1609 354 d=null;
adam@1609 355 return str;
adam@1609 356 };
adam@1609 357
adam@1609 358 // End of code from Philip S Tellis
adam@1609 359
adam@1487 360
adamc@729 361 // Error handling
adamc@729 362
adamc@729 363 function whine(msg) {
adamc@1260 364 alert(msg);
adamc@1260 365 throw msg;
adamc@729 366 }
adamc@729 367
adamc@810 368 function pf(loc) {
adamc@1260 369 throw ("Pattern match failure (" + loc + ")");
adamc@729 370 }
adamc@729 371
adam@1553 372 var lameDuck = false;
adam@1553 373
adamc@798 374 function runHandlers(kind, ls, arg) {
adam@1553 375 if (!lameDuck) {
adam@1553 376 if (ls == null)
adam@1553 377 alert(kind + ": " + arg);
adam@1553 378 for (; ls; ls = ls.next)
adam@1553 379 try {
adam@1553 380 exec({c:"a", f:{c:"a", f:ls.data, x:{c:"c", v:arg}}, x:{c:"c", v:null}});
adam@1553 381 } catch (v) { }
adam@1553 382 }
adamc@729 383 }
adamc@729 384
adamc@729 385 var errorHandlers = null;
adamc@729 386
adamc@729 387 function onError(f) {
adamc@1260 388 errorHandlers = cons(f, errorHandlers);
adamc@729 389 }
adamc@729 390
adamc@729 391 function er(s) {
adamc@1260 392 runHandlers("Error", errorHandlers, s);
adamc@1260 393 throw {uw_error: s};
adamc@729 394 }
adamc@729 395
adamc@729 396 var failHandlers = null;
adamc@729 397
adamc@729 398 function onFail(f) {
adamc@1260 399 failHandlers = cons(f, failHandlers);
adamc@729 400 }
adamc@729 401
adamc@729 402 function doExn(v) {
adamc@1260 403 if (v == null || v.uw_error == null) {
adamc@1260 404 var s = (v == null ? "null" : v.message ? v.message : v.toString());
adamc@1260 405 if (v != null && v.fileName && v.lineNumber)
adamc@1260 406 s += " (" + v.fileName + ":" + v.lineNumber + ")";
adamc@1260 407 runHandlers("Fail", failHandlers, s);
adamc@1260 408 }
adamc@729 409 }
adamc@729 410
adamc@729 411 var disconnectHandlers = null;
adamc@729 412
adamc@976 413 function flift(f) {
kkallio@1484 414 return {c: "c", v:{env:cons(f,null), body:{c:"v", n:1}}};
adamc@976 415 }
adamc@976 416
adamc@729 417 function onDisconnect(f) {
adamc@1260 418 disconnectHandlers = cons(flift(f), disconnectHandlers);
adamc@729 419 }
adamc@729 420
adamc@729 421 function discon() {
adamc@1260 422 runHandlers("Disconnect", disconnectHandlers, null);
adamc@729 423 }
adamc@729 424
adamc@729 425 var connectHandlers = null;
adamc@729 426
adamc@729 427 function onConnectFail(f) {
adamc@1260 428 connectHandlers = cons(flift(f), connectHandlers);
adamc@729 429 }
adamc@729 430
adamc@729 431 function conn() {
adamc@1260 432 runHandlers("Connect", connectHandlers, null);
adamc@729 433 }
adamc@729 434
adamc@729 435 var serverHandlers = null;
adamc@729 436
adamc@729 437 function onServerError(f) {
adamc@1260 438 serverHandlers = cons(f, serverHandlers);
adamc@729 439 }
adamc@729 440
adamc@729 441 function servErr(s) {
adamc@1260 442 window.setTimeout(function () { runHandlers("Server", serverHandlers, s); }, 0);
adamc@729 443 }
adamc@729 444
adam@1555 445 // Key events
adam@1555 446
adam@1555 447 var uw_event = null;
adam@1555 448
adam@1555 449 function kc() {
adam@1555 450 return window.event ? event.keyCode : (uw_event ? uw_event.which : 0);
adam@1555 451 }
adam@1555 452
adam@1555 453 // Document events
adam@1555 454
adam@1555 455 function uw_handler(name, f) {
adam@1555 456 var old = document[name];
adam@1555 457 if (old == undefined)
adam@1559 458 document[name] = function(event) { uw_event = event; execF(f); };
adam@1555 459 else
adam@1559 460 document[name] = function(event) { uw_event = event; old(); execF(f); };
adam@1555 461 }
adam@1555 462
adam@1555 463 function uw_onClick(f) {
adam@1555 464 uw_handler("onclick", f);
adam@1555 465 }
adam@1555 466
adam@1555 467 function uw_onDblclick(f) {
adam@1555 468 uw_handler("ondblclick", f);
adam@1555 469 }
adam@1555 470
adam@1555 471 function uw_onMousedown(f) {
adam@1555 472 uw_handler("onmousedown", f);
adam@1555 473 }
adam@1555 474
adam@1555 475 function uw_onMouseup(f) {
adam@1555 476 uw_handler("onmouseup", f);
adam@1555 477 }
adam@1555 478
adam@1555 479 function uw_keyHandler(name, f) {
adam@1555 480 var old = document[name];
adam@1555 481 if (old == undefined)
adam@1559 482 document[name] = function(event) { uw_event = event; execF(execF(f, kc())); };
adam@1555 483 else
adam@1559 484 document[name] = function(event) { uw_event = event; old(); execF(execF(f, kc())); };
adam@1555 485 }
adam@1555 486
adam@1555 487 function uw_onKeydown(f) {
adam@1555 488 uw_keyHandler("onkeydown", f);
adam@1555 489 }
adam@1555 490
adam@1555 491 function uw_onKeypress(f) {
adam@1555 492 uw_keyHandler("onkeypress", f);
adam@1555 493 }
adam@1555 494
adam@1555 495 function uw_onKeyup(f) {
adam@1555 496 uw_keyHandler("onkeyup", f);
adam@1555 497 }
adam@1555 498
adam@1559 499 // Cancelling of further event processing
adam@1559 500
adam@1559 501 function uw_preventDefault() {
adam@1559 502 var e = window.event ? window.event : uw_event;
adam@1559 503 e.returnValue = false;
adam@1559 504 if (e.preventDefault) e.preventDefault();
adam@1559 505 }
adam@1559 506
adam@1559 507 function uw_stopPropagation() {
adam@1559 508 var e = window.event ? window.event : uw_event;
adam@1559 509 e.cancelBubble = true;
adam@1559 510 if (e.stopPropagation) e.stopPropagation();
adam@1559 511 }
adamc@729 512
adamc@692 513 // Embedding closures in XML strings
adamc@692 514
adamc@693 515 function cs(f) {
adamc@1260 516 return {closure: f};
adamc@693 517 }
adamc@693 518
adamc@693 519 function isWeird(v) {
adamc@1260 520 return v.closure != null || v.cat1 != null;
adamc@693 521 }
adamc@693 522
adamc@692 523 function cat(s1, s2) {
adamc@1260 524 if (isWeird(s1) || isWeird(s2))
adamc@1260 525 return {cat1: s1, cat2: s2};
adamc@1260 526 else
adamc@1260 527 return s1 + s2;
adamc@692 528 }
adamc@692 529
adamc@692 530 var closures = [];
adamc@703 531 var freeClosures = null;
adamc@692 532
adamc@692 533 function newClosure(f) {
adamc@1260 534 var n;
adamc@1260 535 if (freeClosures == null) {
adamc@1260 536 n = closures.length;
adamc@1260 537 } else {
adamc@1260 538 n = freeClosures.data;
adamc@1260 539 freeClosures = freeClosures.next;
adamc@1260 540 }
adamc@1260 541 closures[n] = f;
adamc@1260 542 return n;
adamc@692 543 }
adamc@692 544
adamc@703 545 function freeClosure(n) {
adamc@1260 546 closures[n] = null;
adamc@1260 547 freeClosures = cons(n, freeClosures);
adamc@703 548 }
adamc@703 549
adamc@692 550 function cr(n) {
adamc@1260 551 return closures[n];
adamc@692 552 }
adamc@692 553
adamc@703 554 function flatten(cls, tr) {
adamc@1260 555 if (tr.cat1 != null)
adamc@1260 556 return flatten(cls, tr.cat1) + flatten(cls, tr.cat2);
adamc@1260 557 else if (tr.closure != null) {
adamc@1260 558 var cl = newClosure(tr.closure);
adamc@1260 559 cls.v = cons(cl, cls.v);
adamc@1260 560 return "cr(" + cl + ")";
adamc@1260 561 } else
adamc@1260 562 return tr;
adamc@692 563 }
adamc@692 564
adamc@728 565 function flattenLocal(s) {
adamc@1260 566 var cls = {v : null};
adamc@1260 567 var r = flatten(cls, s);
adamc@1260 568 for (cl = cls.v; cl != null; cl = cl.next)
adamc@1260 569 freeClosure(cl.data);
adamc@1260 570 return r;
adamc@728 571 }
adamc@728 572
adamc@692 573
adamc@692 574
adamc@692 575 // Dynamic tree management
adamc@692 576
adamc@690 577 function populate(node) {
adamc@1260 578 var s = node.signal;
adamc@1260 579 var oldSources = node.sources;
adamc@1260 580 try {
adamc@1260 581 var sr = execF(s, null);
adamc@1260 582 var newSources = sr._sources;
adamc@690 583
adamc@1260 584 for (var sp = oldSources; sp; sp = sp.next)
adamc@1260 585 if (!member(sp.data, newSources))
adamc@1260 586 sp.data.dyns = remove(node, sp.data.dyns);
adamc@690 587
adamc@1260 588 for (var sp = newSources; sp; sp = sp.next)
adamc@1260 589 if (!member(sp.data, oldSources))
adamc@1260 590 sp.data.dyns = cons(node, sp.data.dyns);
adamc@690 591
adamc@1260 592 node.sources = newSources;
adamc@1260 593 node.recreate(sr._data);
adamc@1260 594 } catch (v) {
adamc@1260 595 doExn(v);
adamc@1260 596 }
adamc@579 597 }
adamc@574 598
adamc@580 599 function sc(v) {
adamc@1260 600 return {data : v, dyns : null};
adamc@580 601 }
adamc@580 602 function sv(s, v) {
adam@1491 603 if (s.data != v) {
adam@1491 604 s.data = v;
adamc@967 605
adam@1491 606 for (var ls = s.dyns; ls; ls = ls.next)
adam@1491 607 if (!ls.dead)
adam@1491 608 populate(ls.data);
adam@1491 609 }
adamc@580 610 }
adamc@601 611 function sg(s) {
adamc@1260 612 return s.data;
adamc@601 613 }
adamc@579 614
adamc@580 615 function ss(s) {
adamc@1260 616 return {env:cons(s, null), body:{c:"r", l:
adamc@1260 617 cons({n:"sources", v:{c:"c", v:cons(s, null)}},
adamc@1260 618 cons({n:"data", v:{c:"f", f:sg, a:cons({c:"v", n:1}, null)}}, null))}};
adamc@580 619 }
adamc@580 620 function sr(v) {
adamc@1260 621 return {env:null, body:{c:"c", v:{_sources : null, _data : v}}};
adamc@580 622 }
adamc@580 623 function sb(x,y) {
adamc@1260 624 return {env:cons(y,cons(x,null)),
adamc@1260 625 body:{c:"=",
adamc@1260 626 e1:{c:"a", f:{c:"v", n:2}, x:{c:"c", v:null}},
adamc@1260 627 e2:{c:"=",
adamc@1260 628 e1:{c:"a",
adamc@1260 629 f:{c:"a", f:{c:"v", n:2}, x:{c:".", r:{c:"v", n:0}, f:"data"}},
adamc@1260 630 x:{c:"c", v:null}},
adamc@1260 631 e2:{c:"r", l:cons(
adamc@1260 632 {n:"sources", v:{c:"f", f:union, a:cons({c:".", r:{c:"v", n:1}, f:"sources"},
adamc@1260 633 cons({c:".", r:{c:"v", n:0}, f:"sources"}, null))}},
adamc@1260 634 cons({n:"data", v:{c:".", r:{c:"v", n:0}, f:"data"}}, null))}}}};
adamc@580 635 }
adamc@841 636 function scur(s) {
adamc@1260 637 return execF(s, null)._data;
adamc@841 638 }
adamc@571 639
adamc@604 640 function lastParent() {
adamc@1260 641 var pos = document.body;
adamc@604 642
adamc@1260 643 while (pos.lastChild && pos.lastChild.nodeType == 1)
adamc@1260 644 pos = pos.lastChild;
adamc@600 645
adamc@1260 646 pos = pos.parentNode;
adamc@1039 647
adamc@1260 648 return pos;
adamc@600 649 }
adamc@600 650
adamc@898 651 function parent() {
adamc@1260 652 return thisScript ? thisScript.parentNode : lastParent();
adamc@898 653 }
adamc@898 654
adamc@604 655 function addNode(node) {
adamc@1260 656 if (thisScript)
adamc@1260 657 thisScript.parentNode.replaceChild(node, thisScript);
adamc@1260 658 else
adamc@1260 659 lastParent().appendChild(node);
adamc@603 660 }
adamc@603 661
adamc@690 662 var thisScript = null;
adamc@690 663
adamc@604 664 function runScripts(node) {
adamc@1260 665 if (node.getElementsByTagName) {
adamc@1260 666 var savedScript = thisScript;
adamc@603 667
adamc@1260 668 var scripts = node.getElementsByTagName("script"), scriptsCopy = [];
adamc@1260 669 var len = scripts.length;
adamc@1260 670 for (var i = 0; i < len; ++i)
adamc@1260 671 scriptsCopy[i] = scripts[i];
adamc@1260 672 for (var i = 0; i < len; ++i) {
adamc@1260 673 thisScript = scriptsCopy[i];
adamc@1039 674
adamc@1260 675 try {
adamc@1260 676 eval(thisScript.text);
adamc@1260 677 } catch (v) {
adamc@1260 678 doExn(v);
adamc@1260 679 }
adamc@1260 680 if (thisScript.parentNode)
adamc@1260 681 thisScript.parentNode.removeChild(thisScript);
adamc@1260 682 }
adamc@1260 683
adamc@1260 684 thisScript = savedScript;
adamc@729 685 }
adamc@603 686 }
adamc@603 687
adamc@603 688
adamc@692 689 // Dynamic tree entry points
adamc@692 690
adamc@967 691 function killScript(scr) {
adamc@1260 692 scr.dead = true;
adamc@1260 693 for (var ls = scr.sources; ls; ls = ls.next)
adamc@1260 694 ls.data.dyns = remove(scr, ls.data.dyns);
adamc@1260 695 for (var ls = scr.closures; ls; ls = ls.next)
adamc@1260 696 freeClosure(ls.data);
adamc@967 697 }
adamc@967 698
adam@1538 699 // Sometimes we wind up with tables that contain <script>s outside the single <tbody>.
adam@1538 700 // To avoid dealing with that case, we normalize by moving <script>s into <tbody>.
adam@1538 701 function normalizeTable(table) {
adam@1538 702 var orig = table;
adam@1538 703
adam@1538 704 var script, next;
adam@1538 705
adam@1538 706 while (table.tagName != "TABLE")
adam@1538 707 table = table.parentNode;
adam@1538 708
adam@1538 709 for (var tbody = table.firstChild; tbody; tbody = tbody.nextSibling) {
adam@1538 710 if (tbody.tagName == "TBODY") {
adam@1538 711 for (script = table.firstChild; script && script != tbody; script = next) {
adam@1538 712 next = script.nextSibling;
adam@1538 713
adam@1538 714 tbody.insertBefore(script, tbody.firstChild);
adam@1538 715 }
adam@1538 716
adam@1538 717 return;
adam@1538 718 }
adam@1538 719 }
adam@1538 720
adam@1538 721 var tbody = document.createElement("tbody");
adam@1538 722 for (script = table.firstChild; script; script = next) {
adam@1538 723 next = script.nextSibling;
rmbruijn@1597 724
adam@1538 725 tbody.insertBefore(script, tbody.firstChild);
adam@1538 726 }
adam@1538 727 table.appendChild(tbody);
adam@1538 728 }
adam@1538 729
adamc@901 730 function dyn(pnode, s) {
adamc@1260 731 var x = document.createElement("script");
adamc@1260 732 x.dead = false;
adamc@1260 733 x.signal = s;
adamc@1260 734 x.sources = null;
adamc@1260 735 x.closures = null;
adamc@899 736
adamc@1260 737 var firstChild = null;
adamc@899 738
adamc@1260 739 x.recreate = function(v) {
adamc@1260 740 for (var ls = x.closures; ls; ls = ls.next)
adamc@1260 741 freeClosure(ls.data);
adamc@945 742
adamc@946 743 var next;
adamc@1260 744 for (var child = firstChild; child && child != x; child = next) {
adamc@1260 745 next = child.nextSibling;
adamc@1260 746
adamc@1260 747 killScript(child);
adamc@1260 748 if (child.getElementsByTagName) {
adamc@1260 749 var arr = child.getElementsByTagName("script");
adamc@1260 750 for (var i = 0; i < arr.length; ++i)
adamc@1260 751 killScript(arr[i]);
adamc@1260 752 }
adamc@1260 753
adamc@1260 754 if (child.parentNode)
adamc@1260 755 child.parentNode.removeChild(child);
adamc@946 756 }
adamc@903 757
adamc@1260 758 var cls = {v : null};
adamc@1260 759 var html = flatten(cls, v);
adamc@1260 760 if (pnode != "table" && pnode != "tr")
adamc@1260 761 html = dynPrefix + html;
adamc@1260 762 x.closures = cls.v;
adamc@946 763
adamc@1260 764 if (pnode == "table") {
adam@1538 765 normalizeTable(x.parentNode);
adam@1538 766
adamc@1260 767 var dummy = document.createElement("body");
adamc@1260 768 dummy.innerHTML = "<table>" + html + "</table>";
adamc@1260 769 runScripts(dummy);
adamc@1260 770 var table = x.parentNode;
adamc@1260 771
adamc@1260 772 if (table) {
adamc@1260 773 firstChild = null;
adamc@1260 774 var tbody;
adamc@1260 775
adamc@1260 776 var arr = dummy.getElementsByTagName("tbody");
adamc@1260 777
adamc@1260 778 var tbody;
adamc@1260 779 if (arr.length > 0 && arr[0].parentNode == dummy.firstChild) {
adamc@1260 780 tbody = arr[0];
adamc@1260 781 var next;
adamc@1260 782 for (var node = dummy.firstChild.firstChild; node; node = next) {
adamc@1260 783 next = node.nextSibling;
adamc@1260 784
adamc@1260 785 if (node.tagName != "TBODY")
adamc@1260 786 tbody.appendChild(node);
adamc@1260 787 }
adamc@1260 788 } else
adamc@1260 789 tbody = dummy.firstChild;
adamc@1260 790
adamc@1260 791 var next;
adamc@1260 792 firstChild = document.createElement("script");
adamc@1260 793 table.insertBefore(firstChild, x);
adamc@1260 794 for (var node = tbody.firstChild; node; node = next) {
adamc@1260 795 next = node.nextSibling;
adamc@1260 796 table.insertBefore(node, x);
adamc@1260 797 }
adamc@1260 798 }
adamc@1260 799 } else if (pnode == "tr") {
adamc@1260 800 var dummy = document.createElement("body");
adamc@1260 801 dummy.innerHTML = "<table><tr>" + html + "</tr></table>";
adamc@1260 802 runScripts(dummy);
adamc@1260 803 var table = x.parentNode;
adamc@1260 804
adamc@1260 805 if (table) {
adamc@1260 806 var arr = dummy.getElementsByTagName("tr");
adamc@1260 807 firstChild = null;
adamc@1260 808 var tr;
adamc@1260 809 if (arr.length > 0 && table != null)
adamc@1260 810 tr = arr[0];
adamc@1260 811 else
adamc@1260 812 tr = dummy.firstChild;
adamc@1260 813
adamc@1260 814 var next;
adamc@1260 815 firstChild = document.createElement("script");
adamc@1260 816 table.insertBefore(firstChild, x);
adamc@1260 817 for (var node = tr.firstChild; node; node = next) {
adamc@1260 818 next = node.nextSibling;
adamc@1260 819 table.insertBefore(node, x);
adamc@1260 820 }
adamc@1260 821 }
adamc@1260 822 } else {
adamc@1260 823 firstChild = document.createElement("span");
adamc@1260 824 firstChild.innerHTML = html;
adamc@1260 825 runScripts(firstChild);
adamc@1260 826 if (x.parentNode)
adamc@1260 827 x.parentNode.insertBefore(firstChild, x);
adamc@903 828 }
adamc@1260 829 };
adamc@899 830
adamc@1260 831 addNode(x);
adamc@1260 832 populate(x);
adamc@571 833 }
adamc@582 834
adam@1463 835 function input(x, s, recreate, type, name) {
adam@1463 836 if (name) x.name = name;
adamc@1260 837 if (type) x.type = type;
adamc@1260 838 x.dead = false;
adamc@1260 839 x.signal = ss(s);
adamc@1260 840 x.sources = null;
adamc@1260 841 x.recreate = recreate(x);
adamc@1260 842 addNode(x);
adamc@1260 843 populate(x);
adamc@816 844
adamc@1260 845 return x;
adamc@816 846 }
adamc@816 847
adam@1463 848 function inp(s, name) {
adamc@1260 849 var x = input(document.createElement("input"), s,
adam@1463 850 function(x) { return function(v) { if (x.value != v) x.value = v; }; }, null, name);
adamc@1260 851 x.value = s.data;
adam@1491 852 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
adamc@816 853
adamc@1260 854 return x;
adamc@816 855 }
adamc@816 856
adam@1479 857 function selectValue(x) {
adam@1479 858 if (x.options.length == 0)
adam@1479 859 return "";
adam@1479 860 else
adam@1479 861 return x.options[x.selectedIndex].value;
adam@1479 862 }
adam@1479 863
adam@1479 864 function setSelectValue(x, v) {
adam@1479 865 for (var i = 0; i < x.options.length; ++i) {
adam@1479 866 if(x.options[i].value == v) {
adam@1479 867 x.selectedIndex = i;
adam@1479 868 return;
adam@1479 869 }
adam@1479 870 }
adam@1479 871 }
adam@1479 872
adamc@816 873 function sel(s, content) {
adamc@1260 874 var dummy = document.createElement("span");
adamc@1260 875 dummy.innerHTML = "<select>" + content + "</select>";
adam@1479 876 var x = input(dummy.firstChild, s, function(x) { return function(v) { if (selectValue(x) != v) setSelectValue(x, v); }; });
adam@1479 877
adam@1479 878 for (var i = 0; i < x.options.length; ++i) {
adam@1479 879 if (x.options[i].value == "")
adam@1479 880 x.options[i].value = x.options[i].text;
adam@1479 881 else
adam@1479 882 x.options[i].value = x.options[i].value.substring(1);
adam@1479 883 }
adam@1479 884
adam@1479 885 setSelectValue(x, s.data);
adam@1479 886 if (selectValue(x) != s.data)
adam@1479 887 sv(s, selectValue(x));
adam@1479 888 x.onchange = function() { sv(s, selectValue(x)) };
adamc@797 889
adamc@1260 890 return x;
adamc@598 891 }
adamc@598 892
adamc@817 893 function chk(s) {
adamc@1260 894 var x = input(document.createElement("input"), s,
adamc@1260 895 function(x) { return function(v) { if (x.checked != v) x.checked = v; }; }, "checkbox");
adamc@1260 896 x.defaultChecked = x.checked = s.data;
adam@1610 897 x.onclick = x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.checked) };
adamc@817 898
adamc@1260 899 return x;
adamc@817 900 }
adamc@817 901
adamc@1099 902 function tbx(s) {
adamc@1260 903 var x = input(document.createElement("textarea"), s,
adamc@1260 904 function(x) { return function(v) { if (x.innerHTML != v) x.innerHTML = v; }; });
adamc@1260 905 x.innerHTML = s.data;
adam@1491 906 x.onkeyup = x.oninput = x.onchange = x.onpropertychange = function() { sv(s, x.value) };
adamc@1099 907
adamc@1260 908 return x;
adamc@1099 909 }
adamc@1099 910
adamc@800 911 function addOnChange(x, f) {
adamc@1260 912 var old = x.onchange;
adam@1290 913 if (old == null)
adam@1290 914 x.onchange = f;
adam@1290 915 else
adam@1290 916 x.onchange = function() { old(); f(); };
adam@1290 917 }
adam@1290 918
adam@1290 919 function addOnKeyUp(x, f) {
adam@1290 920 var old = x.onkeyup;
adam@1290 921 if (old == null)
adam@1290 922 x.onkeyup = f;
adam@1290 923 else
adam@1290 924 x.onkeyup = function(x) { old(x); f(x); };
adamc@800 925 }
adamc@800 926
adamc@692 927
adamc@692 928 // Basic string operations
adamc@692 929
adamc@597 930 function eh(x) {
adamc@1260 931 if (x == null)
adamc@1260 932 return "NULL";
adamc@1260 933 else
adamc@1260 934 return x.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
adamc@597 935 }
adamc@597 936
adamc@582 937 function ts(x) { return x.toString() }
adamc@586 938 function bs(b) { return (b ? "True" : "False") }
adamc@586 939
adamc@1057 940 function id(x) { return x; }
adamc@1038 941 function sub(s, i) { return s.charAt(i); }
adamc@828 942 function suf(s, i) { return s.substring(i); }
adamc@828 943 function slen(s) { return s.length; }
adamc@829 944 function sidx(s, ch) {
adamc@1260 945 var r = s.indexOf(ch);
adamc@1260 946 if (r == -1)
adamc@1260 947 return null;
adamc@1260 948 else
adamc@1260 949 return r;
adamc@829 950 }
adamc@831 951 function sspn(s, chs) {
adamc@1260 952 for (var i = 0; i < s.length; ++i)
adamc@1260 953 if (chs.indexOf(s.charAt(i)) != -1)
adamc@1260 954 return i;
adamc@831 955
adamc@1260 956 return null;
adamc@831 957 }
adamc@829 958 function schr(s, ch) {
adamc@1260 959 var r = s.indexOf(ch);
adamc@1260 960 if (r == -1)
adamc@1260 961 return null;
adamc@1260 962 else
adamc@1260 963 return s.substring(r);
adamc@829 964 }
adamc@829 965 function ssub(s, start, len) {
adamc@1260 966 return s.substring(start, start+len);
adamc@829 967 }
adamc@821 968
adamc@649 969 function pi(s) {
adamc@1260 970 var r = parseInt(s);
adamc@1260 971 if (r.toString() == s)
adamc@1260 972 return r;
adamc@1260 973 else
adamc@1260 974 er("Can't parse int: " + s);
adamc@649 975 }
adamc@649 976
adamc@649 977 function pfl(s) {
adamc@1260 978 var r = parseFloat(s);
adamc@1260 979 if (r.toString() == s)
adamc@1260 980 return r;
adamc@1260 981 else
adamc@1260 982 er("Can't parse float: " + s);
adamc@649 983 }
adamc@649 984
adamc@838 985 function pio(s) {
adamc@1260 986 var r = parseInt(s);
adamc@1260 987 if (r.toString() == s)
adamc@1260 988 return r;
adamc@1260 989 else
adamc@1260 990 return null;
adamc@838 991 }
adamc@838 992
adamc@838 993 function pflo(s) {
adamc@1260 994 var r = parseFloat(s);
adamc@1260 995 if (r.toString() == s)
adamc@1260 996 return r;
adamc@1260 997 else
adamc@1260 998 return null;
adamc@838 999 }
adamc@838 1000
adamc@692 1001 function uf(s) {
adamc@1260 1002 if (s.length == 0)
adamc@1260 1003 return "_";
adamc@1260 1004 s = s.replace(new RegExp ("\\.", "g"), ".2E");
adamc@1260 1005 return (s.charAt(0) == '_' ? "_" : "") + encodeURIComponent(s).replace(new RegExp ("%", "g"), ".");
adamc@691 1006 }
adamc@691 1007
adamc@692 1008 function uu(s) {
adamc@1260 1009 if (s.length > 0 && s.charAt(0) == '_') {
adamc@1260 1010 s = s.substring(1);
adamc@1260 1011 } else if (s.length >= 3 && (s.charAt(0) == '%' || s.charAt(0) == '.')
adamc@1260 1012 && s.charAt(1) == '5' && (s.charAt(2) == 'f' || s.charAt(2) == 'F'))
adamc@1260 1013 s = s.substring(3);
adamc@1260 1014 s = s.replace(new RegExp ("\\+", "g"), " ");
adamc@1260 1015 s = s.replace(new RegExp ("\\.", "g"), "%");
adamc@1260 1016 return decodeURIComponent(s);
adamc@905 1017 }
adamc@905 1018
adamc@1108 1019 function atr(s) {
adamc@1260 1020 return s.replace(new RegExp ("\"", "g"), "&quot;").replace(new RegExp ("&", "g"), "&amp;")
adamc@1260 1021 }
adamc@1108 1022
adamc@912 1023 function ub(b) {
adamc@1260 1024 return b ? "1" : "0";
adamc@912 1025 }
adamc@912 1026
adamc@905 1027 function uul(getToken, getData) {
adamc@1260 1028 var tok = getToken();
adamc@1260 1029 if (tok == "Nil") {
adamc@1260 1030 return null;
adamc@1260 1031 } else if (tok == "Cons") {
adamc@1260 1032 var d = getData();
adamc@1260 1033 var l = uul(getToken, getData);
adamc@1260 1034 return {_1:d, _2:l};
adamc@1260 1035 } else
adamc@1260 1036 whine("Can't unmarshal list (" + tok + ")");
adamc@692 1037 }
adamc@692 1038
adamc@961 1039 function strcmp(str1, str2) {
adamc@1260 1040 return ((str1 == str2) ? 0 : ((str1 > str2) ? 1 : -1));
adamc@961 1041 }
adamc@692 1042
adamc@603 1043
adamc@692 1044 // Remote calls
adamc@609 1045
adamc@703 1046 var client_id = null;
adamc@668 1047 var client_pass = 0;
adamc@668 1048 var url_prefix = "/";
adamc@673 1049 var timeout = 60;
adam@1552 1050 var isPost = false;
adamc@668 1051
adamc@668 1052 function getXHR(uri)
adamc@609 1053 {
adamc@609 1054 try {
adamc@1260 1055 return new XMLHttpRequest();
adamc@609 1056 } catch (e) {
adamc@1260 1057 try {
adamc@1260 1058 return new ActiveXObject("Msxml2.XMLHTTP");
adamc@1260 1059 } catch (e) {
adamc@1260 1060 try {
adamc@1260 1061 return new ActiveXObject("Microsoft.XMLHTTP");
adamc@1260 1062 } catch (e) {
adamc@1260 1063 whine("Your browser doesn't seem to support AJAX.");
adamc@1260 1064 }
adamc@1260 1065 }
adamc@609 1066 }
adamc@609 1067 }
adamc@609 1068
adamc@736 1069 var sig = null;
adamc@736 1070
adamc@1042 1071 var unloading = false, inFlight = null;
adamc@1042 1072
adamc@1042 1073 function unload() {
adamc@1260 1074 unloading = true;
adamc@1042 1075
adamc@1260 1076 for (; inFlight; inFlight = inFlight.next) {
adamc@1260 1077 inFlight.data.abort();
adamc@1260 1078 }
adamc@1042 1079 }
adamc@1042 1080
adamc@736 1081 function requestUri(xhr, uri, needsSig) {
adamc@1260 1082 if (unloading)
adamc@1260 1083 return;
adamc@1042 1084
adamc@1260 1085 xhr.open("POST", uri, true);
adamc@1260 1086 xhr.setRequestHeader("Content-type", "text/plain");
adam@1549 1087 try {
adam@1549 1088 xhr.setRequestHeader("Content-length", "0");
adam@1549 1089 xhr.setRequestHeader("Connection", "close");
adam@1549 1090 } catch (e) { }
adamc@668 1091
adamc@1260 1092 if (client_id != null) {
adamc@1260 1093 xhr.setRequestHeader("UrWeb-Client", client_id.toString());
adamc@1260 1094 xhr.setRequestHeader("UrWeb-Pass", client_pass.toString());
adamc@1260 1095 }
adamc@668 1096
adamc@1260 1097 if (needsSig) {
adamc@1260 1098 if (sig == null)
adamc@1260 1099 whine("Missing cookie signature!");
adamc@736 1100
adamc@1260 1101 xhr.setRequestHeader("UrWeb-Sig", sig);
adamc@1260 1102 }
adamc@736 1103
adamc@1260 1104 inFlight = cons(xhr, inFlight);
adamc@1260 1105 xhr.send(null);
adamc@668 1106 }
adamc@668 1107
adamc@1042 1108 function xhrFinished(xhr) {
adamc@1260 1109 xhr.abort();
adamc@1260 1110 inFlight = remove(xhr, inFlight);
adamc@1042 1111 }
adamc@1042 1112
adamc@1111 1113 function unurlify(parse, s) {
adamc@1260 1114 return parse(s);
adamc@1111 1115 }
adamc@1111 1116
adam@1385 1117 function redirect(s) {
adam@1385 1118 window.location = s;
adam@1385 1119 }
adam@1385 1120
adamc@978 1121 function rc(prefix, uri, parse, k, needsSig) {
adamc@1260 1122 uri = cat(prefix, uri);
adamc@1260 1123 uri = flattenLocal(uri);
adamc@1260 1124 var xhr = getXHR();
adamc@609 1125
adamc@1260 1126 xhr.onreadystatechange = function() {
adamc@1260 1127 if (xhr.readyState == 4) {
adamc@1260 1128 var isok = false;
adamc@612 1129
adamc@1260 1130 try {
adamc@1260 1131 if (xhr.status == 200)
adamc@1260 1132 isok = true;
adamc@1260 1133 } catch (e) { }
adamc@612 1134
adamc@1260 1135 if (isok) {
adam@1446 1136 var lines = xhr.responseText.split("\n");
adam@1446 1137 if (lines.length != 2)
adam@1446 1138 whine("Bad RPC response lines");
adam@1446 1139
adam@1446 1140 eval(lines[0]);
adam@1446 1141
adamc@1260 1142 try {
adam@1446 1143 k(parse(lines[1]));
adamc@1260 1144 } catch (v) {
adamc@1260 1145 doExn(v);
adamc@1260 1146 }
adamc@1260 1147 } else {
adamc@1260 1148 conn();
adamc@1260 1149 }
adamc@1260 1150
adamc@1260 1151 xhrFinished(xhr);
adamc@729 1152 }
adamc@1260 1153 };
adamc@1042 1154
adamc@1260 1155 requestUri(xhr, uri, needsSig);
adamc@609 1156 }
adamc@667 1157
adamc@667 1158 function path_join(s1, s2) {
adamc@1260 1159 if (s1.length > 0 && s1.charAt(s1.length-1) == '/')
adamc@1260 1160 return s1 + s2;
adamc@1260 1161 else
adamc@1260 1162 return s1 + "/" + s2;
adamc@667 1163 }
adamc@667 1164
adamc@670 1165 var channels = [];
adamc@670 1166
adamc@670 1167 function newQueue() {
adamc@1260 1168 return { front : null, back : null };
adamc@670 1169 }
adamc@670 1170 function enqueue(q, v) {
adamc@1260 1171 if (q.front == null) {
adamc@1260 1172 q.front = cons(v, null);
adamc@1260 1173 q.back = q.front;
adamc@1260 1174 } else {
adamc@1260 1175 var node = cons(v, null);
adamc@1260 1176 q.back.next = node;
adamc@1260 1177 q.back = node;
adamc@1260 1178 }
adamc@670 1179 }
adamc@670 1180 function dequeue(q) {
adamc@670 1181 if (q.front == null)
adamc@1260 1182 return null;
adamc@1260 1183 else {
adamc@1260 1184 var r = q.front.data;
adamc@1260 1185 q.front = q.front.next;
adamc@1260 1186 if (q.front == null)
adamc@1260 1187 q.back = null;
adamc@1260 1188 return r;
adamc@1260 1189 }
adamc@670 1190 }
adamc@670 1191
adamc@670 1192 function newChannel() {
adamc@1260 1193 return { msgs : newQueue(), listeners : newQueue() };
adamc@670 1194 }
adamc@670 1195
adamc@667 1196 function listener() {
adamc@1260 1197 var uri = path_join(url_prefix, ".msgs");
adamc@1260 1198 var xhr = getXHR();
adamc@1260 1199 var tid, orsc, onTimeout;
adamc@673 1200
adamc@1260 1201 var connect = function () {
adamc@1260 1202 xhr.onreadystatechange = orsc;
adamc@1260 1203 tid = window.setTimeout(onTimeout, timeout * 500);
adamc@1260 1204 requestUri(xhr, uri, false);
adamc@1260 1205 }
adamc@673 1206
adamc@1260 1207 orsc = function() {
adamc@1260 1208 if (xhr.readyState == 4) {
adamc@1260 1209 window.clearTimeout(tid);
adamc@673 1210
adamc@1260 1211 var isok = false;
adamc@667 1212
adamc@1260 1213 try {
adamc@1260 1214 if (xhr.status == 200)
adamc@1260 1215 isok = true;
adamc@1260 1216 } catch (e) { }
adamc@667 1217
adamc@1260 1218 if (isok) {
adam@1447 1219 var text = xhr.responseText;
adamc@1260 1220 if (text == "")
adamc@1260 1221 return;
adamc@1260 1222 var lines = text.split("\n");
adamc@889 1223
adam@1552 1224 if (lines.length == 1 && lines[0] == "R") {
adam@1553 1225 lameDuck = true;
adam@1553 1226
adam@1552 1227 if (isPost)
adam@1552 1228 history.back();
adam@1552 1229 else
adam@1552 1230 location.reload();
adam@1552 1231
adam@1552 1232 return;
adam@1552 1233 }
adam@1552 1234
adamc@1260 1235 if (lines.length < 2) {
adamc@1260 1236 discon();
adamc@1260 1237 return;
adamc@1260 1238 }
adamc@1260 1239
adam@1447 1240 var messageReader = function(i) {
adam@1447 1241 if (i+1 >= lines.length) {
adam@1447 1242 xhrFinished(xhr);
adam@1447 1243 connect();
adam@1447 1244 }
adam@1447 1245 else {
adam@1447 1246 var chn = lines[i];
adam@1447 1247 var msg = lines[i+1];
adamc@1260 1248
adam@1447 1249 if (chn == "E") {
adam@1447 1250 eval(msg);
adam@1447 1251 window.setTimeout(function() { messageReader(i+2); }, 0);
adam@1447 1252 } else {
adam@1447 1253 if (chn < 0)
adam@1447 1254 whine("Out-of-bounds channel in message from remote server");
adamc@1260 1255
adam@1447 1256 var ch;
adamc@1260 1257
adam@1447 1258 if (chn >= channels.length || channels[chn] == null) {
adam@1447 1259 ch = newChannel();
adam@1447 1260 channels[chn] = ch;
adam@1447 1261 } else
adam@1447 1262 ch = channels[chn];
adamc@1260 1263
adam@1447 1264 var listener = dequeue(ch.listeners);
adam@1447 1265 if (listener == null) {
adam@1447 1266 enqueue(ch.msgs, msg);
adam@1447 1267 } else {
adam@1447 1268 try {
adam@1447 1269 listener(msg);
adam@1447 1270 } catch (v) {
adam@1447 1271 doExn(v);
adam@1447 1272 }
adam@1447 1273 }
adam@1447 1274
adam@1447 1275 messageReader(i+2);
adamc@1260 1276 }
adamc@1260 1277 }
adamc@1260 1278 }
adamc@1260 1279
adam@1447 1280 messageReader(0);
adamc@1260 1281 }
adamc@1260 1282 else {
adamc@1260 1283 try {
adamc@1260 1284 if (xhr.status != 0)
adamc@1260 1285 servErr("Error querying remote server for messages: " + xhr.status);
adamc@1260 1286 } catch (e) { }
adamc@1260 1287 }
adamc@729 1288 }
adamc@1260 1289 };
adamc@669 1290
adamc@1260 1291 onTimeout = function() {
adamc@1260 1292 xhrFinished(xhr);
adamc@1260 1293 connect();
adamc@1260 1294 };
adamc@670 1295
adamc@673 1296 connect();
adamc@667 1297 }
adamc@670 1298
adamc@670 1299 function rv(chn, parse, k) {
adamc@1260 1300 if (chn == null)
adamc@1260 1301 return;
adamc@682 1302
adamc@1260 1303 if (chn < 0)
adamc@1260 1304 whine("Out-of-bounds channel receive");
adamc@670 1305
adamc@1260 1306 var ch;
adamc@670 1307
adamc@1260 1308 if (chn >= channels.length || channels[chn] == null) {
adamc@1260 1309 ch = newChannel();
adamc@1260 1310 channels[chn] = ch;
adamc@1260 1311 } else
adamc@1260 1312 ch = channels[chn];
adamc@670 1313
adamc@1260 1314 var msg = dequeue(ch.msgs);
adamc@1260 1315 if (msg == null) {
adamc@1260 1316 enqueue(ch.listeners, function(msg) { k(parse(msg)); });
adamc@1260 1317 } else {
adamc@1260 1318 try {
adamc@1260 1319 k(parse(msg));
adamc@1260 1320 } catch (v) {
adamc@1260 1321 doExn(v);
adamc@1260 1322 }
adamc@728 1323 }
adamc@670 1324 }
adamc@693 1325
adamc@978 1326 function sl(ms, k) {
adamc@1260 1327 window.setTimeout(function() { k(null); }, ms);
adamc@1021 1328 }
adamc@1021 1329
adamc@1021 1330 function sp(e) {
adamc@1260 1331 execF(e, null);
adamc@978 1332 }
adamc@978 1333
adamc@693 1334
adamc@970 1335 // The Ur interpreter
adamc@970 1336
adamc@970 1337 var urfuncs = [];
adamc@970 1338
adamc@970 1339 function lookup(env, n) {
adamc@1260 1340 while (env != null) {
adamc@1260 1341 if (n == 0)
adamc@1260 1342 return env.data;
adamc@1260 1343 else {
adamc@1260 1344 --n;
adamc@1260 1345 env = env.next;
adamc@1260 1346 }
adamc@970 1347 }
adamc@970 1348
adamc@1260 1349 whine("Out-of-bounds Ur variable reference");
adamc@970 1350 }
adamc@970 1351
adamc@974 1352 function execP(env, p, v) {
adamc@1260 1353 switch (p.c) {
adamc@1260 1354 case "w":
adamc@1260 1355 return env;
adamc@1260 1356 case "v":
adamc@1260 1357 return cons(v, env);
adamc@1260 1358 case "c":
adamc@1260 1359 if (v == p.v)
adamc@1260 1360 return env;
adamc@1260 1361 else
adamc@1260 1362 return false;
adamc@1260 1363 case "s":
adamc@1260 1364 if (v == null)
adamc@1260 1365 return false;
adamc@1260 1366 else
adamc@1260 1367 return execP(env, p.p, p.n ? v.v : v);
adamc@1260 1368 case "1":
adamc@1260 1369 if (v.n != p.n)
adamc@1260 1370 return false;
adamc@1260 1371 else
adamc@1260 1372 return execP(env, p.p, v.v);
adamc@1260 1373 case "r":
adamc@1260 1374 for (var fs = p.l; fs != null; fs = fs.next) {
adamc@1260 1375 env = execP(env, fs.data.p, v["_" + fs.data.n]);
adamc@1260 1376 if (env == false)
adamc@1260 1377 return false;
adamc@1260 1378 }
adamc@1260 1379 return env;
adamc@1260 1380 default:
adamc@1260 1381 whine("Unknown Ur pattern kind" + p.c);
adamc@974 1382 }
adamc@974 1383 }
adamc@974 1384
adamc@970 1385 function exec0(env, e) {
adamc@1260 1386 return exec1(env, null, e);
adamc@1020 1387 }
adamc@1020 1388
adamc@1020 1389 function exec1(env, stack, e) {
adamc@1260 1390 var stack, usedK = false;
adamc@970 1391
adamc@1260 1392 var saveEnv = function() {
adamc@1260 1393 if (stack.next != null && stack.next.data.c != "<")
adamc@1260 1394 stack = cons({c: "<", env: env}, stack.next);
adamc@1260 1395 else
adamc@1260 1396 stack = stack.next;
adamc@1260 1397 };
adamc@983 1398
adamc@1260 1399 while (true) {
adamc@1260 1400 switch (e.c) {
adamc@1260 1401 case "c":
adamc@1260 1402 var v = e.v;
adamc@1260 1403 if (stack == null)
adamc@1260 1404 return v;
adamc@1260 1405 var fr = stack.data;
adamc@970 1406
adamc@1260 1407 switch (fr.c) {
adamc@1260 1408 case "s":
adamc@1260 1409 e = {c: "c", v: {v: v}};
adamc@1260 1410 stack = stack.next;
adamc@1260 1411 break;
adamc@1260 1412 case "1":
adamc@1260 1413 e = {c: "c", v: {n: fr.n, v: v}};
adamc@1260 1414 stack = stack.next;
adamc@1260 1415 break;
adamc@1260 1416 case "f":
adamc@1260 1417 fr.args[fr.pos++] = v;
adamc@1260 1418 if (fr.a == null) {
adamc@1260 1419 var res;
adamc@1260 1420 stack = stack.next;
adamc@1035 1421
adamc@1260 1422 if (fr.f.apply)
adamc@1260 1423 res = fr.f.apply(null, fr.args);
adamc@1260 1424 else if (fr.args.length == 0)
adamc@1260 1425 res = fr.f();
adamc@1260 1426 else if (fr.args.length == 1)
adamc@1260 1427 res = fr.f(fr.args[0]);
adamc@1260 1428 else if (fr.args.length == 2)
adamc@1260 1429 res = fr.f(fr.args[0], fr.args[1]);
adamc@1260 1430 else if (fr.args.length == 3)
adamc@1260 1431 res = fr.f(fr.args[0], fr.args[1], fr.args[2]);
adamc@1260 1432 else if (fr.args.length == 4)
adamc@1260 1433 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3]);
adamc@1260 1434 else if (fr.args.length == 5)
adamc@1260 1435 res = fr.f(fr.args[0], fr.args[1], fr.args[2], fr.args[3], fr.args[4]);
adamc@1260 1436 else
adamc@1260 1437 whine("Native function has " + fr.args.length + " args, but there is no special case for that count.");
adamc@1035 1438
adamc@1260 1439 e = {c: "c", v: res};
adamc@1260 1440 if (usedK) return null;
adamc@1260 1441 } else {
adamc@1260 1442 e = fr.a.data;
adamc@1260 1443 fr.a = fr.a.next;
adamc@1260 1444 }
adamc@1260 1445 break;
adamc@1260 1446 case "a1":
adamc@1260 1447 e = fr.x;
adamc@1260 1448 stack = cons({c: "a2", f: v}, stack.next);
adamc@1260 1449 break;
adamc@1260 1450 case "a2":
adamc@1260 1451 if (fr.f == null)
adamc@1260 1452 whine("Ur: applying null function");
adamc@1260 1453 else if (fr.f.body) {
adamc@1260 1454 saveEnv();
adamc@1260 1455 env = cons(v, fr.f.env);
adamc@1260 1456 e = fr.f.body;
adamc@1260 1457 } else {
adamc@1260 1458 e = {c: "c", v: fr.f(v)};
adamc@1260 1459 stack = stack.next;
adamc@1260 1460 }
adamc@1260 1461 break;
adamc@1260 1462 case "<":
adamc@1260 1463 env = fr.env;
adamc@1260 1464 stack = stack.next;
adamc@1260 1465 break;
adamc@1260 1466 case "r":
adamc@1260 1467 fr.fs["_" + fr.n] = v;
adamc@1260 1468 if (fr.l == null) {
adamc@1260 1469 e = {c: "c", v: fr.fs};
adamc@1260 1470 stack = stack.next;
adamc@1260 1471 } else {
adamc@1260 1472 fr.n = fr.l.data.n;
adamc@1260 1473 e = fr.l.data.v;
adamc@1260 1474 fr.l = fr.l.next;
adamc@1260 1475 }
adamc@1260 1476 break;
adamc@1260 1477 case ".":
adamc@1260 1478 e = {c: "c", v: v["_" + fr.f]};
adamc@1260 1479 stack = stack.next;
adamc@1260 1480 break;
adamc@1260 1481 case ";":
adamc@1260 1482 e = fr.e2;
adamc@1260 1483 stack = stack.next;
adamc@1260 1484 break;
adamc@1260 1485 case "=":
adamc@1260 1486 saveEnv();
adamc@1260 1487 env = cons(v, env);
adamc@1260 1488 e = fr.e2;
adamc@1260 1489 break;
adamc@1260 1490 case "m":
adamc@1260 1491 var ps;
adamc@1260 1492 for (ps = fr.p; ps != null; ps = ps.next) {
adamc@1260 1493 var r = execP(env, ps.data.p, v);
adamc@1260 1494 if (r != false) {
adamc@1260 1495 saveEnv();
adamc@1260 1496 env = r;
adamc@1260 1497 e = ps.data.b;
adamc@1260 1498 break;
adamc@1260 1499 }
adamc@1260 1500 }
adamc@1260 1501 if (ps == null)
adamc@1260 1502 whine("Match failure in Ur interpretation");
adamc@1260 1503 break;
adamc@1260 1504 default:
adamc@1260 1505 whine("Unknown Ur continuation kind " + fr.c);
adamc@1260 1506 }
adamc@1260 1507
adamc@1260 1508 break;
adamc@1260 1509 case "v":
adamc@1260 1510 e = {c: "c", v: lookup(env, e.n)};
adamc@1260 1511 break;
adamc@1260 1512 case "n":
adamc@1260 1513 var idx = e.n;
adamc@1260 1514 e = urfuncs[idx];
adamc@1260 1515 if (e.c == "t")
adamc@1262 1516 e = urfuncs[idx] = eval("(" + e.f + ")");
adamc@1260 1517 break;
adamc@1260 1518 case "s":
adamc@1260 1519 stack = cons({c: "s"}, stack);
adamc@1260 1520 e = e.v;
adamc@1260 1521 break;
adamc@1260 1522 case "1":
adamc@1260 1523 stack = cons({c: "1", n: e.n}, stack);
adamc@1260 1524 e = e.v;
adamc@1260 1525 break;
adamc@1260 1526 case "f":
adamc@1260 1527 if (e.a == null)
adamc@1260 1528 e = {c: "c", v: (eval(e.f))()};
adamc@1260 1529 else {
adamc@1260 1530 var args = [];
adamc@1260 1531 stack = cons({c: "f", f: eval(e.f), args: args, pos: 0, a: e.a.next}, stack);
adamc@1260 1532 if (!e.a.data.c) alert("[2] fr.f = " + e.f + "; 0 = " + e.a.data);
adamc@1260 1533 e = e.a.data;
adamc@1260 1534 }
adamc@1260 1535 break;
adamc@1260 1536 case "l":
adamc@1260 1537 e = {c: "c", v: {env: env, body: e.b}};
adamc@1260 1538 break;
adamc@1260 1539 case "a":
adamc@1260 1540 stack = cons({c: "a1", x: e.x}, stack);
adamc@1260 1541 e = e.f;
adamc@1260 1542 break;
adamc@1260 1543 case "r":
adamc@1260 1544 if (e.l == null)
adamc@1260 1545 whine("Empty Ur record in interpretation");
adamc@1260 1546 var fs = {};
adamc@1260 1547 stack = cons({c: "r", n: e.l.data.n, fs: fs, l: e.l.next}, stack);
adamc@1260 1548 e = e.l.data.v;
adamc@1260 1549 break;
adamc@1260 1550 case ".":
adamc@1260 1551 stack = cons({c: ".", f: e.f}, stack);
adamc@1260 1552 e = e.r;
adamc@1260 1553 break;
adamc@1260 1554 case ";":
adamc@1260 1555 stack = cons({c: ";", e2: e.e2}, stack);
adamc@1260 1556 e = e.e1;
adamc@1260 1557 break;
adamc@1260 1558 case "=":
adamc@1260 1559 stack = cons({c: "=", e2: e.e2}, stack);
adamc@1260 1560 e = e.e1;
adamc@1260 1561 break;
adamc@1260 1562 case "m":
adamc@1260 1563 stack = cons({c: "m", p: e.p}, stack);
adamc@1260 1564 e = e.e;
adamc@1260 1565 break;
adamc@1260 1566 case "e":
adamc@1260 1567 e = {c: "c", v: cs({c: "wc", env: env, body: e.e})};
adamc@1260 1568 break;
adamc@1260 1569 case "wc":
adamc@1260 1570 env = e.env;
adamc@1260 1571 e = e.body;
adamc@1260 1572 break;
adamc@1260 1573 case "K":
adamc@1260 1574 { var savedStack = stack.next, savedEnv = env;
adamc@1260 1575 e = {c: "c", v: function(v) { return exec1(savedEnv, savedStack, {c: "c", v: v}); } };}
adamc@1260 1576 usedK = true;
rmbruijn@1597 1577 break;
adamc@1260 1578 default:
adamc@1260 1579 whine("Unknown Ur expression kind " + e.c);
adamc@970 1580 }
adamc@970 1581 }
adamc@970 1582 }
adamc@970 1583
adamc@976 1584 function execD(e) {
adamc@1260 1585 return exec0(null, e);
adamc@976 1586 }
adamc@976 1587
adamc@970 1588 function exec(e) {
adamc@1260 1589 var r = exec0(null, e);
adamc@970 1590
adamc@1260 1591 if (r != null && r.body != null)
adamc@1260 1592 return function(v) { return exec0(cons(v, r.env), r.body); };
adamc@1260 1593 else
adamc@1260 1594 return r;
adamc@895 1595 }
adamc@895 1596
adamc@977 1597 function execF(f, x) {
adamc@1260 1598 return exec0(cons(x, f.env), f.body);
adamc@977 1599 }
adamc@977 1600
adamc@895 1601
adam@1290 1602 // Wrappers
adam@1290 1603
adam@1290 1604 function confrm(s) {
adam@1290 1605 return confirm(s) ? true : false;
adam@1290 1606 }
adam@1290 1607
adam@1290 1608
adamc@1323 1609 // URL blessing
adamc@1323 1610
adamc@1323 1611 var urlRules = null;
adamc@1323 1612
adamc@1323 1613 function checkUrl(s) {
adamc@1323 1614 for (var r = urlRules; r; r = r.next) {
adamc@1323 1615 var ru = r.data;
adamc@1323 1616 if (ru.prefix ? s.indexOf(ru.pattern) == 0 : s == ru.pattern)
adamc@1323 1617 return ru.allow ? s : null;
adamc@1323 1618 }
adamc@1323 1619
adamc@1323 1620 return null;
adamc@1323 1621 }
adamc@1323 1622
adamc@1323 1623 function bless(s) {
adamc@1323 1624 u = checkUrl(s);
adamc@1323 1625 if (u == null)
adamc@1323 1626 er("Disallowed URL: " + s);
adamc@1323 1627 return u;
adamc@1323 1628 }
adamc@1323 1629
adam@1556 1630
adam@1556 1631 // ID generation
adam@1556 1632
adam@1556 1633 var nextId = 0;
adam@1556 1634
adam@1556 1635 function fresh() {
adam@1556 1636 return (--nextId).toString();
adam@1556 1637 }
adam@1556 1638
rmbruijn@1597 1639
adamc@693 1640 // App-specific code