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
|
#include "web_dom.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <emscripten/emscripten.h>
static struct dom_elem *dom_node_init(char *type, int size_attrs)
{
struct dom_elem *e = calloc(1, sizeof(*e));
e->type = type; // string literal, not freed
e->size_attrs = size_attrs;
e->attr_names = malloc(size_attrs * sizeof(*(e->attr_names)));
e->attr_vals = malloc(size_attrs * sizeof(*(e->attr_vals)));
return e;
}
struct dom_elem *dom_elem_init(char *ns, char *name, int size_attrs)
{
struct dom_elem *e = dom_node_init("element", size_attrs);
e->ns = ns ? strdup(ns) : NULL;
e->name = strdup(name);
return e;
}
struct dom_elem *dom_text_init(char *content)
{
struct dom_elem *e = dom_node_init("text", 0);
e->content = strdup(content);
return e;
}
void dom_elem_free(struct dom_elem *e)
{
for (int i = 0; i < e->n_attrs; ++i) {
free(e->attr_names[i]);
free(e->attr_vals[i]);
}
for (int i = 0; i < e->n_children; ++i)
dom_elem_free(e->children[i]);
free(e->ns);
free(e->name);
free(e->content);
free(e->attr_names);
free(e->attr_vals);
free(e->children);
free(e);
}
void dom_elem_add_attr(struct dom_elem *e, char *name, char *val)
{
if (e->n_attrs == e->size_attrs) {
e->size_attrs += e->size_attrs ? e->size_attrs : 1;
e->attr_names = realloc(e->attr_names,
e->size_attrs * sizeof(*(e->attr_names)));
e->attr_vals = realloc(e->attr_vals,
e->size_attrs * sizeof(*(e->attr_vals)));
}
e->attr_names[e->n_attrs] = strdup(name);
e->attr_vals[e->n_attrs++] = strdup(val);
}
void dom_elem_add_attrd(struct dom_elem *e, char *name, double val)
{
char str[512];
sprintf(str, "%f", val);
dom_elem_add_attr(e, name, str);
}
void dom_elem_add_child(struct dom_elem *e, struct dom_elem *child)
{
if (e->n_children == e->size_children) {
e->size_children += e->size_children ? e->size_children : 1;
e->children = realloc(e->children,
e->size_children * sizeof(*(e->children)));
}
e->children[e->n_children++] = child;
}
inline void dom_append_elemv(char *sel, int elemc, struct dom_elem **elemv)
{
EM_ASM({elemAppendElemv($0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11)},
sel, elemc, elemv,
offsetof(struct dom_elem, type),
offsetof(struct dom_elem, ns),
offsetof(struct dom_elem, name),
offsetof(struct dom_elem, content),
offsetof(struct dom_elem, n_attrs),
offsetof(struct dom_elem, attr_names),
offsetof(struct dom_elem, attr_vals),
offsetof(struct dom_elem, n_children),
offsetof(struct dom_elem, children));
}
inline char *dom_get_content(char *sel)
{
return (char *)EM_ASM_INT({return elemGetContent($0)}, sel);
}
inline void dom_set_content(char *sel, char *str)
{
EM_ASM({elemSetContent($0, $1)}, sel, str);
}
inline void dom_set_value(char *sel, char *str)
{
EM_ASM({elemSetValue($0, $1)}, sel, str);
}
inline bool dom_has_class(char *sel, char *class)
{
return EM_ASM_INT({return elemHasClass($0, $1)}, sel, class);
}
inline void dom_add_class(char *sel, char *class)
{
EM_ASM({elemAddClass($0, $1)}, sel, class);
}
inline void dom_remove_class(char *sel, char *class)
{
EM_ASM({elemRemoveClass($0, $1)}, sel, class);
}
inline void dom_toggle_class(char *sel, char *class)
{
EM_ASM({elemToggleClass($0, $1)}, sel, class);
}
inline void dom_set_uniq_class(char *sel, char *class, char *sel_set)
{
EM_ASM({elemSetUniqClass($0, $1, $2)}, sel, class, sel_set);
}
inline void dom_set_style_property_int(char *prop, int val)
{
EM_ASM({rootSetStylePropertyInt($0, $1)}, prop, val);
}
inline void dom_scroll_to_bottom(char *sel)
{
EM_ASM({elemScrollToBottom($0)}, sel);
}
inline void dom_scroll_to_center_child(char *sel, char *sel_child)
{
EM_ASM({elemScrollToCenterChild($0, $1)}, sel, sel_child);
}
inline void dom_enable_exit_dialogue(bool enable)
{
EM_ASM({enableExitDialogue($0)}, enable);
}
|