Skip to content

Commit 454cd39

Browse files
committed
LCS diff priority, head or tail
1 parent 320b0c8 commit 454cd39

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ function mergeSettings(settings){
118118
root: 'body'
119119
},
120120
diff: {
121+
// LCS diff priority, `head` or `tail`
122+
priority: 'head',
121123
// highlight mask styles
122124
highlight: {
123125
add: {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "page-monitor",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"description": "monitor pages and diff the dom change with phantomjs",
55
"main": "index.js",
66
"scripts": {

phantomjs/diff.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function isMatch(left, right){
4242
}
4343

4444
/**
45-
* Longest common subsequence
45+
* Longest common subsequence (obverse)
4646
* @param {Array} left
4747
* @param {Array} right
4848
* @param {Function} match
@@ -86,6 +86,45 @@ function LCS(left, right, match){
8686
return lastLine.pop() || [];
8787
}
8888

89+
/**
90+
* Longest common subsequence (reverse)
91+
* @param {Array} left
92+
* @param {Array} right
93+
* @param {Function} match
94+
* @returns {Array}
95+
* @constructor
96+
*/
97+
function LCS2(left, right, match){
98+
var lastLine = [];
99+
var currLine = [];
100+
left.forEach(function(old){
101+
right.forEach(function(cur, x){
102+
if(match(old, cur)){
103+
var sequence = (lastLine[x-1] || []).slice(0);
104+
sequence.push({ l: old, r: cur });
105+
currLine[x] = sequence;
106+
} else {
107+
var lSeq = currLine[x-1];
108+
var tSeq = lastLine[x];
109+
if(lSeq && tSeq){
110+
if(lSeq.length > tSeq.length){
111+
currLine[x] = lSeq.slice(0);
112+
} else {
113+
currLine[x] = tSeq.slice(0);
114+
}
115+
} else if(lSeq) {
116+
currLine[x] = lSeq.slice(0);
117+
} else if(tSeq) {
118+
currLine[x] = tSeq.slice(0);
119+
}
120+
}
121+
});
122+
lastLine = currLine;
123+
currLine = [];
124+
});
125+
return (lastLine.pop() || []);
126+
}
127+
89128
/**
90129
* diff change
91130
* @param {Object} left
@@ -102,7 +141,7 @@ var diff = function(left, right, opt){
102141
if(left.style !== right.style){
103142
change.type |= opt.changeType.STYLE;
104143
}
105-
LCS(left.child, right.child, isMatch).forEach(function(node){
144+
(opt.priority === 'tail' ? LCS2 : LCS)(left.child, right.child, isMatch).forEach(function(node){
106145
var old = node.l;
107146
var cur = node.r;
108147
cur.matched = old.matched = true;

0 commit comments

Comments
 (0)