純JS文本比較工具

来源:http://www.cnblogs.com/raotf/archive/2016/02/17/5194484.html
-Advertisement-
Play Games

前段時間由於工作需要寫了一個純JS文本比較工具 在這裡與大家分享下 演算法有待優化,還希望大家多多指教 先上效果圖: 奉上源碼(把源碼保存為html格式的文件就可以直接運行了): 1 <!doctype html> 2 <html> 3 <head> 4 <title>文本比較工具</title> 5


前段時間由於工作需要寫了一個純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(">"	   

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、下載 去nodejs下載node.msi安裝文件包,裡面包含了node.js和npm; 雙擊node.msi就行了,選擇安裝路徑和npm; 二、設置環境變數 [新版本都不需要設計環境變數了,軟體會自動寫入環境變數] 電腦(或者我的電腦)右擊屬性-》高級系統設置-》環境變數。 新建一個用戶變數。
  • 分享4種input元素滑塊UI樣式是一款使用CSS3來渲染樣式,使用JavaScript來處理滑塊的滑鼠拖動事件。效果圖如下: 線上預覽 源碼下載 實現的代碼。 js代碼: var range_els = document.querySelectorAll('input[type=range]'),
  • constructor屬性始終指向創建當前對象的構造函數。比如下麵例子: // 等價於 var foo = new Array(1, 56, 34, 12); var arr = [1, 56, 34, 12]; console.log(arr.constructor === Array); // 
  • 1.一列佈局 (一)一列自適應 自適應瀏覽器,隨著瀏覽器的拉伸而變化,一般寬度採用百分比的寫法,很簡<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>一列佈局自適應</title> <style type="t
  • p[name]{background:red;} 只使用屬性名p[name=ghr]{background:red;} 指定屬性名,並指定了該屬性的屬性值p[name~=old]{background:red;}此屬性值是一個詞列表,並且以空格隔開,其中詞列表中包含了一個value詞p[name^=
  • [原文鏈接] https://www.huangwenchao.com.cn/2015/03/html5-image-preview.html 問題 加入我們在 HTML 裡面有一個圖片上傳控制項: <input id="upload_image" type="file" name="image" a
  • 心心念念的新年過完了~卻沒念到年會,更沒念到年終獎~哎,以任何理由不發年終獎的公司都是臭流氓~然,我們公司沒有理由,壓根兒就沒提這事,哇卡卡卡卡!!! ======================== 華麗麗的跳水線=========================== 年後第一天,公司人不多,閑來
  • Media Queries 詳解
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...