js_browser.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Browser bindings for the embedded MuJS interpreter.
  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. struct js_browser_host {
  11. void (*status)(const char *msg, int error);
  12. void (*report)(const char *msg);
  13. void (*navigate)(const char *url);
  14. void (*changed)(void);
  15. /* Viewport-relative border box for getBoundingClientRect(); returns 0
  16. * when the node has no laid-out box. */
  17. int (*rect)(unsigned int node, int *x, int *y, int *w, int *h);
  18. /* Live form state; return 1 when the node has a control state. */
  19. int (*form_value)(unsigned int node, char *buf, int cap);
  20. int (*form_checked)(unsigned int node);
  21. int (*form_selected)(unsigned int node);
  22. int (*form_set_value)(unsigned int node, const char *value);
  23. int (*form_set_checked)(unsigned int node, int checked);
  24. int (*form_set_selected)(unsigned int node, int index);
  25. /* Collect a form's listed controls (input/select/textarea/button). */
  26. int (*form_elements)(unsigned int form, unsigned int *out, int cap);
  27. int (*form_submit)(unsigned int form); /* JS submit(), no event */
  28. int (*form_reset)(unsigned int form); /* JS reset(), fires reset */
  29. int (*click)(unsigned int node); /* element.click() */
  30. /* Computed style for getComputedStyle(); return 1 when known. */
  31. int (*computed)(unsigned int node, const char *property, char *buf,
  32. int cap);
  33. const char *page_url; /* location.href for every script on the page */
  34. unsigned int width; /* viewport size for window.inner* */
  35. unsigned int height;
  36. };
  37. /* Create a state for this document. Returns 0 on success. */
  38. int js_browser_start(struct br_doc *doc, const struct js_browser_host *host);
  39. void js_browser_stop(void);
  40. /* Run one script. `url` is used in error messages and by location.href. */
  41. int js_browser_run(const char *source, const char *url);
  42. /* Run due setTimeout/setInterval callbacks; call once per UI loop. */
  43. void js_browser_tick(void);
  44. /* Dispatch a DOM event at a node. Returns 0 when a handler called
  45. * preventDefault(). Bubbles to ancestors, document and window. */
  46. int js_browser_event(unsigned int node, const char *type, int bubbles,
  47. int cancelable);
  48. int js_browser_active(void);
  49. #endif