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