js_browser.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /* Same-document #fragment change: update the page URL in place
  54. * (history, address bar) without loading; 0 on success. */
  55. int (*set_hash)(const char *url);
  56. const char *page_url; /* location.href for every script on the page */
  57. unsigned int width; /* viewport size for window.inner* */
  58. unsigned int height;
  59. };
  60. /* Create a state for this document. Returns 0 on success. */
  61. int js_browser_start(struct br_doc *doc, const struct js_browser_host *host);
  62. void js_browser_stop(void);
  63. /* Run one script. `url` is used in error messages and by location.href. */
  64. int js_browser_run(const char *source, const char *url);
  65. /* Run one <script type="module">; its imports load through
  66. * host.load_script. */
  67. int js_browser_run_module(const char *source, const char *url);
  68. /* Queue a hashchange event on window (the host changed the fragment, e.g.
  69. * a click on <a href="#/apps">); it fires from the next tick. */
  70. void js_browser_hashchange(void);
  71. /* Dispatch a plain event (e.g. "resize") on window now. */
  72. void js_browser_window_event(const char *type);
  73. /* Run due setTimeout/setInterval callbacks; call once per UI loop. */
  74. void js_browser_tick(void);
  75. /* Dispatch a DOM event at a node. Returns 0 when a handler called
  76. * preventDefault(). Bubbles to ancestors, document and window. */
  77. int js_browser_event(unsigned int node, const char *type, int bubbles,
  78. int cancelable);
  79. int js_browser_active(void);
  80. #endif