-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
145 lines (111 loc) Β· 3.48 KB
/
patch.js
File metadata and controls
145 lines (111 loc) Β· 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Todo:
* Character data support
* Merge multiple updates into one packet
*/
var pa = require('./private-attributes');
var uuid = require('uuid');
const UUID_KEY = 'data-uuid';
const DEAD_NODE_NAME = 'dead';
const PATCH_NODE_NAME = 'patch';
const SNAPSHOT_NODE_NAME = 'snapshot';
function Patch (global, root, broadcast, filter) {
var document = root.ownerDocument;
function generateUUID () {
return uuid.v4();
}
// todo - add uuid to textNodes
function treeUUID (el, childNodes) {
var nodes = [el];
if (childNodes && el.nodeType === 1) {
nodes = nodes.concat(el.querySelectorAll('*'));
}
nodes.forEach((e) => {
if (!pa.has(e, UUID_KEY)) {
// if (e === root) {
// pa.set(e, UUID_KEY, ROOT_UUID);
// } else {
pa.set(e, UUID_KEY, generateUUID());
// }
}
});
}
function cloneWithUUID (el) {
var result;
if (el.nodeType === 1) {
if (filter && !filter(el)) {
return document.createTextNode('');
}
result = el.cloneNode(false);
} else if (el.nodeType === 3) {
result = document.createTextNode(el.nodeValue);
} else {
throw new Error('Invalid node type to clone');
}
if (el.nodeType === 1) {
var uuid = pa.get(el, UUID_KEY);
if (!uuid) {
treeUUID(el, el.childNodes);
uuid = pa.get(el, UUID_KEY);
}
result.setAttribute(UUID_KEY, uuid);
el.childNodes.forEach((n) => {
if (n.nodeType === 1) {
result.appendChild(cloneWithUUID(n));
} else {
result.appendChild(n.cloneNode(true));
}
});
}
return result;
}
this.getSnapshot = function () {
var snapshot = document.createElement(SNAPSHOT_NODE_NAME);
snapshot.appendChild(cloneWithUUID(root));
return snapshot.outerHTML;
};
treeUUID(root, true);
var observer = new global.MutationObserver(function (mutations) {
var patch = document.createElement(PATCH_NODE_NAME);
// Cache attribute mutations to merge them
var attributeMutations = {};
mutations.forEach(function (mutation) {
treeUUID(mutation.target, true);
var uuid = pa.get(mutation.target, UUID_KEY);
var el;
if (mutation.type === 'attributes' && attributeMutations[uuid]) {
el = attributeMutations[uuid];
} else {
el = document.createElement(mutation.target.nodeName);
el.setAttribute(UUID_KEY, uuid);
patch.appendChild(el);
}
if (mutation.type === 'attributes') {
attributeMutations[uuid] = el;
}
switch (mutation.type) {
case 'attributes':
el.setAttribute(mutation.attributeName, global.HTMLElement.prototype.getAttribute.call(mutation.target, mutation.attributeName));
break;
case 'childList':
mutation.addedNodes.forEach((m) => {
treeUUID(m, true);
el.appendChild(cloneWithUUID(m));
});
mutation.removedNodes.forEach((m) => {
var removed = document.createElement(DEAD_NODE_NAME);
removed.setAttribute(UUID_KEY, pa.get(m, UUID_KEY));
el.appendChild(removed);
});
break;
default:
console.error(`Unknown mutation type '${mutation.type}'.`);
break;
}
});
broadcast(patch.outerHTML);
});
var config = { attributes: true, subtree: true, childList: true, characterData: true };
observer.observe(root, config);
}
module.exports = Patch;