Skip to content

Commit 6531b95

Browse files
committed
add modify-value test by @matthewwolfe, fix value bug when value is an emoty string, increase version to 2.0.3, fixes #40
1 parent 6bb9b28 commit 6531b95

File tree

5 files changed

+76
-10
lines changed

5 files changed

+76
-10
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "diff-dom",
33
"main": "diffDOM.js",
4-
"version": "2.0.2",
4+
"version": "2.0.3",
55
"homepage": "https://github.com/fiduswriter/diffDOM",
66
"authors": [
77
"Johannes Wilm <[email protected]>"

diffDOM.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,13 @@
647647
);
648648
}
649649
if (this.valueDiffing) {
650-
if (node.value) {
650+
if (node.value !== undefined) {
651651
objNode.value = node.value;
652652
}
653-
if (node.checked) {
653+
if (node.checked !== undefined) {
654654
objNode.checked = node.checked;
655655
}
656-
if (node.selected) {
656+
if (node.selected !== undefined) {
657657
objNode.selected = node.selected;
658658
}
659659
}
@@ -942,12 +942,7 @@
942942
return true;
943943
}
944944
diffs.forEach(function(diff) {
945-
// console.log(JSON.stringify(diff));
946-
// console.log(JSON.stringify(tree));
947-
// console.log(this.objToNode(tree).outerHTML);
948945
dobj.applyVirtualDiff(tree, diff);
949-
// console.log(JSON.stringify(tree));
950-
// console.log(this.objToNode(tree).outerHTML);
951946
});
952947
return true;
953948
},

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ <h2>Tests</h2>
1717

1818
<p>There is also a <a href="tests/random-unlimited.html">version of the random tests that reloads if no errors have been found</a>, mainly useful for finding errors.</p>
1919

20+
<p>The <a href="tests/modify-value.html">input value modification test</a>, contributed by user matthewwolfe, tests diffing changes to input fields.</p>
21+
2022
<p>The <a href="tests/site-integration.html">site integration test</a>, contributed by user unbug, allows for testing for speed on a 170kb site real-world-like site where it is integrated with jQuery.</p>
2123

2224
<h2>Demo</h2>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "diff-dom",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "A diff for DOM elements, as client-side JavaScript code. Gets all modifications, insertions and removals between two DOM fragments.",
55
"main": "diffDOM.js",
66
"directories": {

tests/modify-value.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="../diffDOM.js">
5+
</script>
6+
<script>
7+
8+
document.addEventListener("DOMContentLoaded", function(event) {
9+
// ------------- Testing the issue -------------------- //
10+
// ---------------------------------------------------- //
11+
12+
var dd = new diffDOM();
13+
14+
// create two sets of identical html
15+
var element1 = document.createElement('form');
16+
17+
var input = document.createElement('input');
18+
input.setAttribute('type', 'text');
19+
input.setAttribute('value', 'testing');
20+
21+
var submit = document.createElement('input');
22+
submit.setAttribute('type', 'submit');
23+
24+
var instructions = document.createElement('div');
25+
instructions.innerHTML = '<p>1. Change the text<br>2. Click "submit" once<br>\
26+
3. Check if the input field value stays the same<br>4. Click "submit" a\
27+
second time<br>5. Check if input field is empty.</p>';
28+
29+
document.body.appendChild(instructions);
30+
31+
element1.appendChild(input);
32+
element1.appendChild(submit);
33+
34+
document.body.appendChild(element1);
35+
36+
// element2 will remain virtual, and we will diff element1 with element2
37+
// to determine if we actually need to update the DOM or not.
38+
var element2 = element1.cloneNode(true);
39+
40+
// event listener for when the input value changes
41+
element1.addEventListener('input', function(e){
42+
// update the virtual element, then diff and see if there needs to be updates
43+
element2.firstChild.setAttribute('value', e.target.value);
44+
45+
var diff = dd.diff(element1, element2);
46+
dd.apply(element1, diff);
47+
48+
console.log(diff);
49+
});
50+
51+
// event listener for pressing the submit button
52+
element1.addEventListener('submit', function(e){
53+
e.preventDefault();
54+
55+
// update the virtual element, then diff and see if there needs to be updates
56+
element2.firstChild.setAttribute('value', '');
57+
58+
var diff = dd.diff(element1, element2);
59+
dd.apply(element1, diff);
60+
61+
console.log(diff);
62+
});
63+
64+
});
65+
</script>
66+
</head>
67+
<body>
68+
</body>
69+
</html>

0 commit comments

Comments
 (0)