xbintsc 0.6.9 → 0.6.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/checker/globals.js +2 -1
- package/dist/src/checker/globals.js.map +1 -1
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +1 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/codegen/generator/state.js +3 -0
- package/dist/src/codegen/generator/state.js.map +1 -1
- package/dist/src/codegen/generator/tables.js +3 -0
- package/dist/src/codegen/generator/tables.js.map +1 -1
- package/dist/src/driver/compiler.js +1 -1
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.js +1 -0
- package/dist/src/driver/config.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +2 -0
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/driver/version.js.map +1 -1
- package/dist/src/extensions/node/crypto/index.js +12 -1
- package/dist/src/extensions/node/crypto/index.js.map +1 -1
- package/dist/tests/driver/config.test.js +15 -2
- package/dist/tests/driver/config.test.js.map +1 -1
- package/dist/tests/e2e/compile-builtins.test.js +26 -0
- package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
- package/dist/tests/e2e/compile-node.test.js +37 -1
- package/dist/tests/e2e/compile-node.test.js.map +1 -1
- package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
- package/dist/tests/e2e/crypto-random.test.js +63 -0
- package/dist/tests/e2e/crypto-random.test.js.map +1 -0
- package/dist/tests/e2e/date-parse.test.js +27 -1
- package/dist/tests/e2e/date-parse.test.js.map +1 -1
- package/dist/tests/e2e/gui-css-coverage.test.js +1 -1
- package/dist/tests/e2e/gui-dom.test.js +29 -0
- package/dist/tests/e2e/gui-dom.test.js.map +1 -1
- package/dist/tests/e2e/gui-form.test.d.ts +9 -0
- package/dist/tests/e2e/gui-form.test.js +359 -0
- package/dist/tests/e2e/gui-form.test.js.map +1 -0
- package/dist/tests/e2e/gui-helpers.d.ts +2 -0
- package/dist/tests/e2e/gui-helpers.js +1 -0
- package/dist/tests/e2e/gui-helpers.js.map +1 -1
- package/dist/tests/e2e/gui-layout.test.js +47 -0
- package/dist/tests/e2e/gui-layout.test.js.map +1 -1
- package/dist/tests/e2e/gui-paint.test.js +108 -0
- package/dist/tests/e2e/gui-paint.test.js.map +1 -1
- package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
- package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
- package/dist/tests/e2e/gui-style.test.js +82 -0
- package/dist/tests/e2e/gui-style.test.js.map +1 -1
- package/dist/tests/e2e/gui-unsupported.test.d.ts +6 -0
- package/dist/tests/e2e/gui-unsupported.test.js +124 -0
- package/dist/tests/e2e/gui-unsupported.test.js.map +1 -0
- package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
- package/dist/tests/e2e/json-parse-errors.test.js +48 -0
- package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
- package/doc/ai/extensions.md +7 -4
- package/doc/ai/language-support.md +2 -1
- package/doc/ai/zh-CN/extensions.md +6 -3
- package/doc/ai/zh-CN/language-support.md +2 -1
- package/doc/gui/api.md +8 -2
- package/doc/gui/dom.md +81 -4
- package/doc/gui/input.md +74 -3
- package/doc/gui/known-issues.md +12 -5
- package/doc/gui/layout.md +19 -6
- package/doc/gui/rendering.md +16 -9
- package/doc/implemented/node/crypto.md +8 -0
- package/doc/implemented/node/fs.md +1 -1
- package/doc/implemented/stdlib.md +10 -2
- package/doc/zh-CN/gui/api.md +8 -2
- package/doc/zh-CN/gui/dom.md +70 -4
- package/doc/zh-CN/gui/input.md +65 -4
- package/doc/zh-CN/gui/known-issues.md +11 -4
- package/doc/zh-CN/gui/layout.md +14 -4
- package/doc/zh-CN/gui/rendering.md +13 -10
- package/doc/zh-CN/implemented/node/crypto.md +6 -0
- package/doc/zh-CN/implemented/node/fs.md +1 -1
- package/doc/zh-CN/implemented/stdlib.md +10 -2
- package/package.json +7 -7
- package/runtime/ext_gui/clip_values.cpp +11 -0
- package/runtime/ext_gui/css.cpp +27 -2
- package/runtime/ext_gui/css_match.cpp +21 -1
- package/runtime/ext_gui/css_selector.cpp +23 -0
- package/runtime/ext_gui/css_sheet.cpp +7 -0
- package/runtime/ext_gui/document.cpp +15 -0
- package/runtime/ext_gui/document_animation.cpp +4 -1
- package/runtime/ext_gui/dom.h +41 -2
- package/runtime/ext_gui/dom_api.cpp +3 -0
- package/runtime/ext_gui/dom_api_document.cpp +28 -0
- package/runtime/ext_gui/dom_api_events.cpp +17 -14
- package/runtime/ext_gui/dom_api_form.cpp +567 -0
- package/runtime/ext_gui/dom_api_internal.h +46 -214
- package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
- package/runtime/ext_gui/dom_api_node.cpp +10 -3
- package/runtime/ext_gui/dom_api_node_read.cpp +1 -1
- package/runtime/ext_gui/dom_api_proto.cpp +45 -0
- package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
- package/runtime/ext_gui/dom_parse.cpp +118 -1
- package/runtime/ext_gui/effect_values.cpp +28 -7
- package/runtime/ext_gui/form_focus.cpp +293 -0
- package/runtime/ext_gui/form_input.cpp +448 -0
- package/runtime/ext_gui/form_internal.h +30 -0
- package/runtime/ext_gui/form_shared.cpp +126 -0
- package/runtime/ext_gui/form_state.cpp +536 -0
- package/runtime/ext_gui/form_state.h +97 -0
- package/runtime/ext_gui/gradient_values.cpp +25 -5
- package/runtime/ext_gui/gui_engine.h +40 -4
- package/runtime/ext_gui/gui_events.cpp +63 -48
- package/runtime/ext_gui/inline_svg.cpp +3 -0
- package/runtime/ext_gui/layout.cpp +6 -0
- package/runtime/ext_gui/layout.h +4 -0
- package/runtime/ext_gui/layout_flow.cpp +35 -3
- package/runtime/ext_gui/layout_inline.cpp +80 -7
- package/runtime/ext_gui/media_query.cpp +7 -1
- package/runtime/ext_gui/paint.cpp +62 -24
- package/runtime/ext_gui/paint.h +7 -4
- package/runtime/ext_gui/paint_clip.cpp +74 -19
- package/runtime/ext_gui/paint_form.cpp +389 -0
- package/runtime/ext_gui/paint_internal.h +13 -1
- package/runtime/ext_gui/renderer_clip.cpp +19 -17
- package/runtime/ext_gui/style.cpp +5 -1
- package/runtime/ext_gui/style.h +5 -0
- package/runtime/ext_gui/style_cascade.cpp +2 -0
- package/runtime/ext_gui/style_internal.h +7 -2
- package/runtime/ext_gui/style_print.cpp +33 -0
- package/runtime/ext_gui/style_text.cpp +11 -0
- package/runtime/ext_gui/style_ua.cpp +42 -0
- package/runtime/ext_gui/style_values.cpp +89 -90
- package/runtime/ext_gui/transform.cpp +2 -0
- package/runtime/ext_gui/warnings.cpp +298 -0
- package/runtime/ext_gui/warnings.h +59 -0
- package/runtime/ext_gui/window.cpp +31 -55
- package/runtime/ext_gui/window_diagnostics.cpp +58 -0
- package/runtime/ext_gui/window_emit.cpp +51 -0
- package/runtime/ext_node/crypto/crypto.c +3 -2
- package/runtime/ext_node/crypto/crypto_hash.h +13 -0
- package/runtime/ext_node/crypto/crypto_random.c +339 -0
- package/runtime/ext_node/fs/async_worker.c +1 -1
- package/runtime/ext_node/fs/fs_ops.c +1 -1
- package/runtime/ext_node/fs/parts/platform.inc +65 -0
- package/runtime/rt.h +27 -0
- package/runtime/xt_crypto.c +132 -0
- package/runtime/xt_random.c +143 -0
- package/runtime/xt_regex/matcher.inc +9 -28
- package/runtime/xt_regex/parser.inc +65 -2
- package/runtime/xt_regex/types.inc +11 -1
- package/runtime/xt_regex.h +6 -2
- package/runtime/xt_stdlib/dispatch.inc +2 -0
- package/runtime/xt_stdlib/math.inc +1 -1
- package/runtime/xt_stdlib2/date.inc +204 -4
- package/runtime/xt_stdlib2/json.inc +4 -546
- package/runtime/xt_stdlib2/json_parse.inc +347 -0
- package/runtime/xt_stdlib2/json_stringify.inc +368 -0
- package/runtime/xt_stdlib2/regexp-match.inc +6 -11
- package/runtime/xt_stdlib2.c +0 -4
- package/runtime/xt_typed_array/elements.inc +6 -1
- package/src/checker/globals.ts +2 -0
- package/src/cli/main.ts +1 -0
- package/src/codegen/generator/state.ts +3 -0
- package/src/codegen/generator/tables.ts +3 -0
- package/src/driver/compiler.ts +1 -1
- package/src/driver/config.ts +1 -0
- package/src/driver/runtime-objects.ts +2 -0
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/crypto/index.ts +12 -1
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — form-control state helpers.
|
|
3
|
+
*
|
|
4
|
+
* The engine is byte-oriented (UTF-8) while the DOM selection API is
|
|
5
|
+
* UTF-16-code-unit-oriented, so the conversions at the bottom are the single
|
|
6
|
+
* place that maps between the two. Everything else classifies an element,
|
|
7
|
+
* initialises its live value from the authored markup, or measures the
|
|
8
|
+
* intrinsic size a control gets when CSS leaves it `auto`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
#include "form_state.h"
|
|
12
|
+
|
|
13
|
+
#include <algorithm>
|
|
14
|
+
#include <cctype>
|
|
15
|
+
#include <cstdlib>
|
|
16
|
+
#include <vector>
|
|
17
|
+
|
|
18
|
+
#include "layout.h"
|
|
19
|
+
|
|
20
|
+
namespace xtgui {
|
|
21
|
+
|
|
22
|
+
namespace {
|
|
23
|
+
|
|
24
|
+
std::string lowerAscii(const std::string &text) {
|
|
25
|
+
std::string out = text;
|
|
26
|
+
for (char &c : out) c = (char)std::tolower((unsigned char)c);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
std::string trimAscii(const std::string &text) {
|
|
31
|
+
size_t begin = 0;
|
|
32
|
+
size_t end = text.size();
|
|
33
|
+
while (begin < end && (unsigned char)text[begin] <= ' ') begin++;
|
|
34
|
+
while (end > begin && (unsigned char)text[end - 1] <= ' ') end--;
|
|
35
|
+
return text.substr(begin, end - begin);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Decode one UTF-8/WTF-8 code point; invalid bytes are taken verbatim. */
|
|
39
|
+
uint32_t decodeCodePoint(const std::string &text, size_t offset, size_t *width) {
|
|
40
|
+
const unsigned char *s = (const unsigned char *)text.data();
|
|
41
|
+
size_t length = text.size();
|
|
42
|
+
unsigned char c = s[offset];
|
|
43
|
+
if (c < 0x80) {
|
|
44
|
+
*width = 1;
|
|
45
|
+
return c;
|
|
46
|
+
}
|
|
47
|
+
if ((c & 0xE0) == 0xC0 && offset + 1 < length) {
|
|
48
|
+
*width = 2;
|
|
49
|
+
return ((uint32_t)(c & 0x1F) << 6) | (s[offset + 1] & 0x3F);
|
|
50
|
+
}
|
|
51
|
+
if ((c & 0xF0) == 0xE0 && offset + 2 < length) {
|
|
52
|
+
*width = 3;
|
|
53
|
+
return ((uint32_t)(c & 0x0F) << 12) | ((uint32_t)(s[offset + 1] & 0x3F) << 6) |
|
|
54
|
+
(s[offset + 2] & 0x3F);
|
|
55
|
+
}
|
|
56
|
+
if ((c & 0xF8) == 0xF0 && offset + 3 < length) {
|
|
57
|
+
*width = 4;
|
|
58
|
+
return ((uint32_t)(c & 0x07) << 18) | ((uint32_t)(s[offset + 1] & 0x3F) << 12) |
|
|
59
|
+
((uint32_t)(s[offset + 2] & 0x3F) << 6) | (s[offset + 3] & 0x3F);
|
|
60
|
+
}
|
|
61
|
+
*width = 1;
|
|
62
|
+
return c;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
int codePointUnits(uint32_t code) { return code >= 0x10000 ? 2 : 1; }
|
|
66
|
+
|
|
67
|
+
void collectOptions(const Node *node, std::vector<const Node *> *out) {
|
|
68
|
+
if (node->isElement() && node->isTag("option")) out->push_back(node);
|
|
69
|
+
for (const std::unique_ptr<Node> &child : node->children) collectOptions(child.get(), out);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void collectText(const Node *node, std::string *out) {
|
|
73
|
+
if (node->isText()) {
|
|
74
|
+
*out += node->text;
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
for (const std::unique_ptr<Node> &child : node->children) collectText(child.get(), out);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
std::string collapseWhitespace(const std::string &text) {
|
|
81
|
+
std::string out;
|
|
82
|
+
bool pending_space = false;
|
|
83
|
+
for (char c : text) {
|
|
84
|
+
if ((unsigned char)c <= ' ') {
|
|
85
|
+
pending_space = !out.empty();
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (pending_space) {
|
|
89
|
+
out.push_back(' ');
|
|
90
|
+
pending_space = false;
|
|
91
|
+
}
|
|
92
|
+
out.push_back(c);
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Parse the integer value of a `size`/`cols`/`rows` attribute. */
|
|
98
|
+
int intAttribute(const Node *node, const char *name, int fallback, int minimum) {
|
|
99
|
+
const std::string *value = node->attr(name);
|
|
100
|
+
if (value == nullptr) return fallback;
|
|
101
|
+
int parsed = (int)std::strtol(value->c_str(), nullptr, 10);
|
|
102
|
+
return parsed >= minimum ? parsed : fallback;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const Node *firstOption(const Node *select) {
|
|
106
|
+
if (select == nullptr) return nullptr;
|
|
107
|
+
std::vector<const Node *> options;
|
|
108
|
+
collectOptions(select, &options);
|
|
109
|
+
return options.empty() ? nullptr : options.front();
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const Node *optionAt(const Node *select, int index) {
|
|
113
|
+
if (select == nullptr || index < 0) return nullptr;
|
|
114
|
+
std::vector<const Node *> options;
|
|
115
|
+
collectOptions(select, &options);
|
|
116
|
+
if ((size_t)index >= options.size()) return nullptr;
|
|
117
|
+
return options[(size_t)index];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
} // namespace
|
|
121
|
+
|
|
122
|
+
/* -- classification ------------------------------------------------------- */
|
|
123
|
+
|
|
124
|
+
bool xt_form_is_input(const Node *node) {
|
|
125
|
+
return node != nullptr && node->isElement() && node->tag == "input";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
bool xt_form_is_textarea(const Node *node) {
|
|
129
|
+
return node != nullptr && node->isElement() && node->tag == "textarea";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
bool xt_form_is_select(const Node *node) {
|
|
133
|
+
return node != nullptr && node->isElement() && node->tag == "select";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
bool xt_form_is_control(const Node *node) {
|
|
137
|
+
return xt_form_is_input(node) || xt_form_is_textarea(node) || xt_form_is_select(node);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
std::string xt_form_type(const Node *node) {
|
|
141
|
+
if (xt_form_is_textarea(node)) return "textarea";
|
|
142
|
+
if (xt_form_is_select(node)) return "select-one";
|
|
143
|
+
if (node == nullptr || !node->isElement()) return "text";
|
|
144
|
+
if (node->tag == "button") return "button";
|
|
145
|
+
if (!xt_form_is_input(node)) return "";
|
|
146
|
+
const std::string *attr = node->attr("type");
|
|
147
|
+
std::string type = attr != nullptr ? lowerAscii(trimAscii(*attr)) : "text";
|
|
148
|
+
if (type.empty()) type = "text";
|
|
149
|
+
/* HTML's aliases: `datetime` is `datetime-local`, and an unknown type behaves
|
|
150
|
+
* as `text` (including the reflected `type` property). */
|
|
151
|
+
if (type == "datetime") return "datetime-local";
|
|
152
|
+
static const char *const known[] = {
|
|
153
|
+
"text", "search", "tel", "url", "email", "password",
|
|
154
|
+
"date", "month", "week", "time", "datetime-local", "number",
|
|
155
|
+
"range", "color", "checkbox", "radio", "file", "submit",
|
|
156
|
+
"image", "reset", "button", "hidden",
|
|
157
|
+
};
|
|
158
|
+
for (const char *name : known) {
|
|
159
|
+
if (type == name) return type;
|
|
160
|
+
}
|
|
161
|
+
return "text";
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
bool xt_form_is_text_like(const Node *node) {
|
|
165
|
+
if (!xt_form_is_control(node)) return false;
|
|
166
|
+
if (xt_form_is_textarea(node)) return true;
|
|
167
|
+
std::string type = xt_form_type(node);
|
|
168
|
+
return type == "text" || type == "search" || type == "tel" || type == "url" ||
|
|
169
|
+
type == "email" || type == "password" || type == "number" || type == "date" ||
|
|
170
|
+
type == "month" || type == "week" || type == "time" || type == "datetime-local";
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
bool xt_form_is_button_like(const Node *node) {
|
|
174
|
+
if (!xt_form_is_input(node)) return false;
|
|
175
|
+
std::string type = xt_form_type(node);
|
|
176
|
+
return type == "button" || type == "submit" || type == "reset" || type == "image";
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
bool xt_form_is_checkable(const Node *node) {
|
|
180
|
+
if (!xt_form_is_input(node)) return false;
|
|
181
|
+
std::string type = xt_form_type(node);
|
|
182
|
+
return type == "checkbox" || type == "radio";
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
bool xt_form_is_disabled(const Node *node) {
|
|
186
|
+
return node != nullptr && node->isElement() && node->attr("disabled") != nullptr;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
bool xt_form_is_readonly(const Node *node) {
|
|
190
|
+
return node != nullptr && node->isElement() && node->attr("readonly") != nullptr;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
bool xt_form_is_focusable(const Node *node) {
|
|
194
|
+
if (node == nullptr || !node->isElement() || node->is_pseudo) return false;
|
|
195
|
+
if (xt_form_is_disabled(node)) return false;
|
|
196
|
+
if (xt_form_is_control(node)) {
|
|
197
|
+
if (xt_form_is_input(node) && xt_form_type(node) == "hidden") return false;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
if (node->tag == "button") return true;
|
|
201
|
+
if (node->tag == "a" && node->attr("href") != nullptr) return true;
|
|
202
|
+
if (node->attr("contenteditable") != nullptr) return true;
|
|
203
|
+
const std::string *tabindex = node->attr("tabindex");
|
|
204
|
+
if (tabindex != nullptr) {
|
|
205
|
+
int value = (int)std::strtol(tabindex->c_str(), nullptr, 10);
|
|
206
|
+
return value >= -1;
|
|
207
|
+
}
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/* -- state ---------------------------------------------------------------- */
|
|
212
|
+
|
|
213
|
+
std::string xt_form_option_label(const Node *option) {
|
|
214
|
+
if (option == nullptr) return "";
|
|
215
|
+
std::string text;
|
|
216
|
+
collectText(option, &text);
|
|
217
|
+
return collapseWhitespace(text);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
std::string xt_form_option_value(const Node *option) {
|
|
221
|
+
if (option == nullptr) return "";
|
|
222
|
+
const std::string *value = option->attr("value");
|
|
223
|
+
return value != nullptr ? *value : xt_form_option_label(option);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
Node *xt_form_selected_option(Node *select) {
|
|
227
|
+
return const_cast<Node *>(xt_form_selected_option(static_cast<const Node *>(select)));
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const Node *xt_form_selected_option(const Node *select) {
|
|
231
|
+
if (!xt_form_is_select(select)) return nullptr;
|
|
232
|
+
const FormState *state = xt_form_state(select);
|
|
233
|
+
if (state == nullptr) return nullptr;
|
|
234
|
+
const Node *option = optionAt(select, state->selected_index);
|
|
235
|
+
if (option == nullptr) option = firstOption(select);
|
|
236
|
+
return option;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
FormState *xt_form_state(Node *node) {
|
|
240
|
+
if (!xt_form_is_control(node)) return nullptr;
|
|
241
|
+
if (!node->form) node->form = std::unique_ptr<FormState>(new FormState());
|
|
242
|
+
if (!node->form->initialized) xt_form_init(node);
|
|
243
|
+
return node->form.get();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const FormState *xt_form_state(const Node *node) {
|
|
247
|
+
return xt_form_state(const_cast<Node *>(node));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
void xt_form_init(Node *node) {
|
|
251
|
+
if (!xt_form_is_control(node)) return;
|
|
252
|
+
if (node->form && node->form->initialized) return;
|
|
253
|
+
if (!node->form) node->form = std::unique_ptr<FormState>(new FormState());
|
|
254
|
+
FormState &state = *node->form;
|
|
255
|
+
if (xt_form_is_textarea(node)) {
|
|
256
|
+
std::string text;
|
|
257
|
+
collectText(node, &text);
|
|
258
|
+
state.value = text;
|
|
259
|
+
} else if (xt_form_is_select(node)) {
|
|
260
|
+
std::vector<const Node *> options;
|
|
261
|
+
collectOptions(node, &options);
|
|
262
|
+
int selected = -1;
|
|
263
|
+
for (size_t i = 0; i < options.size(); i++) {
|
|
264
|
+
if (options[i]->attr("selected") != nullptr) {
|
|
265
|
+
selected = (int)i;
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (selected < 0 && !options.empty()) selected = 0;
|
|
270
|
+
state.selected_index = selected;
|
|
271
|
+
const Node *option = optionAt(node, selected);
|
|
272
|
+
state.value = option != nullptr ? xt_form_option_value(option) : "";
|
|
273
|
+
} else {
|
|
274
|
+
std::string type = xt_form_type(node);
|
|
275
|
+
if (type == "checkbox" || type == "radio") {
|
|
276
|
+
state.checked = node->attr("checked") != nullptr;
|
|
277
|
+
state.checked_initialized = true;
|
|
278
|
+
const std::string *value = node->attr("value");
|
|
279
|
+
state.value = value != nullptr ? *value : "on";
|
|
280
|
+
} else if (type == "range") {
|
|
281
|
+
const std::string *value = node->attr("value");
|
|
282
|
+
state.value = value != nullptr ? *value : "50";
|
|
283
|
+
} else if (type == "color") {
|
|
284
|
+
const std::string *value = node->attr("value");
|
|
285
|
+
state.value = value != nullptr ? *value : "#000000";
|
|
286
|
+
} else {
|
|
287
|
+
const std::string *value = node->attr("value");
|
|
288
|
+
state.value = value != nullptr ? *value : "";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
state.selection_start = (int)xt_form_utf16_length(state.value);
|
|
292
|
+
state.selection_end = state.selection_start;
|
|
293
|
+
state.initialized = true;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
std::string xt_form_value(const Node *node) {
|
|
297
|
+
const FormState *state = xt_form_state(node);
|
|
298
|
+
if (state == nullptr) return "";
|
|
299
|
+
if (xt_form_is_select(node)) {
|
|
300
|
+
const Node *option = xt_form_selected_option(node);
|
|
301
|
+
return option != nullptr ? xt_form_option_value(option) : "";
|
|
302
|
+
}
|
|
303
|
+
return state->value;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
void xt_form_set_value(Node *node, const std::string &value) {
|
|
307
|
+
FormState *state = xt_form_state(node);
|
|
308
|
+
if (state == nullptr) return;
|
|
309
|
+
if (xt_form_is_select(node)) {
|
|
310
|
+
std::vector<const Node *> options;
|
|
311
|
+
collectOptions(node, &options);
|
|
312
|
+
for (size_t i = 0; i < options.size(); i++) {
|
|
313
|
+
if (xt_form_option_value(options[i]) == value) {
|
|
314
|
+
state->selected_index = (int)i;
|
|
315
|
+
state->value = value;
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
state->value = value;
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
state->value = value;
|
|
323
|
+
/* Assigning `value` moves the caret to the end, as browsers do. */
|
|
324
|
+
size_t length = xt_form_utf16_length(value);
|
|
325
|
+
state->selection_start = (int)length;
|
|
326
|
+
state->selection_end = (int)length;
|
|
327
|
+
state->selection_direction = 0;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
bool xt_form_checked(const Node *node) {
|
|
331
|
+
const FormState *state = xt_form_state(node);
|
|
332
|
+
return state != nullptr && state->checked;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
void xt_form_set_checked(Node *node, bool checked) {
|
|
336
|
+
FormState *state = xt_form_state(node);
|
|
337
|
+
if (state == nullptr) return;
|
|
338
|
+
state->checked = checked;
|
|
339
|
+
state->checked_initialized = true;
|
|
340
|
+
if (checked && xt_form_type(node) == "radio") xt_form_radio_group_set(node);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
bool xt_form_radio_group_set(Node *node) {
|
|
344
|
+
if (!xt_form_is_checkable(node) || xt_form_type(node) != "radio") return false;
|
|
345
|
+
const std::string *name = node->attr("name");
|
|
346
|
+
if (name == nullptr || name->empty()) return false;
|
|
347
|
+
const Node *root = node;
|
|
348
|
+
while (root->parent != nullptr) root = root->parent;
|
|
349
|
+
bool changed = false;
|
|
350
|
+
struct Walk {
|
|
351
|
+
const Node *root;
|
|
352
|
+
const Node *self;
|
|
353
|
+
const std::string &name;
|
|
354
|
+
bool *changed;
|
|
355
|
+
void visit(const Node *current) const {
|
|
356
|
+
if (current != self && current->isElement() && current->isTag("input") &&
|
|
357
|
+
xt_form_type(current) == "radio") {
|
|
358
|
+
const std::string *other = current->attr("name");
|
|
359
|
+
if (other != nullptr && *other == name) {
|
|
360
|
+
FormState *state = xt_form_state(const_cast<Node *>(current));
|
|
361
|
+
if (state != nullptr && state->checked) {
|
|
362
|
+
state->checked = false;
|
|
363
|
+
*changed = true;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const std::unique_ptr<Node> &child : current->children) visit(child.get());
|
|
368
|
+
}
|
|
369
|
+
} walk = {root, node, *name, &changed};
|
|
370
|
+
walk.visit(root);
|
|
371
|
+
return changed;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
std::string xt_form_button_label(const Node *node) {
|
|
375
|
+
if (node == nullptr) return "";
|
|
376
|
+
const std::string *value = node->attr("value");
|
|
377
|
+
if (value != nullptr && !value->empty()) return *value;
|
|
378
|
+
std::string type = xt_form_type(node);
|
|
379
|
+
if (type == "submit") return "Submit";
|
|
380
|
+
if (type == "reset") return "Reset";
|
|
381
|
+
if (type == "image") return "Submit";
|
|
382
|
+
return "Button";
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/* -- intrinsic size ------------------------------------------------------- */
|
|
386
|
+
|
|
387
|
+
void xt_form_intrinsic_size(const Node *node, const XtStyle &style, float *width, float *height) {
|
|
388
|
+
*width = 0.0f;
|
|
389
|
+
*height = 0.0f;
|
|
390
|
+
if (!xt_form_is_control(node)) return;
|
|
391
|
+
float line_height = style.font_size * 1.2f;
|
|
392
|
+
float char_width = xt_layout_text_width("0", style);
|
|
393
|
+
if (char_width <= 0.0f) char_width = style.font_size * 0.5f;
|
|
394
|
+
if (xt_form_is_textarea(node)) {
|
|
395
|
+
int cols = intAttribute(node, "cols", 20, 1);
|
|
396
|
+
int rows = intAttribute(node, "rows", 2, 1);
|
|
397
|
+
*width = char_width * (float)cols;
|
|
398
|
+
*height = line_height * (float)rows;
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (xt_form_is_select(node)) {
|
|
402
|
+
std::vector<const Node *> options;
|
|
403
|
+
collectOptions(node, &options);
|
|
404
|
+
float widest = 0.0f;
|
|
405
|
+
for (const Node *option : options) {
|
|
406
|
+
widest = std::max(widest, xt_layout_text_width(xt_form_option_label(option), style));
|
|
407
|
+
}
|
|
408
|
+
*width = widest + 22.0f;
|
|
409
|
+
*height = line_height + 4.0f;
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
std::string type = xt_form_type(node);
|
|
413
|
+
if (type == "hidden") return;
|
|
414
|
+
if (type == "checkbox" || type == "radio") {
|
|
415
|
+
*width = 13.0f;
|
|
416
|
+
*height = 13.0f;
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (type == "range") {
|
|
420
|
+
*width = 129.0f;
|
|
421
|
+
*height = 20.0f;
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (type == "color") {
|
|
425
|
+
*width = 44.0f;
|
|
426
|
+
*height = 20.0f;
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (type == "file") {
|
|
430
|
+
*width = xt_layout_text_width("Choose File", style) + 16.0f;
|
|
431
|
+
*height = line_height + 4.0f;
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (xt_form_is_button_like(node)) {
|
|
435
|
+
*width = xt_layout_text_width(xt_form_button_label(node), style) + 16.0f;
|
|
436
|
+
*height = line_height + 6.0f;
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
int size = intAttribute(node, "size", 20, 1);
|
|
440
|
+
*width = char_width * (float)size;
|
|
441
|
+
*height = line_height + 2.0f;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/* -- UTF-8 / UTF-16 ------------------------------------------------------- */
|
|
445
|
+
|
|
446
|
+
size_t xt_form_utf16_length(const std::string &text) {
|
|
447
|
+
size_t units = 0;
|
|
448
|
+
size_t offset = 0;
|
|
449
|
+
while (offset < text.size()) {
|
|
450
|
+
size_t width = 0;
|
|
451
|
+
uint32_t code = decodeCodePoint(text, offset, &width);
|
|
452
|
+
units += (size_t)codePointUnits(code);
|
|
453
|
+
offset += width;
|
|
454
|
+
}
|
|
455
|
+
return units;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
size_t xt_form_utf16_byte(const std::string &text, size_t units) {
|
|
459
|
+
size_t offset = 0;
|
|
460
|
+
size_t current = 0;
|
|
461
|
+
while (offset < text.size()) {
|
|
462
|
+
if (current >= units) return offset;
|
|
463
|
+
size_t width = 0;
|
|
464
|
+
uint32_t code = decodeCodePoint(text, offset, &width);
|
|
465
|
+
int count = codePointUnits(code);
|
|
466
|
+
/* A target inside a surrogate pair snaps to the code point's first byte;
|
|
467
|
+
* UTF-8 cannot represent half of a supplementary character. */
|
|
468
|
+
if (current + (size_t)count > units) return offset;
|
|
469
|
+
current += (size_t)count;
|
|
470
|
+
offset += width;
|
|
471
|
+
}
|
|
472
|
+
return text.size();
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
size_t xt_form_utf16_index(const std::string &text, size_t bytes) {
|
|
476
|
+
size_t offset = 0;
|
|
477
|
+
size_t units = 0;
|
|
478
|
+
while (offset < text.size() && offset < bytes) {
|
|
479
|
+
size_t width = 0;
|
|
480
|
+
uint32_t code = decodeCodePoint(text, offset, &width);
|
|
481
|
+
units += (size_t)codePointUnits(code);
|
|
482
|
+
offset += width;
|
|
483
|
+
}
|
|
484
|
+
return units;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
std::string xt_form_utf16_slice(const std::string &text, size_t start_units, size_t end_units) {
|
|
488
|
+
size_t begin = xt_form_utf16_byte(text, start_units);
|
|
489
|
+
size_t end = xt_form_utf16_byte(text, end_units);
|
|
490
|
+
if (end <= begin) return "";
|
|
491
|
+
return text.substr(begin, end - begin);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
size_t xt_form_utf16_prev(const std::string &text, size_t units) {
|
|
495
|
+
if (units == 0) return 0;
|
|
496
|
+
size_t offset = 0;
|
|
497
|
+
size_t current = 0;
|
|
498
|
+
size_t previous = 0;
|
|
499
|
+
while (offset < text.size() && current < units) {
|
|
500
|
+
size_t width = 0;
|
|
501
|
+
uint32_t code = decodeCodePoint(text, offset, &width);
|
|
502
|
+
previous = current;
|
|
503
|
+
current += (size_t)codePointUnits(code);
|
|
504
|
+
offset += width;
|
|
505
|
+
}
|
|
506
|
+
return previous;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
size_t xt_form_utf16_next(const std::string &text, size_t units) {
|
|
510
|
+
size_t offset = 0;
|
|
511
|
+
size_t current = 0;
|
|
512
|
+
while (offset < text.size()) {
|
|
513
|
+
size_t width = 0;
|
|
514
|
+
uint32_t code = decodeCodePoint(text, offset, &width);
|
|
515
|
+
int count = codePointUnits(code);
|
|
516
|
+
if (current >= units) return current + (size_t)count;
|
|
517
|
+
current += (size_t)count;
|
|
518
|
+
offset += width;
|
|
519
|
+
}
|
|
520
|
+
return current;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
std::string xt_form_mask(const std::string &text) {
|
|
524
|
+
static const char kBullet[] = "\xE2\x80\xA2";
|
|
525
|
+
std::string out;
|
|
526
|
+
size_t offset = 0;
|
|
527
|
+
while (offset < text.size()) {
|
|
528
|
+
size_t width = 0;
|
|
529
|
+
(void)decodeCodePoint(text, offset, &width);
|
|
530
|
+
out.append(kBullet, 3);
|
|
531
|
+
offset += width;
|
|
532
|
+
}
|
|
533
|
+
return out;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
} // namespace xtgui
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc GUI engine — form-control classification, value state and the
|
|
3
|
+
* UTF-8/UTF-16 conversions the DOM selection API needs.
|
|
4
|
+
*
|
|
5
|
+
* A form control's live value lives in `Node::form` (`FormState`). These
|
|
6
|
+
* helpers are shared by the DOM bindings (`dom_api_form.cpp`), input dispatch
|
|
7
|
+
* (`form_input.cpp`), layout (`layout_flow.cpp`) and paint (`paint_form.cpp`),
|
|
8
|
+
* so "what kind of control is this" and "which bytes sit at UTF-16 offset N"
|
|
9
|
+
* are answered one way everywhere.
|
|
10
|
+
*/
|
|
11
|
+
#ifndef XT_GUI_FORM_STATE_H
|
|
12
|
+
#define XT_GUI_FORM_STATE_H
|
|
13
|
+
|
|
14
|
+
#include <cstddef>
|
|
15
|
+
#include <string>
|
|
16
|
+
|
|
17
|
+
#include "dom.h"
|
|
18
|
+
|
|
19
|
+
namespace xtgui {
|
|
20
|
+
|
|
21
|
+
/* -- classification ------------------------------------------------------- */
|
|
22
|
+
|
|
23
|
+
bool xt_form_is_input(const Node *node);
|
|
24
|
+
bool xt_form_is_textarea(const Node *node);
|
|
25
|
+
bool xt_form_is_select(const Node *node);
|
|
26
|
+
/** `<input>`, `<textarea>` or `<select>`. */
|
|
27
|
+
bool xt_form_is_control(const Node *node);
|
|
28
|
+
/** The input type, lowercased and normalised (`"text"` when absent/unknown).
|
|
29
|
+
* `textarea`/`select` answer their tag names. */
|
|
30
|
+
std::string xt_form_type(const Node *node);
|
|
31
|
+
/** Single- or multi-line editable text (`input` text family or `textarea`). */
|
|
32
|
+
bool xt_form_is_text_like(const Node *node);
|
|
33
|
+
/** `input type=button|submit|reset|image`. */
|
|
34
|
+
bool xt_form_is_button_like(const Node *node);
|
|
35
|
+
/** `checkbox` or `radio`. */
|
|
36
|
+
bool xt_form_is_checkable(const Node *node);
|
|
37
|
+
bool xt_form_is_disabled(const Node *node);
|
|
38
|
+
bool xt_form_is_readonly(const Node *node);
|
|
39
|
+
/** Whether the element can receive focus (and therefore keyboard input). */
|
|
40
|
+
bool xt_form_is_focusable(const Node *node);
|
|
41
|
+
|
|
42
|
+
/* -- state ---------------------------------------------------------------- */
|
|
43
|
+
|
|
44
|
+
/** The control's state, creating and initialising it on first use. */
|
|
45
|
+
FormState *xt_form_state(Node *node);
|
|
46
|
+
const FormState *xt_form_state(const Node *node);
|
|
47
|
+
/** Read the initial value from the `value` attribute / `<textarea>` text /
|
|
48
|
+
* selected `<option>`. A no-op once initialised. */
|
|
49
|
+
void xt_form_init(Node *node);
|
|
50
|
+
std::string xt_form_value(const Node *node);
|
|
51
|
+
void xt_form_set_value(Node *node, const std::string &value);
|
|
52
|
+
bool xt_form_checked(const Node *node);
|
|
53
|
+
void xt_form_set_checked(Node *node, bool checked);
|
|
54
|
+
/** Uncheck every other radio in the same group (same `name`). Returns true when
|
|
55
|
+
* something changed. */
|
|
56
|
+
bool xt_form_radio_group_set(Node *node);
|
|
57
|
+
|
|
58
|
+
/* -- button/option labels ------------------------------------------------- */
|
|
59
|
+
|
|
60
|
+
/** The label an `<input type=button|submit|reset>` paints: its `value` or the
|
|
61
|
+
* UA default (`Submit`, `Reset`, `Button`). */
|
|
62
|
+
std::string xt_form_button_label(const Node *node);
|
|
63
|
+
/** An `<option>`'s submitted value (`value` attribute, else its text). */
|
|
64
|
+
std::string xt_form_option_value(const Node *option);
|
|
65
|
+
/** An `<option>`'s visible text (whitespace-collapsed). */
|
|
66
|
+
std::string xt_form_option_label(const Node *option);
|
|
67
|
+
/** The currently selected `<option>` of a `<select>`, or null. */
|
|
68
|
+
Node *xt_form_selected_option(Node *select);
|
|
69
|
+
const Node *xt_form_selected_option(const Node *select);
|
|
70
|
+
|
|
71
|
+
/* -- intrinsic size ------------------------------------------------------- */
|
|
72
|
+
|
|
73
|
+
/** The content-box size a control gets when `width`/`height` are `auto`.
|
|
74
|
+
* `style` is the control's computed style (needed to measure its text). */
|
|
75
|
+
void xt_form_intrinsic_size(const Node *node, const struct XtStyle &style, float *width,
|
|
76
|
+
float *height);
|
|
77
|
+
|
|
78
|
+
/* -- UTF-8 / UTF-16 ------------------------------------------------------- */
|
|
79
|
+
|
|
80
|
+
/** JavaScript string length: UTF-16 code units. */
|
|
81
|
+
size_t xt_form_utf16_length(const std::string &text);
|
|
82
|
+
/** Byte offset of UTF-16 code-unit index `units` (clamped to the text end). */
|
|
83
|
+
size_t xt_form_utf16_byte(const std::string &text, size_t units);
|
|
84
|
+
/** UTF-16 index of byte offset `bytes` (clamped to the text end). */
|
|
85
|
+
size_t xt_form_utf16_index(const std::string &text, size_t bytes);
|
|
86
|
+
/** `text.slice(startUnits, endUnits)` as UTF-8 bytes. */
|
|
87
|
+
std::string xt_form_utf16_slice(const std::string &text, size_t start_units, size_t end_units);
|
|
88
|
+
/** Step one code point left/right in UTF-16 units. */
|
|
89
|
+
size_t xt_form_utf16_prev(const std::string &text, size_t units);
|
|
90
|
+
size_t xt_form_utf16_next(const std::string &text, size_t units);
|
|
91
|
+
|
|
92
|
+
/** Replace every code point with U+2022 (password field masking). */
|
|
93
|
+
std::string xt_form_mask(const std::string &text);
|
|
94
|
+
|
|
95
|
+
} // namespace xtgui
|
|
96
|
+
|
|
97
|
+
#endif /* XT_GUI_FORM_STATE_H */
|