forked from CJex/regulex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
412 lines (374 loc) · 13.8 KB
/
index.html
File metadata and controls
412 lines (374 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Regulex:JavaScript Regular Expression Visualizer.</title>
<style>
body,html * {margin:0;padding:0;font-family:sans-serif;}
body {background: #303030;min-width: 700px;}
.code,.code * {font-family: "DejaVu Sans Mono",monospace;}
.header{margin:14px 0;padding:0 16px;padding-top:40px;padding-bottom: 20px;border-bottom:2px dashed grey;min-height:60px;}
.header h1 {font-size:3em;color:#40C0FF;}
.header h1 em{color:#BBE0E0;font-size:small;padding-left:2em;font-style: normal;font-weight:normal;display:inline-block;}
#inputCt,#errorBox {margin:8px 16px;}
#inputCt div.re {background:#EEE;font-size:1.2em;line-height: 1.4em;color:#333;border:1px solid black;padding:4px;font-weight: bold;word-break:break-all;word-wrap:break-word;}
#inputCt div.re table {border:none;padding:0;margin:0;width:100%;}
#inputCt div.re .input {color:#3030C0;padding:0 2px;border:none;background:#EEE;font-size:1.2em;line-height: 1.4em;height:1.4em;font-weight: bold;word-break:break-all;word-wrap:break-word;margin:0;width:auto;}
#inputCt div.re #input {display:table;width:100%;}
#inputCt label {color:white;cursor:pointer;display:inline-block;}
#inputCt label input {margin-right:4px;}
#errorBox {background:#EEE;font-size:1.2em;line-height: 1.4em;border:1px solid black;padding:4px;color:darkred;white-space: pre;word-wrap:normal;word-break:keep-all;display: none;overflow:auto;}
#visualIt,#embedIt,#exportIt {font-size: 16px;line-height: 16px;display: inline-block;margin: 10px;margin-left:0;padding:12px;padding-bottom:10px;color:#EEF;border:none;cursor:pointer;background: #40B0EF;background-image: linear-gradient(to bottom, #40B0EF,#3060A0);border-radius: 12px;text-decoration: none;}
#visualIt:hover,#embedIt:hover,#exportIt:hover {color:#FEF;background: #3CB0FD;background-image: linear-gradient(to bottom, #3CB0FD, #3498DF);text-decoration: none;}
#graphCt {padding:4px;margin:0px 16px;border:1px solid black;background: #EEE;overflow: auto;cursor:move;-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;}
#graphCt svg {display:block;margin:0 auto;}
footer {text-align: center;border-top:1px solid grey;margin:1em auto;padding:10px;font-size:1.2em;}
footer,footer a {color:#EEF;}
body.embed {background: #EEE;}
body.embed,body.embed #graphCt {margin:0;padding:0;border:none;}
body.embed h1,body.embed #inputCt,body.embed #github {display:none !important;}
body.embed footer {display:none;}
body.embed div.footer,body.embed div.footer a {color:white;font-size: 14px;}
body.embed div.footer {display:block;width:auto;margin:0;padding:2px 10px;background:rgb(75,0,130);opacity: 0.9;position:fixed;right:0;bottom:0;}
body.embed{height:100%;}
body.embed #graphCt {height: 100vh;}
#testReg{border:1px solid #ddd;padding:3px 5px;height:30px;font-size:16px;width:400px;float:right;margin-top:-80px;opacity: 0.9;}
#testReg:focus{outline: none;}
</style>
</head>
<body>
<script>
var params=getParams();
if (params.embed) {
document.body.className+= " embed";
}
function trim(s) {
return s.replace(/^\s+/,'').replace(/\s+$/,'');
}
function getParams() {
var params=location.hash;
if (!params || params.length<2) {
params={embed:false,re:"",highlight:true,flags:''};
} else {
params=params.slice(2);
params=params.split("&").reduce(function (p,a) {
a=a.split("=");
p[a[0]]=a[1];
return p;
},{});
params.embed=params.embed==='true';
params.flags=params.flags || '';
params.re=params.re?trim(decodeURIComponent(params.re)):'';
}
return params;
}
</script>
<div class="header">
<h1>Regulex <em>JavaScript Regular Expression Visualizer.</em></h1>
<input id="testReg" type="text" placeholder="测验框,可以进行数据测试">
</div>
<div id="inputCt">
<div class="re code">
<table>
<tr>
<td style="width:1em">/</td>
<td style="width:auto">
<input id="input" class="input" value="^(a|b)*?$" />
</td>
<td style="width:1em">/</td>
<td style="width:3em" id="flagBox"></td>
</tr>
</table>
</div>
<button id="visualIt">Visualize</button>
<button id="exportIt">Export Image</button>
<button id="embedIt">Embed On My Site!</button>
<label><input type="checkbox" name="flag" value="i" />IgnoreCase</label>
<label><input type="checkbox" name="flag" value="m" />Multiline</label>
<label><input type="checkbox" name="flag" value="g" />GlobalMatch</label>
</div>
<p id="errorBox" class="code">Error Message</p>
<div id="graphCt" class="code" style="position:relative;"><a id="github" href="https://github.com/JexCheng/regulex" target="_blank" style="display:inline-block;width:149px;height:149px;position: absolute; top: 0; right: 0; border: 0;"><img style="width:149px;height:149px;position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a></div>
<!--script src="http://cdn.staticfile.org/require.js/2.1.15/require.min.js"></script-->
<!--script src="http://libs.useso.com/js/require.js/2.1.11/require.min.js"></script-->
<script type="text/javascript" charset="utf-8">
params.debug=true;
/*
//var raphael="http://cdn.staticfile.org/raphael/2.1.2/raphael-min.js";
var raphael="http://libs.useso.com/js/raphael/2.1.2/raphael-min.js";
*/
function $(id) {return document.getElementById(id)}
function $$(q) {return document.querySelector(q)}
var raphael='src/libs/raphael';
var visualize='src/visualize';
var parse='src/parse';
var Kit='src/Kit';
if (params.debug) {
document.write('<script src="src/libs/require.js" charset="utf-8"><'+'/script>');
window.addEventListener('DOMContentLoaded',function () {
require([raphael,visualize,parse,Kit],init);
});
} else {
document.write('<script src="dest/regulex.js" charset="utf-8"><'+'/script>');
window.addEventListener('DOMContentLoaded',function () {
raphael=require('regulex').Raphael;
parse = require('regulex').parse;
visualize = require('regulex').visualize;
Kit=require('regulex').Kit;
init(raphael,visualize,parse,Kit);
});
}
function init(R,visualize,parse,K) {
var paper = R('graphCt', 10, 10);
var input=$('input');
var inputCt=$('inputCt');
var visualBtn=$('visualIt');
var embedBtn=$('embedIt');
var exportBtn=$('exportIt');
var errorBox=$('errorBox');
var flags=document.getElementsByName('flag');
var flagBox=$('flagBox');
var getInputValue=function () {
return trim(input.value);
};
var setInputValue=function (v) {
return trim(v);
};
if (params.flags) {
setFlags(params.flags);
}
if (params.re) {
setInputValue(params.re);
}
initListeners();
dragGraph($('graphCt'));
visualIt();
function visualIt(skipError) {
var re=getInputValue();
changeHash();
hideError();
var ret;
try {ret=parse(re)}
catch (e) {
if (e instanceof parse.RegexSyntaxError) {
if (!skipError) {
showError(re,e);
}
} else throw e;
return false;
}
visualize(ret,getFlags(),paper);
return true;
}
function hideError() {
errorBox.style.display='none';
}
function showError(re,err) {
errorBox.style.display='block';
var msg=["Error:"+err.message,""];
if (typeof err.lastIndex==='number') {
msg.push(re);
msg.push(K.repeats('-',err.lastIndex)+"^");
}
setInnerText(errorBox,msg.join("\n"));
}
function changeHash() {
var re=getInputValue();
var flags=getFlags();
location.hash="#!embed=false&flags="+flags+"&re="+encodeURIComponent(params.re=re);
}
function initListeners() {
var LF='\n'.charCodeAt(0),CR='\r'.charCodeAt(0);
input.addEventListener('keydown',onEnter);
input.addEventListener('keyup',onKeyup);
input.addEventListener('paste',function (evt) {
var content=trim(evt.clipboardData.getData('text'));
if (content[0]==='/' && /\/[img]*$/.test(content)) {
evt.preventDefault();
var endIndex=content.lastIndexOf('/');
setFlags(content.slice(endIndex+1));
content=content.slice(1,endIndex);
setInputValue(content);
}
setTimeout(visualIt,50);
});
visualBtn.addEventListener('click',function () {
visualIt();
});
embedBtn.addEventListener('click',function () {
if (!visualIt()) return false;
var src=location.href;
var i=src.indexOf('#');
src=i>0?src.slice(0,i):src;
changeHash();
var re=getInputValue();
var html='<iframe frameborder="0" width="'+Math.ceil(paper.width)+'" height="'+Math.ceil(paper.height)+'" src="'+src+'#!embed=true&flags='+getFlags()+'&re='+encodeURIComponent(re)+'"></iframe>'
window.prompt("Copy the html code:",html);
});
exportBtn.addEventListener('click',function () {
exportImage();
});
/*
window.addEventListener('hashchange',function () {
if (manualHash) return;
var p=getParams();
if (p.re && p.re!==params.re) {
params.re=p.re;
setInputValue(p.re);
visualIt();
}
});*/
for (var i=0,l=flags.length;i<l;i++) {
flags[i].addEventListener('change',onChangeFlags);
}
function onChangeFlags(e) {
setInnerText(flagBox,getFlags());
visualIt();
changeHash();
}
var onKeyupTid;
function onKeyup(e) {
if (e.keyCode===LF || e.keyCode===CR) {
return true;
}
clearTimeout(onKeyupTid);
onKeyupTid=setTimeout(function () {
var skipError=true;
visualIt(skipError);
},100);
}
function onEnter(e) {
if (e.keyCode===LF || e.keyCode===CR) {
e.preventDefault();
e.stopPropagation();
visualIt();
}
}
}
function getFlags() {
var fg='';
for (var i=0,l=flags.length;i<l;i++) {
if (flags[i].checked) fg+=flags[i].value;
}
return fg;
}
function setFlags(fg) {
for (var i=0,l=fg.length;i<l;i++) {
if (~fg.indexOf(flags[i].value)) flags[i].checked=true;
else flags[i].checked=false;
}
setInnerText(flagBox,fg);
}
function exportImage() {
svg = graphCt.getElementsByTagName('svg')[0];
var canvas = document.createElement( "canvas" );
var ctx = canvas.getContext( "2d" );
var img = new Image;
img.setAttribute('src',svgDataURL(svg));
canvas.setAttribute('width',svg.clientWidth || parseInt(getComputedStyle(svg).width));
canvas.setAttribute('height',svg.clientHeight || parseInt(getComputedStyle(svg).height));
img.onload=function () {
ctx.drawImage( img, 0, 0 );
location.href=canvas.toDataURL( "image/png" );
};
}
function svgDataURL(svg) {
var svgAsXML = (new XMLSerializer).serializeToString(svg);
return "data:image/svg+xml," + encodeURIComponent(svgAsXML);
}
function dragGraph(g) {
g.addEventListener('mousedown',startMove);
function startMove(e) {
clearSelect();
var x=e.clientX,y=e.clientY;
g.addEventListener('mousemove',onMove);
document.addEventListener('mouseup',unbind,true);
window.addEventListener('mouseup',unbind,true);
function unbind(e) {
g.removeEventListener('mousemove',onMove);
document.removeEventListener('mouseup',unbind,true);
window.removeEventListener('mouseup',unbind,true);
}
function onMove(e) {
var dx=x-e.clientX,dy=y-e.clientY;
if (dx>0 && g.scrollWidth-g.scrollLeft-g.clientWidth<2
|| dx<0 && g.scrollLeft<1) {
document.documentElement.scrollLeft+=dx;
document.body.scrollLeft+=dx;
} else {
g.scrollLeft+=dx;
}
if (dy>0 && g.scrollHeight-g.scrollTop-g.clientHeight<2
|| dy<0 && g.scrollTop<1) {
document.documentElement.scrollTop+=dy;
document.body.scrollTop+=dy;
} else {
g.scrollTop+=dy;
}
x=e.clientX;
y=e.clientY;
}
}
}
function getInnerText(ele) {
if (!ele) return '';
var node=ele.firstChild,results=[];
if (!node) return '';
do {
if (node.nodeType===document.TEXT_NODE) results.push(node.nodeValue);
else results.push(getInnerText(node));
} while (node=node.nextSibling);
return results.join('');
}
function setInnerText(ele,s) {
ele.innerHTML='';
var t=document.createTextNode('');
t.nodeValue=s;
ele.appendChild(t);
return s;
}
function clearSelect() {
if (window.getSelection) {
if (window.getSelection().empty) { // Chrome
window.getSelection().empty();
} else if (window.getSelection().removeAllRanges) { // Firefox
window.getSelection().removeAllRanges();
}
} else if (document.selection) { // IE
document.selection.empty();
}
}
}
</script>
<script>
(function(){
//测验框
var testInput = document.getElementById('testReg');
var regInput = document.getElementById('input');
testInput.onkeyup = testFn;
regInput.onkeyup = testFn;
function testFn(){
var regText = regInput.value;
var reg = new RegExp(regText);
if(testInput.value){
if(reg.test(testInput.value)){
testInput.style.background = '#00CC00'
testInput.style.color = '#fff'
}else{
testInput.style.background = '#FC0505'
testInput.style.color = '#fff'
}
}else{
testInput.style.background = ''
}
}
})();
</script>
<footer>Created by <a href="http://jex.im/" target="_blank">Jex</a>.</footer>
<script>
if (params.embed) {
document.write('<div class="footer"><a href="'+location.href+'&embed=false" target="_blank">Generated by Regulex.</a></div>');
}
</script>
</body>
</html>