forked from WebReflection/uhtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.js
More file actions
158 lines (148 loc) · 3.87 KB
/
Copy pathhandlers.js
File metadata and controls
158 lines (148 loc) · 3.87 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
146
147
148
149
150
151
152
153
154
155
156
157
158
import udomdiff from 'udomdiff';
import {isArray, slice} from './array.js';
import {getNode, wireType} from './node.js';
const get = (item, i) => item.nodeType === wireType ?
((1 / i) < 0 ?
/* istanbul ignore next */
(i ? item.remove() : item.lastChild) :
/* istanbul ignore next */
(i ? item.valueOf() : item.firstChild)) :
item
;
const handleAnything = (comment, nodes) => {
let oldValue;
const text = document.createTextNode('');
const anyContent = newValue => {
switch (typeof newValue) {
case 'string':
case 'number':
case 'boolean':
if (oldValue !== newValue) {
oldValue = newValue;
text.textContent = newValue;
nodes = udomdiff(
comment.parentNode,
nodes,
[text],
get,
comment
);
}
break;
case 'object':
case 'undefined':
if (newValue == null) {
nodes = udomdiff(comment.parentNode, nodes, [], get, comment);
break;
}
default:
oldValue = newValue;
if (isArray(newValue)) {
if (newValue.length === 0)
nodes = udomdiff(comment.parentNode, nodes, [], get, comment);
else {
switch (typeof newValue[0]) {
case 'string':
case 'number':
case 'boolean':
anyContent(String(newValue));
break;
default:
nodes = udomdiff(
comment.parentNode,
nodes,
newValue,
get,
comment
);
break;
}
}
}
/* istanbul ignore else */
else if ('ELEMENT_NODE' in newValue) {
nodes = udomdiff(
comment.parentNode,
nodes,
newValue.nodeType === 11 ?
slice.call(newValue.childNodes) :
[newValue],
get,
comment
);
}
break;
}
};
return anyContent;
};
const handleAttribute = (node, name) => {
// hooks and ref
if (name === 'ref')
return ref => { ref.current = node; };
// direct setters
if (name.slice(0, 1) === '.') {
const setter = name.slice(1);
return value => { node[setter] = value; }
}
let oldValue;
// events
if (name.slice(0, 2) === 'on') {
let type = name.slice(2);
const lower = name.toLowerCase();
/* istanbul ignore next */
if (lower in node)
type = lower;
return newValue => {
const info = isArray(newValue) ? newValue : [newValue, false];
if (oldValue !== info[0]) {
if (oldValue)
node.removeEventListener(type, oldValue, info[1]);
if (oldValue = info[0])
node.addEventListener(type, oldValue, info[1]);
}
};
}
// all other cases
let noOwner = true;
const attribute = document.createAttribute(name);
return newValue => {
if (oldValue !== newValue) {
oldValue = newValue;
if (oldValue == null) {
if (!noOwner) {
node.removeAttributeNode(attribute);
noOwner = true;
}
}
else {
attribute.value = newValue;
/* istanbul ignore else */
if (noOwner) {
node.setAttributeNode(attribute);
noOwner = false;
}
}
}
};
};
/* istanbul ignore next */
const handleText = node => {
let oldValue;
return newValue => {
if (oldValue !== newValue) {
oldValue = newValue;
node.textContent = newValue == null ? '' : newValue;
}
};
};
export function handlers(options) {
const {type, path} = options;
const node = path.reduce(getNode, this);
return type === 'node' ?
handleAnything(node, []) :
(type === 'attr' ?
handleAttribute(node, options.name) :
/* istanbul ignore next */
handleText(node));
};