62dada464e276acfc41b87cb27ac544f010e7b49
[project/luci.git] / libs / web / htdocs / luci-static / resources / cbi.js
1 /*
2         LuCI - Lua Configuration Interface
3
4         Copyright 2008 Steven Barth <steven@midlink.org>
5         Copyright 2008-2010 Jo-Philipp Wich <xm@subsignal.org>
6
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
10
11         http://www.apache.org/licenses/LICENSE-2.0
12 */
13
14 var cbi_d = [];
15 var cbi_t = [];
16 var cbi_c = [];
17
18 var cbi_validators = {
19
20         'integer': function(v)
21         {
22                 return (v.match(/^-?[0-9]+$/) != null);
23         },
24
25         'uinteger': function(v)
26         {
27                 return (cbi_validators.integer(v) && (v >= 0));
28         },
29
30         'ipaddr': function(v)
31         {
32                 return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v);
33         },
34
35         'ip4addr': function(v)
36         {
37                 if( v.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)(\/(\d+))?$/) )
38                 {
39                         return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
40                                (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
41                                (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
42                                (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
43                                (!RegExp.$5 || ((RegExp.$6 >= 0) && (RegExp.$6 <= 32)))
44                         ;
45                 }
46
47                 return false;
48         },
49
50         'ip6addr': function(v)
51         {
52                 if( v.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
53                 {
54                         if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
55                         {
56                                 var addr = RegExp.$1;
57
58                                 if( addr == '::' )
59                                 {
60                                         return true;
61                                 }
62
63                                 if( addr.indexOf('.') > 0 )
64                                 {
65                                         var off = addr.lastIndexOf(':');
66
67                                         if( !(off && cbi_validators.ip4addr(addr.substr(off+1))) )
68                                                 return false;
69
70                                         addr = addr.substr(0, off) + ':0:0';
71                                 }
72
73                                 if( addr.indexOf('::') >= 0 )
74                                 {
75                                         var colons = 0;
76                                         var fill = '0';
77
78                                         for( var i = 1; i < (addr.length-1); i++ )
79                                                 if( addr.charAt(i) == ':' )
80                                                         colons++;
81
82                                         if( colons > 7 )
83                                                 return false;
84
85                                         for( var i = 0; i < (7 - colons); i++ )
86                                                 fill += ':0';
87
88                                         if (addr.match(/^(.*?)::(.*?)$/))
89                                                 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
90                                                        (RegExp.$2 ? ':' + RegExp.$2 : '');
91                                 }
92
93                                 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
94                         }
95                 }
96
97                 return false;
98         },
99
100         'port': function(v)
101         {
102                 return cbi_validators.integer(v) && (v >= 0) && (v <= 65535);
103         },
104
105         'portrange': function(v)
106         {
107                 if( v.match(/^(\d+)-(\d+)$/) )
108                 {
109                         var p1 = RegExp.$1;
110                         var p2 = RegExp.$2;
111
112                         return cbi_validators.port(p1) &&
113                                cbi_validators.port(p2) &&
114                                (parseInt(p1) <= parseInt(p2))
115                         ;
116                 }
117                 else
118                 {
119                         return cbi_validators.port(v);
120                 }
121         },
122
123         'macaddr': function(v)
124         {
125                 return (v.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
126         },
127
128         'host': function(v)
129         {
130                 return cbi_validators.hostname(v) || cbi_validators.ipaddr(v);
131         },
132
133         'hostname': function(v)
134         {
135                 return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null);
136         },
137
138         'wpakey': function(v)
139         {
140                 if( v.length == 64 )
141                         return (v.match(/^[a-fA-F0-9]{64}$/) != null);
142                 else
143                         return (v.length >= 8) && (v.length <= 63);
144         },
145
146         'wepkey': function(v)
147         {
148                 if( v.substr(0,2) == 's:' )
149                         v = v.substr(2);
150
151                 if( (v.length == 10) || (v.length == 26) )
152                         return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
153                 else
154                         return (v.length == 5) || (v.length == 13);
155         },
156
157         'uciname': function(v)
158         {
159                 return (v.match(/^[a-zA-Z0-9_]+$/) != null);
160         },
161
162         'range': function(v, args)
163         {
164                 var min = parseInt(args[0]);
165                 var max = parseInt(args[1]);
166                 var val = parseInt(v);
167
168                 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
169                         return ((val >= min) && (val <= max));
170
171                 return false;
172         }
173 };
174
175
176 function cbi_d_add(field, dep, next) {
177         var obj = document.getElementById(field);
178         if (obj) {
179                 var entry
180                 for (var i=0; i<cbi_d.length; i++) {
181                         if (cbi_d[i].id == field) {
182                                 entry = cbi_d[i];
183                                 break;
184                         }
185                 }
186                 if (!entry) {
187                         entry = {
188                                 "node": obj,
189                                 "id": field,
190                                 "parent": obj.parentNode.id,
191                                 "next": next,
192                                 "deps": []
193                         };
194                         cbi_d.unshift(entry);
195                 }
196                 entry.deps.push(dep)
197         }
198 }
199
200 function cbi_d_checkvalue(target, ref) {
201         var t = document.getElementById(target);
202         var value;
203
204         if (!t) {
205                 var tl = document.getElementsByName(target);
206
207                 if( tl.length > 0 && tl[0].type == 'radio' )
208                         for( var i = 0; i < tl.length; i++ )
209                                 if( tl[i].checked ) {
210                                         value = tl[i].value;
211                                         break;
212                                 }
213
214                 value = value ? value : "";
215         } else if (!t.value) {
216                 value = "";
217         } else {
218                 value = t.value;
219
220                 if (t.type == "checkbox") {
221                         value = t.checked ? value : "";
222                 }
223         }
224
225         return (value == ref)
226 }
227
228 function cbi_d_check(deps) {
229         var reverse;
230         var def = false;
231         for (var i=0; i<deps.length; i++) {
232                 var istat = true;
233                 reverse = false;
234                 for (var j in deps[i]) {
235                         if (j == "!reverse") {
236                                 reverse = true;
237                         } else if (j == "!default") {
238                                 def = true;
239                                 istat = false;
240                         } else {
241                                 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
242                         }
243                 }
244                 if (istat) {
245                         return !reverse;
246                 }
247         }
248         return def;
249 }
250
251 function cbi_d_update() {
252         var state = false;
253         for (var i=0; i<cbi_d.length; i++) {
254                 var entry = cbi_d[i];
255                 var next  = document.getElementById(entry.next)
256                 var node  = document.getElementById(entry.id)
257                 var parent = document.getElementById(entry.parent)
258
259                 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
260                         node.parentNode.removeChild(node);
261                         state = true;
262                         if( entry.parent )
263                                 cbi_c[entry.parent]--;
264                 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
265                         if (!next) {
266                                 parent.appendChild(entry.node);
267                         } else {
268                                 next.parentNode.insertBefore(entry.node, next);
269                         }
270                         state = true;
271                         if( entry.parent )
272                                 cbi_c[entry.parent]++;
273                 }
274         }
275
276         if (entry && entry.parent) {
277                 cbi_t_update();
278         }
279
280         if (state) {
281                 cbi_d_update();
282         }
283 }
284
285 function cbi_bind(obj, type, callback, mode) {
286         if (!obj.addEventListener) {
287                 obj.attachEvent('on' + type,
288                         function(){
289                                 var e = window.event;
290
291                                 if (!e.target && e.srcElement)
292                                         e.target = e.srcElement;
293
294                                 return !!callback(e);
295                         }
296                 );
297         } else {
298                 obj.addEventListener(type, callback, !!mode);
299         }
300         return obj;
301 }
302
303 function cbi_combobox(id, values, def, man) {
304         var selid = "cbi.combobox." + id;
305         if (document.getElementById(selid)) {
306                 return
307         }
308
309         var obj = document.getElementById(id)
310         var sel = document.createElement("select");
311                 sel.id = selid;
312                 sel.className = 'cbi-input-select';
313
314         if (obj.nextSibling) {
315                 obj.parentNode.insertBefore(sel, obj.nextSibling);
316         } else {
317                 obj.parentNode.appendChild(sel);
318         }
319
320         var dt = obj.getAttribute('cbi_datatype');
321         var op = obj.getAttribute('cbi_optional');
322
323         if (dt)
324                 cbi_validate_field(sel, op == 'true', dt);
325
326         if (!values[obj.value]) {
327                 if (obj.value == "") {
328                         var optdef = document.createElement("option");
329                         optdef.value = "";
330                         optdef.appendChild(document.createTextNode(def));
331                         sel.appendChild(optdef);
332                 } else {
333                         var opt = document.createElement("option");
334                         opt.value = obj.value;
335                         opt.selected = "selected";
336                         opt.appendChild(document.createTextNode(obj.value));
337                         sel.appendChild(opt);
338                 }
339         }
340
341         for (var i in values) {
342                 var opt = document.createElement("option");
343                 opt.value = i;
344
345                 if (obj.value == i) {
346                         opt.selected = "selected";
347                 }
348
349                 opt.appendChild(document.createTextNode(values[i]));
350                 sel.appendChild(opt);
351         }
352
353         var optman = document.createElement("option");
354         optman.value = "";
355         optman.appendChild(document.createTextNode(man));
356         sel.appendChild(optman);
357
358         obj.style.display = "none";
359
360         cbi_bind(sel, "change", function() {
361                 if (sel.selectedIndex == sel.options.length - 1) {
362                         obj.style.display = "inline";
363                         sel.parentNode.removeChild(sel);
364                         obj.focus();
365                 } else {
366                         obj.value = sel.options[sel.selectedIndex].value;
367                 }
368
369                 try {
370                         cbi_d_update();
371                 } catch (e) {
372                         //Do nothing
373                 }
374         })
375 }
376
377 function cbi_combobox_init(id, values, def, man) {
378         var obj = document.getElementById(id);
379         cbi_bind(obj, "blur", function() {
380                 cbi_combobox(id, values, def, man)
381         });
382         cbi_combobox(id, values, def, man);
383 }
384
385 function cbi_filebrowser(id, url, defpath) {
386         var field   = document.getElementById(id);
387         var browser = window.open(
388                 url + ( field.value || defpath || '' ) + '?field=' + id,
389                 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
390         );
391
392         browser.focus();
393 }
394
395 function cbi_dynlist_init(name)
396 {
397         function cbi_dynlist_renumber(e)
398         {
399                 var count = 1;
400                 var childs = e.parentNode.childNodes;
401
402                 for( var i = 0; i < childs.length; i++ )
403                         if( childs[i].name == name )
404                                 childs[i].id = name + '.' + (count++);
405
406                 e.focus();
407         }
408
409         function cbi_dynlist_keypress(ev)
410         {
411                 ev = ev ? ev : window.event;
412
413                 var se = ev.target ? ev.target : ev.srcElement;
414
415                 if (se.nodeType == 3)
416                         se = se.parentNode;
417
418                 switch (ev.keyCode)
419                 {
420                         /* backspace, delete */
421                         case 8:
422                         case 46:
423                                 if (se.value.length == 0)
424                                 {
425                                         if (ev.preventDefault)
426                                                 ev.preventDefault();
427
428                                         return false;
429                                 }
430
431                                 return true;
432
433                         /* enter, arrow up, arrow down */
434                         case 13:
435                         case 38:
436                         case 40:
437                                 if (ev.preventDefault)
438                                         ev.preventDefault();
439
440                                 return false;
441                 }
442
443                 return true;
444         }
445
446         function cbi_dynlist_keydown(ev)
447         {
448                 ev = ev ? ev : window.event;
449
450                 var se = ev.target ? ev.target : ev.srcElement;
451
452                 if (se.nodeType == 3)
453                         se = se.parentNode;
454
455                 var prev = se.previousSibling;
456                 while (prev && prev.name != name)
457                         prev = prev.previousSibling;
458
459                 var next = se.nextSibling;
460                 while (next && next.name != name)
461                         next = next.nextSibling;
462
463                 switch (ev.keyCode)
464                 {
465                         /* backspace, delete */
466                         case 8:
467                         case 46:
468                                 var jump = (ev.keyCode == 8)
469                                         ? (prev || next) : (next || prev);
470
471                                 if (se.value.length == 0 && jump)
472                                 {
473                                         se.parentNode.removeChild(se.nextSibling);
474                                         se.parentNode.removeChild(se);
475
476                                         cbi_dynlist_renumber(jump);
477
478                                         if (ev.preventDefault)
479                                                 ev.preventDefault();
480
481                                         return false;
482                                 }
483
484                                 break;
485
486                         /* enter */
487                         case 13:
488                                 var n = document.createElement('input');
489                                         n.name       = se.name;
490                                         n.type       = se.type;
491
492                                 cbi_bind(n, 'keydown',  cbi_dynlist_keydown);
493                                 cbi_bind(n, 'keypress', cbi_dynlist_keypress);
494
495                                 if (next)
496                                 {
497                                         se.parentNode.insertBefore(n, next);
498                                         se.parentNode.insertBefore(document.createElement('br'), next);
499                                 }
500                                 else
501                                 {
502                                         se.parentNode.appendChild(n);
503                                         se.parentNode.appendChild(document.createElement('br'));
504                                 }
505
506                                 var dt = se.getAttribute('cbi_datatype');
507                                 var op = se.getAttribute('cbi_optional') == 'true';
508
509                                 if (dt)
510                                         cbi_validate_field(n, op, dt);
511
512                                 cbi_dynlist_renumber(n);
513                                 break;
514
515                         /* arrow up */
516                         case 38:
517                                 if (prev)
518                                         prev.focus();
519
520                                 break;
521
522                         /* arrow down */
523                         case 40:
524                                 if (next)
525                                         next.focus();
526
527                                 break;
528                 }
529
530                 return true;
531         }
532
533         var inputs = document.getElementsByName(name);
534         for( var i = 0; i < inputs.length; i++ )
535         {
536                 cbi_bind(inputs[i], 'keydown',  cbi_dynlist_keydown);
537                 cbi_bind(inputs[i], 'keypress', cbi_dynlist_keypress);
538         }
539 }
540
541 //Hijacks the CBI form to send via XHR (requires Prototype)
542 function cbi_hijack_forms(layer, win, fail, load) {
543         var forms = layer.getElementsByTagName('form');
544         for (var i=0; i<forms.length; i++) {
545                 $(forms[i]).observe('submit', function(event) {
546                         // Prevent the form from also submitting the regular way
547                         event.stop();
548
549                         // Submit via XHR
550                         event.element().request({
551                                 onSuccess: win,
552                                 onFailure: fail
553                         });
554
555                         if (load) {
556                                 load();
557                         }
558                 });
559         }
560 }
561
562
563 function cbi_t_add(section, tab) {
564         var t = document.getElementById('tab.' + section + '.' + tab);
565         var c = document.getElementById('container.' + section + '.' + tab);
566
567         if( t && c ) {
568                 cbi_t[section] = (cbi_t[section] || [ ]);
569                 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
570         }
571 }
572
573 function cbi_t_switch(section, tab) {
574         if( cbi_t[section] && cbi_t[section][tab] ) {
575                 var o = cbi_t[section][tab];
576                 var h = document.getElementById('tab.' + section);
577                 for( var tid in cbi_t[section] ) {
578                         var o2 = cbi_t[section][tid];
579                         if( o.tab.id != o2.tab.id ) {
580                                 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
581                                 o2.container.style.display = 'none';
582                         }
583                         else {
584                                 if(h) h.value = tab;
585                                 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
586                                 o2.container.style.display = 'block';
587                         }
588                 }
589         }
590         return false
591 }
592
593 function cbi_t_update() {
594         var hl_tabs = [ ];
595
596         for( var sid in cbi_t )
597                 for( var tid in cbi_t[sid] )
598                         if( cbi_c[cbi_t[sid][tid].cid] == 0 ) {
599                                 cbi_t[sid][tid].tab.style.display = 'none';
600                         }
601                         else if( cbi_t[sid][tid].tab && cbi_t[sid][tid].tab.style.display == 'none' ) {
602                                 cbi_t[sid][tid].tab.style.display = '';
603
604                                 var t = cbi_t[sid][tid].tab;
605                                 t.className += ' cbi-tab-highlighted';
606                                 hl_tabs.push(t);
607                         }
608
609         if( hl_tabs.length > 0 )
610                 window.setTimeout(function() {
611                         for( var i = 0; i < hl_tabs.length; i++ )
612                                 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
613                 }, 750);
614 }
615
616
617 function cbi_validate_form(form, errmsg)
618 {
619         /* if triggered by a section removal or addition, don't validate */
620         if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
621                 return true;
622
623         if( form.cbi_validators )
624         {
625                 for( var i = 0; i < form.cbi_validators.length; i++ )
626                 {
627                         var validator = form.cbi_validators[i];
628                         if( !validator() && errmsg )
629                         {
630                                 alert(errmsg);
631                                 return false;
632                         }
633                 }
634         }
635
636         return true;
637 }
638
639 function cbi_validate_reset(form)
640 {
641         window.setTimeout(
642                 function() { cbi_validate_form(form, null) }, 100
643         );
644
645         return true;
646 }
647
648 function cbi_validate_field(cbid, optional, type)
649 {
650         var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
651         var vargs;
652
653         if( type.match(/^(\w+)\(([^\(\)]+)\)/) )
654         {
655                 type  = RegExp.$1;
656                 vargs = RegExp.$2.split(/\s*,\s*/);
657         }
658
659         var vldcb = cbi_validators[type];
660
661         if( field && vldcb )
662         {
663                 var validator = function()
664                 {
665                         // is not detached
666                         if( field.form )
667                         {
668                                 field.className = field.className.replace(/ cbi-input-invalid/g, '');
669
670                                 // validate value
671                                 var value = (field.options && field.options.selectedIndex > -1)
672                                         ? field.options[field.options.selectedIndex].value : field.value;
673
674                                 if( !(((value.length == 0) && optional) || vldcb(value, vargs)) )
675                                 {
676                                         // invalid
677                                         field.className += ' cbi-input-invalid';
678                                         return false;
679                                 }
680                         }
681
682                         return true;
683                 };
684
685                 if( ! field.form.cbi_validators )
686                         field.form.cbi_validators = [ ];
687
688                 field.form.cbi_validators.push(validator);
689
690                 cbi_bind(field, "blur",  validator);
691                 cbi_bind(field, "keyup", validator);
692
693                 if (field.nodeName == 'SELECT')
694                 {
695                         cbi_bind(field, "change", validator);
696                         cbi_bind(field, "click",  validator);
697                 }
698
699                 field.setAttribute("cbi_validate", validator);
700                 field.setAttribute("cbi_datatype", type);
701                 field.setAttribute("cbi_optional", (!!optional).toString());
702
703                 validator();
704
705                 var fcbox = document.getElementById('cbi.combobox.' + field.id);
706                 if (fcbox)
707                         cbi_validate_field(fcbox, optional, type);
708         }
709 }
710
711 if( ! String.serialize )
712         String.serialize = function(o)
713         {
714                 switch(typeof(o))
715                 {
716                         case 'object':
717                                 // null
718                                 if( o == null )
719                                 {
720                                         return 'null';
721                                 }
722
723                                 // array
724                                 else if( o.length )
725                                 {
726                                         var i, s = '';
727
728                                         for( var i = 0; i < o.length; i++ )
729                                                 s += (s ? ', ' : '') + String.serialize(o[i]);
730
731                                         return '[ ' + s + ' ]';
732                                 }
733
734                                 // object
735                                 else
736                                 {
737                                         var k, s = '';
738
739                                         for( k in o )
740                                                 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
741
742                                         return '{ ' + s + ' }';
743                                 }
744
745                                 break;
746
747                         case 'string':
748                                 // complex string
749                                 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
750                                         return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
751
752                                 // simple string
753                                 else
754                                         return '"' + o + '"';
755
756                                 break;
757
758                         default:
759                                 return o.toString();
760                 }
761         }
762
763
764 if( ! String.format )
765         String.format = function()
766         {
767                 if (!arguments || arguments.length < 1 || !RegExp)
768                         return;
769
770                 var html_esc = [/&/g, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
771                 var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
772
773                 function esc(s, r) {
774                         for( var i = 0; i < r.length; i += 2 )
775                                 s = s.replace(r[i], r[i+1]);
776                         return s;
777                 }
778
779                 var str = arguments[0];
780                 var out = '';
781                 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/;
782                 var a = b = [], numSubstitutions = 0, numMatches = 0;
783
784                 while( a = re.exec(str) )
785                 {
786                         var m = a[1];
787                         var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
788                         var pPrecision = a[6], pType = a[7];
789
790                         numMatches++;
791
792                         if (pType == '%')
793                         {
794                                 subst = '%';
795                         }
796                         else
797                         {
798                                 if (numSubstitutions++ < arguments.length)
799                                 {
800                                         var param = arguments[numSubstitutions];
801
802                                         var pad = '';
803                                         if (pPad && pPad.substr(0,1) == "'")
804                                                 pad = leftpart.substr(1,1);
805                                         else if (pPad)
806                                                 pad = pPad;
807
808                                         var justifyRight = true;
809                                         if (pJustify && pJustify === "-")
810                                                 justifyRight = false;
811
812                                         var minLength = -1;
813                                         if (pMinLength)
814                                                 minLength = parseInt(pMinLength);
815
816                                         var precision = -1;
817                                         if (pPrecision && pType == 'f')
818                                                 precision = parseInt(pPrecision.substring(1));
819
820                                         var subst = param;
821
822                                         switch(pType)
823                                         {
824                                                 case 'b':
825                                                         subst = (parseInt(param) || 0).toString(2);
826                                                         break;
827
828                                                 case 'c':
829                                                         subst = String.fromCharCode(parseInt(param) || 0);
830                                                         break;
831
832                                                 case 'd':
833                                                         subst = (parseInt(param) || 0);
834                                                         break;
835
836                                                 case 'u':
837                                                         subst = Math.abs(parseInt(param) || 0);
838                                                         break;
839
840                                                 case 'f':
841                                                         subst = (precision > -1)
842                                                                 ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision)
843                                                                 : (parseFloat(param) || 0.0);
844                                                         break;
845
846                                                 case 'o':
847                                                         subst = (parseInt(param) || 0).toString(8);
848                                                         break;
849
850                                                 case 's':
851                                                         subst = param;
852                                                         break;
853
854                                                 case 'x':
855                                                         subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
856                                                         break;
857
858                                                 case 'X':
859                                                         subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
860                                                         break;
861
862                                                 case 'h':
863                                                         subst = esc(param, html_esc);
864                                                         break;
865
866                                                 case 'q':
867                                                         subst = esc(param, quot_esc);
868                                                         break;
869
870                                                 case 'j':
871                                                         subst = String.serialize(param);
872                                                         break;
873                                         }
874                                 }
875                         }
876
877                         out += leftpart + subst;
878                         str = str.substr(m.length);
879                 }
880
881                 return out + str;
882         }