纯JS文本比较工具

前段时间由于工作需要写了一个纯JS文本比较工具

在这里与大家分享下

算法有待优化,还希望大家多多指教

先上效果图:

奉上源码(把源码保存为html格式的文件就可以直接运行了):

  1 <!doctype html>
  2 <html>
  3     <head>
  4         <title>文本比较工具</title>
  5         <style type="text/css">
  6             *{padding:0px;margin:0px;}
  7             html,body{
  8                 overflow-y: hidden;
  9             }
 10             .edit_div{
 11                 border: 1px solid #CCCCCC;
 12                 overflow: auto;
 13                 position: relative;
 14             }
 15             .edit_div textarea{
 16                 resize:none;
 17                 background: none repeat scroll 0 0 transparent;
 18                 border: 0 none;
 19                 width: 100%;
 20                 height:200px;
 21                 overflow-y: scroll;
 22                 position: absolute;
 23                 left: 0px;
 24                 top: 0px;
 25                 z-index: 2;
 26                 font-size: 18px;
 27                 white-space: pre-wrap;
 28                 word-wrap: break-word;
 29                 word-break:break-all;
 30             }
 31             .edit_div pre{
 32                 overflow-y: scroll;
 33                 white-space: pre-wrap;
 34                 word-wrap: break-word;
 35                 word-break:break-all;
 36                 width: 100%;
 37                 height:200px;
 38                 text-align: left;
 39                 color: #ffffff;
 40                 z-index: 1;
 41                 font-size: 18px;
 42             }
 43     </style>
 44     </head>
 45     <body>
 46         <table style="width:100%">
 47             <tr>
 48                 <td style="width:50%">
 49                     <div class="edit_div">
 50                         <pre id="edit_pre_1"></pre>
 51                         <textarea id="edit_textarea_1" onscroll="test1_scroll()" oninput="textchange()" onpropertychange="textchange()"></textarea>
 52                     </div>
 53                 </td>
 54                 <td style="width:50%">
 55                     <div class="edit_div">
 56                         <pre  id="edit_pre_2"></pre>
 57                         <textarea id="edit_textarea_2" onscroll="test2_scroll()" oninput="textchange()" onpropertychange="textchange()"></textarea>
 58                     </div>
 59                 </td>
 60             </tr>
 61         </table>
 62         <script type="text/javascript">
 63             function test1_scroll(){
 64                 document.getElementById("edit_pre_1").scrollTop=document.getElementById("edit_textarea_1").scrollTop;
 65                 document.getElementById("edit_pre_2").scrollTop=document.getElementById("edit_pre_1").scrollTop;
 66                 document.getElementById("edit_textarea_2").scrollTop=document.getElementById("edit_textarea_1").scrollTop;
 67             }
 68             function test2_scroll(){
 69                 document.getElementById("edit_pre_2").scrollTop=document.getElementById("edit_textarea_2").scrollTop;
 70                 document.getElementById("edit_pre_1").scrollTop=document.getElementById("edit_pre_2").scrollTop;
 71                 document.getElementById("edit_textarea_1").scrollTop=document.getElementById("edit_textarea_2").scrollTop;
 72             }
 73             function textchange(){
 74                     var op = eq({ value1: document.getElementById("edit_textarea_1").value, value2: document.getElementById("edit_textarea_2").value });
 75                     document.getElementById("edit_pre_1").innerHTML=op.value1+"\r\n";
 76                     document.getElementById("edit_pre_2").innerHTML=op.value2+"\r\n";
 77             }
 78             function eq(op) {
 79                 if(!op){
 80                     return op;
 81                 }
 82                 if(!op.value1_style){
 83                     op.value1_style="background-color:#FEC8C8;";
 84                 }
 85                 if(!op.value2_style){
 86                     op.value2_style="background-color:#FEC8C8;";
 87                 }
 88                 if(!op.eq_min){
 89                     op.eq_min=3;
 90                 }
 91                 if(!op.eq_index){
 92                     op.eq_index=5;
 93                 }
 94                 if (!op.value1 || !op.value2) {
 95                     return op;
 96                 }
 97                 var ps = {
 98                     v1_i: 0,
 99                     v1_new_value: "",
100                     v2_i: 0,
101                     v2_new_value: ""
102                 };
103                 while (ps.v1_i < op.value1.length && ps.v2_i < op.value2.length) {
104                     if (op.value1[ps.v1_i] == op.value2[ps.v2_i]) {
105                         ps.v1_new_value += op.value1[ps.v1_i].replace(/</g,"&lt;").replace(">","&gt;");
106                         ps.v2_new_value += op.value2[ps.v2_i].replace(/</g,"&lt;").replace(">","&gt;");
107                         ps.v1_i += 1;
108                         ps.v2_i += 1;
109                         if (ps.v1_i >= op.value1.length) {
110                             ps.v2_new_value += "<span style=‘" + op.value2_style + "‘>" + op.value2.substr(ps.v2_i).replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
111                             break;
112                         }
113                         if (ps.v2_i >= op.value2.length) {
114                             ps.v1_new_value += "<span style=‘" + op.value1_style + "‘>" + op.value1.substr(ps.v1_i).replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
115                             break;
116                         }
117                     } else {
118                         ps.v1_index = ps.v1_i + 1;
119                         ps.v1_eq_length = 0;
120                         ps.v1_eq_max = 0;
121                         ps.v1_start = ps.v1_i + 1;
122                         while (ps.v1_index < op.value1.length) {
123                             if (op.value1[ps.v1_index] == op.value2[ps.v2_i + ps.v1_eq_length]) {
124                                 ps.v1_eq_length += 1;
125                             } else if (ps.v1_eq_length > 0) {
126                                 if (ps.v1_eq_max < ps.v1_eq_length) {
127                                     ps.v1_eq_max = ps.v1_eq_length;
128                                     ps.v1_start = ps.v1_index - ps.v1_eq_length;
129                                 }
130                                 ps.v1_eq_length = 0;
131                                 break;//只寻找最近的
132                             }
133                             ps.v1_index += 1;
134                         }
135                         if (ps.v1_eq_max < ps.v1_eq_length) {
136                             ps.v1_eq_max = ps.v1_eq_length;
137                             ps.v1_start = ps.v1_index - ps.v1_eq_length;
138                         }
139
140                         ps.v2_index = ps.v2_i + 1;
141                         ps.v2_eq_length = 0;
142                         ps.v2_eq_max = 0;
143                         ps.v2_start = ps.v2_i + 1;
144                         while (ps.v2_index < op.value2.length) {
145                             if (op.value2[ps.v2_index] == op.value1[ps.v1_i + ps.v2_eq_length]) {
146                                 ps.v2_eq_length += 1;
147                             } else if (ps.v2_eq_length > 0) {
148                                 if (ps.v2_eq_max < ps.v2_eq_length) {
149                                     ps.v2_eq_max = ps.v2_eq_length;
150                                     ps.v2_start = ps.v2_index - ps.v2_eq_length;
151                                 }
152                                 ps.v1_eq_length = 0;
153                                 break;//只寻找最近的
154                             }
155                             ps.v2_index += 1;
156                         }
157                         if (ps.v2_eq_max < ps.v2_eq_length) {
158                             ps.v2_eq_max = ps.v2_eq_length;
159                             ps.v2_start = ps.v2_index - ps.v2_eq_length;
160                         }
161                         if (ps.v1_eq_max < op.eq_min && ps.v1_start - ps.v1_i > op.eq_index) {
162                             ps.v1_eq_max = 0;
163                         }
164                         if (ps.v2_eq_max < op.eq_min && ps.v2_start - ps.v2_i > op.eq_index) {
165                             ps.v2_eq_max = 0;
166                         }
167                         if ((ps.v1_eq_max == 0 && ps.v2_eq_max == 0)) {
168                             ps.v1_new_value += "<span style=‘" + op.value1_style + "‘>" + op.value1[ps.v1_i].replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
169                             ps.v2_new_value += "<span style=‘" + op.value2_style + "‘>" + op.value2[ps.v2_i].replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
170                             ps.v1_i += 1;
171                             ps.v2_i += 1;
172
173                             if (ps.v1_i >= op.value1.length) {
174                                 ps.v2_new_value += "<span style=‘" + op.value2_style + "‘>" + op.value2.substr(ps.v2_i).replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
175                                 break;
176                             }
177                             if (ps.v2_i >= op.value2.length) {
178                                 ps.v1_new_value += "<span style=‘" + op.value1_style + "‘>" + op.value1.substr(ps.v1_i).replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
179                                 break;
180                             }
181                         } else if (ps.v1_eq_max > ps.v2_eq_max) {
182                             ps.v1_new_value += "<span style=‘" + op.value1_style + "‘>" + op.value1.substr(ps.v1_i, ps.v1_start - ps.v1_i).replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
183                             ps.v1_i = ps.v1_start;
184                         } else {
185                             ps.v2_new_value += "<span style=‘" + op.value2_style + "‘>" + op.value2.substr(ps.v2_i, ps.v2_start - ps.v2_i).replace(/</g,"&lt;").replace(">","&gt;") + "</span>";
186                             ps.v2_i = ps.v2_start;
187                         }
188                     }
189                 }
190                 op.value1 = ps.v1_new_value;
191                 op.value2 = ps.v2_new_value;
192                 return op;
193             }
194             function settextheight(){
195                 var heigth=(document.documentElement.clientHeight-6)+"px"
196                 document.getElementById("edit_pre_1").style.height=heigth;
197                 document.getElementById("edit_textarea_1").style.height=heigth;
198                 document.getElementById("edit_pre_2").style.height=heigth;
199                 document.getElementById("edit_textarea_2").style.height=heigth;
200             }
201             window.onload=function(){
202                 settextheight();
203                 window.onresize=function(){
204                     settextheight();
205                 }
206             }
207         </script>
208     </body>
209 </html>

纯JS文本比较工具源码

时间: 2024-08-05 19:36:26

纯JS文本比较工具的相关文章

纯js实现html转pdf

项目开发中遇到了一个变态需求,需要把一整个页面导出为pdf格式,而且要保留页面上的所有的表格.svg图片和样式.简而言之,就是希望像截图一样,把整个页面截下来,然后保存成pdf.咋不上天呢--查了一下,能够实现html转pdf的方法还是挺多的,大概有以下几种:1.大部分浏览器就有这个功能.然而我们客户要的可不是这个,人家要的是能够在系统中主动触发的导出为pdf功能,所以这种方案pass.2.利用第三方工具.我找到了一种利用wkhtmltopdf这种工具来导出的方案,自己在我们的项目中试了一下,效

Ajax,纯Js+Jquery

AJAX:Asynchronous Javascript and xml 异步,Js和Xml 交互式网页开发 不刷新页面,与服务器交互 详情请参照Jquery工具指南用在浏览器端的技术,无刷新,通过XmlHttpRequest访问页面纯js版---------- if(XmlHttpRequest){ //判断是否支持XmlHttpRequest xhr= new XmlHttpRequest(); // 创建XmlHttpRequest对象 } else{ xhr= new Activexob

我用的一些Node.js开发工具、开发包、框架等总结

开发工具 1.WebStorm,毫无疑问非他莫属,跨平台,强大的代码提示,支持Nodejs调试,此外还支持vi编辑模式,这点我很喜欢. 2.做些小型项目用Sublime Text. 3.Browserify:将你的nodejs模块应用到浏览器中 4.nvm:nodejs版本管理工具,你可能会用到多个nodejs版本(如v0.11.x支持generator的nodejs和stable的v0.10.x版本),用它可以方便切换 测试&自动化 1.mocha:一个简单.灵活有趣的 JavaScript

纯js客服插件集qq、旺旺、skype、百度hi、msn

原文 纯js客服插件集qq.旺旺.skype.百度hi.msn 客服插件,集qq.旺旺.skype.百度hi.msn 等 即时通讯工具,并可自己添加支持的通讯工具,极简主义,用法自己琢磨.我的博客 http://www.qiling.org <script> //在线客服插件 powered by casejs 极简主义 http://www.mlrzw.cn function CaseService(caseServiceConfig) { this.config = caseService

JS模块化工具requirejs教程(一):初识requirejs

随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用.单元测试等等一系列复杂的需求. RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一.最新版本的RequireJS压缩后只有14K,堪称非常轻量.它还同时可以和其他的框架协同工作,使用RequireJS必将使您的前端代码质量得以提升. requirejs能带来什么好处 官方对requ

js常用工具库XCLJsTool v1.0发布

最近有空,整了一个js的工具库,还没有正式用于项目中,自己也没有时间写测试用例,想了一下,还是贴出来给大家看看,如果有问题,请留言,非常感谢!项目我放在了github上面,会经常更新的,过段时间会发布一版! /** * 欢迎使用本程序,您可以任意修改.复制.分享本程序所有代码,只需要保留本注释即可,谢谢! * 项目地址:<span style="color:#ff0000;">https://github.com/xucongli1989/XCLJsTool</spa

Highcharts纯js图表库,以后可以跟客户说,你跟阿里云ECS用的图表库是同款

Highcharts是一款纯javascript编写的图表库,能够很简便的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达18种不同类型的图表,可以满足常用的Web图表需求 ! 近来维护我的阿里云服务器,进入后台偶然发现阿里云管理后台数据图表用的也是Highcharts,刚好正需要WEB端展示数据的东西,研究哈… Highcharts官网:http://www.highcharts.com Highcharts中文站:ht

纯js页面跳转整理

js方式的页面跳转1.window.location.href方式    <script language="javascript" type="text/javascript">           window.location.href="http://updn.cn";     </script>2.window.navigate方式跳转   <script language="javascript

纯js制作的弹球游戏

纯js的弹球游戏,撞壁自动返回,按钮放置暂停移动,移开开始移动 1 <!-- 2 author:zhangjie 3 date :2016-7-23 4 --> 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <title></title> 9 <meta charset="UTF-8"> 10 <script type='text/javascript'> 11