-
Notifications
You must be signed in to change notification settings - Fork 88
/
VNode.cpp
119 lines (100 loc) · 2.73 KB
/
VNode.cpp
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
#include "VNode.hpp"
#ifndef ASMDOM_JS_SIDE
#include <emscripten/val.h>
#include <emscripten/bind.h>
#endif
#include <cstdint>
#include <string>
#include <unordered_map>
namespace asmdom {
unsigned int currentHash = 0;
std::unordered_map<std::string, unsigned int> hashes;
void VNode::normalize(const bool injectSvgNamespace) {
if (!(hash & isNormalized)) {
if (data.attrs.count("key")) {
hash |= hasKey;
key = data.attrs["key"];
data.attrs.erase("key");
}
if (sel[0] == '!') {
hash |= isComment;
sel = "";
} else {
children.erase(std::remove(children.begin(), children.end(), (VNode*)NULL), children.end());
Attrs::iterator it = data.attrs.begin();
while (it != data.attrs.end()) {
if (it->first == "ns") {
hash |= hasNS;
ns = it->second;
it = data.attrs.erase(it);
} else if (it->second == "false") {
it = data.attrs.erase(it);
} else {
if (it->second == "true") {
it->second = "";
}
++it;
}
}
bool addNS = injectSvgNamespace || (sel[0] == 's' && sel[1] == 'v' && sel[2] == 'g');
if (addNS) {
hash |= hasNS;
ns = "http://www.w3.org/2000/svg";
}
if (!data.attrs.empty()) hash |= hasAttrs;
#ifndef ASMDOM_JS_SIDE
if (!data.props.empty()) hash |= hasProps;
if (!data.callbacks.empty()) hash |= hasCallbacks;
#endif
if (!children.empty()) {
hash |= hasDirectChildren;
Children::size_type i = children.size();
while (i--) {
children[i]->normalize(
addNS && sel != "foreignObject"
);
}
}
if (sel[0] == '\0') {
hash |= isFragment;
} else {
if (hashes[sel] == 0) {
hashes[sel] = ++currentHash;
}
hash |= (hashes[sel] << 13) | isElement;
#ifndef ASMDOM_JS_SIDE
if ((hash & hasCallbacks) && data.callbacks.count("ref")) {
hash |= hasRef;
}
#endif
}
}
hash |= isNormalized;
}
};
void deleteVNode(const VNode* const vnode) {
if (!(vnode->hash & hasText)) {
Children::size_type i = vnode->children.size();
while (i--) deleteVNode(vnode->children[i]);
}
delete vnode;
};
VNode::~VNode() {
if (hash & hasText) {
Children::size_type i = children.size();
while (i--) delete children[i];
}
};
#ifndef ASMDOM_JS_SIDE
emscripten::val functionCallback(const std::uintptr_t& vnode, std::string callback, emscripten::val event) {
Callbacks cbs = reinterpret_cast<VNode*>(vnode)->data.callbacks;
if (!cbs.count(callback)) {
callback = "on" + callback;
}
return emscripten::val(cbs[callback](event));
};
EMSCRIPTEN_BINDINGS(function_callback) {
emscripten::function("functionCallback", &functionCallback, emscripten::allow_raw_pointers());
};
#endif
}