js_browser.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Browser bindings for the embedded JavaScript engine (QuickJS via js_mu).
  2. *
  3. * The host owns the document and the UI; this layer exposes the DOM surface
  4. * (elements, attributes, selectors, classList/dataset/style, innerHTML,
  5. * timers) plus document/window/location/navigator globals, and leaves the
  6. * node tree operations to browser_html.c. */
  7. #ifndef JS_BROWSER_H
  8. #define JS_BROWSER_H
  9. struct br_doc;
  10. /* One synchronous HTTP exchange for fetch()/XMLHttpRequest. `body` is a
  11. * malloc()ed buffer owned by the caller after the call (may be NULL). */
  12. struct js_http_resp {
  13. int status;
  14. char ctype[64];
  15. char url[512]; /* final URL after redirects */
  16. char *body;
  17. unsigned int len;
  18. };
  19. struct js_browser_host {
  20. void (*status)(const char *msg, int error);
  21. void (*report)(const char *msg);
  22. void (*navigate)(const char *url);
  23. void (*changed)(void);
  24. /* Viewport-relative border box for getBoundingClientRect(); returns 0
  25. * when the node has no laid-out box. */
  26. int (*rect)(unsigned int node, int *x, int *y, int *w, int *h);
  27. /* Live form state; return 1 when the node has a control state. */
  28. int (*form_value)(unsigned int node, char *buf, int cap);
  29. int (*form_checked)(unsigned int node);
  30. int (*form_selected)(unsigned int node);
  31. int (*form_set_value)(unsigned int node, const char *value);
  32. int (*form_set_checked)(unsigned int node, int checked);
  33. int (*form_set_selected)(unsigned int node, int index);
  34. /* Collect a form's listed controls (input/select/textarea/button). */
  35. int (*form_elements)(unsigned int form, unsigned int *out, int cap);
  36. int (*form_submit)(unsigned int form); /* JS submit(), no event */
  37. int (*form_reset)(unsigned int form); /* JS reset(), fires reset */
  38. int (*click)(unsigned int node); /* element.click() */
  39. /* Computed style for getComputedStyle(); return 1 when known. */
  40. int (*computed)(unsigned int node, const char *property, char *buf,
  41. int cap);
  42. /* fetch()/XHR: returns 0 and fills `r`, or -1 with `err` set. */
  43. int (*http)(const char *method, const char *url, const char *ctype,
  44. const char *body, unsigned int body_len,
  45. struct js_http_resp *r, char *err, int errcap);
  46. /* Module source for an absolute URL, malloc()ed and NUL-terminated
  47. * (the caller frees it); NULL when it cannot be loaded. */
  48. char *(*load_script)(const char *url, unsigned int *len);
  49. /* Web Storage for this page's origin; kind 0 = local, 1 = session.
  50. * With `set` the text is stored (returns 0); otherwise it is read into
  51. * out (returns its length, or -1). */
  52. int (*storage)(int kind, const char *set, char *out, int cap);
  53. const char *page_url; /* location.href for every script on the page */
  54. unsigned int width; /* viewport size for window.inner* */
  55. unsigned int height;
  56. };
  57. /* Create a state for this document. Returns 0 on success. */
  58. int js_browser_start(struct br_doc *doc, const struct js_browser_host *host);
  59. void js_browser_stop(void);
  60. /* Run one script. `url` is used in error messages and by location.href. */
  61. int js_browser_run(const char *source, const char *url);
  62. /* Run one <script type="module">; its imports load through
  63. * host.load_script. */
  64. int js_browser_run_module(const char *source, const char *url);
  65. /* Run due setTimeout/setInterval callbacks; call once per UI loop. */
  66. void js_browser_tick(void);
  67. /* Dispatch a DOM event at a node. Returns 0 when a handler called
  68. * preventDefault(). Bubbles to ancestors, document and window. */
  69. int js_browser_event(unsigned int node, const char *type, int bubbles,
  70. int cancelable);
  71. int js_browser_active(void);
  72. #endif