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.
Files changed (164) hide show
  1. package/dist/src/checker/globals.js +2 -1
  2. package/dist/src/checker/globals.js.map +1 -1
  3. package/dist/src/cli/help.d.ts +1 -1
  4. package/dist/src/cli/main.js +1 -0
  5. package/dist/src/cli/main.js.map +1 -1
  6. package/dist/src/codegen/generator/state.js +3 -0
  7. package/dist/src/codegen/generator/state.js.map +1 -1
  8. package/dist/src/codegen/generator/tables.js +3 -0
  9. package/dist/src/codegen/generator/tables.js.map +1 -1
  10. package/dist/src/driver/compiler.js +1 -1
  11. package/dist/src/driver/compiler.js.map +1 -1
  12. package/dist/src/driver/config.js +1 -0
  13. package/dist/src/driver/config.js.map +1 -1
  14. package/dist/src/driver/runtime-objects.d.ts +1 -1
  15. package/dist/src/driver/runtime-objects.js +2 -0
  16. package/dist/src/driver/runtime-objects.js.map +1 -1
  17. package/dist/src/driver/version.d.ts +1 -1
  18. package/dist/src/driver/version.js +1 -1
  19. package/dist/src/driver/version.js.map +1 -1
  20. package/dist/src/extensions/node/crypto/index.js +12 -1
  21. package/dist/src/extensions/node/crypto/index.js.map +1 -1
  22. package/dist/tests/driver/config.test.js +15 -2
  23. package/dist/tests/driver/config.test.js.map +1 -1
  24. package/dist/tests/e2e/compile-builtins.test.js +26 -0
  25. package/dist/tests/e2e/compile-builtins.test.js.map +1 -1
  26. package/dist/tests/e2e/compile-node.test.js +37 -1
  27. package/dist/tests/e2e/compile-node.test.js.map +1 -1
  28. package/dist/tests/e2e/crypto-random.test.d.ts +9 -0
  29. package/dist/tests/e2e/crypto-random.test.js +63 -0
  30. package/dist/tests/e2e/crypto-random.test.js.map +1 -0
  31. package/dist/tests/e2e/date-parse.test.js +27 -1
  32. package/dist/tests/e2e/date-parse.test.js.map +1 -1
  33. package/dist/tests/e2e/gui-css-coverage.test.js +1 -1
  34. package/dist/tests/e2e/gui-dom.test.js +29 -0
  35. package/dist/tests/e2e/gui-dom.test.js.map +1 -1
  36. package/dist/tests/e2e/gui-form.test.d.ts +9 -0
  37. package/dist/tests/e2e/gui-form.test.js +359 -0
  38. package/dist/tests/e2e/gui-form.test.js.map +1 -0
  39. package/dist/tests/e2e/gui-helpers.d.ts +2 -0
  40. package/dist/tests/e2e/gui-helpers.js +1 -0
  41. package/dist/tests/e2e/gui-helpers.js.map +1 -1
  42. package/dist/tests/e2e/gui-layout.test.js +47 -0
  43. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  44. package/dist/tests/e2e/gui-paint.test.js +108 -0
  45. package/dist/tests/e2e/gui-paint.test.js.map +1 -1
  46. package/dist/tests/e2e/gui-script-import-order.test.js +6 -1
  47. package/dist/tests/e2e/gui-script-import-order.test.js.map +1 -1
  48. package/dist/tests/e2e/gui-style.test.js +82 -0
  49. package/dist/tests/e2e/gui-style.test.js.map +1 -1
  50. package/dist/tests/e2e/gui-unsupported.test.d.ts +6 -0
  51. package/dist/tests/e2e/gui-unsupported.test.js +124 -0
  52. package/dist/tests/e2e/gui-unsupported.test.js.map +1 -0
  53. package/dist/tests/e2e/json-parse-errors.test.d.ts +9 -0
  54. package/dist/tests/e2e/json-parse-errors.test.js +48 -0
  55. package/dist/tests/e2e/json-parse-errors.test.js.map +1 -0
  56. package/doc/ai/extensions.md +7 -4
  57. package/doc/ai/language-support.md +2 -1
  58. package/doc/ai/zh-CN/extensions.md +6 -3
  59. package/doc/ai/zh-CN/language-support.md +2 -1
  60. package/doc/gui/api.md +8 -2
  61. package/doc/gui/dom.md +81 -4
  62. package/doc/gui/input.md +74 -3
  63. package/doc/gui/known-issues.md +12 -5
  64. package/doc/gui/layout.md +19 -6
  65. package/doc/gui/rendering.md +16 -9
  66. package/doc/implemented/node/crypto.md +8 -0
  67. package/doc/implemented/node/fs.md +1 -1
  68. package/doc/implemented/stdlib.md +10 -2
  69. package/doc/zh-CN/gui/api.md +8 -2
  70. package/doc/zh-CN/gui/dom.md +70 -4
  71. package/doc/zh-CN/gui/input.md +65 -4
  72. package/doc/zh-CN/gui/known-issues.md +11 -4
  73. package/doc/zh-CN/gui/layout.md +14 -4
  74. package/doc/zh-CN/gui/rendering.md +13 -10
  75. package/doc/zh-CN/implemented/node/crypto.md +6 -0
  76. package/doc/zh-CN/implemented/node/fs.md +1 -1
  77. package/doc/zh-CN/implemented/stdlib.md +10 -2
  78. package/package.json +7 -7
  79. package/runtime/ext_gui/clip_values.cpp +11 -0
  80. package/runtime/ext_gui/css.cpp +27 -2
  81. package/runtime/ext_gui/css_match.cpp +21 -1
  82. package/runtime/ext_gui/css_selector.cpp +23 -0
  83. package/runtime/ext_gui/css_sheet.cpp +7 -0
  84. package/runtime/ext_gui/document.cpp +15 -0
  85. package/runtime/ext_gui/document_animation.cpp +4 -1
  86. package/runtime/ext_gui/dom.h +41 -2
  87. package/runtime/ext_gui/dom_api.cpp +3 -0
  88. package/runtime/ext_gui/dom_api_document.cpp +28 -0
  89. package/runtime/ext_gui/dom_api_events.cpp +17 -14
  90. package/runtime/ext_gui/dom_api_form.cpp +567 -0
  91. package/runtime/ext_gui/dom_api_internal.h +46 -214
  92. package/runtime/ext_gui/dom_api_mutation.cpp +1 -1
  93. package/runtime/ext_gui/dom_api_node.cpp +10 -3
  94. package/runtime/ext_gui/dom_api_node_read.cpp +1 -1
  95. package/runtime/ext_gui/dom_api_proto.cpp +45 -0
  96. package/runtime/ext_gui/dom_api_webgl_methods.h +217 -0
  97. package/runtime/ext_gui/dom_parse.cpp +118 -1
  98. package/runtime/ext_gui/effect_values.cpp +28 -7
  99. package/runtime/ext_gui/form_focus.cpp +293 -0
  100. package/runtime/ext_gui/form_input.cpp +448 -0
  101. package/runtime/ext_gui/form_internal.h +30 -0
  102. package/runtime/ext_gui/form_shared.cpp +126 -0
  103. package/runtime/ext_gui/form_state.cpp +536 -0
  104. package/runtime/ext_gui/form_state.h +97 -0
  105. package/runtime/ext_gui/gradient_values.cpp +25 -5
  106. package/runtime/ext_gui/gui_engine.h +40 -4
  107. package/runtime/ext_gui/gui_events.cpp +63 -48
  108. package/runtime/ext_gui/inline_svg.cpp +3 -0
  109. package/runtime/ext_gui/layout.cpp +6 -0
  110. package/runtime/ext_gui/layout.h +4 -0
  111. package/runtime/ext_gui/layout_flow.cpp +35 -3
  112. package/runtime/ext_gui/layout_inline.cpp +80 -7
  113. package/runtime/ext_gui/media_query.cpp +7 -1
  114. package/runtime/ext_gui/paint.cpp +62 -24
  115. package/runtime/ext_gui/paint.h +7 -4
  116. package/runtime/ext_gui/paint_clip.cpp +74 -19
  117. package/runtime/ext_gui/paint_form.cpp +389 -0
  118. package/runtime/ext_gui/paint_internal.h +13 -1
  119. package/runtime/ext_gui/renderer_clip.cpp +19 -17
  120. package/runtime/ext_gui/style.cpp +5 -1
  121. package/runtime/ext_gui/style.h +5 -0
  122. package/runtime/ext_gui/style_cascade.cpp +2 -0
  123. package/runtime/ext_gui/style_internal.h +7 -2
  124. package/runtime/ext_gui/style_print.cpp +33 -0
  125. package/runtime/ext_gui/style_text.cpp +11 -0
  126. package/runtime/ext_gui/style_ua.cpp +42 -0
  127. package/runtime/ext_gui/style_values.cpp +89 -90
  128. package/runtime/ext_gui/transform.cpp +2 -0
  129. package/runtime/ext_gui/warnings.cpp +298 -0
  130. package/runtime/ext_gui/warnings.h +59 -0
  131. package/runtime/ext_gui/window.cpp +31 -55
  132. package/runtime/ext_gui/window_diagnostics.cpp +58 -0
  133. package/runtime/ext_gui/window_emit.cpp +51 -0
  134. package/runtime/ext_node/crypto/crypto.c +3 -2
  135. package/runtime/ext_node/crypto/crypto_hash.h +13 -0
  136. package/runtime/ext_node/crypto/crypto_random.c +339 -0
  137. package/runtime/ext_node/fs/async_worker.c +1 -1
  138. package/runtime/ext_node/fs/fs_ops.c +1 -1
  139. package/runtime/ext_node/fs/parts/platform.inc +65 -0
  140. package/runtime/rt.h +27 -0
  141. package/runtime/xt_crypto.c +132 -0
  142. package/runtime/xt_random.c +143 -0
  143. package/runtime/xt_regex/matcher.inc +9 -28
  144. package/runtime/xt_regex/parser.inc +65 -2
  145. package/runtime/xt_regex/types.inc +11 -1
  146. package/runtime/xt_regex.h +6 -2
  147. package/runtime/xt_stdlib/dispatch.inc +2 -0
  148. package/runtime/xt_stdlib/math.inc +1 -1
  149. package/runtime/xt_stdlib2/date.inc +204 -4
  150. package/runtime/xt_stdlib2/json.inc +4 -546
  151. package/runtime/xt_stdlib2/json_parse.inc +347 -0
  152. package/runtime/xt_stdlib2/json_stringify.inc +368 -0
  153. package/runtime/xt_stdlib2/regexp-match.inc +6 -11
  154. package/runtime/xt_stdlib2.c +0 -4
  155. package/runtime/xt_typed_array/elements.inc +6 -1
  156. package/src/checker/globals.ts +2 -0
  157. package/src/cli/main.ts +1 -0
  158. package/src/codegen/generator/state.ts +3 -0
  159. package/src/codegen/generator/tables.ts +3 -0
  160. package/src/driver/compiler.ts +1 -1
  161. package/src/driver/config.ts +1 -0
  162. package/src/driver/runtime-objects.ts +2 -0
  163. package/src/driver/version.ts +1 -1
  164. package/src/extensions/node/crypto/index.ts +12 -1
@@ -0,0 +1,567 @@
1
+ /*
2
+ * xbintsc GUI engine — form-control DOM bindings.
3
+ *
4
+ * Exposes the live value state (`value`, `checked`, selection range, `type`,
5
+ * `disabled`, `placeholder`, ...) of `<input>`/`<textarea>`/`<select>` and the
6
+ * small set of methods a form script uses (`setSelectionRange`, `select`).
7
+ * Text editing itself lives in `form_input.cpp`; painting in `paint_form.cpp`.
8
+ */
9
+
10
+ #include "dom_api_internal.h"
11
+
12
+ #include "form_state.h"
13
+
14
+ #include <cstdio>
15
+ #include <cstdlib>
16
+ #include <string>
17
+ #include <vector>
18
+
19
+ namespace domapi {
20
+
21
+ static void form_dirty(XtGuiWindow *win) {
22
+ if (win == nullptr) return;
23
+ win->geometry.dirty = 1;
24
+ win->present_dirty = 1;
25
+ }
26
+
27
+ static bool is_form_element(const xtgui::Node *node) {
28
+ if (node == nullptr || !node->isElement()) return false;
29
+ return xtgui::xt_form_is_control(node) || node->tag == "button" || node->tag == "option" ||
30
+ node->tag == "optgroup" || node->tag == "fieldset" || node->tag == "label" ||
31
+ node->tag == "form";
32
+ }
33
+
34
+ static bool text_control(const xtgui::Node *node) {
35
+ return node != nullptr && xtgui::xt_form_is_text_like(node);
36
+ }
37
+
38
+ static int clamp_selection(const xtgui::Node *node, int value) {
39
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
40
+ std::string text = state != nullptr ? state->value : "";
41
+ int length = (int)xtgui::xt_form_utf16_length(text);
42
+ if (value < 0) return 0;
43
+ if (value > length) return length;
44
+ return value;
45
+ }
46
+
47
+ static void set_selection(xtgui::Node *node, int start, int end, int direction) {
48
+ if (!text_control(node)) return;
49
+ int length = (int)xtgui::xt_form_utf16_length(xtgui::xt_form_value(node));
50
+ if (start < 0) start = 0;
51
+ if (end < 0) end = 0;
52
+ if (start > length) start = length;
53
+ if (end > length) end = length;
54
+ if (start > end) {
55
+ int swap = start;
56
+ start = end;
57
+ end = swap;
58
+ if (direction == 1) direction = -1;
59
+ }
60
+ if (direction == 0) direction = start < end ? 1 : 0;
61
+ xtgui::FormState *state = xtgui::xt_form_state(node);
62
+ if (state == nullptr) return;
63
+ state->selection_start = start;
64
+ state->selection_end = end;
65
+ state->selection_direction = direction;
66
+ }
67
+
68
+ #define METHOD(name) xt_value name(xt_value self, xt_value env, int32_t argc, xt_value *argv)
69
+
70
+ METHOD(node_value_get) {
71
+ (void)env;
72
+ (void)argc;
73
+ (void)argv;
74
+ xtgui::Node *node = resolve_node(self, nullptr);
75
+ if (node == nullptr) return XT_UNDEFINED;
76
+ if (xtgui::xt_form_is_control(node)) {
77
+ return xt_string_from_cstr(xtgui::xt_form_value(node).c_str());
78
+ }
79
+ if (node->isElement() && node->tag == "option") {
80
+ return xt_string_from_cstr(xtgui::xt_form_option_value(node).c_str());
81
+ }
82
+ if (node->isElement() && node->tag == "button") {
83
+ const std::string *value = node->attr("value");
84
+ return xt_string_from_cstr(value != nullptr ? value->c_str() : "");
85
+ }
86
+ return XT_UNDEFINED;
87
+ }
88
+
89
+ METHOD(node_value_set) {
90
+ (void)env;
91
+ XtGuiWindow *win = nullptr;
92
+ xtgui::Node *node = resolve_node(self, &win);
93
+ if (node == nullptr) return XT_UNDEFINED;
94
+ std::string value = argc >= 1 ? to_string_value(xt_arg(argc, argv, 0)) : "";
95
+ if (xtgui::xt_form_is_control(node)) {
96
+ xtgui::xt_form_set_value(node, value);
97
+ form_dirty(win);
98
+ } else if (node->isElement() && node->tag == "option") {
99
+ set_attribute(win, node, "value", value);
100
+ } else if (node->isElement() && node->tag == "button") {
101
+ set_attribute(win, node, "value", value);
102
+ }
103
+ return XT_UNDEFINED;
104
+ }
105
+
106
+ METHOD(node_default_value_get) {
107
+ (void)env;
108
+ (void)argc;
109
+ (void)argv;
110
+ xtgui::Node *node = resolve_node(self, nullptr);
111
+ if (node == nullptr) return XT_UNDEFINED;
112
+ if (xtgui::xt_form_is_textarea(node)) {
113
+ std::string text;
114
+ for (const std::unique_ptr<xtgui::Node> &child : node->children) {
115
+ if (child->isText()) text += child->text;
116
+ }
117
+ return xt_string_from_cstr(text.c_str());
118
+ }
119
+ if (xtgui::xt_form_is_control(node) || node->tag == "option") {
120
+ const std::string *value = node->attr("value");
121
+ return xt_string_from_cstr(value != nullptr ? value->c_str() : "");
122
+ }
123
+ return XT_UNDEFINED;
124
+ }
125
+
126
+ METHOD(node_checked_get) {
127
+ (void)env;
128
+ (void)argc;
129
+ (void)argv;
130
+ xtgui::Node *node = resolve_node(self, nullptr);
131
+ if (node == nullptr) return XT_FALSE;
132
+ return xtgui::xt_form_checked(node) ? XT_TRUE : XT_FALSE;
133
+ }
134
+
135
+ METHOD(node_checked_set) {
136
+ (void)env;
137
+ XtGuiWindow *win = nullptr;
138
+ xtgui::Node *node = resolve_node(self, &win);
139
+ if (node == nullptr || !xtgui::xt_form_is_checkable(node)) return XT_UNDEFINED;
140
+ xtgui::xt_form_set_checked(node, argc >= 1 ? value_truthy(xt_arg(argc, argv, 0)) : false);
141
+ mark_dirty(win);
142
+ form_dirty(win);
143
+ return XT_UNDEFINED;
144
+ }
145
+
146
+ METHOD(node_type_get) {
147
+ (void)env;
148
+ (void)argc;
149
+ (void)argv;
150
+ xtgui::Node *node = resolve_node(self, nullptr);
151
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
152
+ std::string type = xtgui::xt_form_type(node);
153
+ return xt_string_from_cstr(type.c_str());
154
+ }
155
+
156
+ METHOD(node_type_set) {
157
+ (void)env;
158
+ XtGuiWindow *win = nullptr;
159
+ xtgui::Node *node = resolve_node(self, &win);
160
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
161
+ if (!xtgui::xt_form_is_input(node) && node->tag != "button" && node->tag != "select" &&
162
+ node->tag != "textarea") {
163
+ return XT_UNDEFINED;
164
+ }
165
+ set_attribute(win, node, "type", argc >= 1 ? to_string_value(xt_arg(argc, argv, 0)) : "text");
166
+ if (node->form) node->form->initialized = false;
167
+ if (xtgui::xt_form_is_control(node)) {
168
+ xtgui::xt_form_init(node);
169
+ form_dirty(win);
170
+ }
171
+ return XT_UNDEFINED;
172
+ }
173
+
174
+ METHOD(node_name_get) {
175
+ (void)env;
176
+ (void)argc;
177
+ (void)argv;
178
+ xtgui::Node *node = resolve_node(self, nullptr);
179
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
180
+ const std::string *value = node->attr("name");
181
+ return xt_string_from_cstr(value != nullptr ? value->c_str() : "");
182
+ }
183
+
184
+ METHOD(node_name_set) {
185
+ (void)env;
186
+ XtGuiWindow *win = nullptr;
187
+ xtgui::Node *node = resolve_node(self, &win);
188
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
189
+ set_attribute(win, node, "name", argc >= 1 ? to_string_value(xt_arg(argc, argv, 0)) : "");
190
+ return XT_UNDEFINED;
191
+ }
192
+
193
+ METHOD(node_disabled_get) {
194
+ (void)env;
195
+ (void)argc;
196
+ (void)argv;
197
+ xtgui::Node *node = resolve_node(self, nullptr);
198
+ if (node == nullptr || !is_form_element(node)) return XT_UNDEFINED;
199
+ return xtgui::xt_form_is_disabled(node) ? XT_TRUE : XT_FALSE;
200
+ }
201
+
202
+ METHOD(node_disabled_set) {
203
+ (void)env;
204
+ XtGuiWindow *win = nullptr;
205
+ xtgui::Node *node = resolve_node(self, &win);
206
+ if (node == nullptr || !is_form_element(node)) return XT_UNDEFINED;
207
+ if (argc >= 1 && value_truthy(xt_arg(argc, argv, 0))) {
208
+ set_attribute(win, node, "disabled", "");
209
+ } else {
210
+ remove_attribute_raw(node, "disabled");
211
+ mark_dirty(win);
212
+ }
213
+ form_dirty(win);
214
+ return XT_UNDEFINED;
215
+ }
216
+
217
+ METHOD(node_read_only_get) {
218
+ (void)env;
219
+ (void)argc;
220
+ (void)argv;
221
+ xtgui::Node *node = resolve_node(self, nullptr);
222
+ if (node == nullptr || !xtgui::xt_form_is_control(node)) return XT_UNDEFINED;
223
+ return xtgui::xt_form_is_readonly(node) ? XT_TRUE : XT_FALSE;
224
+ }
225
+
226
+ METHOD(node_read_only_set) {
227
+ (void)env;
228
+ XtGuiWindow *win = nullptr;
229
+ xtgui::Node *node = resolve_node(self, &win);
230
+ if (node == nullptr || !xtgui::xt_form_is_control(node)) return XT_UNDEFINED;
231
+ if (argc >= 1 && value_truthy(xt_arg(argc, argv, 0))) {
232
+ set_attribute(win, node, "readonly", "");
233
+ } else {
234
+ remove_attribute_raw(node, "readonly");
235
+ mark_dirty(win);
236
+ }
237
+ return XT_UNDEFINED;
238
+ }
239
+
240
+ METHOD(node_required_get) {
241
+ (void)env;
242
+ (void)argc;
243
+ (void)argv;
244
+ xtgui::Node *node = resolve_node(self, nullptr);
245
+ if (node == nullptr || !xtgui::xt_form_is_control(node)) return XT_FALSE;
246
+ return node->attr("required") != nullptr ? XT_TRUE : XT_FALSE;
247
+ }
248
+
249
+ METHOD(node_required_set) {
250
+ (void)env;
251
+ XtGuiWindow *win = nullptr;
252
+ xtgui::Node *node = resolve_node(self, &win);
253
+ if (node == nullptr || !xtgui::xt_form_is_control(node)) return XT_UNDEFINED;
254
+ if (argc >= 1 && value_truthy(xt_arg(argc, argv, 0))) {
255
+ set_attribute(win, node, "required", "");
256
+ } else {
257
+ remove_attribute_raw(node, "required");
258
+ mark_dirty(win);
259
+ }
260
+ return XT_UNDEFINED;
261
+ }
262
+
263
+ METHOD(node_placeholder_get) {
264
+ (void)env;
265
+ (void)argc;
266
+ (void)argv;
267
+ xtgui::Node *node = resolve_node(self, nullptr);
268
+ if (node == nullptr || !xtgui::xt_form_is_control(node)) return XT_UNDEFINED;
269
+ const std::string *value = node->attr("placeholder");
270
+ return xt_string_from_cstr(value != nullptr ? value->c_str() : "");
271
+ }
272
+
273
+ METHOD(node_placeholder_set) {
274
+ (void)env;
275
+ XtGuiWindow *win = nullptr;
276
+ xtgui::Node *node = resolve_node(self, &win);
277
+ if (node == nullptr || !xtgui::xt_form_is_control(node)) return XT_UNDEFINED;
278
+ std::string value = argc >= 1 ? to_string_value(xt_arg(argc, argv, 0)) : "";
279
+ if (value.empty()) {
280
+ remove_attribute_raw(node, "placeholder");
281
+ mark_dirty(win);
282
+ } else {
283
+ set_attribute(win, node, "placeholder", value);
284
+ }
285
+ form_dirty(win);
286
+ return XT_UNDEFINED;
287
+ }
288
+
289
+ METHOD(node_selection_start_get) {
290
+ (void)env;
291
+ (void)argc;
292
+ (void)argv;
293
+ xtgui::Node *node = resolve_node(self, nullptr);
294
+ if (!text_control(node)) return XT_UNDEFINED;
295
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
296
+ return xt_number(state != nullptr ? state->selection_start : 0);
297
+ }
298
+
299
+ METHOD(node_selection_start_set) {
300
+ (void)env;
301
+ XtGuiWindow *win = nullptr;
302
+ xtgui::Node *node = resolve_node(self, &win);
303
+ if (!text_control(node)) return XT_UNDEFINED;
304
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
305
+ int end = state != nullptr ? state->selection_end : 0;
306
+ int start = clamp_selection(node, argc >= 1 ? (int)xt_to_number(xt_arg(argc, argv, 0)) : 0);
307
+ set_selection(node, start, end > start ? end : start, 1);
308
+ form_dirty(win);
309
+ return XT_UNDEFINED;
310
+ }
311
+
312
+ METHOD(node_selection_end_get) {
313
+ (void)env;
314
+ (void)argc;
315
+ (void)argv;
316
+ xtgui::Node *node = resolve_node(self, nullptr);
317
+ if (!text_control(node)) return XT_UNDEFINED;
318
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
319
+ return xt_number(state != nullptr ? state->selection_end : 0);
320
+ }
321
+
322
+ METHOD(node_selection_end_set) {
323
+ (void)env;
324
+ XtGuiWindow *win = nullptr;
325
+ xtgui::Node *node = resolve_node(self, &win);
326
+ if (!text_control(node)) return XT_UNDEFINED;
327
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
328
+ int start = state != nullptr ? state->selection_start : 0;
329
+ int end = clamp_selection(node, argc >= 1 ? (int)xt_to_number(xt_arg(argc, argv, 0)) : 0);
330
+ set_selection(node, start < end ? start : end, end, 1);
331
+ form_dirty(win);
332
+ return XT_UNDEFINED;
333
+ }
334
+
335
+ METHOD(node_selection_direction_get) {
336
+ (void)env;
337
+ (void)argc;
338
+ (void)argv;
339
+ xtgui::Node *node = resolve_node(self, nullptr);
340
+ if (!text_control(node)) return XT_UNDEFINED;
341
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
342
+ int direction = state != nullptr ? state->selection_direction : 0;
343
+ return xt_string_from_cstr(direction > 0 ? "forward" : direction < 0 ? "backward" : "none");
344
+ }
345
+
346
+ METHOD(node_set_selection_range) {
347
+ (void)env;
348
+ XtGuiWindow *win = nullptr;
349
+ xtgui::Node *node = resolve_node(self, &win);
350
+ if (!text_control(node)) return XT_UNDEFINED;
351
+ int start = argc >= 1 ? (int)xt_to_number(xt_arg(argc, argv, 0)) : 0;
352
+ int end = argc >= 2 ? (int)xt_to_number(xt_arg(argc, argv, 1)) : start;
353
+ std::string direction = argc >= 3 ? to_string_value(xt_arg(argc, argv, 2)) : "none";
354
+ int sign = direction == "forward" ? 1 : direction == "backward" ? -1 : 0;
355
+ set_selection(node, start, end, sign);
356
+ form_dirty(win);
357
+ return XT_UNDEFINED;
358
+ }
359
+
360
+ METHOD(node_select) {
361
+ (void)env;
362
+ (void)argc;
363
+ (void)argv;
364
+ XtGuiWindow *win = nullptr;
365
+ xtgui::Node *node = resolve_node(self, &win);
366
+ if (!text_control(node)) return XT_UNDEFINED;
367
+ int length = (int)xtgui::xt_form_utf16_length(xtgui::xt_form_value(node));
368
+ set_selection(node, 0, length, 1);
369
+ xt_gui_set_focus(win, node);
370
+ form_dirty(win);
371
+ return XT_UNDEFINED;
372
+ }
373
+
374
+ METHOD(node_form_get) {
375
+ (void)env;
376
+ (void)argc;
377
+ (void)argv;
378
+ XtGuiWindow *win = nullptr;
379
+ xtgui::Node *node = resolve_node(self, &win);
380
+ if (node == nullptr) return XT_UNDEFINED;
381
+ for (xtgui::Node *parent = node->parent; parent != nullptr; parent = parent->parent) {
382
+ if (parent->isElement() && parent->tag == "form") return xt_gui_node_handle(win, parent);
383
+ }
384
+ return XT_UNDEFINED;
385
+ }
386
+
387
+ METHOD(node_options_get) {
388
+ (void)env;
389
+ (void)argc;
390
+ (void)argv;
391
+ XtGuiWindow *win = nullptr;
392
+ xtgui::Node *node = resolve_node(self, &win);
393
+ xt_value array = xt_array_new(0, NULL);
394
+ if (!xtgui::xt_form_is_select(node)) return array;
395
+ std::vector<const xtgui::Node *> options;
396
+ struct Collect {
397
+ std::vector<const xtgui::Node *> *out;
398
+ void visit(const xtgui::Node *current) const {
399
+ if (current->isElement() && current->isTag("option")) out->push_back(current);
400
+ for (const std::unique_ptr<xtgui::Node> &child : current->children) visit(child.get());
401
+ }
402
+ } collect = {&options};
403
+ collect.visit(node);
404
+ for (const xtgui::Node *option : options) xt_array_push(array, xt_gui_node_handle(win, option));
405
+ return array;
406
+ }
407
+
408
+ METHOD(node_selected_index_get) {
409
+ (void)env;
410
+ (void)argc;
411
+ (void)argv;
412
+ xtgui::Node *node = resolve_node(self, nullptr);
413
+ if (!xtgui::xt_form_is_select(node)) return xt_number(-1);
414
+ const xtgui::FormState *state = xtgui::xt_form_state(node);
415
+ return xt_number(state != nullptr ? state->selected_index : -1);
416
+ }
417
+
418
+ METHOD(node_selected_index_set) {
419
+ (void)env;
420
+ XtGuiWindow *win = nullptr;
421
+ xtgui::Node *node = resolve_node(self, &win);
422
+ if (!xtgui::xt_form_is_select(node)) return XT_UNDEFINED;
423
+ xtgui::FormState *state = xtgui::xt_form_state(node);
424
+ if (state == nullptr) return XT_UNDEFINED;
425
+ int index = argc >= 1 ? (int)xt_to_number(xt_arg(argc, argv, 0)) : -1;
426
+ std::vector<const xtgui::Node *> options;
427
+ struct Collect {
428
+ std::vector<const xtgui::Node *> *out;
429
+ void visit(const xtgui::Node *current) const {
430
+ if (current->isElement() && current->isTag("option")) out->push_back(current);
431
+ for (const std::unique_ptr<xtgui::Node> &child : current->children) visit(child.get());
432
+ }
433
+ } collect = {&options};
434
+ collect.visit(node);
435
+ if (index < 0 || (size_t)index >= options.size()) index = options.empty() ? -1 : 0;
436
+ state->selected_index = index;
437
+ if (index >= 0) state->value = xtgui::xt_form_option_value(options[(size_t)index]);
438
+ mark_dirty(win);
439
+ form_dirty(win);
440
+ return XT_UNDEFINED;
441
+ }
442
+
443
+ static xt_value attrum_get(xt_value self, int32_t argc, xt_value *argv, const char *name,
444
+ xt_value fallback) {
445
+ (void)argc;
446
+ (void)argv;
447
+ xtgui::Node *node = resolve_node(self, nullptr);
448
+ if (node == nullptr || !node->isElement()) return fallback;
449
+ const std::string *value = node->attr(name);
450
+ return xt_string_from_cstr(value != nullptr ? value->c_str() : "");
451
+ }
452
+
453
+ static xt_value attrum_set(xt_value self, int32_t argc, xt_value *argv, const char *name,
454
+ bool numeric) {
455
+ XtGuiWindow *win = nullptr;
456
+ xtgui::Node *node = resolve_node(self, &win);
457
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
458
+ if (numeric && argc >= 1 && xt_to_number(xt_arg(argc, argv, 0)) < 0) {
459
+ remove_attribute_raw(node, name);
460
+ mark_dirty(win);
461
+ return XT_UNDEFINED;
462
+ }
463
+ set_attribute(win, node, name, argc >= 1 ? to_string_value(xt_arg(argc, argv, 0)) : "");
464
+ return XT_UNDEFINED;
465
+ }
466
+
467
+ METHOD(node_max_length_get) {
468
+ (void)env;
469
+ xtgui::Node *node = resolve_node(self, nullptr);
470
+ if (node == nullptr || !node->isElement()) return xt_number(-1);
471
+ const std::string *value = node->attr("maxlength");
472
+ return xt_number(value != nullptr ? std::strtod(value->c_str(), nullptr) : -1);
473
+ }
474
+
475
+ METHOD(node_max_length_set) {
476
+ (void)env;
477
+ return attrum_set(self, argc, argv, "maxlength", true);
478
+ }
479
+
480
+ METHOD(node_min_get) {
481
+ (void)env;
482
+ return attrum_get(self, argc, argv, "min", xt_string_from_cstr(""));
483
+ }
484
+
485
+ METHOD(node_min_set) {
486
+ (void)env;
487
+ return attrum_set(self, argc, argv, "min", false);
488
+ }
489
+
490
+ METHOD(node_max_get) {
491
+ (void)env;
492
+ return attrum_get(self, argc, argv, "max", xt_string_from_cstr(""));
493
+ }
494
+
495
+ METHOD(node_max_set) {
496
+ (void)env;
497
+ return attrum_set(self, argc, argv, "max", false);
498
+ }
499
+
500
+ METHOD(node_step_get) {
501
+ (void)env;
502
+ return attrum_get(self, argc, argv, "step", xt_string_from_cstr(""));
503
+ }
504
+
505
+ METHOD(node_step_set) {
506
+ (void)env;
507
+ return attrum_set(self, argc, argv, "step", false);
508
+ }
509
+
510
+ METHOD(node_tab_index_get) {
511
+ (void)env;
512
+ (void)argc;
513
+ (void)argv;
514
+ xtgui::Node *node = resolve_node(self, nullptr);
515
+ if (node == nullptr || !node->isElement()) return xt_number(-1);
516
+ const std::string *value = node->attr("tabindex");
517
+ if (value != nullptr) return xt_number(std::strtod(value->c_str(), nullptr));
518
+ return xtgui::xt_form_is_focusable(node) ? xt_number(0) : xt_number(-1);
519
+ }
520
+
521
+ METHOD(node_tab_index_set) {
522
+ (void)env;
523
+ XtGuiWindow *win = nullptr;
524
+ xtgui::Node *node = resolve_node(self, &win);
525
+ if (node == nullptr || !node->isElement()) return XT_UNDEFINED;
526
+ int value = argc >= 1 ? (int)xt_to_number(xt_arg(argc, argv, 0)) : -1;
527
+ char text[16];
528
+ std::snprintf(text, sizeof(text), "%d", value);
529
+ set_attribute(win, node, "tabindex", text);
530
+ return XT_UNDEFINED;
531
+ }
532
+
533
+ METHOD(node_form_submit) {
534
+ (void)env;
535
+ (void)argc;
536
+ (void)argv;
537
+ XtGuiWindow *win = nullptr;
538
+ xtgui::Node *node = resolve_node(self, &win);
539
+ if (node == nullptr || !node->isElement() || node->tag != "form") return XT_UNDEFINED;
540
+ xt_gui_form_submit_request(win, node, nullptr);
541
+ return XT_UNDEFINED;
542
+ }
543
+
544
+ METHOD(node_form_request_submit) {
545
+ (void)env;
546
+ XtGuiWindow *win = nullptr;
547
+ xtgui::Node *node = resolve_node(self, &win);
548
+ if (node == nullptr || !node->isElement() || node->tag != "form") return XT_UNDEFINED;
549
+ xtgui::Node *submitter = argc >= 1 ? resolve_node(xt_arg(argc, argv, 0), nullptr) : nullptr;
550
+ xt_gui_form_submit_request(win, node, submitter);
551
+ return XT_UNDEFINED;
552
+ }
553
+
554
+ METHOD(node_form_reset) {
555
+ (void)env;
556
+ (void)argc;
557
+ (void)argv;
558
+ XtGuiWindow *win = nullptr;
559
+ xtgui::Node *node = resolve_node(self, &win);
560
+ if (node == nullptr || !node->isElement() || node->tag != "form") return XT_UNDEFINED;
561
+ xt_gui_form_reset(win, node);
562
+ return XT_UNDEFINED;
563
+ }
564
+
565
+ #undef METHOD
566
+
567
+ } // namespace domapi