/* Browser bindings for the embedded JavaScript engine (QuickJS via js_mu). * * The host owns the document and the UI; this layer exposes the DOM surface * (elements, attributes, selectors, classList/dataset/style, innerHTML, * timers) plus document/window/location/navigator globals, and leaves the * node tree operations to browser_html.c. */ #ifndef JS_BROWSER_H #define JS_BROWSER_H struct br_doc; /* One synchronous HTTP exchange for fetch()/XMLHttpRequest. `body` is a * malloc()ed buffer owned by the caller after the call (may be NULL). */ struct js_http_resp { int status; char ctype[64]; char url[512]; /* final URL after redirects */ char *body; unsigned int len; }; struct js_browser_host { void (*status)(const char *msg, int error); void (*report)(const char *msg); void (*navigate)(const char *url); void (*changed)(void); /* Viewport-relative border box for getBoundingClientRect(); returns 0 * when the node has no laid-out box. */ int (*rect)(unsigned int node, int *x, int *y, int *w, int *h); /* Live form state; return 1 when the node has a control state. */ int (*form_value)(unsigned int node, char *buf, int cap); int (*form_checked)(unsigned int node); int (*form_selected)(unsigned int node); int (*form_set_value)(unsigned int node, const char *value); int (*form_set_checked)(unsigned int node, int checked); int (*form_set_selected)(unsigned int node, int index); /* Collect a form's listed controls (input/select/textarea/button). */ int (*form_elements)(unsigned int form, unsigned int *out, int cap); int (*form_submit)(unsigned int form); /* JS submit(), no event */ int (*form_reset)(unsigned int form); /* JS reset(), fires reset */ int (*click)(unsigned int node); /* element.click() */ /* Computed style for getComputedStyle(); return 1 when known. */ int (*computed)(unsigned int node, const char *property, char *buf, int cap); /* fetch()/XHR: returns 0 and fills `r`, or -1 with `err` set. */ int (*http)(const char *method, const char *url, const char *ctype, const char *body, unsigned int body_len, struct js_http_resp *r, char *err, int errcap); /* Module source for an absolute URL, malloc()ed and NUL-terminated * (the caller frees it); NULL when it cannot be loaded. */ char *(*load_script)(const char *url, unsigned int *len); /* Web Storage for this page's origin; kind 0 = local, 1 = session. * With `set` the text is stored (returns 0); otherwise it is read into * out (returns its length, or -1). */ int (*storage)(int kind, const char *set, char *out, int cap); /* Same-document #fragment change: update the page URL in place * (history, address bar) without loading; 0 on success. */ int (*set_hash)(const char *url); const char *page_url; /* location.href for every script on the page */ unsigned int width; /* viewport size for window.inner* */ unsigned int height; }; /* Create a state for this document. Returns 0 on success. */ int js_browser_start(struct br_doc *doc, const struct js_browser_host *host); void js_browser_stop(void); /* Run one script. `url` is used in error messages and by location.href. */ int js_browser_run(const char *source, const char *url); /* Run one