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
@@ -9,6 +9,8 @@
9
9
 
10
10
  #include "dom_internal.h"
11
11
 
12
+ #include "warnings.h"
13
+
12
14
  #include <cstdlib>
13
15
  #include <cstring>
14
16
  #include <string>
@@ -306,7 +308,119 @@ bool closesOpenElement(const std::string &open, const std::string &next) {
306
308
 
307
309
  } // namespace
308
310
 
309
- std::unique_ptr<Node> xt_html_parse(const std::string &html) {
311
+ namespace {
312
+
313
+ /** Elements the HTML tree builder puts in `<head>` when the source omits it. */
314
+ bool isHeadElement(const Node *node) {
315
+ if (node == nullptr || !node->isElement()) return false;
316
+ return node->tag == "title" || node->tag == "meta" || node->tag == "link" ||
317
+ node->tag == "base" || node->tag == "style";
318
+ }
319
+
320
+ /** A parsed fragment (`<div>…</div>`, no `<html>`) still needs `document.body`
321
+ * and the HTML structure the UA stylesheet assumes (`body { margin: 8px }`,
322
+ * `:root`, `html`/`body` block boxes). Browsers synthesise the missing elements
323
+ * during tree construction; the engine does the same after parsing, moving
324
+ * head-like top-level nodes into `<head>` and everything else into `<body>`. */
325
+ void synthesizeDocumentStructure(Node *document) {
326
+ Node *html = nullptr;
327
+ for (const std::unique_ptr<Node> &child : document->children) {
328
+ if (child->isElement() && child->tag == "html") {
329
+ html = child.get();
330
+ break;
331
+ }
332
+ }
333
+ if (html == nullptr) {
334
+ auto html_node = std::make_unique<Node>(NodeType::Element);
335
+ html_node->tag = "html";
336
+ auto head_node = std::make_unique<Node>(NodeType::Element);
337
+ head_node->tag = "head";
338
+ auto body_node = std::make_unique<Node>(NodeType::Element);
339
+ body_node->tag = "body";
340
+ std::vector<std::unique_ptr<Node>> top_level;
341
+ top_level.swap(document->children);
342
+ for (std::unique_ptr<Node> &child : top_level) {
343
+ if (child->isElement() && isHeadElement(child.get())) {
344
+ head_node->append(std::move(child));
345
+ } else if (child->isText() && xt_dom_trim(child->text).empty()) {
346
+ /* Inter-element whitespace outside <body> is dropped, like the HTML
347
+ * parser's "before head/body" insertion modes. */
348
+ } else {
349
+ body_node->append(std::move(child));
350
+ }
351
+ }
352
+ html_node->append(std::move(head_node));
353
+ html_node->append(std::move(body_node));
354
+ document->append(std::move(html_node));
355
+ return;
356
+ }
357
+
358
+ Node *head = nullptr;
359
+ Node *body = nullptr;
360
+ for (const std::unique_ptr<Node> &child : html->children) {
361
+ if (child->isElement() && child->tag == "head") head = child.get();
362
+ if (child->isElement() && child->tag == "body") body = child.get();
363
+ }
364
+ if (head == nullptr) {
365
+ auto head_node = std::make_unique<Node>(NodeType::Element);
366
+ head_node->tag = "head";
367
+ Node *reference = html->children.empty() ? nullptr : html->children.front().get();
368
+ html->insertChild(std::move(head_node), reference);
369
+ head = html->firstElementChild();
370
+ /* `insertChild` may append when the reference is not a child. */
371
+ for (const std::unique_ptr<Node> &child : html->children) {
372
+ if (child->isElement() && child->tag == "head") head = child.get();
373
+ }
374
+ }
375
+ /* Move stray head elements (a `<style>` after `</head>`, a fragment's style)
376
+ * into the head so the cascade sees them in the usual place. */
377
+ {
378
+ std::vector<std::unique_ptr<Node>> moved;
379
+ for (auto it = html->children.begin(); it != html->children.end();) {
380
+ if (it->get() != head && it->get() != body && isHeadElement(it->get())) {
381
+ moved.push_back(std::move(*it));
382
+ it = html->children.erase(it);
383
+ } else {
384
+ ++it;
385
+ }
386
+ }
387
+ for (std::unique_ptr<Node> &child : moved) head->append(std::move(child));
388
+ }
389
+ if (body == nullptr) {
390
+ auto body_node = std::make_unique<Node>(NodeType::Element);
391
+ body_node->tag = "body";
392
+ std::vector<std::unique_ptr<Node>> moved;
393
+ for (auto it = html->children.begin(); it != html->children.end();) {
394
+ if (it->get() == head || it->get() == body) {
395
+ ++it;
396
+ continue;
397
+ }
398
+ moved.push_back(std::move(*it));
399
+ it = html->children.erase(it);
400
+ }
401
+ html->append(std::move(body_node));
402
+ body = html->children.back().get();
403
+ for (std::unique_ptr<Node> &child : moved) body->append(std::move(child));
404
+ }
405
+ /* Stray body content directly under <html> (a `<div>` after `</body>`, a
406
+ * fragment mixed with an authored body) belongs in the body. */
407
+ {
408
+ std::vector<std::unique_ptr<Node>> moved;
409
+ for (auto it = html->children.begin(); it != html->children.end();) {
410
+ if (it->get() == head || it->get() == body) {
411
+ ++it;
412
+ continue;
413
+ }
414
+ moved.push_back(std::move(*it));
415
+ it = html->children.erase(it);
416
+ }
417
+ for (std::unique_ptr<Node> &child : moved) body->append(std::move(child));
418
+ }
419
+ }
420
+
421
+ } // namespace
422
+
423
+ std::unique_ptr<Node> xt_html_parse(const std::string &html, bool synthesize_structure) {
310
424
  Tokenizer tokenizer(html);
311
425
  std::vector<Token> tokens = tokenizer.run();
312
426
 
@@ -326,8 +440,10 @@ std::unique_ptr<Node> xt_html_parse(const std::string &html) {
326
440
  while (stack.size() > 1 && closesOpenElement(stack.back()->tag, token.name)) {
327
441
  stack.pop_back();
328
442
  }
443
+ xt_warn_unsupported_element(token.name);
329
444
  Node *element = stack.back()->addElement(token.name);
330
445
  element->attrs = std::move(token.attrs);
446
+ for (const Attr &attr : element->attrs) xt_warn_unsupported_attribute(token.name, attr.name);
331
447
  bool isVoid = voidElements().count(token.name) != 0;
332
448
  if (!isVoid && !token.selfClosing) stack.push_back(element);
333
449
  continue;
@@ -342,6 +458,7 @@ std::unique_ptr<Node> xt_html_parse(const std::string &html) {
342
458
  }
343
459
  }
344
460
 
461
+ if (synthesize_structure) synthesizeDocumentStructure(document.get());
345
462
  return document;
346
463
  }
347
464
 
@@ -14,6 +14,7 @@
14
14
  #include "css_internal.h"
15
15
  #include "style_internal.h"
16
16
  #include "style_values_internal.h"
17
+ #include "warnings.h"
17
18
 
18
19
  namespace xtgui {
19
20
 
@@ -223,7 +224,8 @@ static bool parseShadowLengths(const std::string &value, Length *dx, Length *dy,
223
224
  return true;
224
225
  }
225
226
 
226
- static bool parseFilterList(const std::string &value, std::vector<FilterOp> *out) {
227
+ static bool parseFilterList(const std::string &value, const char *property,
228
+ std::vector<FilterOp> *out) {
227
229
  std::string text = xt_css_trim(value);
228
230
  std::string lower = xt_css_lower(text);
229
231
  if (lower == "none" || text.empty()) {
@@ -260,6 +262,7 @@ static bool parseFilterList(const std::string &value, std::vector<FilterOp> *out
260
262
  i = close + 1;
261
263
 
262
264
  FilterOp op;
265
+ bool knownName = true;
263
266
  bool known = true;
264
267
  if (name == "blur") {
265
268
  op.kind = FilterKind::Blur;
@@ -281,8 +284,12 @@ static bool parseFilterList(const std::string &value, std::vector<FilterOp> *out
281
284
  else if (name == "invert") op.kind = FilterKind::Invert;
282
285
  else if (name == "saturate") op.kind = FilterKind::Saturate;
283
286
  else if (name == "opacity") op.kind = FilterKind::Opacity;
284
- else known = false;
285
- if (known && !parseFilterAmount(args, &op.amount)) known = false;
287
+ else knownName = false;
288
+ if (knownName && !parseFilterAmount(args, &op.amount)) known = false;
289
+ }
290
+ if (!knownName) {
291
+ xt_warn_unsupported_function(property, name);
292
+ continue;
286
293
  }
287
294
  if (known) out->push_back(op);
288
295
  }
@@ -396,10 +403,24 @@ static bool parseAnimationShorthand(const std::string &value, std::vector<Animat
396
403
 
397
404
  bool xt_style_apply_effect(XtStyle *style, const std::string &property,
398
405
  const std::string &value) {
399
- if (property == "filter") return parseFilterList(value, &style->filters);
400
- if (property == "backdrop-filter") return parseFilterList(value, &style->backdrop_filters);
401
- if (property == "text-shadow") return parseTextShadow(value, &style->text_shadow);
402
- if (property == "box-shadow") return parseBoxShadow(value, &style->box_shadow);
406
+ /* A recognised property is "handled" even when the value fails to parse: the
407
+ * caller reports only unknown property names, never invalid values. */
408
+ if (property == "filter") {
409
+ parseFilterList(value, "filter", &style->filters);
410
+ return true;
411
+ }
412
+ if (property == "backdrop-filter") {
413
+ parseFilterList(value, "backdrop-filter", &style->backdrop_filters);
414
+ return true;
415
+ }
416
+ if (property == "text-shadow") {
417
+ parseTextShadow(value, &style->text_shadow);
418
+ return true;
419
+ }
420
+ if (property == "box-shadow") {
421
+ parseBoxShadow(value, &style->box_shadow);
422
+ return true;
423
+ }
403
424
  if (property == "animation") {
404
425
  parseAnimationShorthand(value, &style->animations);
405
426
  return true;
@@ -0,0 +1,293 @@
1
+ /*
2
+ * xbintsc GUI engine — pointer activation, tab order and form submit/reset.
3
+ *
4
+ * Text editing lives in `form_input.cpp`; this unit owns what a click or a Tab
5
+ * does: focus placement, checkbox/radio/select activation, the tabbable order,
6
+ * and the `submit`/`reset` events of a `<form>`.
7
+ */
8
+
9
+ #include "gui_engine.h"
10
+
11
+ #include <algorithm>
12
+ #include <cctype>
13
+ #include <cmath>
14
+ #include <cstdio>
15
+ #include <cstdlib>
16
+ #include <memory>
17
+ #include <string>
18
+ #include <vector>
19
+
20
+ #include "form_internal.h"
21
+ #include "form_state.h"
22
+ #include "layout.h"
23
+
24
+ namespace {
25
+
26
+ using xtgui::FormState;
27
+ using xtgui::Node;
28
+
29
+ void set_range_value(XtGuiWindow *win, Node *node, float x) {
30
+ if (win == nullptr || win->document == nullptr) return;
31
+ xt_gui_flush_dom(win);
32
+ const xtgui::LayoutBox *box = win->document->boxOf(node);
33
+ if (box == nullptr || box->content_width <= 0.0f) return;
34
+ float fraction = (x + win->scroll_x - box->content_x) / box->content_width;
35
+ if (fraction < 0.0f) fraction = 0.0f;
36
+ if (fraction > 1.0f) fraction = 1.0f;
37
+ auto number_attr = [node](const char *name, double fallback) {
38
+ const std::string *value = node->attr(name);
39
+ if (value == nullptr) return fallback;
40
+ return std::strtod(value->c_str(), nullptr);
41
+ };
42
+ double minimum = number_attr("min", 0.0);
43
+ double maximum = number_attr("max", 100.0);
44
+ double step = number_attr("step", 1.0);
45
+ if (maximum <= minimum) maximum = minimum + 1.0;
46
+ double value = minimum + fraction * (maximum - minimum);
47
+ if (step > 0.0) value = minimum + std::floor((value - minimum) / step + 0.5) * step;
48
+ char buffer[64];
49
+ if (value == std::floor(value)) {
50
+ std::snprintf(buffer, sizeof(buffer), "%d", (int)value);
51
+ } else {
52
+ std::snprintf(buffer, sizeof(buffer), "%.6g", value);
53
+ }
54
+ xtgui::xt_form_set_value(node, buffer);
55
+ xtgui::formdetail::dirty(win);
56
+ xtgui::formdetail::emit_input(win, node, "", "insertReplacementText");
57
+ }
58
+
59
+ bool reset_form(XtGuiWindow *win, Node *form) {
60
+ if (form == nullptr || !form->isElement() || form->tag != "form") return false;
61
+ xt_value payload = xtgui::formdetail::make_payload("reset");
62
+ if (xt_gui_emit_dom_event(win, "reset", form, payload)) return false;
63
+ xtgui::formdetail::reset_controls(win, form);
64
+ xtgui::formdetail::restyle(win);
65
+ return true;
66
+ }
67
+
68
+ void activate_control(XtGuiWindow *win, Node *node) {
69
+ if (node == nullptr || xtgui::xt_form_is_disabled(node)) return;
70
+ if (node->isElement() && node->tag == "label") {
71
+ Node *target = nullptr;
72
+ const std::string *for_attr = node->attr("for");
73
+ if (for_attr != nullptr) {
74
+ const Node *root = node;
75
+ while (root->parent != nullptr) root = root->parent;
76
+ struct FindById {
77
+ const std::string &id;
78
+ const Node *operator()(const Node *current) const {
79
+ if (current->isElement()) {
80
+ const std::string *value = current->attr("id");
81
+ if (value != nullptr && *value == id) return current;
82
+ }
83
+ for (const std::unique_ptr<Node> &child : current->children) {
84
+ if (const Node *found = (*this)(child.get())) return found;
85
+ }
86
+ return nullptr;
87
+ }
88
+ } find = {*for_attr};
89
+ target = const_cast<Node *>(find(root));
90
+ }
91
+ if (target == nullptr) {
92
+ struct FindControl {
93
+ const Node *operator()(const Node *current) const {
94
+ if (xtgui::xt_form_is_focusable(current)) return current;
95
+ for (const std::unique_ptr<Node> &child : current->children) {
96
+ if (const Node *found = (*this)(child.get())) return found;
97
+ }
98
+ return nullptr;
99
+ }
100
+ } find_control;
101
+ target = const_cast<Node *>(find_control(node));
102
+ }
103
+ if (target != nullptr && target != node) {
104
+ xt_gui_set_focus(win, target);
105
+ activate_control(win, target);
106
+ }
107
+ return;
108
+ }
109
+ if (xtgui::xt_form_is_button_like(node)) {
110
+ std::string type = xtgui::xt_form_type(node);
111
+ if (type == "reset") {
112
+ reset_form(win, xtgui::formdetail::form_ancestor(node));
113
+ } else if (type == "submit" || type == "image") {
114
+ xtgui::formdetail::submit_form(win, xtgui::formdetail::form_ancestor(node), node);
115
+ }
116
+ return;
117
+ }
118
+ if (node->isElement() && node->tag == "button") {
119
+ const std::string *attr = node->attr("type");
120
+ std::string type = attr != nullptr ? *attr : "submit";
121
+ for (char &c : type) c = (char)std::tolower((unsigned char)c);
122
+ if (type == "reset") {
123
+ reset_form(win, xtgui::formdetail::form_ancestor(node));
124
+ } else if (type != "button") {
125
+ xtgui::formdetail::submit_form(win, xtgui::formdetail::form_ancestor(node), node);
126
+ }
127
+ return;
128
+ }
129
+ if (xtgui::xt_form_is_checkable(node)) {
130
+ if (xtgui::xt_form_type(node) == "radio") {
131
+ xtgui::xt_form_set_checked(node, true);
132
+ } else {
133
+ xtgui::xt_form_set_checked(node, !xtgui::xt_form_checked(node));
134
+ }
135
+ xtgui::formdetail::restyle(win);
136
+ xtgui::formdetail::emit_input(win, node, "", "insertReplacementText");
137
+ xtgui::formdetail::emit_change(win, node);
138
+ return;
139
+ }
140
+ if (xtgui::xt_form_is_select(node)) {
141
+ FormState *state = xtgui::xt_form_state(node);
142
+ if (state == nullptr) return;
143
+ int count = 0;
144
+ struct Count {
145
+ int *count;
146
+ void visit(const Node *current) const {
147
+ if (current->isElement() && current->isTag("option")) (*count)++;
148
+ for (const std::unique_ptr<Node> &child : current->children) visit(child.get());
149
+ }
150
+ } counter = {&count};
151
+ counter.visit(node);
152
+ if (count > 0) {
153
+ state->selected_index = (state->selected_index + 1) % count;
154
+ const Node *option = xtgui::xt_form_selected_option(node);
155
+ state->value = option != nullptr ? xtgui::xt_form_option_value(option) : "";
156
+ xtgui::formdetail::restyle(win);
157
+ xtgui::formdetail::emit_input(win, node, "", "insertReplacementText");
158
+ xtgui::formdetail::emit_change(win, node);
159
+ }
160
+ }
161
+ }
162
+
163
+ struct TabStop {
164
+ Node *node;
165
+ int index;
166
+ int order;
167
+ };
168
+
169
+ int tab_index_of(const Node *node) {
170
+ const std::string *value = node->attr("tabindex");
171
+ if (value != nullptr) return (int)std::strtol(value->c_str(), nullptr, 10);
172
+ return 0;
173
+ }
174
+
175
+ void collect_tab_stops(const Node *node, std::vector<TabStop> *out, int *order) {
176
+ if (node->isElement() && xtgui::xt_form_is_focusable(node)) {
177
+ int index = tab_index_of(node);
178
+ if (index >= 0) out->push_back({const_cast<Node *>(node), index, (*order)++});
179
+ }
180
+ for (const std::unique_ptr<Node> &child : node->children) {
181
+ collect_tab_stops(child.get(), out, order);
182
+ }
183
+ }
184
+
185
+ void ensure_visible(XtGuiWindow *win, Node *node) {
186
+ if (win == nullptr || win->document == nullptr) return;
187
+ xt_gui_flush_dom(win);
188
+ const xtgui::LayoutBox *box = win->document->boxOf(node);
189
+ if (box == nullptr) return;
190
+ float viewport_width = 0;
191
+ float viewport_height = 0;
192
+ xt_gui_window_viewport(win, &viewport_width, &viewport_height);
193
+ float x = win->scroll_x;
194
+ float y = win->scroll_y;
195
+ if (box->x < x) x = box->x;
196
+ else if (box->x + box->width > x + viewport_width) x = box->x + box->width - viewport_width;
197
+ if (box->y < y) y = box->y;
198
+ else if (box->y + box->height > y + viewport_height) y = box->y + box->height - viewport_height;
199
+ xt_gui_scroll_to(win, x, y);
200
+ }
201
+
202
+ } // namespace
203
+
204
+ void xt_gui_form_focus_at(XtGuiWindow *win, xtgui::Node *node, float x) {
205
+ if (win == nullptr || node == nullptr) return;
206
+ xt_gui_set_focus(win, node);
207
+ if (!xtgui::formdetail::editable(node)) return;
208
+ FormState *state = xtgui::xt_form_state(node);
209
+ if (state == nullptr) return;
210
+ int caret = xtgui::formdetail::caret_at_x(win, node, x);
211
+ state->selection_start = caret;
212
+ state->selection_end = caret;
213
+ state->selection_direction = 0;
214
+ xtgui::formdetail::dirty(win);
215
+ }
216
+
217
+ void xt_gui_form_pointer(XtGuiWindow *win, const char *type, xtgui::Node *node, float x, float y) {
218
+ (void)y;
219
+ if (win == nullptr || node == nullptr || !node->isElement()) return;
220
+ std::string event = type;
221
+ if (event == "mousedown") {
222
+ if (xtgui::xt_form_is_focusable(node)) xt_gui_form_focus_at(win, node, x);
223
+ if (xtgui::xt_form_is_input(node) && xtgui::xt_form_type(node) == "range") {
224
+ FormState *state = xtgui::xt_form_state(node);
225
+ if (state != nullptr) state->dragging = true;
226
+ set_range_value(win, node, x);
227
+ }
228
+ return;
229
+ }
230
+ if (event == "mousemove") {
231
+ FormState *state = xtgui::xt_form_state(node);
232
+ if (state != nullptr && state->dragging && xtgui::xt_form_is_input(node) &&
233
+ xtgui::xt_form_type(node) == "range") {
234
+ set_range_value(win, node, x);
235
+ }
236
+ return;
237
+ }
238
+ if (event == "mouseup") {
239
+ FormState *state = xtgui::xt_form_state(node);
240
+ if (state != nullptr) {
241
+ bool was_dragging = state->dragging;
242
+ state->dragging = false;
243
+ if (was_dragging && xtgui::xt_form_is_input(node) &&
244
+ xtgui::xt_form_type(node) == "range") {
245
+ xtgui::formdetail::emit_change(win, node);
246
+ }
247
+ }
248
+ return;
249
+ }
250
+ if (event == "click") activate_control(win, node);
251
+ }
252
+
253
+ void xt_gui_form_activate(XtGuiWindow *win, xtgui::Node *node) { activate_control(win, node); }
254
+
255
+ void xt_gui_focus_step(XtGuiWindow *win, bool forward) {
256
+ if (win == nullptr || win->document == nullptr) return;
257
+ std::vector<TabStop> stops;
258
+ int order = 0;
259
+ collect_tab_stops(win->document->root(), &stops, &order);
260
+ if (stops.empty()) {
261
+ xt_gui_set_focus(win, nullptr);
262
+ return;
263
+ }
264
+ std::stable_sort(stops.begin(), stops.end(), [](const TabStop &a, const TabStop &b) {
265
+ if (a.index == 0 && b.index == 0) return a.order < b.order;
266
+ if (a.index == 0) return false;
267
+ if (b.index == 0) return true;
268
+ if (a.index != b.index) return a.index < b.index;
269
+ return a.order < b.order;
270
+ });
271
+ int current = -1;
272
+ Node *focused = const_cast<Node *>(win->document->focus());
273
+ for (size_t i = 0; i < stops.size(); i++) {
274
+ if (stops[i].node == focused) {
275
+ current = (int)i;
276
+ break;
277
+ }
278
+ }
279
+ int next;
280
+ if (current < 0) {
281
+ next = forward ? 0 : (int)stops.size() - 1;
282
+ } else {
283
+ next = (int)((size_t)current + (forward ? 1u : (size_t)stops.size() - 1u)) % (int)stops.size();
284
+ }
285
+ xt_gui_set_focus(win, stops[(size_t)next].node);
286
+ ensure_visible(win, stops[(size_t)next].node);
287
+ }
288
+
289
+ void xt_gui_form_submit_request(XtGuiWindow *win, xtgui::Node *form, xtgui::Node *submitter) {
290
+ xtgui::formdetail::submit_form(win, form, submitter);
291
+ }
292
+
293
+ void xt_gui_form_reset(XtGuiWindow *win, xtgui::Node *form) { reset_form(win, form); }