69
69
* it will automatically be URL-encoded.
70
70
* \param onclick Optional string. Will be set as the "onclick" attribute
71
71
* of the "a" element.
72
* \param dontencode Optional boolean. If true, will not encode the href.
73
* if including query strings, you must set this to true and use build_url
74
* to escape the URI correctly.
75
72
* \return DOM Element object.
77
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
74
function dom_make_link_elem(tagname, text, title, href, onclick)
79
76
if (text == null) text = "";
80
77
if (href == null) href = "";
81
78
var elem = document.createElement(tagname);
82
79
var link = document.createElement("a");
83
if (dontencode != true)
84
href = urlencode_path(href);
85
link.setAttribute("href", href);
80
link.setAttribute("href", encodeURI(href));
87
82
link.setAttribute("title", title);
88
83
if (onclick != null)
98
93
function dom_make_img(src, width, height, title, alt)
100
95
var img = document.createElement("img");
101
img.setAttribute("src", urlencode_path(src));
96
img.setAttribute("src", src);
102
97
if (width != null)
103
98
img.setAttribute("width", width);
104
99
if (height != null)
154
149
* args is never null, though it may be empty.
156
151
* All strings are decoded/unescaped. Reserved characters
157
* (; , / ? : @ & = + * $) are not decoded except in args and path.
152
* (; , / ? : @ & = + * $) are not decoded except in args.
159
154
* \param url String. A URL. To read from the current browser window, use
160
155
* window.location.href.
311
307
url += ":" + obj.server_port.toString();
312
308
if (("path" in obj) && obj.path != null)
314
var path = urlencode_path(obj.path.toString());
310
var path = obj.path.toString();
315
311
if (url.length > 0 && path.length > 0 && path[0] != "/")
316
312
path = "/" + path;
319
315
if (("query_string" in obj) && obj.query_string != null)
320
query_string = encodeURI(obj.query_string.toString());
316
query_string = obj.query_string.toString();
321
317
else if (("args" in obj) && obj.args != null)
322
318
query_string = make_query_string(obj.args);
324
320
if (query_string != null)
325
321
url += "?" + query_string;
330
/** URL-encodes a path. This is a special case of URL encoding as all
331
* characters *except* the slash must be encoded.
333
function urlencode_path(path)
335
/* Split up the path, URLEncode each segment with encodeURIComponent,
338
var split = path.split('/');
339
for (var i=0; i<split.length; i++)
340
split[i] = encodeURIComponent(split[i]);
341
path = path_join.apply(null, split);
342
if (split[0] == "" && split.length > 1) path = "/" + path;
323
return encodeURI(url);
346
326
/** Given an argument map, as output in the args parameter of the return of
429
409
/* FIXME: Encoding not supported here (should not matter if we
430
410
* only use ASCII names */
431
data += "--" + boundary + "\r\n"
411
data += "--" + boundary + "\n"
432
412
+ "Content-Disposition: form-data; name=\"" + arg_key
437
417
for (var arg_key in args)
471
451
return path_join(root_dir, path);
474
/** Shorthand for make_path(path_join(app, ...))
475
* Creates an absolute path for a given path within a given app.
477
function app_path(app /*,...*/)
479
return make_path(path_join.apply(null, arguments));
482
/** Given a path, gets the "basename" (the last path segment).
484
function path_basename(path)
486
segments = path.split("/");
487
if (segments[segments.length-1].length == 0)
488
return segments[segments.length-2];
490
return segments[segments.length-1];
493
/** Given a string str, determines whether it ends with substr */
494
function endswith(str, substring)
496
if (str.length < substring.length) return false;
497
return str.substr(str.length - substring.length) == substring;
500
/** Equivalent to Python's repr.
501
* Gets the JavaScript string representation.
502
* Actually just calls JSON.stringify.
506
return JSON.stringify(str);
509
/** Removes all occurences of a value from an array.
511
Array.prototype.removeall = function(val)
516
for (i=0; i<arr.length; i++)
519
if (arr[i] != val) j++;
524
454
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
525
455
* response, and returns an XMLHttpRequest object containing the completed
539
469
if (content_type != "multipart/form-data")
540
470
content_type = "application/x-www-form-urlencoded";
541
path = app_path(app, path);
471
path = make_path(path_join(app, path));
543
473
/* A random string, for multipart/form-data
544
474
* (This is not checked against anywhere else, it is solely defined and