-
Notifications
You must be signed in to change notification settings - Fork 88
/
VNode.hpp
179 lines (164 loc) · 4.49 KB
/
VNode.hpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef asmdom_VNode_hpp
#define asmdom_VNode_hpp
#include <emscripten/val.h>
#include <vector>
#include <string>
#ifdef ASMDOM_JS_SIDE
#include <map>
#else
#include <functional>
#include <utility>
#include <unordered_map>
#endif
namespace asmdom {
#ifdef ASMDOM_JS_SIDE
typedef std::map<std::string, std::string> Attrs;
#else
typedef std::function<bool(emscripten::val)> Callback;
typedef std::unordered_map<std::string, std::string> Attrs;
typedef std::unordered_map<std::string, emscripten::val> Props;
typedef std::unordered_map<std::string, Callback> Callbacks;
#endif
enum VNodeFlags {
// NodeType
isElement = 1,
isText = 1 << 1,
isComment = 1 << 2,
isFragment = 1 << 3,
// flags
hasKey = 1 << 4,
hasText = 1 << 5,
hasAttrs = 1 << 6,
hasProps = 1 << 7,
hasCallbacks = 1 << 8,
hasDirectChildren = 1 << 9,
hasChildren = hasDirectChildren | hasText,
hasRef = 1 << 10,
hasNS = 1 << 11,
isNormalized = 1 << 12,
// masks
isElementOrFragment = isElement | isFragment,
nodeType = isElement | isText | isComment | isFragment,
removeNodeType = ~0 ^ nodeType,
extractSel = ~0 << 13,
id = extractSel | hasKey | nodeType
};
struct Data {
Data() {};
#ifdef ASMDOM_JS_SIDE
Data(
const Data& data
): attrs(data.attrs) {};
Data(
const Attrs& dataAttrs
): attrs(dataAttrs) {};
#else
Data(
const Data& data
): attrs(data.attrs), props(data.props), callbacks(data.callbacks) {};
Data(
const Attrs& dataAttrs,
const Props& dataProps = Props(),
const Callbacks& dataCallbacks = Callbacks()
): attrs(dataAttrs), props(dataProps), callbacks(dataCallbacks) {};
Data(
const Attrs& dataAttrs,
const Callbacks& dataCallbacks
): attrs(dataAttrs), callbacks(dataCallbacks) {};
Data(
const Props& dataProps,
const Callbacks& dataCallbacks = Callbacks()
): props(dataProps), callbacks(dataCallbacks) {};
Data(
const Callbacks& dataCallbacks
): callbacks(dataCallbacks) {};
#endif
Attrs attrs;
#ifndef ASMDOM_JS_SIDE
Props props;
Callbacks callbacks;
#endif
};
struct VNode {
private:
void normalize(const bool injectSvgNamespace);
public:
VNode(
const std::string& nodeSel
): sel(nodeSel) {};
VNode(
const std::string& nodeSel,
const std::string& nodeText
): sel(nodeSel) {
normalize();
if (hash & isComment) {
sel = nodeText;
} else {
children.push_back(new VNode(nodeText, true));
hash |= hasText;
}
};
VNode(
const std::string& nodeText,
const bool textNode
) {
if (textNode) {
normalize();
sel = nodeText;
// replace current type with text type
hash = (hash & removeNodeType) | isText;
} else {
sel = nodeText;
normalize();
}
};
VNode(
const std::string& nodeSel,
const Data& nodeData
): sel(nodeSel), data(nodeData) {};
VNode(
const std::string& nodeSel,
const std::vector<VNode*>& nodeChildren
): sel(nodeSel), children(nodeChildren) {};
VNode(
const std::string& nodeSel,
VNode* child
): sel(nodeSel), children{ child } {};
VNode(
const std::string& nodeSel,
const Data& nodeData,
const std::string& nodeText
): sel(nodeSel), data(nodeData) {
normalize();
if (hash & isComment) {
sel = nodeText;
} else {
children.push_back(new VNode(nodeText, true));
hash |= hasText;
}
};
VNode(
const std::string& nodeSel,
const Data& nodeData,
const std::vector<VNode*>& nodeChildren
): sel(nodeSel), data(nodeData), children(nodeChildren) {};
VNode(
const std::string& nodeSel,
const Data& nodeData,
VNode* child
): sel(nodeSel), data(nodeData), children{ child } {};
~VNode();
void normalize() { normalize(false); };
// contains selector for elements and fragments, text for comments and textNodes
std::string sel;
std::string key;
std::string ns;
unsigned int hash = 0;
Data data;
int elm = 0;
std::vector<VNode*> children;
};
void deleteVNode(const VNode* const vnode);
typedef std::vector<VNode*> Children;
}
#endif