2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2012 Jo-Philipp Wich <jow@openwrt.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 var cbi_strings = { path: {}, label: {} };
19 return (/^-?\d+$/.test(x) ? +x : NaN);
23 return (/^-?\d+(?:\.\d+)?$/.test(x) ? +x : NaN);
26 var cbi_validators = {
33 'uinteger': function()
35 return (Int(this) >= 0);
45 return (Dec(this) >= 0);
50 return cbi_validators.ip4addr.apply(this) ||
51 cbi_validators.ip6addr.apply(this);
56 if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/))
58 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
59 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
60 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
61 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
62 ((RegExp.$6.indexOf('.') < 0)
63 ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32))
64 : (cbi_validators.ip4addr.apply(RegExp.$6)))
73 if( this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
75 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
84 if( addr.indexOf('.') > 0 )
86 var off = addr.lastIndexOf(':');
88 if( !(off && cbi_validators.ip4addr.apply(addr.substr(off+1))) )
91 addr = addr.substr(0, off) + ':0:0';
94 if( addr.indexOf('::') >= 0 )
99 for( var i = 1; i < (addr.length-1); i++ )
100 if( addr.charAt(i) == ':' )
106 for( var i = 0; i < (7 - colons); i++ )
109 if (addr.match(/^(.*?)::(.*?)$/))
110 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
111 (RegExp.$2 ? ':' + RegExp.$2 : '');
114 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
124 return (p >= 0 && p <= 65535);
127 'portrange': function()
129 if (this.match(/^(\d+)-(\d+)$/))
133 return (p1 <= p2 && p2 <= 65535);
136 return cbi_validators.port.apply(this);
139 'macaddr': function()
141 return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
144 'host': function(ipv4only)
146 return cbi_validators.hostname.apply(this) ||
147 ((ipv4only != 1) && cbi_validators.ipaddr.apply(this)) ||
148 ((ipv4only == 1) && cb_validators.ip4addr.apply(this));
151 'hostname': function()
153 if (this.length <= 253)
154 return (this.match(/^[a-zA-Z0-9]+$/) != null ||
155 (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
156 this.match(/[^0-9.]/)));
161 'network': function()
163 return cbi_validators.uciname.apply(this) ||
164 cbi_validators.host.apply(this);
167 'hostport': function(ipv4only)
169 var hp = this.split(/:/);
172 return (cbi_validators.host.apply(hp[0], ipv4only) &&
173 cbi_validators.port.apply(hp[1]));
178 'ip4addrport': function()
180 var hp = this.split(/:/);
183 return (cbi_validators.ipaddr.apply(hp[0]) &&
184 cbi_validators.port.apply(hp[1]));
188 'ipaddrport': function(bracket)
190 if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
193 return (cbi_validators.ip4addr.apply(addr) &&
194 cbi_validators.port.apply(port));
195 } else if ((bracket == 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
198 return (cbi_validators.ip6addr.apply(addr) &&
199 cbi_validators.port.apply(port));
200 } else if ((bracket != 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
203 return (cbi_validators.ip6addr.apply(addr) &&
204 cbi_validators.port.apply(port));
215 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
217 return (v.length >= 8) && (v.length <= 63);
224 if ( v.substr(0,2) == 's:' )
227 if( (v.length == 10) || (v.length == 26) )
228 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
230 return (v.length == 5) || (v.length == 13);
233 'uciname': function()
235 return (this.match(/^[a-zA-Z0-9_]+$/) != null);
238 'range': function(min, max)
241 return (val >= +min && val <= +max);
246 return (Dec(this) >= +min);
251 return (Dec(this) <= +max);
254 'rangelength': function(min, max)
257 return ((val.length >= +min) && (val.length <= +max));
260 'minlength': function(min)
262 return ((''+this).length >= +min);
265 'maxlength': function(max)
267 return ((''+this).length <= +max);
272 for (var i = 0; i < arguments.length; i += 2)
274 if (typeof arguments[i] != 'function')
276 if (arguments[i] == this)
280 else if (arguments[i].apply(this, arguments[i+1]))
290 for (var i = 0; i < arguments.length; i += 2)
292 if (typeof arguments[i] != 'function')
294 if (arguments[i] != this)
298 else if (!arguments[i].apply(this, arguments[i+1]))
308 return cbi_validators.or.apply(
309 this.replace(/^[ \t]*![ \t]*/, ''), arguments);
312 'list': function(subvalidator, subargs)
314 if (typeof subvalidator != 'function')
317 var tokens = this.match(/[^ \t]+/g);
318 for (var i = 0; i < tokens.length; i++)
319 if (!subvalidator.apply(tokens[i], subargs))
324 'phonedigit': function()
326 return (this.match(/^[0-9\*#!\.]+$/) != null);
328 'timehhmmss': function()
330 return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
332 'dateyyyymmdd': function()
337 if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
338 var year = RegExp.$1;
339 var month = RegExp.$2;
342 var days_in_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
343 function is_leap_year(year) {
344 return ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0);
346 function get_days_in_month(month, year) {
347 if ((month == 2) && is_leap_year(year)) {
350 return days_in_month[month];
353 /* Firewall rules in the past don't make sense */
357 if ((month <= 0) || (month > 12)) {
360 if ((day <= 0) || (day > get_days_in_month(month, year))) {
372 function cbi_d_add(field, dep, index) {
373 var obj = (typeof(field) === 'string') ? document.getElementById(field) : field;
376 for (var i=0; i<cbi_d.length; i++) {
377 if (cbi_d[i].id == obj.id) {
386 "parent": obj.parentNode.id,
390 cbi_d.unshift(entry);
396 function cbi_d_checkvalue(target, ref) {
397 var t = document.getElementById(target);
401 var tl = document.getElementsByName(target);
403 if( tl.length > 0 && (tl[0].type == 'radio' || tl[0].type == 'checkbox'))
404 for( var i = 0; i < tl.length; i++ )
405 if( tl[i].checked ) {
410 value = value ? value : "";
411 } else if (!t.value) {
416 if (t.type == "checkbox") {
417 value = t.checked ? value : "";
421 return (value == ref)
424 function cbi_d_check(deps) {
427 for (var i=0; i<deps.length; i++) {
430 for (var j in deps[i]) {
431 if (j == "!reverse") {
433 } else if (j == "!default") {
437 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
447 function cbi_d_update() {
449 for (var i=0; i<cbi_d.length; i++) {
450 var entry = cbi_d[i];
451 var node = document.getElementById(entry.id);
452 var parent = document.getElementById(entry.parent);
454 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
455 node.parentNode.removeChild(node);
457 } else if (parent && (!node || !node.parentNode) && cbi_d_check(entry.deps)) {
458 var next = undefined;
460 for (next = parent.firstChild; next; next = next.nextSibling) {
461 if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index) {
467 parent.appendChild(entry.node);
469 parent.insertBefore(entry.node, next);
475 // hide optionals widget if no choices remaining
476 if (parent && parent.parentNode && parent.getAttribute('data-optionals'))
477 parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : '';
480 if (entry && entry.parent) {
482 cbi_tag_last(parent);
490 function cbi_init() {
493 nodes = document.querySelectorAll('[data-strings]');
495 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
496 var str = JSON.parse(node.getAttribute('data-strings'));
497 for (var key in str) {
498 for (var key2 in str[key]) {
499 var dst = cbi_strings[key] || (cbi_strings[key] = { });
500 dst[key2] = str[key][key2];
505 nodes = document.querySelectorAll('[data-depends]');
507 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
508 var index = parseInt(node.getAttribute('data-index'), 10);
509 var depends = JSON.parse(node.getAttribute('data-depends'));
510 if (!isNaN(index) && depends.length > 0) {
511 for (var alt = 0; alt < depends.length; alt++) {
512 cbi_d_add(node, depends[alt], index);
517 nodes = document.querySelectorAll('[data-update]');
519 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
520 var events = node.getAttribute('data-update').split(' ');
521 for (var j = 0, event; (event = events[j]) !== undefined; j++) {
522 cbi_bind(node, event, cbi_d_update);
526 nodes = document.querySelectorAll('[data-choices]');
528 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
529 var choices = JSON.parse(node.getAttribute('data-choices'));
532 for (var j = 0; j < choices[0].length; j++)
533 options[choices[0][j]] = choices[1][j];
535 var def = (node.getAttribute('data-optional') === 'true')
536 ? node.placeholder || '' : null;
538 cbi_combobox_init(node, options, def,
539 node.getAttribute('data-manual'));
542 nodes = document.querySelectorAll('[data-dynlist]');
544 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
545 var choices = JSON.parse(node.getAttribute('data-dynlist'));
548 if (choices[0] && choices[0].length) {
551 for (var j = 0; j < choices[0].length; j++)
552 options[choices[0][j]] = choices[1][j];
555 cbi_dynlist_init(node, choices[2], choices[3], options);
558 nodes = document.querySelectorAll('[data-type]');
560 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
561 cbi_validate_field(node, node.getAttribute('data-optional') === 'true',
562 node.getAttribute('data-type'));
568 function cbi_bind(obj, type, callback, mode) {
569 if (!obj.addEventListener) {
570 obj.attachEvent('on' + type,
572 var e = window.event;
574 if (!e.target && e.srcElement)
575 e.target = e.srcElement;
577 return !!callback(e);
581 obj.addEventListener(type, callback, !!mode);
586 function cbi_combobox(id, values, def, man, focus) {
587 var selid = "cbi.combobox." + id;
588 if (document.getElementById(selid)) {
592 var obj = document.getElementById(id)
593 var sel = document.createElement("select");
595 sel.index = obj.index;
596 sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select');
598 if (obj.nextSibling) {
599 obj.parentNode.insertBefore(sel, obj.nextSibling);
601 obj.parentNode.appendChild(sel);
604 var dt = obj.getAttribute('cbi_datatype');
605 var op = obj.getAttribute('cbi_optional');
608 cbi_validate_field(sel, op == 'true', dt);
610 if (!values[obj.value]) {
611 if (obj.value == "") {
612 var optdef = document.createElement("option");
614 optdef.appendChild(document.createTextNode(typeof(def) === 'string' ? def : cbi_strings.label.choose));
615 sel.appendChild(optdef);
617 var opt = document.createElement("option");
618 opt.value = obj.value;
619 opt.selected = "selected";
620 opt.appendChild(document.createTextNode(obj.value));
621 sel.appendChild(opt);
625 for (var i in values) {
626 var opt = document.createElement("option");
629 if (obj.value == i) {
630 opt.selected = "selected";
633 opt.appendChild(document.createTextNode(values[i]));
634 sel.appendChild(opt);
637 var optman = document.createElement("option");
639 optman.appendChild(document.createTextNode(typeof(man) === 'string' ? man : cbi_strings.label.custom));
640 sel.appendChild(optman);
642 obj.style.display = "none";
644 cbi_bind(sel, "change", function() {
645 if (sel.selectedIndex == sel.options.length - 1) {
646 obj.style.display = "inline";
648 sel.parentNode.removeChild(sel);
651 obj.value = sel.options[sel.selectedIndex].value;
661 // Retrigger validation in select
668 function cbi_combobox_init(id, values, def, man) {
669 var obj = (typeof(id) === 'string') ? document.getElementById(id) : id;
670 cbi_bind(obj, "blur", function() {
671 cbi_combobox(obj.id, values, def, man, true);
673 cbi_combobox(obj.id, values, def, man, false);
676 function cbi_filebrowser(id, defpath) {
677 var field = document.getElementById(id);
678 var browser = window.open(
679 cbi_strings.path.browser + ( field.value || defpath || '' ) + '?field=' + id,
680 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
686 function cbi_browser_init(id, defpath)
688 function cbi_browser_btnclick(e) {
689 cbi_filebrowser(id, defpath);
693 var field = document.getElementById(id);
695 var btn = document.createElement('img');
696 btn.className = 'cbi-image-button';
697 btn.src = cbi_strings.path.resource + '/cbi/folder.gif';
698 field.parentNode.insertBefore(btn, field.nextSibling);
700 cbi_bind(btn, 'click', cbi_browser_btnclick);
703 function cbi_dynlist_init(parent, datatype, optional, choices)
705 var prefix = parent.getAttribute('data-prefix');
706 var holder = parent.getAttribute('data-placeholder');
710 function cbi_dynlist_redraw(focus, add, del)
714 while (parent.firstChild)
716 var n = parent.firstChild;
721 if (n.nodeName.toLowerCase() == 'input')
722 values.push(n.value || '');
723 else if (n.nodeName.toLowerCase() == 'select')
724 values[values.length-1] = n.options[n.selectedIndex].value;
727 parent.removeChild(n);
733 values.splice(focus, 0, '');
735 else if (values.length == 0)
741 for (var i = 0; i < values.length; i++)
743 var t = document.createElement('input');
744 t.id = prefix + '.' + (i+1);
749 t.className = 'cbi-input-text';
751 if (i == 0 && holder)
753 t.placeholder = holder;
756 var b = document.createElement('img');
757 b.src = cbi_strings.path.resource + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif');
758 b.className = 'cbi-image-button';
760 parent.appendChild(t);
761 parent.appendChild(b);
762 if (datatype == 'file')
764 cbi_browser_init(t.id, parent.getAttribute('data-browser-path'));
767 parent.appendChild(document.createElement('br'));
771 cbi_validate_field(t.id, ((i+1) == values.length) || optional, datatype);
776 cbi_combobox_init(t.id, choices, '', cbi_strings.label.custom);
779 cbi_bind(b, 'keydown', cbi_dynlist_keydown);
780 cbi_bind(b, 'keypress', cbi_dynlist_keypress);
782 if (i == focus || -i == focus)
787 cbi_bind(t, 'keydown', cbi_dynlist_keydown);
788 cbi_bind(t, 'keypress', cbi_dynlist_keypress);
794 else if (-i == focus)
798 /* force cursor to end */
805 cbi_bind(b, 'click', cbi_dynlist_btnclick);
809 function cbi_dynlist_keypress(ev)
811 ev = ev ? ev : window.event;
813 var se = ev.target ? ev.target : ev.srcElement;
815 if (se.nodeType == 3)
820 /* backspace, delete */
823 if (se.value.length == 0)
825 if (ev.preventDefault)
833 /* enter, arrow up, arrow down */
837 if (ev.preventDefault)
846 function cbi_dynlist_keydown(ev)
848 ev = ev ? ev : window.event;
850 var se = ev.target ? ev.target : ev.srcElement;
852 if (se.nodeType == 3)
855 var prev = se.previousSibling;
856 while (prev && prev.name != prefix)
857 prev = prev.previousSibling;
859 var next = se.nextSibling;
860 while (next && next.name != prefix)
861 next = next.nextSibling;
863 /* advance one further in combobox case */
864 if (next && next.nextSibling.name == prefix)
865 next = next.nextSibling;
869 /* backspace, delete */
872 var del = (se.nodeName.toLowerCase() == 'select')
873 ? true : (se.value.length == 0);
877 if (ev.preventDefault)
880 var focus = se.index;
884 cbi_dynlist_redraw(focus, -1, se.index);
893 cbi_dynlist_redraw(-1, se.index, -1);
914 function cbi_dynlist_btnclick(ev)
916 ev = ev ? ev : window.event;
918 var se = ev.target ? ev.target : ev.srcElement;
919 var input = se.previousSibling;
920 while (input && input.name != prefix) {
921 input = input.previousSibling;
924 if (se.src.indexOf('remove') > -1)
928 cbi_dynlist_keydown({
935 cbi_dynlist_keydown({
944 cbi_dynlist_redraw(NaN, -1, -1);
948 function cbi_t_add(section, tab) {
949 var t = document.getElementById('tab.' + section + '.' + tab);
950 var c = document.getElementById('container.' + section + '.' + tab);
953 cbi_t[section] = (cbi_t[section] || [ ]);
954 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
958 function cbi_t_switch(section, tab) {
959 if( cbi_t[section] && cbi_t[section][tab] ) {
960 var o = cbi_t[section][tab];
961 var h = document.getElementById('tab.' + section);
962 for( var tid in cbi_t[section] ) {
963 var o2 = cbi_t[section][tid];
964 if( o.tab.id != o2.tab.id ) {
965 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
966 o2.container.style.display = 'none';
970 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
971 o2.container.style.display = 'block';
978 function cbi_t_update() {
982 for( var sid in cbi_t )
983 for( var tid in cbi_t[sid] )
985 var t = cbi_t[sid][tid].tab;
986 var c = cbi_t[sid][tid].container;
988 if (!c.firstElementChild) {
989 t.style.display = 'none';
991 else if (t.style.display == 'none') {
992 t.style.display = '';
993 t.className += ' cbi-tab-highlighted';
1001 if (hl_tabs.length > 0)
1002 window.setTimeout(function() {
1003 for( var i = 0; i < hl_tabs.length; i++ )
1004 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
1011 function cbi_validate_form(form, errmsg)
1013 /* if triggered by a section removal or addition, don't validate */
1014 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
1017 if( form.cbi_validators )
1019 for( var i = 0; i < form.cbi_validators.length; i++ )
1021 var validator = form.cbi_validators[i];
1022 if( !validator() && errmsg )
1033 function cbi_validate_reset(form)
1036 function() { cbi_validate_form(form, null) }, 100
1042 function cbi_validate_compile(code)
1051 for (var i = 0; i < code.length; i++)
1059 switch (code.charCodeAt(i))
1071 var label = code.substring(pos, i);
1072 label = label.replace(/\\(.)/g, '$1');
1073 label = label.replace(/^[ \t]+/g, '');
1074 label = label.replace(/[ \t]+$/g, '');
1076 if (label && !isNaN(label))
1078 stack.push(parseFloat(label));
1080 else if (label.match(/^(['"]).*\1$/))
1082 stack.push(label.replace(/^(['"])(.*)\1$/, '$2'));
1084 else if (typeof cbi_validators[label] == 'function')
1086 stack.push(cbi_validators[label]);
1091 throw "Syntax error, unhandled token '"+label+"'";
1096 depth += (code.charCodeAt(i) == 40);
1102 if (typeof stack[stack.length-2] != 'function')
1103 throw "Syntax error, argument list follows non-function";
1105 stack[stack.length-1] =
1106 arguments.callee(code.substring(pos, i));
1117 function cbi_validate_field(cbid, optional, type)
1119 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
1120 var vstack; try { vstack = cbi_validate_compile(type); } catch(e) { };
1122 if (field && vstack && typeof vstack[0] == "function")
1124 var validator = function()
1129 field.className = field.className.replace(/ cbi-input-invalid/g, '');
1132 var value = (field.options && field.options.selectedIndex > -1)
1133 ? field.options[field.options.selectedIndex].value : field.value;
1135 if (!(((value.length == 0) && optional) || vstack[0].apply(value, vstack[1])))
1138 field.className += ' cbi-input-invalid';
1146 if( ! field.form.cbi_validators )
1147 field.form.cbi_validators = [ ];
1149 field.form.cbi_validators.push(validator);
1151 cbi_bind(field, "blur", validator);
1152 cbi_bind(field, "keyup", validator);
1154 if (field.nodeName == 'SELECT')
1156 cbi_bind(field, "change", validator);
1157 cbi_bind(field, "click", validator);
1160 field.setAttribute("cbi_validate", validator);
1161 field.setAttribute("cbi_datatype", type);
1162 field.setAttribute("cbi_optional", (!!optional).toString());
1166 var fcbox = document.getElementById('cbi.combobox.' + field.id);
1168 cbi_validate_field(fcbox, optional, type);
1172 function cbi_row_swap(elem, up, store)
1174 var tr = elem.parentNode;
1175 while (tr && tr.nodeName.toLowerCase() != 'tr')
1181 var table = tr.parentNode;
1182 while (table && table.nodeName.toLowerCase() != 'table')
1183 table = table.parentNode;
1189 var e = up ? table.rows.length : table.rows.length - 1;
1191 for (var idx = s; idx < e; idx++)
1193 if (table.rows[idx] == tr)
1196 tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]);
1198 tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]);
1205 for (idx = 2; idx < table.rows.length; idx++)
1207 table.rows[idx].className = table.rows[idx].className.replace(
1208 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2))
1211 if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) )
1212 ids.push(RegExp.$1);
1215 var input = document.getElementById(store);
1217 input.value = ids.join(' ');
1222 function cbi_tag_last(container)
1226 for (var i = 0; i < container.childNodes.length; i++)
1228 var c = container.childNodes[i];
1229 if (c.nodeType == 1 && c.nodeName.toLowerCase() == 'div')
1231 c.className = c.className.replace(/ cbi-value-last$/, '');
1238 last.className += ' cbi-value-last';
1242 String.prototype.serialize = function()
1259 for( var i = 0; i < o.length; i++ )
1260 s += (s ? ', ' : '') + String.serialize(o[i]);
1262 return '[ ' + s + ' ]';
1271 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
1273 return '{ ' + s + ' }';
1280 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
1281 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
1285 return '"' + o + '"';
1290 return o.toString();
1294 String.prototype.format = function()
1299 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1300 var quot_esc = [/"/g, '"', /'/g, '''];
1302 function esc(s, r) {
1303 if (typeof(s) !== 'string' && !(s instanceof String))
1306 for( var i = 0; i < r.length; i += 2 )
1307 s = s.replace(r[i], r[i+1]);
1313 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
1314 var a = b = [], numSubstitutions = 0, numMatches = 0;
1316 while (a = re.exec(str))
1319 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
1320 var pPrecision = a[6], pType = a[7];
1330 if (numSubstitutions < arguments.length)
1332 var param = arguments[numSubstitutions++];
1335 if (pPad && pPad.substr(0,1) == "'")
1336 pad = leftpart.substr(1,1);
1342 var justifyRight = true;
1343 if (pJustify && pJustify === "-")
1344 justifyRight = false;
1348 minLength = +pMinLength;
1351 if (pPrecision && pType == 'f')
1352 precision = +pPrecision.substring(1);
1359 subst = (+param || 0).toString(2);
1363 subst = String.fromCharCode(+param || 0);
1367 subst = ~~(+param || 0);
1371 subst = ~~Math.abs(+param || 0);
1375 subst = (precision > -1)
1376 ? ((+param || 0.0)).toFixed(precision)
1381 subst = (+param || 0).toString(8);
1389 subst = ('' + (+param || 0).toString(16)).toLowerCase();
1393 subst = ('' + (+param || 0).toString(16)).toUpperCase();
1397 subst = esc(param, html_esc);
1401 subst = esc(param, quot_esc);
1405 subst = String.serialize(param);
1412 var ts = (param || 0);
1415 tm = Math.floor(ts / 60);
1420 th = Math.floor(tm / 60);
1425 td = Math.floor(th / 24);
1430 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
1431 : String.format('%dh %dm %ds', th, tm, ts);
1436 var mf = pMinLength ? +pMinLength : 1000;
1437 var pr = pPrecision ? ~~(10 * +('0' + pPrecision)) : 2;
1440 var val = (+param || 0);
1441 var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
1443 for (i = 0; (i < units.length) && (val > mf); i++)
1446 subst = (i ? val.toFixed(pr) : val) + units[i];
1454 subst = subst.toString();
1455 for (var i = subst.length; i < pMinLength; i++)
1456 if (pJustify == '-')
1457 subst = subst + ' ';
1459 subst = pad + subst;
1462 out += leftpart + subst;
1463 str = str.substr(m.length);
1469 String.prototype.nobr = function()
1471 return this.replace(/[\s\n]+/g, ' ');
1474 String.serialize = function()
1477 for (var i = 1; i < arguments.length; i++)
1478 a.push(arguments[i]);
1479 return ''.serialize.apply(arguments[0], a);
1482 String.format = function()
1485 for (var i = 1; i < arguments.length; i++)
1486 a.push(arguments[i]);
1487 return ''.format.apply(arguments[0], a);
1490 String.nobr = function()
1493 for (var i = 1; i < arguments.length; i++)
1494 a.push(arguments[i]);
1495 return ''.nobr.apply(arguments[0], a);