-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregexer.min.js
22 lines (22 loc) · 7.28 KB
/
regexer.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @package RegExer
* @version 1.2
*
* @author m13p4
* @copyright Pavel Meliantchenkov
*/
var RegExer=function(f){function a(r,d,a,b){r=document.createElement(r);b=b||[];k(r,d);a&&(r.innerHTML=a);for(d=0;d<b.length;d++)r.appendChild(b[d]);return r}function k(r,d,a){var b=Object.prototype.toString.call(d),h="[object Object]"===b?d:{},g;"[object String]"===b&&"[object String]"===Object.prototype.toString.call(a)&&(h[d]=a);for(g in h)r.setAttribute(g,h[g])}function c(r,d){for(var a in d)r.addEventListener(a,d[a])}function e(){B?(CSSC("#"+v.getAttribute("id")).set("height",t.offsetHeight+
"px"),CSSC("#"+F.getAttribute("id")).set("height",t.offsetHeight+"px")):(v.style.height=t.offsetHeight+"px",F.style.height=t.offsetHeight+"px")}function b(r){if(l.value){try{u=new RegExp(l.value,(q?"i":"")+(m?"g":"")+(w?"m":""));g=new RegExpGrpPos(u,!0);for(var d=g.match(v.value),a=new formatedString(v.value),b,h,c=0;c<d.length;c++){b=d[c];h=c%2?"b":"a";a.addOpt(b[0][1],'<span class="f '+h+" n"+c+'">');a.addOpt(b[0][1]+b[0][0].length,"</span>");for(var e=1;e<b.length;e++)a.addOpt(b[e][1],'<span class="g '+
h+" n"+e+'">'),a.addOpt(b[e][1]+b[e][0].length,"</span>")}t.innerHTML="<span>"+a.getFormText(!0)+" </span>";B?CSSC("textarea#"+l.getAttribute("id")).set("border-color","#ccc"):l.style.borderColor="#ccc";x.innerHTML=encode(l.value)+" ";var p=l.value;n=[[0,p.length]];z=[0];d=0;h=b=!1;for(var f=0;f<p.length;f++){var k=p[f];if("\\"!==p[f-1])h=!0;else if("("===k||")"===k){h=!1;for(var y=f-2;0<=y;y--)if("\\"===p[y])h=!h;else break}if(h&&"("===k)n.push([f,null]),d=n.length-1;else if(h&&")"===k)for(b=
!1,d=0,y=n.length-1;0<=y;y--)if(null===n[y][1])if(b){d=y;break}else n[y][1]=f,b=!0;z[f+1]=d}"replace"===G&&(F.value=v.value.replace(u,J.value));if(B){a=new formatedString(l.value);p={};for(c=1;c<n.length;c++)for(e=0;e<n[c].length;e++)p[n[c][e]]?p[n[c][e]].push(c):p[n[c][e]]=[c];for(c in p)for(e=0;e<p[c].length;e++)a.addOpt(c,'<span class="hg_cssc gn'+p[c][e]+'">'),a.addOpt(parseInt(c)+1,"</span>");x.innerHTML=a.getFormText(!0)+" "}K=!1}catch(O){console.log(O),B?CSSC("textarea#"+l.getAttribute("id")).set("border-color",
"#f00"):l.style.borderColor="#f00",K=!0}r&&(t.scrollTop=this.scrollTop)}}function h(){if(!K){var a=l.selectionStart;if(B){for(var d=1;d<=n.length;d++)CSSC(".g.n"+d).set("background-color","rgba(0, 0, 0, 0)"),CSSC(".hg_cssc.gn"+d).set("background-color","transparent");CSSC(".g.n"+z[a]).set("background-color","rgba(0, 0, 0, 0.2)");CSSC(".hg_cssc.gn"+z[a]).set("background-color","#ffecb3")}else{var b=t.getElementsByClassName("g");for(d=0;d<b.length;d++)b[d].classList.remove("s");if(z[a]){b=t.getElementsByClassName("g n"+
z[a]);for(d=0;d<b.length;d++)b[d].classList.add("s");(a=0<z[a]?n[z[a]]:null)&&A!==a?(d=new formatedString(l.value),d.addOpt(a[0],'<span class="hg">'),d.addOpt(a[0]+1,"</span>"),d.addOpt(a[1],'<span class="hg">'),d.addOpt(a[1]+1,"</span>"),x.innerHTML=d.getFormText(!0)+" ",A=a):A!==a&&(x.innerHTML=l.value+" ",A=a)}else null!==A&&(x.innerHTML=encode(l.value)+" ",A=null)}}}function p(a){if("match"===a||"replace"===a){k(H,{class:""});k(I,{class:""});var d=I;"match"===a&&(d=H);d.classList.add("sel");
k(M,{class:a});k(N,{class:a});e();G=a;b()}}var u,g,q=!1,m=!0,w=!1,n=[],z=[],B="undefined"!==typeof CSSC,K=!1,G="match",A=null;this.version="1.2";this.setRegEx=function(a){var d=a;a instanceof RegExp&&(d=a.source,w=!!a.multiline,m=!!a.global,q=!!a.ignoreCase,C.checked=q,D.checked=m,E.checked=w);l.innerHTML=d;b()};this.setText=function(a){v.innerHTML=a;b()};f=f?"[object String]"===Object.prototype.toString.call(f)?document.getElementById(f):f:document.body;var l=a("textarea",{id:"regexer_input_input"});
var x=a("pre",{id:"regexer_input_pre"});var J=a("input",{id:"regexer_input_replace",type:"text"});var M=a("div",{id:"regexer_input"},!1,[l,x,J]);var v=a("textarea",{id:"regexer_text_txt"});var t=a("pre",{id:"regexer_text_pre"});var F=a("textarea",{id:"regexer_replace_txt",readonly:"readonly"});var N=a("div",{id:"regexer_text"},!1,[v,t,F]);var H=a("a",{id:"regex_controll_match",class:"match"===G?"sel":""},"match");var I=a("a",{id:"regex_controll_replace",class:"replace"===G?"sel":""},"replace");var C=
a("input",{id:"regex_controll_modifier_i",type:"checkbox",title:"Perform case-insensitive matching"});var D=a("input",{id:"regex_controll_modifier_g",type:"checkbox",title:"Perform a global match (find all matches rather than stopping after the first match)"});var E=a("input",{id:"regex_controll_modifier_m",type:"checkbox",title:"Perform multiline matching"});var L=a("div",{id:"regex_controll_modifier"},"Modifier: ",[C,a("label",{for:"regex_controll_modifier_i",title:"Perform case-insensitive matching"},
"I"),a("span",{}," "),D,a("label",{for:"regex_controll_modifier_g",title:"Perform a global match (find all matches rather than stopping after the first match)"},"G"),a("span",{}," "),E,a("label",{for:"regex_controll_modifier_m",title:"Perform multiline matching"},"M"),a("span",{}," ")]);C.checked=q;D.checked=m;E.checked=w;L=a("div",{id:"regex_controll"},!1,[H,I,L]);f.appendChild(L);f.appendChild(M);f.appendChild(N);e();(function(){var a=function(a){var d=!1;0!==a.keyCode&&
(40>=a.keyCode&&13!==a.keyCode&&8!==a.keyCode&&9!==a.keyCode&&32!==a.keyCode||112<=a.keyCode&&150>=a.keyCode||91===a.keyCode||92===a.keyCode)||(b(),d=!0);h();return d};c(v,{scroll:function(){t.scrollTop=this.scrollTop},change:function(){t.innerHTML="<span>"+encode(this.value)+"</span>\n";t.scrollTop=this.scrollTop;b()},keyup:function(b){a(b)}});c(l,{scroll:function(){x.scrollTop=this.scrollTop},change:function(){x.scrollTop=this.scrollTop},keyup:function(b){x.scrollTop=this.scrollTop;a(b)},click:function(a){h()}});
c(J,{keyup:function(b){a(b)}});c(I,{click:function(){p("replace")}});c(H,{click:function(){p("match")}});c(C,{change:function(){q=C.checked;b()}});c(D,{change:function(){m=D.checked;b()}});c(E,{change:function(){w=E.checked;b()}});c(window,{resize:function(){e()}})})();(l&&l.innerHTML||v&&v.innerHTML)&&b()},RegExpGrpPos=function(f,a){var k=null,c=!!a,e=function(a){var b=[],e=0,f=a.index;b[0]=[a[0],c?f:e];for(var g=1;g<a.length;g++){var q=k[g-1]?k[g-1][0]:0;var m=k[g-2]?k[g-2][0]:0;q>m?e=c?b[g-1][1]-
f:b[g-1][1]:q<m&&(e=c?b[g-1][1]+b[g-1][0].length-f:b[g-1][1]+b[g-1][0].length);a[g]?(e=a[0].indexOf(a[g],e),b[g]=[a[g],c?e+f:e],e+=a[g].length):b[g]=["",c?e+f:e]}return b};this.match=function(a){for(var b=f.toString(),c=[],u=0,g,q,m=0;m<b.length;m++)if(g=b[m],"("===g&&"\\"!==b[m-1])u++,c.push([u,m]);else if(")"===g&&"\\"!==b[m-1])u--;else if(("("===g||")"===g)&&"\\"===b[m-1]){q=!1;for(var w=m-2;-1<w;w--)if("\\"===b[w])q=!q;else break;q&&"("===g?(u++,c.push([u,m])):q&&")"===g&&u--}k=0===u?c:!1;var n;
b=null;for(c=[];null!==(n=f.exec(a))&&b!==n.index;)c.push(e(n)),b=n.index;return c};this.setPositionAbsolute=function(a){c=!!a}},formatedString=function(f){var a={},k=function(a){return a.replace(/[\x26\x0A\x3c\x3e\x22\x27]/g,function(a){return"&#"+a.charCodeAt(0)+";"})};this.addOpt=function(c,e){a[c]||(a[c]=[]);a[c].push(e)};this.getOpts=function(){return a};this.getOrgText=function(){return f};this.getFormText=function(c){var e=0,b="",h;for(h in a){b=c?b+k(f.substr(e,h-e)):b+f.substr(e,h-e);for(e=
0;e<a[h].length;e++)b+=a[h][e];e=h}return b=c?b+k(f.substr(e)):b+f.substr(e)}},encode=function(f){return f.replace(/[\x26\x0A\x3c\x3e\x22\x27]/g,function(a){return"&#"+a.charCodeAt(0)+";"})};