-
Notifications
You must be signed in to change notification settings - Fork 228
/
test.html
236 lines (226 loc) · 8.05 KB
/
test.html
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
<!doctype html>
<html>
<head>
<title>文本比较工具</title>
<style type="text/css">
.text textarea {
resize: none;
background: none repeat scroll 0 0 transparent;
border: 0 none;
width: 97%;
height: 500px;
overflow-y: scroll;
position: absolute;
left: 0;
top: 0;
z-index: 2;
font-size: 18px;
white-space: pre-wrap;
word-wrap: break-word;
word-break: break-all;
border: 1px solid red;
}
.text {
overflow-y: scroll;
white-space: pre-wrap;
word-wrap: break-word;
word-break: break-all;
width: 50%;
float: left;
text-align: left;
z-index: 1;
font-size: 18px;
position: relative;
min-height: 510px;
}
.add {
color: red;
}
.delete {
color: blue;
text-decoration: line-through;
}
.button {
width: 100%;
line-height: 30px;
font-size: 30px;
border: 1px solid green;
padding: 10px;
cursor: pointer;
text-align: center;
}
#result-box {
padding: 20px;
border: 1px solid blue;
}
.compare-box:after{
content: "";
display:block;
clear: both;
}
#result{
font-size: 20px;
}
</style>
</head>
<body>
<div id="result-box">
<pre id="result"></pre>
</div>
<div class="compare-box">
<div class="text left-box">
<textarea id="edit_textarea_1"></textarea>
</div>
<div class="text right-box">
<textarea id="edit_textarea_2"></textarea>
</div>
</div>
<div class="button">比较</div>
<script type="text/javascript">
function textchange() {
var op = eq({ value1: document.getElementById("edit_textarea_1").value, value2: document.getElementById("edit_textarea_2").value });
document.getElementById("result").innerHTML = op.value2 + "\r\n";
}
function eq(op) {
if (!op) {
return op;
}
if (!op.eq_min) {
op.eq_min = 3;
}
if (!op.eq_index) {
op.eq_index = 5;
}
if (!op.value1 || !op.value2) {
return op;
}
var ps = {
v1_i: 0,
v1_new_value: "",
v2_i: 0,
v2_new_value: ""
};
while (ps.v1_i < op.value1.length && ps.v2_i < op.value2.length) {
if (op.value1[ps.v1_i] == op.value2[ps.v2_i]) {
//ps.v1_new_value += op.value1[ps.v1_i].replace(/</g,"<").replace(">",">");
ps.v2_new_value += op.value2[ps.v2_i].replace(/</g, "<").replace(">", ">");
ps.v1_i += 1;
ps.v2_i += 1;
//值2增加的部分
if (ps.v1_i >= op.value1.length) {
ps.v2_new_value += "<span class='add'>" + op.value2.substr(ps.v2_i).replace(/</g, "<").replace(">", ">") + "</span>";
break;
}
//值1删除的部分
if (ps.v2_i >= op.value2.length) {
//ps.v1_new_value += "<span style='" + op.value1_style + "'>值1多的" + op.value1.substr(ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>";
ps.v2_new_value += "<span class='delete'>" + op.value1.substr(ps.v1_i).replace(/</g, "<").replace(">", ">") + "</span>";
break;
}
} else {
ps.v1_index = ps.v1_i + 1;
ps.v1_eq_length = 0;
ps.v1_eq_max = 0;
ps.v1_start = ps.v1_i + 1;
while (ps.v1_index < op.value1.length) {
if (op.value1[ps.v1_index] == op.value2[ps.v2_i + ps.v1_eq_length]) {
ps.v1_eq_length += 1;
} else if (ps.v1_eq_length > 0) {
if (ps.v1_eq_max < ps.v1_eq_length) {
ps.v1_eq_max = ps.v1_eq_length;
ps.v1_start = ps.v1_index - ps.v1_eq_length;
}
ps.v1_eq_length = 0;
break;//只寻找最近的
}
ps.v1_index += 1;
}
if (ps.v1_eq_max < ps.v1_eq_length) {
ps.v1_eq_max = ps.v1_eq_length;
ps.v1_start = ps.v1_index - ps.v1_eq_length;
}
ps.v2_index = ps.v2_i + 1;
ps.v2_eq_length = 0;
ps.v2_eq_max = 0;
ps.v2_start = ps.v2_i + 1;
while (ps.v2_index < op.value2.length) {
if (op.value2[ps.v2_index] == op.value1[ps.v1_i + ps.v2_eq_length]) {
ps.v2_eq_length += 1;
} else if (ps.v2_eq_length > 0) {
if (ps.v2_eq_max < ps.v2_eq_length) {
ps.v2_eq_max = ps.v2_eq_length;
ps.v2_start = ps.v2_index - ps.v2_eq_length;
}
ps.v1_eq_length = 0;
break;//只寻找最近的
}
ps.v2_index += 1;
}
if (ps.v2_eq_max < ps.v2_eq_length) {
ps.v2_eq_max = ps.v2_eq_length;
ps.v2_start = ps.v2_index - ps.v2_eq_length;
}
if (ps.v1_eq_max < op.eq_min && ps.v1_start - ps.v1_i > op.eq_index) {
ps.v1_eq_max = 0;
}
if (ps.v2_eq_max < op.eq_min && ps.v2_start - ps.v2_i > op.eq_index) {
ps.v2_eq_max = 0;
}
if ((ps.v1_eq_max == 0 && ps.v2_eq_max == 0)) {
//两个值的字不同
//ps.v1_new_value += "<span style='" + op.value1_style + "'>不同的" + op.value1[ps.v1_i].replace(/</g,"<").replace(">",">") + "</span>";
ps.v2_new_value += "<span class='delete'>" + op.value1[ps.v1_i].replace(/</g, "<").replace(">", ">") + "</span>";
ps.v2_new_value += "<span class='add'>" + op.value2[ps.v2_i].replace(/</g, "<").replace(">", ">") + "</span>";
ps.v1_i += 1;
ps.v2_i += 1;
if (ps.v1_i >= op.value1.length) {
//值2增加的部分
ps.v2_new_value += "<span class='add'>" + op.value2.substr(ps.v2_i).replace(/</g, "<").replace(">", ">") + "</span>";
break;
}
if (ps.v2_i >= op.value2.length) {
//值1删除的部分
//ps.v1_new_value += "<span style='" + op.value1_style + "'>值1多的" + op.value1.substr(ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>";
ps.v2_new_value += "<span class='delete'>" + op.value1.substr(ps.v1_i).replace(/</g, "<").replace(">", ">") + "</span>";
break;
}
//值1删除的
} else if (ps.v1_eq_max > ps.v2_eq_max) {
//ps.v1_new_value += "<span style='" + op.value1_style + "'>值1删除的" + op.value1.substr(ps.v1_i, ps.v1_start - ps.v1_i).replace(/</g,"<").replace(">",">") + "</span>";
ps.v2_new_value += "<span class='delete'>" + op.value1.substr(ps.v1_i, ps.v1_start - ps.v1_i).replace(/</g, "<").replace(">", ">") + "</span>";
ps.v1_i = ps.v1_start;
} else {
//值2增加的
ps.v2_new_value += "<span class='add'>" + op.value2.substr(ps.v2_i, ps.v2_start - ps.v2_i).replace(/</g, "<").replace(">", ">") + "</span>";
ps.v2_i = ps.v2_start;
}
}
}
op.value1 = ps.v1_new_value;
op.value2 = ps.v2_new_value;
//有多个连着修改的放在一起
var addRule = /<span class='add'>((?!<span).)+<\/span>/g;
var deleteRule = /<span class='delete'>((?!<span).)+<\/span>/g;
var allRule = /(<span class='delete'>((?!<span).)+<\/span><span class='add'>((?!<span).)+<\/span>){2,}/g;
op.value2 = op.value2.replace(allRule, function (str) {
var beforText = "", afterText = "";
var beforeMatch = str.match(deleteRule);
for (var i = 0; i < beforeMatch.length; i++) {
var m = beforeMatch[i].match(deleteRule);
beforText += RegExp.$1;
}
var afterMatch = str.match(addRule);
for (var i = 0; i < afterMatch.length; i++) {
var m = afterMatch[i].match(addRule);
afterText += RegExp.$1;
}
return "<span class='delete'>" + beforText + "</span><span class='add'>" + afterText + "</span>";
});
return op;
}
window.onload = function () {
document.getElementsByClassName("button")[0].addEventListener("click", textchange);
}
</script>
</body>
</html>