|
| 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