Correction du remplacement de texte
This commit is contained in:
parent
2763a32e91
commit
1ce1788801
|
@ -2,10 +2,10 @@ var ref=function(a,b){for(var d=null;null==d||a.hasOwnProperty(d);)d="$"+(104857
|
||||||
FormBuilder.regex={reg_in_key:/^\/\^(.+)\$\/$/,reg_out_val:/\{(\$[1-9])\}/,pri_out_val:/\{([a-z-]+)\}/g,pri_in_key:/^\$([a-z-]+)$/,arr_out_set:/^\{\{([a-z-]+)\}\}$/,arr_out_val:/\{([a-z-]+)\.([a-z-]+)\}/g,arr_in_key:/^\$\$([a-z-]+)$/,ref_pri:/^\$[a-f0-9]{6}\$$/};FormBuilder.spread_attr=["children","next_nodes","prev_nodes","attributes","node_link"];FormBuilder.allowed_attr="node node_type next_nodes prev_nodes attributes children text repeat".split(" ");
|
FormBuilder.regex={reg_in_key:/^\/\^(.+)\$\/$/,reg_out_val:/\{(\$[1-9])\}/,pri_out_val:/\{([a-z-]+)\}/g,pri_in_key:/^\$([a-z-]+)$/,arr_out_set:/^\{\{([a-z-]+)\}\}$/,arr_out_val:/\{([a-z-]+)\.([a-z-]+)\}/g,arr_in_key:/^\$\$([a-z-]+)$/,ref_pri:/^\$[a-f0-9]{6}\$$/};FormBuilder.spread_attr=["children","next_nodes","prev_nodes","attributes","node_link"];FormBuilder.allowed_attr="node node_type next_nodes prev_nodes attributes children text repeat".split(" ");
|
||||||
FormBuilder.prototype.add_definition=function(a){for(var b in a)this.defs_object[b]=a[b]};FormBuilder.prototype.build=function(a){a=null==a?{}:a;for(var b in a)a[b]=ref(this.ref_table,a[b]);this.built_form=JSON.parse(JSON.stringify(this.form_object));this.built_form=FormBuilder.formatFormObject(this.built_form,this.defs_object);this.built_form=FormBuilder.replaceStatements(this.built_form,a,this.defs_object,this.ref_table)};
|
FormBuilder.prototype.add_definition=function(a){for(var b in a)this.defs_object[b]=a[b]};FormBuilder.prototype.build=function(a){a=null==a?{}:a;for(var b in a)a[b]=ref(this.ref_table,a[b]);this.built_form=JSON.parse(JSON.stringify(this.form_object));this.built_form=FormBuilder.formatFormObject(this.built_form,this.defs_object);this.built_form=FormBuilder.replaceStatements(this.built_form,a,this.defs_object,this.ref_table)};
|
||||||
FormBuilder.prototype.attach=function(a){if(!(a instanceof Element)&&null==this.parent_element)return!1;this.parent_element=a instanceof Element?a:this.parent_element};FormBuilder.formatFormObject=function(a,b){if(a.hasOwnProperty("children"))for(var d in a.children)a.children[d].parent=a,FormBuilder.formatFormObject(a.children[d],b);return a};
|
FormBuilder.prototype.attach=function(a){if(!(a instanceof Element)&&null==this.parent_element)return!1;this.parent_element=a instanceof Element?a:this.parent_element};FormBuilder.formatFormObject=function(a,b){if(a.hasOwnProperty("children"))for(var d in a.children)a.children[d].parent=a,FormBuilder.formatFormObject(a.children[d],b);return a};
|
||||||
FormBuilder.fetchNodeDefinition=function(a,b){var d,e;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return{def:b[a]};for(d in b)if(r.test(d)&&(e=new RegExp(d.slice(1,-1)),e.test(a))){matches={};for(e=1;e<RegExp.length&&10>e;e++)matches["$"+e]=RegExp["$"+e];return{def:b[d],scope:matches}}return{}};
|
FormBuilder.fetchNodeDefinition=function(a,b){var d,f;r=FormBuilder.regex.reg_in_key;if("undefined"!=typeof b&&b.hasOwnProperty(a))return{def:b[a]};for(d in b)if(r.test(d)&&(f=new RegExp(d.slice(1,-1)),f.test(a))){matches={};for(f=1;f<RegExp.length&&10>f;f++)matches["$"+f]=RegExp["$"+f];return{def:b[d],scope:matches}}return{}};
|
||||||
FormBuilder.replaceStatements=function(a,b,d,e){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var c,h,f,g,n,k,l;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(c in a.scope)b[c]=a.scope[c];if(a.hasOwnProperty("node")&&"string"==typeof a.node&&(f=FormBuilder.fetchNodeDefinition(a.node,d),f.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(f.def)),f.hasOwnProperty("scope"))))for(c in f.scope)b[c]=f.scope[c];for(c in a)if("string"==typeof a[c])if(a[c]=
|
FormBuilder.replaceStatements=function(a,b,d,f){a=a instanceof Object?a:{};b=b instanceof Object?JSON.parse(JSON.stringify(b)):{};var c,h,e,g,k,l,n;if(a.hasOwnProperty("scope")&&a.scope instanceof Object)for(c in a.scope)b[c]=a.scope[c];if(a.hasOwnProperty("node")&&"string"==typeof a.node&&(e=FormBuilder.fetchNodeDefinition(a.node,d),e.hasOwnProperty("def")&&(a.node_link=JSON.parse(JSON.stringify(e.def)),e.hasOwnProperty("scope"))))for(c in e.scope)b[c]=e.scope[c];for(c in a)if("string"==typeof a[c])if(a[c]=
|
||||||
[a[c]],f=FormBuilder.replaceStatementsArray(a[c][0],b),!1!==f)a[c][0]=f;else{h=FormBuilder.regex.reg_out_val;g=-1;for(k=[];null!==(f=h.exec(a[c][0]))&&!(g>=h.lastIndex);)g=h.lastIndex,k.push(f[1]);for(f in k)b.hasOwnProperty(k[f])||(b[k[f]]=""),h=new RegExp("{\\$"+k[f][1]+"}","g"),a[c][0]=a[c][0].replace(h,b[k[f]]);if(!(0<k.length))for(n in a[c]=FormBuilder.replaceStatementsPrimary(a[c],b),h=FormBuilder.regex.arr_out_val,a[c])if(null!=a[c][n]){for(l=[];null!==(f=h.exec(a[c][n]));)l.push(f);g=0;k=
|
[a[c]],e=FormBuilder.replaceStatementsArray(a[c][0],b),!1!==e)a[c][0]=e;else{h=FormBuilder.regex.reg_out_val;g=-1;for(l=[];null!==(e=h.exec(a[c][0]))&&!(g>=h.lastIndex);)g=h.lastIndex,l.push(e[1]);for(e in l)b.hasOwnProperty(l[e])||(b[l[e]]=""),h=new RegExp("{\\$"+l[e][1]+"}","g"),a[c][0]=a[c][0].replace(h,b[l[e]]);if(!(0<l.length))for(k in a[c]=FormBuilder.replaceStatementsPrimary(a[c],b),h=FormBuilder.regex.arr_out_val,a[c])if(null!=a[c][k]){for(n=[];null!==(e=h.exec(a[c][k]));)n.push(e);g=0;l=
|
||||||
[];for(f in l)(0<g||0<l[f].index)&&k.push(a[c][n].substr(g,l[f].index)),b.hasOwnProperty(l[f][1])||(b[l[f][1]]=""),k.push(b[l[f][1]]),g=l[f].index+l[f][0].length;g<a[c][n].length&&k.push(a[c][n].substr(g,a[c][n].length));0<l.length&&(a[c]=a[c].slice(0,n).concat(k).concat(a[c].slice(n+1)))}}for(c in a)FormBuilder.regex.pri_in_key.test(c)?b[c.substring(1)]=ref(e,a[c]):FormBuilder.regex.arr_in_key.test(c)&&(b[c.substring(2)]=ref(e,a[c]));b=JSON.parse(JSON.stringify(b));for(c in a)if(-1<FormBuilder.spread_attr.indexOf(c))if(a[c]instanceof
|
[];for(e in n)(0<g||0<n[e].index)&&l.push(a[c][k].substr(g,n[e].index)),b.hasOwnProperty(n[e][1])||(b[n[e][1]]=""),l.push(b[n[e][1]]),g=n[e].index+n[e][0].length;g<a[c][k].length&&l.push(a[c][k].substr(g,a[c][k].length));0<n.length&&(a[c]=a[c].slice(0,k).concat(l).concat(a[c].slice(k+1)))}}for(c in a)FormBuilder.regex.pri_in_key.test(c)?b[c.substring(1)]=ref(f,a[c]):FormBuilder.regex.arr_in_key.test(c)&&(b[c.substring(2)]=ref(f,a[c]));b=JSON.parse(JSON.stringify(b));for(c in a)if(-1<FormBuilder.spread_attr.indexOf(c))if(a[c]instanceof
|
||||||
Array)for(var p in a[c])FormBuilder.replaceStatements(a[c][p],b,d,e);else a[c]instanceof Object&&FormBuilder.replaceStatements(a[c],b,d,e);return a};FormBuilder.replaceStatementsArray=function(a,b){var d=null,d=FormBuilder.regex.arr_out_set.exec(a);if(null===d)return!1;b.hasOwnProperty(d[1])||(b[d[1]]=[]);return b[d[1]]};
|
Array)for(var p in a[c])FormBuilder.replaceStatements(a[c][p],b,d,f);else a[c]instanceof Object&&FormBuilder.replaceStatements(a[c],b,d,f);return a};FormBuilder.replaceStatementsArray=function(a,b){var d=null,d=FormBuilder.regex.arr_out_set.exec(a);if(null===d)return!1;b.hasOwnProperty(d[1])||(b[d[1]]=[]);return b[d[1]]};
|
||||||
FormBuilder.replaceStatementsPrimary=function(a,b){var d=FormBuilder.regex.pri_out_val,e=null,c=[],h,f,g;for(g=0;g<a.length;g++){m=null;c=[];h=0;for(f=[];null!==(e=d.exec(a[g]));)c.push(e);for(e=0;e<c.length;e++)(0<h||0<c[e].index)&&f.push(a[g].substr(h,c[e].index)),b.hasOwnProperty(c[e][1])||(b[c[e][1]]=""),f.push(b[c[e][1]]),h=c[e].index+c[e][0].length;h<a[g].length&&f.push(a[g].substr(h,a[g].length));0<c.length&&(a=a.slice(0,g).concat(f).concat(a.slice(g+1)))}return a};
|
FormBuilder.replaceStatementsPrimary=function(a,b){var d=FormBuilder.regex.pri_out_val,f=null,c=[],h,e,g,k;for(g=0;g<a.length;g++){m=null;c=[];h=0;for(e=[];null!==(f=d.exec(a[g]));)c.push(f);for(f=0;f<c.length;f++)k=c[f][1],(0<h||0<c[f].index)&&e.push(a[g].substr(h,c[f].index-h)),b.hasOwnProperty(k)||(b[k]=""),e.push(b[k]),h=c[f].index+c[f][0].length;h<a[g].length&&e.push(a[g].substr(h,a[g].length));a=a.slice(0,g).concat(e).concat(a.slice(g+1))}return a};
|
||||||
FormBuilder.buildString=function(a,b){var d,e="";if("string"==typeof b&&FormBuilder.regex.ref_pri.test(b))return a[b];for(d in b)e=FormBuilder.regex.ref_pri.test(b[d])&&a.hasOwnProperty(b[d])?a[b[d]]instanceof Array?e+(""+FormBuilder.buildString(a,a[b[d]])):e+(""+a[b[d]]):e+(""+b[d].toString());return e};
|
FormBuilder.buildString=function(a,b){var d,f="";if("string"==typeof b&&FormBuilder.regex.ref_pri.test(b))return a[b];for(d in b)f=FormBuilder.regex.ref_pri.test(b[d])&&a.hasOwnProperty(b[d])?a[b[d]]instanceof Array?f+(""+FormBuilder.buildString(a,a[b[d]])):f+(""+a[b[d]]):f+(""+b[d].toString());return f};
|
||||||
|
|
|
@ -82,6 +82,7 @@ FormBuilder.regex = {
|
||||||
|
|
||||||
pri_out_val: /\{([a-z-]+)\}/g, // Regex associée à une variable primitif à remplacer
|
pri_out_val: /\{([a-z-]+)\}/g, // Regex associée à une variable primitif à remplacer
|
||||||
pri_in_key: /^\$([a-z-]+)$/, // Regex associée à la clé d'une variable primitive
|
pri_in_key: /^\$([a-z-]+)$/, // Regex associée à la clé d'une variable primitive
|
||||||
|
|
||||||
arr_out_set: /^\{\{([a-z-]+)\}\}$/, // Regex associée à un tableau à remplacer
|
arr_out_set: /^\{\{([a-z-]+)\}\}$/, // Regex associée à un tableau à remplacer
|
||||||
arr_out_val: /\{([a-z-]+)\.([a-z-]+)\}/g, // Regex associée à une valeur de tableau à remplacer (primitif)
|
arr_out_val: /\{([a-z-]+)\.([a-z-]+)\}/g, // Regex associée à une valeur de tableau à remplacer (primitif)
|
||||||
arr_in_key: /^\$\$([a-z-]+)$/, // Regex associée à la clé d'un tableau
|
arr_in_key: /^\$\$([a-z-]+)$/, // Regex associée à la clé d'un tableau
|
||||||
|
@ -565,7 +566,7 @@ FormBuilder.replaceStatementsPrimary = function(statements, scope){
|
||||||
var regex = FormBuilder.regex.pri_out_val;
|
var regex = FormBuilder.regex.pri_out_val;
|
||||||
var match = null;
|
var match = null;
|
||||||
var matches = [];
|
var matches = [];
|
||||||
var lasti, parts, p, i;
|
var lasti, parts, p, i, key;
|
||||||
|
|
||||||
|
|
||||||
/* [2] Pour chaque partie de la chaine
|
/* [2] Pour chaque partie de la chaine
|
||||||
|
@ -573,8 +574,10 @@ FormBuilder.replaceStatementsPrimary = function(statements, scope){
|
||||||
for( p = 0 ; p < statements.length ; p++ ){
|
for( p = 0 ; p < statements.length ; p++ ){
|
||||||
|
|
||||||
/* (1) Initialisation */
|
/* (1) Initialisation */
|
||||||
m = null, matches = [],
|
m = null;
|
||||||
lasti = 0, parts = [];
|
matches = [];
|
||||||
|
lasti = 0;
|
||||||
|
parts = [];
|
||||||
|
|
||||||
/* (2) Tant que ça match, on récupère les infos du match */
|
/* (2) Tant que ça match, on récupère les infos du match */
|
||||||
while( (match=regex.exec(statements[p])) !== null )
|
while( (match=regex.exec(statements[p])) !== null )
|
||||||
|
@ -585,16 +588,18 @@ FormBuilder.replaceStatementsPrimary = function(statements, scope){
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
for( i = 0 ; i < matches.length ; i++ ){
|
for( i = 0 ; i < matches.length ; i++ ){
|
||||||
|
|
||||||
|
key = matches[i][1];
|
||||||
|
|
||||||
/* (1) On met la chaine d'avant le match (si existe) */
|
/* (1) On met la chaine d'avant le match (si existe) */
|
||||||
if( lasti > 0 || matches[i].index > 0 )
|
if( lasti > 0 || matches[i].index > 0 )
|
||||||
parts.push( statements[p].substr(lasti, matches[i].index) );
|
parts.push( statements[p].substr(lasti, matches[i].index-lasti) );
|
||||||
|
|
||||||
/* (2) Si la var n'est pas dans le scope, on l'initialise vide */
|
/* (2) Si la var n'est pas dans le scope, on l'initialise vide */
|
||||||
if( !scope.hasOwnProperty(matches[i][1]) )
|
if( !scope.hasOwnProperty(key) )
|
||||||
scope[matches[i][1]] = ''; // on met une chaine vide
|
scope[key] = ''; // on met une chaine vide
|
||||||
|
|
||||||
/* (3) On insère la valeur du scope */
|
/* (3) On insère la valeur du scope */
|
||||||
parts.push( scope[matches[i][1]] );
|
parts.push( scope[key] );
|
||||||
|
|
||||||
/* (4) On met à jour l'index de fin pour la suite */
|
/* (4) On met à jour l'index de fin pour la suite */
|
||||||
lasti = matches[i].index + matches[i][0].length;
|
lasti = matches[i].index + matches[i][0].length;
|
||||||
|
@ -606,9 +611,8 @@ FormBuilder.replaceStatementsPrimary = function(statements, scope){
|
||||||
parts.push( statements[p].substr(lasti, statements[p].length) );
|
parts.push( statements[p].substr(lasti, statements[p].length) );
|
||||||
|
|
||||||
|
|
||||||
/* [4] Si on a remplacé qqch, on remplace statements[p] par sa décomposition
|
/* [4] on remplace statements[p] par sa décomposition
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
if( matches.length > 0 )
|
|
||||||
statements = statements.slice(0, p).concat(parts).concat(statements.slice(p+1));
|
statements = statements.slice(0, p).concat(parts).concat(statements.slice(p+1));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue