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