Skip to content

Commit 2e06af9

Browse files
author
Jonathan Felchlin
committed
Updated roughlyEqual to invalidate a match if IDs are set and they don't match
1 parent bcca7e5 commit 2e06af9

File tree

2 files changed

+128
-4
lines changed

2 files changed

+128
-4
lines changed

diffDOM.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,14 @@
210210

211211
if (e1.attributes && e2.attributes) {
212212

213-
if (e1.attributes.id && e1.attributes.id === e2.attributes.id) {
214-
var idDescriptor = e1.nodeName + '#' + e1.attributes.id;
215-
if (idDescriptor in uniqueDescriptors) {
216-
return true;
213+
if (e1.attributes.id) {
214+
if (e1.attributes.id !== e2.attributes.id) {
215+
return false;
216+
} else {
217+
var idDescriptor = e1.nodeName + '#' + e1.attributes.id;
218+
if (idDescriptor in uniqueDescriptors) {
219+
return true;
220+
}
217221
}
218222
}
219223
if (e1.attributes['class'] && e1.attributes['class'] === e2.attributes['class']) {

tests/list.html

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<!doctype html>
2+
<html>
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>diffDOM</title>
7+
<style>
8+
.success {
9+
background-color: LightGreen;
10+
}
11+
.failure {
12+
background-color: Tomato;
13+
}
14+
.test-elements {
15+
display: none;
16+
}
17+
</style>
18+
<script src="TraceLogger.js">
19+
</script>
20+
<script src="../diffDOM.js">
21+
</script>
22+
</head>
23+
24+
<body>
25+
26+
<h1>Form element test for diffDOM</h1>
27+
28+
29+
<div class="test-elements">
30+
<ul id="list-1"><li id="item-1"></li><li id="item-2"></li><li id="item-3"></li></ul>
31+
<ul id="list-2"><li id="item-1"></li><li id="item-3"></li></ul>
32+
</div>
33+
34+
35+
36+
<script>
37+
38+
function reportDiv() {
39+
document.body.appendChild(document.createElement('div'));
40+
}
41+
42+
function testSuccess() {
43+
document.body.lastChild.classList.add('success');
44+
}
45+
46+
function success(theDiff) {
47+
print('Number of diffs: ' + theDiff.length);
48+
print('...success!');
49+
testSuccess();
50+
}
51+
52+
function testFailure() {
53+
document.body.lastChild.classList.add('failure');
54+
}
55+
56+
function print(text) {
57+
var par = document.createElement('p');
58+
par.textContent = text;
59+
document.body.lastChild.appendChild(par);
60+
}
61+
62+
function setSection(text) {
63+
var header = document.createElement('h2');
64+
header.textContent = text;
65+
document.body.appendChild(header);
66+
}
67+
68+
function reportDivDescription(element1, element2) {
69+
var elTexts = [element1.outerHTML, element2.outerHTML], elements = [element1, element2], i;
70+
71+
for (i=0;i<elements.length;i++) {
72+
if (elements[i].nodeName==='TEXTAREA' && elements[i].textContent !== elements[i].value) {
73+
elTexts[i] += ', value = "' + elements[i].value + '"';
74+
} else if (elements[i].nodeName === 'INPUT' && elements[i].type === 'text' && elements[i].value !== elements[i].getAttribute('value')) {
75+
elTexts[i] += ', value = "' + elements[i].value + '"';
76+
} else if (elements[i].nodeName === 'INPUT' && (elements[i].type === 'radio' || elements[i].type === 'checkbox')) {
77+
elTexts[i] += ', checked = ' + elements[i].checked;
78+
} else if (elements[i].nodeName === 'OPTION') {
79+
elTexts[i] += ', selected = ' + elements[i].selected;
80+
}
81+
}
82+
83+
print("diff operations for " + elTexts[0] + " → " + elTexts[1]);
84+
}
85+
86+
var dd = new diffDOM({
87+
debug: true,
88+
diffcap: 700,
89+
valueDiffing: true
90+
}),
91+
tl = new TraceLogger(dd), elements, first, second, third, fourth, theDiff;
92+
93+
// Textarea
94+
95+
setSection('List');
96+
97+
first = document.getElementById('list-1');
98+
second = document.getElementById('list-2');
99+
100+
first.removeAttribute('id');
101+
second.removeAttribute('id');
102+
103+
reportDiv();
104+
105+
reportDivDescription(first, second);
106+
107+
theDiff = dd.diff(first, second);
108+
109+
if (theDiff.length === 1 && theDiff[0].action === 'removeElement') {
110+
success(theDiff);
111+
} else {
112+
testFailure();
113+
console.log(theDiff);
114+
}
115+
116+
</script>
117+
118+
</body>
119+
120+
</html>

0 commit comments

Comments
 (0)