js_browser.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040
  1. /* Browser bindings for MuJS: the DOM layer (interned node wrappers,
  2. * attributes, query selectors, classList/dataset/style, innerHTML, timers)
  3. * plus document/window/location/navigator globals. */
  4. #include <setjmp.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "ucmd_lib.h"
  10. #include "browser_html.h"
  11. #include "js_browser.h"
  12. #include "mujs.h"
  13. /* Instructions per script/handler. MuJS consumes the budget globally, so
  14. * it is reset before every entry; a runaway loop becomes a catchable
  15. * "script ran too long" exception instead of a frozen browser. */
  16. #define JS_RUN_LIMIT 8000000
  17. #define JS_TIMERS 16
  18. struct js_timer {
  19. int used;
  20. int repeat;
  21. unsigned int deadline;
  22. unsigned int interval;
  23. char ref[32];
  24. };
  25. static struct js_timer timers[JS_TIMERS];
  26. static js_State *J;
  27. static struct br_doc *D;
  28. static struct js_browser_host host;
  29. static const char *current_url;
  30. static jmp_buf panic_env;
  31. static int panic_armed;
  32. static void report_top(js_State *state);
  33. static void report_cb(js_State *state, const char *message)
  34. {
  35. (void)state;
  36. if (host.report)
  37. host.report(message);
  38. }
  39. static void panic_cb(js_State *state)
  40. {
  41. if (panic_armed)
  42. longjmp(panic_env, 1);
  43. if (js_isobject(state, -1))
  44. report_top(state);
  45. else if (host.report)
  46. host.report(js_trystring(state, -1,
  47. "uncaught JavaScript exception"));
  48. if (host.report)
  49. host.report("uncaught JavaScript exception");
  50. }
  51. static unsigned int node_of(void *p)
  52. {
  53. return (unsigned int)(uintptr_t)p - 1;
  54. }
  55. static int node_has(js_State *state, void *p, const char *name);
  56. static int node_put(js_State *state, void *p, const char *name);
  57. static int node_delete(js_State *state, void *p, const char *name);
  58. static void push_element_url(js_State *state, unsigned int node,
  59. const char *name);
  60. /* One wrapper per DOM node: the same element must have the same object
  61. * identity on every access, or library caches (jQuery expando data, Sizzle)
  62. * silently split into disconnected copies. */
  63. static void push_node(js_State *state, unsigned int handle)
  64. {
  65. char key[24];
  66. if (!handle) {
  67. js_pushnull(state);
  68. return;
  69. }
  70. snprintf(key, sizeof(key), "jsonnode_%u", handle);
  71. js_getregistry(state, key);
  72. if (!js_isundefined(state, -1))
  73. return;
  74. js_pop(state, 1);
  75. /* js_newuserdatax() pops a prototype from the top. */
  76. js_pushnull(state);
  77. js_newuserdatax(state, "node", (void *)(uintptr_t)handle,
  78. node_has, node_put, node_delete, 0);
  79. js_copy(state, -1);
  80. js_setregistry(state, key);
  81. }
  82. static unsigned int arg_node(js_State *state, int idx)
  83. {
  84. if (!js_isuserdata(state, idx, "node"))
  85. js_typeerror(state, "not a DOM node");
  86. return (unsigned int)(uintptr_t)js_touserdata(state, idx, "node");
  87. }
  88. static void def_func_data(js_State *state, const char *name,
  89. js_CFunction fn, int length, unsigned int handle)
  90. {
  91. js_newcfunctionx(state, fn, name, length,
  92. (void *)(uintptr_t)handle, 0);
  93. js_setproperty(state, -2, name);
  94. }
  95. static void def_func(js_State *state, const char *name,
  96. js_CFunction fn, int length)
  97. {
  98. js_newcfunction(state, fn, name, length);
  99. js_setproperty(state, -2, name);
  100. }
  101. static void def_global_func(js_State *state, const char *name,
  102. js_CFunction fn, int length)
  103. {
  104. js_newcfunction(state, fn, name, length);
  105. js_defglobal(state, name, JS_DONTENUM);
  106. }
  107. static void push_func_data(js_State *state, const char *name,
  108. js_CFunction fn, int length, unsigned int handle)
  109. {
  110. js_newcfunctionx(state, fn, name, length,
  111. (void *)(uintptr_t)handle, 0);
  112. }
  113. static const char *node_attr(unsigned int node, const char *name)
  114. {
  115. return br_find_attr(D, node, name);
  116. }
  117. static void push_attr(js_State *state, unsigned int node, const char *name,
  118. int null_if_missing)
  119. {
  120. const char *s = node_attr(node, name);
  121. if (s)
  122. js_pushstring(state, s);
  123. else if (null_if_missing)
  124. js_pushnull(state);
  125. else
  126. js_pushstring(state, "");
  127. }
  128. static unsigned int node_name(unsigned int node, char *buf, unsigned int cap)
  129. {
  130. struct br_node *n;
  131. const char *s;
  132. unsigned int i = 0;
  133. if (!D || node >= D->nnode || cap == 0)
  134. return 0;
  135. n = &D->nodes[node];
  136. if (n->type == BR_NODE_TEXT)
  137. s = "#text";
  138. else if (n->type == BR_NODE_COMMENT)
  139. s = "#comment";
  140. else if (n->type == BR_NODE_FRAGMENT)
  141. s = "#document-fragment";
  142. else if (n->tag == BR_TAG_UNKNOWN && n->name_off)
  143. s = D->attrs + n->name_off;
  144. else
  145. s = br_tag_name(n->tag);
  146. while (s && *s && i + 1 < cap) {
  147. char c = *s++;
  148. if (c >= 'a' && c <= 'z')
  149. c = (char)(c - 'a' + 'A');
  150. buf[i++] = c;
  151. }
  152. buf[i] = '\0';
  153. return i;
  154. }
  155. /* ---- DOM events ------------------------------------------------------- *
  156. *
  157. * Listeners live in a fixed table, one entry per (target, type, callback)
  158. * registration, and callbacks are retained through the MuJS registry. The
  159. * dispatcher walks the parent chain for bubbling and runs one listener at a
  160. * time so a handler can safely mutate the tree. `preventDefault` is tracked
  161. * per dispatch in a small event-state table and mirrored onto the event
  162. * object, so a submit handler can cancel the browser's default action. */
  163. #define JS_LISTENERS 64
  164. #define JS_EVENT_SLOTS 8
  165. #define JS_EVENT_TEMPLATES 16
  166. #define JS_TARGET_WINDOW 0xFFFFFFF0u
  167. #define JS_TARGET_DOCUMENT 0xFFFFFFF1u
  168. struct js_listener {
  169. unsigned char used;
  170. unsigned char capture;
  171. unsigned int target;
  172. char type[24];
  173. char ref[32];
  174. };
  175. struct js_event_state {
  176. int used;
  177. int prevented;
  178. int stopped;
  179. int immediate;
  180. char objref[32];
  181. };
  182. struct js_template {
  183. int used;
  184. char ref[32];
  185. };
  186. static struct js_listener listeners[JS_LISTENERS];
  187. static struct js_event_state events[JS_EVENT_SLOTS];
  188. static struct js_template event_templates[JS_EVENT_TEMPLATES];
  189. static void push_event_target(js_State *state, unsigned int target)
  190. {
  191. if (target == JS_TARGET_WINDOW)
  192. js_pushglobal(state);
  193. else if (target == JS_TARGET_DOCUMENT)
  194. js_getglobal(state, "document");
  195. else
  196. push_node(state, target + 1);
  197. }
  198. static void obj_set(js_State *state, int idx, const char *name)
  199. {
  200. /* The value was just pushed, so a negative object index moved down. */
  201. if (idx < 0)
  202. idx--;
  203. js_setproperty(state, idx, name);
  204. }
  205. static void obj_set_string(js_State *state, int idx, const char *name,
  206. const char *value)
  207. {
  208. js_pushstring(state, value ? value : "");
  209. obj_set(state, idx, name);
  210. }
  211. static void obj_set_bool(js_State *state, int idx, const char *name, int value)
  212. {
  213. js_pushboolean(state, value != 0);
  214. obj_set(state, idx, name);
  215. }
  216. static int event_slot(js_State *state)
  217. {
  218. int slot = (int)(uintptr_t)js_currentfunctiondata(state);
  219. if (slot < 0 || slot >= JS_EVENT_SLOTS)
  220. return -1;
  221. return slot;
  222. }
  223. static void event_preventdefault(js_State *state)
  224. {
  225. int slot = event_slot(state);
  226. if (slot >= 0) {
  227. struct js_event_state *ev = &events[slot];
  228. ev->prevented = 1;
  229. if (ev->objref[0]) {
  230. js_getregistry(state, ev->objref);
  231. if (js_isobject(state, -1)) {
  232. js_pushboolean(state, 1);
  233. js_setproperty(state, -2, "defaultPrevented");
  234. }
  235. js_pop(state, 1);
  236. }
  237. }
  238. js_pushundefined(state);
  239. }
  240. static void event_stoppropagation(js_State *state)
  241. {
  242. int slot = event_slot(state);
  243. if (slot >= 0)
  244. events[slot].stopped = 1;
  245. js_pushundefined(state);
  246. }
  247. static void event_stopimmediate(js_State *state)
  248. {
  249. int slot = event_slot(state);
  250. if (slot >= 0) {
  251. events[slot].stopped = 1;
  252. events[slot].immediate = 1;
  253. }
  254. js_pushundefined(state);
  255. }
  256. /* Event-object field helpers. Every dispatch owns a registry key for its
  257. * event object so preventDefault can update the object without access to
  258. * `this` (this MuJS has no this accessor). */
  259. static void ev_set_string(js_State *state, int slot, const char *name,
  260. const char *value)
  261. {
  262. js_getregistry(state, events[slot].objref);
  263. js_pushstring(state, value ? value : "");
  264. js_setproperty(state, -2, name);
  265. js_pop(state, 1);
  266. }
  267. static void ev_set_bool(js_State *state, int slot, const char *name, int value)
  268. {
  269. js_getregistry(state, events[slot].objref);
  270. js_pushboolean(state, value != 0);
  271. js_setproperty(state, -2, name);
  272. js_pop(state, 1);
  273. }
  274. static void ev_set_number(js_State *state, int slot, const char *name,
  275. double value)
  276. {
  277. js_getregistry(state, events[slot].objref);
  278. js_pushnumber(state, value);
  279. js_setproperty(state, -2, name);
  280. js_pop(state, 1);
  281. }
  282. static void ev_set_target(js_State *state, int slot, const char *name,
  283. unsigned int target)
  284. {
  285. js_getregistry(state, events[slot].objref);
  286. push_event_target(state, target);
  287. js_setproperty(state, -2, name);
  288. js_pop(state, 1);
  289. }
  290. static int ev_get_bool(js_State *state, int slot, const char *name, int def)
  291. {
  292. int rv = def;
  293. js_getregistry(state, events[slot].objref);
  294. js_getproperty(state, -1, name);
  295. if (js_isboolean(state, -1))
  296. rv = js_toboolean(state, -1);
  297. js_pop(state, 2);
  298. return rv;
  299. }
  300. static void ev_get_string(js_State *state, int slot, const char *name,
  301. char *out, int cap, const char *def)
  302. {
  303. const char *s = def ? def : "";
  304. int n = 0;
  305. js_getregistry(state, events[slot].objref);
  306. js_getproperty(state, -1, name);
  307. if (js_isstring(state, -1))
  308. s = js_tostring(state, -1);
  309. while (s[n] && n + 1 < cap) {
  310. out[n] = s[n];
  311. n++;
  312. }
  313. out[n] = '\0';
  314. js_pop(state, 2);
  315. }
  316. static void event_bind_methods(js_State *state, int slot)
  317. {
  318. js_getregistry(state, events[slot].objref);
  319. def_func_data(state, "preventDefault", event_preventdefault, 0,
  320. (unsigned int)slot);
  321. def_func_data(state, "stopPropagation", event_stoppropagation, 0,
  322. (unsigned int)slot);
  323. def_func_data(state, "stopImmediatePropagation", event_stopimmediate, 0,
  324. (unsigned int)slot);
  325. js_pop(state, 1);
  326. }
  327. /* The event object is on top of the stack; move it into the registry and
  328. * fill in the standard fields. */
  329. static int event_prepare(js_State *state, unsigned int target)
  330. {
  331. int slot;
  332. for (slot = 0; slot < JS_EVENT_SLOTS; slot++)
  333. if (!events[slot].used)
  334. break;
  335. if (slot == JS_EVENT_SLOTS)
  336. return -1;
  337. events[slot].used = 1;
  338. events[slot].prevented = 0;
  339. events[slot].stopped = 0;
  340. events[slot].immediate = 0;
  341. snprintf(events[slot].objref, sizeof(events[slot].objref),
  342. "jseventobj_%d", slot);
  343. js_setregistry(state, events[slot].objref);
  344. ev_set_bool(state, slot, "defaultPrevented", 0);
  345. ev_set_bool(state, slot, "isTrusted", 0);
  346. ev_set_number(state, slot, "timeStamp", (double)u_ticks_ms());
  347. ev_set_target(state, slot, "target", target);
  348. ev_set_target(state, slot, "currentTarget", target);
  349. ev_set_number(state, slot, "eventPhase", 2);
  350. event_bind_methods(state, slot);
  351. return slot;
  352. }
  353. static int listener_call(js_State *state, const struct js_listener *l,
  354. int slot, unsigned int current_target)
  355. {
  356. js_getregistry(state, l->ref);
  357. if (!js_iscallable(state, -1)) {
  358. js_pop(state, 1);
  359. return 0;
  360. }
  361. push_event_target(state, current_target);
  362. js_getregistry(state, events[slot].objref);
  363. if (js_pcall(state, 1)) {
  364. report_top(state);
  365. return 0;
  366. }
  367. js_pop(state, 1);
  368. return 1;
  369. }
  370. static void listener_phase(js_State *state, unsigned int target,
  371. const char *type, int phase, int slot)
  372. {
  373. int i;
  374. for (i = 0; i < JS_LISTENERS; i++) {
  375. struct js_listener *l = &listeners[i];
  376. if (!l->used || l->target != target ||
  377. strcmp(l->type, type) != 0)
  378. continue;
  379. if (phase == 1 && !l->capture)
  380. continue;
  381. if (phase == 2 && l->capture)
  382. continue;
  383. ev_set_target(state, slot, "currentTarget", target);
  384. listener_call(state, l, slot, target);
  385. if (events[slot].immediate)
  386. return;
  387. }
  388. }
  389. /* supplied_idx: 0 creates a fresh object, otherwise it is the argument
  390. * index of an Event supplied by JavaScript. Leaves a boolean result. */
  391. static void inline_attr_decode(const char *s, char *d, unsigned int cap)
  392. {
  393. unsigned int i = 0, n = 0;
  394. while (s && s[i] && n + 1 < cap) {
  395. if (s[i] == '&') {
  396. if (!strncmp(s + i, "&quot;", 6)) {
  397. d[n++] = '"'; i += 6; continue;
  398. }
  399. if (!strncmp(s + i, "&#39;", 5)) {
  400. d[n++] = '\''; i += 5; continue;
  401. }
  402. if (!strncmp(s + i, "&amp;", 5)) {
  403. d[n++] = '&'; i += 5; continue;
  404. }
  405. if (!strncmp(s + i, "&lt;", 4)) {
  406. d[n++] = '<'; i += 4; continue;
  407. }
  408. if (!strncmp(s + i, "&gt;", 4)) {
  409. d[n++] = '>'; i += 4; continue;
  410. }
  411. }
  412. d[n++] = s[i++];
  413. }
  414. d[n] = '\0';
  415. }
  416. /* Inline on<event> attributes run at the target and while bubbling, after
  417. * the registered listeners for that node. The handler gets the event and
  418. * the element as `this`; a false return prevents the default action. */
  419. static void run_inline_handler(js_State *state, unsigned int node,
  420. const char *type, int slot, int *prevented)
  421. {
  422. char name[40], body[512], src[640];
  423. const char *attr;
  424. int truthy;
  425. if (!D || node >= D->nnode || !events[slot].objref[0])
  426. return;
  427. snprintf(name, sizeof(name), "on%s", type);
  428. attr = br_find_attr(D, node, name);
  429. if (!attr || !attr[0])
  430. return;
  431. inline_attr_decode(attr, body, sizeof(body));
  432. snprintf(src, sizeof(src), "(function(event){ %s\n})", body);
  433. if (js_try(state)) {
  434. js_pop(state, 1);
  435. js_endtry(state);
  436. return;
  437. }
  438. js_loadstring(state, "[inline]", src);
  439. /* The script evaluates the function expression; run it first, then
  440. * call the resulting function with the element as `this`. */
  441. js_pushundefined(state);
  442. if (js_pcall(state, 0) || !js_iscallable(state, -1)) {
  443. js_pop(state, 1);
  444. js_endtry(state);
  445. return;
  446. }
  447. push_node(state, node + 1);
  448. js_getregistry(state, events[slot].objref);
  449. if (js_pcall(state, 1)) {
  450. js_pop(state, 1);
  451. js_endtry(state);
  452. return;
  453. }
  454. truthy = js_toboolean(state, -1);
  455. js_pop(state, 1);
  456. js_endtry(state);
  457. if (!truthy && prevented)
  458. *prevented = 1;
  459. }
  460. static int dispatch_object(js_State *state, unsigned int target,
  461. int supplied_idx, const char *fallback,
  462. int def_bubbles, int def_cancelable)
  463. {
  464. unsigned int path[64];
  465. char type[32];
  466. int np = 0, slot, i, prevented;
  467. struct br_node *nd;
  468. if (supplied_idx)
  469. js_copy(state, supplied_idx);
  470. else
  471. js_newobject(state);
  472. slot = event_prepare(state, target);
  473. if (slot < 0) {
  474. js_pushboolean(state, 1);
  475. return 1;
  476. }
  477. ev_get_string(state, slot, "type", type, sizeof(type), fallback);
  478. if (!type[0]) {
  479. js_unref(state, events[slot].objref);
  480. events[slot].objref[0] = '\0';
  481. events[slot].used = 0;
  482. js_pushboolean(state, 1);
  483. return 1;
  484. }
  485. ev_set_string(state, slot, "type", type);
  486. ev_set_bool(state, slot, "bubbles",
  487. ev_get_bool(state, slot, "bubbles", def_bubbles));
  488. ev_set_bool(state, slot, "cancelable",
  489. ev_get_bool(state, slot, "cancelable", def_cancelable));
  490. path[np++] = target;
  491. if (target != JS_TARGET_WINDOW && target != JS_TARGET_DOCUMENT &&
  492. D && target < D->nnode) {
  493. nd = &D->nodes[target];
  494. while (nd->parent && nd->parent < D->nnode &&
  495. np < (int)(sizeof(path) / sizeof(path[0]))) {
  496. path[np++] = nd->parent;
  497. nd = &D->nodes[nd->parent];
  498. }
  499. }
  500. if (target != JS_TARGET_WINDOW)
  501. path[np++] = JS_TARGET_DOCUMENT;
  502. if (np < (int)(sizeof(path) / sizeof(path[0])))
  503. path[np++] = JS_TARGET_WINDOW;
  504. /* Capture from the outermost ancestor down to the parent. */
  505. for (i = np - 1; i > 0; i--) {
  506. if (events[slot].stopped)
  507. break;
  508. listener_phase(state, path[i], type, 1, slot);
  509. }
  510. /* Target phase: capture and bubble listeners in registration order. */
  511. if (!events[slot].stopped) {
  512. listener_phase(state, path[0], type, 0, slot);
  513. if (!events[slot].stopped)
  514. run_inline_handler(state, path[0], type, slot,
  515. &events[slot].prevented);
  516. }
  517. /* Bubble only for events that ask for it; stopPropagation stops the
  518. * remaining ancestors. */
  519. if (ev_get_bool(state, slot, "bubbles", def_bubbles)) {
  520. for (i = 1; i < np && !events[slot].stopped; i++) {
  521. listener_phase(state, path[i], type, 2, slot);
  522. if (!events[slot].stopped &&
  523. path[i] != JS_TARGET_WINDOW &&
  524. path[i] != JS_TARGET_DOCUMENT)
  525. run_inline_handler(state, path[i], type, slot,
  526. &events[slot].prevented);
  527. }
  528. }
  529. prevented = events[slot].prevented;
  530. js_unref(state, events[slot].objref);
  531. events[slot].objref[0] = '\0';
  532. events[slot].used = 0;
  533. js_pushboolean(state, prevented ? 0 : 1);
  534. return prevented ? 0 : 1;
  535. }
  536. static void add_listener(js_State *state, unsigned int target, int remove)
  537. {
  538. const char *type;
  539. int i;
  540. if (!js_iscallable(state, 2))
  541. return;
  542. type = js_tostring(state, 1);
  543. if (!type[0])
  544. return;
  545. if (!remove) {
  546. for (i = 0; i < JS_LISTENERS; i++)
  547. if (!listeners[i].used)
  548. break;
  549. if (i == JS_LISTENERS)
  550. return;
  551. listeners[i].used = 1;
  552. listeners[i].capture = js_isboolean(state, 3) &&
  553. js_toboolean(state, 3);
  554. listeners[i].target = target;
  555. snprintf(listeners[i].type, sizeof(listeners[i].type), "%s",
  556. type);
  557. js_copy(state, 2);
  558. snprintf(listeners[i].ref, sizeof(listeners[i].ref), "%s",
  559. js_ref(state));
  560. return;
  561. }
  562. for (i = 0; i < JS_LISTENERS; i++) {
  563. struct js_listener *l = &listeners[i];
  564. int eq;
  565. if (!l->used || l->target != target ||
  566. strcmp(l->type, type) != 0)
  567. continue;
  568. js_getregistry(state, l->ref);
  569. js_copy(state, 2);
  570. eq = js_equal(state);
  571. js_pop(state, 2);
  572. if (!eq)
  573. continue;
  574. js_unref(state, l->ref);
  575. l->used = 0;
  576. return;
  577. }
  578. }
  579. static void node_addlistener(js_State *state)
  580. {
  581. unsigned int node = (unsigned int)(uintptr_t)
  582. js_currentfunctiondata(state) - 1;
  583. add_listener(state, node, 0);
  584. }
  585. static void node_removelistener(js_State *state)
  586. {
  587. unsigned int node = (unsigned int)(uintptr_t)
  588. js_currentfunctiondata(state) - 1;
  589. add_listener(state, node, 1);
  590. }
  591. static void node_dispatchevent(js_State *state)
  592. {
  593. unsigned int node = (unsigned int)(uintptr_t)
  594. js_currentfunctiondata(state) - 1;
  595. if (!D || !node) {
  596. js_pushboolean(state, 1);
  597. return;
  598. }
  599. if (js_isobject(state, 1)) {
  600. dispatch_object(state, node, 1, "event", 1, 0);
  601. } else {
  602. char type[32];
  603. const char *s = js_tostring(state, 1);
  604. unsigned int i = 0;
  605. while (s[i] && i + 1 < sizeof(type)) {
  606. type[i] = s[i];
  607. i++;
  608. }
  609. type[i] = '\0';
  610. dispatch_object(state, node, 0, type, 1, 0);
  611. }
  612. }
  613. static void win_addlistener(js_State *state)
  614. {
  615. add_listener(state, JS_TARGET_WINDOW, 0);
  616. }
  617. static void win_removelistener(js_State *state)
  618. {
  619. add_listener(state, JS_TARGET_WINDOW, 1);
  620. }
  621. static void doc_addlistener(js_State *state)
  622. {
  623. add_listener(state, JS_TARGET_DOCUMENT, 0);
  624. }
  625. static void doc_removelistener(js_State *state)
  626. {
  627. add_listener(state, JS_TARGET_DOCUMENT, 1);
  628. }
  629. static void event_init(js_State *state)
  630. {
  631. int slot = (int)(uintptr_t)js_currentfunctiondata(state);
  632. const char *type;
  633. if (slot < 0 || slot >= JS_EVENT_TEMPLATES ||
  634. !event_templates[slot].used)
  635. return;
  636. type = js_tostring(state, 1);
  637. js_getregistry(state, event_templates[slot].ref);
  638. if (!js_isobject(state, -1)) {
  639. js_pop(state, 1);
  640. return;
  641. }
  642. obj_set_string(state, -1, "type", type);
  643. obj_set_bool(state, -1, "bubbles", js_toboolean(state, 2));
  644. obj_set_bool(state, -1, "cancelable", js_toboolean(state, 3));
  645. js_pop(state, 1);
  646. }
  647. static void event_build_object(js_State *state, const char *type,
  648. int bubbles, int cancelable, int with_init)
  649. {
  650. js_newobject(state);
  651. js_pushstring(state, type ? type : "");
  652. js_setproperty(state, -2, "type");
  653. js_pushboolean(state, bubbles != 0);
  654. js_setproperty(state, -2, "bubbles");
  655. js_pushboolean(state, cancelable != 0);
  656. js_setproperty(state, -2, "cancelable");
  657. js_pushboolean(state, 0);
  658. js_setproperty(state, -2, "defaultPrevented");
  659. if (with_init) {
  660. int i;
  661. for (i = 0; i < JS_EVENT_TEMPLATES; i++)
  662. if (!event_templates[i].used)
  663. break;
  664. if (i < JS_EVENT_TEMPLATES) {
  665. event_templates[i].used = 1;
  666. snprintf(event_templates[i].ref,
  667. sizeof(event_templates[i].ref),
  668. "jseventtmpl_%d", i);
  669. js_copy(state, -1);
  670. js_setregistry(state, event_templates[i].ref);
  671. def_func_data(state, "initEvent", event_init, 3,
  672. (unsigned int)i);
  673. }
  674. }
  675. }
  676. static void event_call(js_State *state)
  677. {
  678. const char *type = js_isdefined(state, 1) ? js_tostring(state, 1) : "";
  679. int bubbles = 0, cancelable = 0;
  680. if (js_isobject(state, 2)) {
  681. js_getproperty(state, 2, "bubbles");
  682. bubbles = js_toboolean(state, -1);
  683. js_pop(state, 1);
  684. js_getproperty(state, 2, "cancelable");
  685. cancelable = js_toboolean(state, -1);
  686. js_pop(state, 1);
  687. }
  688. event_build_object(state, type, bubbles, cancelable, 1);
  689. }
  690. static void doc_createevent(js_State *state)
  691. {
  692. const char *type = js_tostring(state, 1);
  693. event_build_object(state, type, 0, 0, 1);
  694. }
  695. static void custom_event_call(js_State *state)
  696. {
  697. event_call(state);
  698. if (js_isobject(state, 2))
  699. js_getproperty(state, 2, "detail");
  700. else
  701. js_pushnull(state);
  702. js_setproperty(state, -2, "detail");
  703. }
  704. static void mouse_event_call(js_State *state)
  705. {
  706. static const struct {
  707. const char *name;
  708. int is_number;
  709. } props[] = {
  710. { "clientX", 1 }, { "clientY", 1 }, { "screenX", 1 },
  711. { "screenY", 1 }, { "button", 1 }, { "buttons", 1 },
  712. { "ctrlKey", 0 }, { "shiftKey", 0 }, { "altKey", 0 },
  713. { "metaKey", 0 }
  714. };
  715. unsigned int i;
  716. event_call(state);
  717. if (!js_isobject(state, 2))
  718. return;
  719. for (i = 0; i < sizeof(props) / sizeof(props[0]); i++) {
  720. js_getproperty(state, 2, props[i].name);
  721. if (props[i].is_number) {
  722. double v = js_isnumber(state, -1) ?
  723. js_tonumber(state, -1) : 0;
  724. js_pop(state, 1);
  725. js_pushnumber(state, v);
  726. } else {
  727. int v = js_toboolean(state, -1);
  728. js_pop(state, 1);
  729. js_pushboolean(state, v);
  730. }
  731. js_setproperty(state, -2, props[i].name);
  732. }
  733. }
  734. int js_browser_event(unsigned int node, const char *type, int bubbles,
  735. int cancelable)
  736. {
  737. int slot, rv;
  738. if (!J || !D)
  739. return 1;
  740. for (slot = 0; slot < JS_EVENT_SLOTS; slot++)
  741. if (!events[slot].used)
  742. break;
  743. if (slot == JS_EVENT_SLOTS)
  744. return 1;
  745. js_setlimit(J, JS_RUN_LIMIT, 0);
  746. rv = dispatch_object(J, node, 0, type, bubbles, cancelable);
  747. js_pop(J, 1);
  748. return rv;
  749. }
  750. static unsigned int node_type_value(unsigned int node)
  751. {
  752. struct br_node *n;
  753. if (!D || node >= D->nnode)
  754. return 0;
  755. n = &D->nodes[node];
  756. if (n->type == BR_NODE_TEXT)
  757. return 3;
  758. if (n->type == BR_NODE_COMMENT)
  759. return 8;
  760. if (n->type == BR_NODE_FRAGMENT)
  761. return 11;
  762. return 1;
  763. }
  764. static void push_child_list(js_State *state, unsigned int node, int elements)
  765. {
  766. struct br_node *n;
  767. unsigned int c, i = 0;
  768. js_newarray(state);
  769. if (!D || node >= D->nnode)
  770. return;
  771. n = &D->nodes[node];
  772. for (c = n->first; c; c = D->nodes[c].next) {
  773. if (elements && D->nodes[c].type != BR_NODE_ELEM)
  774. continue;
  775. push_node(state, c + 1);
  776. js_setindex(state, -2, (int)i++);
  777. }
  778. }
  779. static unsigned int node_prev_sibling(unsigned int node)
  780. {
  781. unsigned int p, c, prev = 0;
  782. if (!D || node >= D->nnode)
  783. return 0;
  784. p = D->nodes[node].parent;
  785. for (c = D->nodes[p].first; c && c != node; c = D->nodes[c].next)
  786. prev = c;
  787. return prev;
  788. }
  789. /* ---- inline style and dataset objects ---- */
  790. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  791. {
  792. unsigned int i;
  793. for (i = 0; i < n; i++) {
  794. char ca = a[i], cb = b[i];
  795. if (ca >= 'A' && ca <= 'Z')
  796. ca = (char)(ca - 'A' + 'a');
  797. if (cb >= 'A' && cb <= 'Z')
  798. cb = (char)(cb - 'A' + 'a');
  799. if (ca != cb)
  800. return 0;
  801. }
  802. return 1;
  803. }
  804. /* camelCase JS property -> CSS property ("marginTop" -> "margin-top"). */
  805. static void css_name(const char *name, char *out, unsigned int cap)
  806. {
  807. unsigned int n = 0, i;
  808. if (!strcmp(name, "cssFloat")) {
  809. if (cap > 6)
  810. memcpy(out, "float", 6);
  811. else if (cap)
  812. out[0] = '\0';
  813. return;
  814. }
  815. for (i = 0; name[i] && n + 2 < cap; i++) {
  816. char c = name[i];
  817. if (c >= 'A' && c <= 'Z') {
  818. if (n == 0 || out[n - 1] != '-')
  819. out[n++] = '-';
  820. out[n++] = (char)(c - 'A' + 'a');
  821. } else {
  822. out[n++] = c;
  823. }
  824. }
  825. if (cap)
  826. out[n] = '\0';
  827. }
  828. /* camelCase dataset property -> attribute name ("fooBar" -> "data-foo-bar"). */
  829. static void data_name(const char *name, char *out, unsigned int cap)
  830. {
  831. unsigned int n = 0, i;
  832. for (i = 0; i < 5 && n + 1 < cap; i++)
  833. out[n++] = "data-"[i];
  834. for (i = 0; name[i] && n + 2 < cap; i++) {
  835. char c = name[i];
  836. if (c >= 'A' && c <= 'Z') {
  837. out[n++] = '-';
  838. c = (char)(c - 'A' + 'a');
  839. }
  840. out[n++] = c;
  841. }
  842. if (cap)
  843. out[n] = '\0';
  844. }
  845. static int style_lookup(const char *style, const char *prop, char *out,
  846. unsigned int cap)
  847. {
  848. unsigned int i = 0, nl = (unsigned int)strlen(prop);
  849. while (style && style[i]) {
  850. unsigned int ns, ne, vs, ve, a, b, k;
  851. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  852. i++;
  853. if (!style[i])
  854. break;
  855. ns = i;
  856. while (style[i] && style[i] != ':' && style[i] != ';')
  857. i++;
  858. ne = i;
  859. if (style[i] != ':') {
  860. if (style[i] == ';')
  861. i++;
  862. continue;
  863. }
  864. i++;
  865. while (style[i] == ' ' || style[i] == '\t')
  866. i++;
  867. vs = i;
  868. while (style[i] && style[i] != ';')
  869. i++;
  870. ve = i;
  871. a = ns;
  872. b = ne;
  873. while (a < b && (style[a] == ' ' || style[a] == '\t'))
  874. a++;
  875. while (b > a && (style[b - 1] == ' ' || style[b - 1] == '\t'))
  876. b--;
  877. while (ve > vs && (style[ve - 1] == ' ' || style[ve - 1] == '\t'))
  878. ve--;
  879. if (b - a == nl && ci_eq_n(style + a, prop, nl)) {
  880. for (k = 0; k < ve - vs && k + 1 < cap; k++)
  881. out[k] = style[vs + k];
  882. out[k < cap ? k : cap - 1] = '\0';
  883. return 1;
  884. }
  885. }
  886. return 0;
  887. }
  888. static unsigned int style_count(const char *style)
  889. {
  890. unsigned int i = 0, n = 0;
  891. while (style && style[i]) {
  892. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  893. i++;
  894. if (!style[i])
  895. break;
  896. while (style[i] && style[i] != ';')
  897. i++;
  898. n++;
  899. }
  900. return n;
  901. }
  902. static unsigned int style_span(const char *style, unsigned int i)
  903. {
  904. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  905. i++;
  906. return i;
  907. }
  908. static void style_update(unsigned int node, const char *prop,
  909. const char *value, int remove)
  910. {
  911. const char *old = br_find_attr(D, node, "style");
  912. char arr[512];
  913. unsigned int n = 0, i = 0, nl = (unsigned int)strlen(prop);
  914. int replaced = 0;
  915. if (remove && !old)
  916. return;
  917. while (old && old[i] && n + 2 < sizeof(arr)) {
  918. unsigned int start = style_span(old, i);
  919. unsigned int ns, ne, ve, a, b, k;
  920. i = start;
  921. if (!old[i])
  922. break;
  923. ns = i;
  924. while (old[i] && old[i] != ':' && old[i] != ';')
  925. i++;
  926. ne = i;
  927. if (old[i] == ':') {
  928. i++;
  929. while (old[i] == ' ' || old[i] == '\t')
  930. i++;
  931. while (old[i] && old[i] != ';')
  932. i++;
  933. }
  934. ve = i;
  935. a = ns;
  936. b = ne;
  937. while (a < b && (old[a] == ' ' || old[a] == '\t'))
  938. a++;
  939. while (b > a && (old[b - 1] == ' ' || old[b - 1] == '\t'))
  940. b--;
  941. if (b - a == nl && ci_eq_n(old + a, prop, nl)) {
  942. replaced = 1;
  943. if (remove || !value || !value[0])
  944. continue;
  945. }
  946. for (k = ns; k < ve && n + 1 < sizeof(arr); k++)
  947. arr[n++] = old[k];
  948. if (n + 1 < sizeof(arr))
  949. arr[n++] = ';';
  950. }
  951. if (replaced && (remove || !value || !value[0])) {
  952. arr[n] = '\0';
  953. br_set_attr(D, node, "style", arr, n);
  954. if (host.changed)
  955. host.changed();
  956. return;
  957. }
  958. if (value && value[0] && n + nl + strlen(value) + 3 < sizeof(arr)) {
  959. for (i = 0; i < nl; i++)
  960. arr[n++] = prop[i];
  961. arr[n++] = ':';
  962. for (i = 0; value[i]; i++)
  963. arr[n++] = value[i];
  964. arr[n++] = ';';
  965. }
  966. arr[n] = '\0';
  967. br_set_attr(D, node, "style", arr, n);
  968. if (host.changed)
  969. host.changed();
  970. }
  971. static void style_getproperty(js_State *state)
  972. {
  973. unsigned int node = node_of(js_currentfunctiondata(state));
  974. char val[256];
  975. if (style_lookup(br_find_attr(D, node, "style"),
  976. js_tostring(state, 1), val, sizeof(val)))
  977. js_pushstring(state, val);
  978. else
  979. js_pushstring(state, "");
  980. }
  981. static void style_setproperty(js_State *state)
  982. {
  983. unsigned int node = node_of(js_currentfunctiondata(state));
  984. style_update(node, js_tostring(state, 1),
  985. js_tostring(state, 2), 0);
  986. }
  987. static void style_removeproperty(js_State *state)
  988. {
  989. unsigned int node = node_of(js_currentfunctiondata(state));
  990. const char *prop = js_tostring(state, 1);
  991. char old[256];
  992. int had = style_lookup(br_find_attr(D, node, "style"), prop,
  993. old, sizeof(old));
  994. style_update(node, prop, "", 1);
  995. if (had)
  996. js_pushstring(state, old);
  997. else
  998. js_pushstring(state, "");
  999. }
  1000. static int style_has(js_State *state, void *p, const char *name)
  1001. {
  1002. unsigned int node = node_of(p);
  1003. const char *style;
  1004. char prop[64], val[256];
  1005. if (!D || node >= D->nnode)
  1006. return 0;
  1007. style = br_find_attr(D, node, "style");
  1008. if (!strcmp(name, "cssText")) {
  1009. js_pushstring(state, style ? style : "");
  1010. return 1;
  1011. }
  1012. if (!strcmp(name, "length")) {
  1013. js_pushnumber(state, style_count(style));
  1014. return 1;
  1015. }
  1016. if (!strcmp(name, "getPropertyValue")) {
  1017. push_func_data(state, name, style_getproperty, 1, node + 1);
  1018. return 1;
  1019. }
  1020. if (!strcmp(name, "setProperty")) {
  1021. push_func_data(state, name, style_setproperty, 2, node + 1);
  1022. return 1;
  1023. }
  1024. if (!strcmp(name, "removeProperty")) {
  1025. push_func_data(state, name, style_removeproperty, 1, node + 1);
  1026. return 1;
  1027. }
  1028. css_name(name, prop, sizeof(prop));
  1029. if (style_lookup(style, prop, val, sizeof(val)))
  1030. js_pushstring(state, val);
  1031. else
  1032. js_pushstring(state, "");
  1033. return 1;
  1034. }
  1035. static int style_put(js_State *state, void *p, const char *name)
  1036. {
  1037. unsigned int node = node_of(p);
  1038. const char *value;
  1039. char prop[64];
  1040. if (!D || node >= D->nnode)
  1041. return 0;
  1042. value = js_tostring(state, -1);
  1043. if (!strcmp(name, "cssText")) {
  1044. br_set_attr(D, node, "style", value, (unsigned int)strlen(value));
  1045. if (host.changed)
  1046. host.changed();
  1047. return 1;
  1048. }
  1049. css_name(name, prop, sizeof(prop));
  1050. style_update(node, prop, value, 0);
  1051. return 1;
  1052. }
  1053. static void push_style_object(js_State *state, unsigned int node)
  1054. {
  1055. char key[32];
  1056. snprintf(key, sizeof(key), "sty_%u", node + 1);
  1057. js_getregistry(state, key);
  1058. if (!js_isundefined(state, -1))
  1059. return;
  1060. js_pop(state, 1);
  1061. js_pushnull(state);
  1062. js_newuserdatax(state, "style", (void *)(uintptr_t)(node + 1),
  1063. style_has, style_put, 0, 0);
  1064. js_copy(state, -1);
  1065. js_setregistry(state, key);
  1066. }
  1067. static int dataset_has(js_State *state, void *p, const char *name)
  1068. {
  1069. unsigned int node = node_of(p);
  1070. char attr[64];
  1071. const char *value;
  1072. if (!D || node >= D->nnode)
  1073. return 0;
  1074. data_name(name, attr, sizeof(attr));
  1075. value = br_find_attr(D, node, attr);
  1076. if (value)
  1077. js_pushstring(state, value);
  1078. else
  1079. js_pushundefined(state);
  1080. return 1;
  1081. }
  1082. static int dataset_put(js_State *state, void *p, const char *name)
  1083. {
  1084. unsigned int node = node_of(p);
  1085. char attr[64];
  1086. const char *value = js_tostring(state, -1);
  1087. if (!D || node >= D->nnode)
  1088. return 0;
  1089. data_name(name, attr, sizeof(attr));
  1090. br_set_attr(D, node, attr, value, (unsigned int)strlen(value));
  1091. if (host.changed)
  1092. host.changed();
  1093. return 1;
  1094. }
  1095. static int dataset_delete(js_State *state, void *p, const char *name)
  1096. {
  1097. unsigned int node = node_of(p);
  1098. char attr[64];
  1099. (void)state;
  1100. if (!D || node >= D->nnode)
  1101. return 0;
  1102. data_name(name, attr, sizeof(attr));
  1103. br_remove_attr(D, node, attr);
  1104. if (host.changed)
  1105. host.changed();
  1106. return 1;
  1107. }
  1108. static void push_dataset(js_State *state, unsigned int node)
  1109. {
  1110. char key[32];
  1111. snprintf(key, sizeof(key), "ds_%u", node + 1);
  1112. js_getregistry(state, key);
  1113. if (!js_isundefined(state, -1))
  1114. return;
  1115. js_pop(state, 1);
  1116. js_pushnull(state);
  1117. js_newuserdatax(state, "dataset", (void *)(uintptr_t)(node + 1),
  1118. dataset_has, dataset_put, dataset_delete, 0);
  1119. js_copy(state, -1);
  1120. js_setregistry(state, key);
  1121. }
  1122. static void node_class_set(unsigned int node, const char *classes)
  1123. {
  1124. br_set_attr(D, node, "class", classes,
  1125. (unsigned int)strlen(classes));
  1126. if (host.changed)
  1127. host.changed();
  1128. }
  1129. static int class_has(const char *classes, const char *name)
  1130. {
  1131. unsigned int i = 0;
  1132. if (!classes)
  1133. return 0;
  1134. while (classes[i]) {
  1135. unsigned int start;
  1136. while (classes[i] == ' ' || classes[i] == '\t')
  1137. i++;
  1138. start = i;
  1139. while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
  1140. i++;
  1141. if (i - start == strlen(name) &&
  1142. !strncmp(classes + start, name, i - start))
  1143. return 1;
  1144. }
  1145. return 0;
  1146. }
  1147. static void class_add(unsigned int node, const char *name, int add)
  1148. {
  1149. const char *old = node_attr(node, "class");
  1150. char buf[256];
  1151. unsigned int n = 0, i = 0;
  1152. if (add) {
  1153. if (class_has(old, name))
  1154. return;
  1155. if (old && old[0]) {
  1156. while (old[i] && n + 1 < sizeof(buf))
  1157. buf[n++] = old[i++];
  1158. if (n + 1 < sizeof(buf))
  1159. buf[n++] = ' ';
  1160. }
  1161. i = 0;
  1162. while (name[i] && n + 1 < sizeof(buf))
  1163. buf[n++] = name[i++];
  1164. } else {
  1165. if (!old)
  1166. return;
  1167. while (old[i]) {
  1168. unsigned int start;
  1169. while (old[i] == ' ' || old[i] == '\t')
  1170. i++;
  1171. start = i;
  1172. while (old[i] && old[i] != ' ' && old[i] != '\t')
  1173. i++;
  1174. if (i - start == strlen(name) &&
  1175. !strncmp(old + start, name, i - start))
  1176. continue;
  1177. if (n && n + 1 < sizeof(buf))
  1178. buf[n++] = ' ';
  1179. {
  1180. unsigned int k;
  1181. for (k = start; k < i && n + 1 < sizeof(buf);
  1182. k++)
  1183. buf[n++] = old[k];
  1184. }
  1185. }
  1186. }
  1187. buf[n] = '\0';
  1188. node_class_set(node, buf);
  1189. }
  1190. static void node_classlist_add(js_State *state)
  1191. {
  1192. class_add(node_of(js_currentfunctiondata(state)),
  1193. js_tostring(state, 1), 1);
  1194. }
  1195. static void node_classlist_remove(js_State *state)
  1196. {
  1197. class_add(node_of(js_currentfunctiondata(state)),
  1198. js_tostring(state, 1), 0);
  1199. }
  1200. static void node_classlist_toggle(js_State *state)
  1201. {
  1202. unsigned int node = node_of(js_currentfunctiondata(state));
  1203. const char *name = js_tostring(state, 1);
  1204. int has = class_has(node_attr(node, "class"), name);
  1205. int want = !has;
  1206. if (js_isdefined(state, 2))
  1207. want = js_toboolean(state, 2);
  1208. if (want != has)
  1209. class_add(node, name, want);
  1210. js_pushboolean(state, want);
  1211. }
  1212. static void node_classlist_contains(js_State *state)
  1213. {
  1214. unsigned int node = node_of(js_currentfunctiondata(state));
  1215. js_pushboolean(state, class_has(node_attr(node, "class"),
  1216. js_tostring(state, 1)));
  1217. }
  1218. static unsigned int class_count(const char *classes)
  1219. {
  1220. unsigned int i = 0, n = 0;
  1221. if (!classes)
  1222. return 0;
  1223. while (classes[i]) {
  1224. while (classes[i] == ' ' || classes[i] == '\t')
  1225. i++;
  1226. if (!classes[i])
  1227. break;
  1228. n++;
  1229. while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
  1230. i++;
  1231. }
  1232. return n;
  1233. }
  1234. static void push_attributes(js_State *state, unsigned int node)
  1235. {
  1236. struct br_node *n;
  1237. const char *p, *end;
  1238. unsigned int count = 0;
  1239. js_newobject(state);
  1240. if (!D || node >= D->nnode)
  1241. return;
  1242. n = &D->nodes[node];
  1243. p = D->attrs + n->extra_off;
  1244. end = p + n->extra_len;
  1245. while (p + 1 < end) {
  1246. const char *an = p;
  1247. const char *av;
  1248. p += strlen(p) + 1;
  1249. if (p >= end)
  1250. break;
  1251. av = p;
  1252. p += strlen(p) + 1;
  1253. js_newobject(state);
  1254. js_pushstring(state, an);
  1255. js_setproperty(state, -2, "name");
  1256. js_pushstring(state, av);
  1257. js_setproperty(state, -2, "value");
  1258. js_pushboolean(state, 1);
  1259. js_setproperty(state, -2, "specified");
  1260. /* Array-like: attributes[i] and attributes[name] both work. */
  1261. js_copy(state, -1);
  1262. js_setproperty(state, -3, an);
  1263. js_setindex(state, -2, (int)count);
  1264. count++;
  1265. }
  1266. js_pushnumber(state, count);
  1267. js_setproperty(state, -2, "length");
  1268. }
  1269. static void push_classlist(js_State *state, unsigned int node)
  1270. {
  1271. js_newobject(state);
  1272. def_func_data(state, "add", node_classlist_add, 1, node + 1);
  1273. def_func_data(state, "remove", node_classlist_remove, 1, node + 1);
  1274. def_func_data(state, "toggle", node_classlist_toggle, 1, node + 1);
  1275. def_func_data(state, "contains", node_classlist_contains, 1, node + 1);
  1276. js_pushnumber(state, class_count(node_attr(node, "class")));
  1277. js_setproperty(state, -2, "length");
  1278. }
  1279. static void node_getattr(js_State *state)
  1280. {
  1281. unsigned int node = node_of(js_currentfunctiondata(state));
  1282. push_attr(state, node, js_tostring(state, 1), 1);
  1283. }
  1284. static void node_setattr(js_State *state)
  1285. {
  1286. unsigned int node = node_of(js_currentfunctiondata(state));
  1287. const char *name = js_tostring(state, 1);
  1288. const char *value = js_tostring(state, 2);
  1289. char lower[64];
  1290. unsigned int i;
  1291. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  1292. char c = name[i];
  1293. if (c >= 'A' && c <= 'Z')
  1294. c = (char)(c - 'A' + 'a');
  1295. lower[i] = c;
  1296. }
  1297. lower[i] = '\0';
  1298. br_set_attr(D, node, lower, value, (unsigned int)strlen(value));
  1299. if (host.changed)
  1300. host.changed();
  1301. }
  1302. static void node_removeattr(js_State *state)
  1303. {
  1304. unsigned int node = node_of(js_currentfunctiondata(state));
  1305. const char *name = js_tostring(state, 1);
  1306. char lower[64];
  1307. unsigned int i;
  1308. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  1309. char c = name[i];
  1310. if (c >= 'A' && c <= 'Z')
  1311. c = (char)(c - 'A' + 'a');
  1312. lower[i] = c;
  1313. }
  1314. lower[i] = '\0';
  1315. br_remove_attr(D, node, lower);
  1316. if (host.changed)
  1317. host.changed();
  1318. }
  1319. static void node_hasattr(js_State *state)
  1320. {
  1321. unsigned int node = node_of(js_currentfunctiondata(state));
  1322. js_pushboolean(state, node_attr(node, js_tostring(state, 1)) != 0);
  1323. }
  1324. static void node_appendchild(js_State *state)
  1325. {
  1326. unsigned int node = node_of(js_currentfunctiondata(state));
  1327. unsigned int child = arg_node(state, 1);
  1328. if (br_append_child(D, node, child) != 0) {
  1329. js_pushnull(state);
  1330. return;
  1331. }
  1332. if (host.changed)
  1333. host.changed();
  1334. push_node(state, child);
  1335. }
  1336. static void node_insertbefore(js_State *state)
  1337. {
  1338. unsigned int node = node_of(js_currentfunctiondata(state));
  1339. unsigned int child = arg_node(state, 1);
  1340. unsigned int before = 0;
  1341. if (!js_isnull(state, 2) && !js_isundefined(state, 2))
  1342. before = arg_node(state, 2);
  1343. if (br_insert_before(D, node, child, before) != 0) {
  1344. js_pushnull(state);
  1345. return;
  1346. }
  1347. if (host.changed)
  1348. host.changed();
  1349. push_node(state, child);
  1350. }
  1351. static void node_removechild(js_State *state)
  1352. {
  1353. unsigned int node = node_of(js_currentfunctiondata(state));
  1354. unsigned int child = arg_node(state, 1);
  1355. if (br_remove_child(D, node, child) != 0) {
  1356. js_pushnull(state);
  1357. return;
  1358. }
  1359. if (host.changed)
  1360. host.changed();
  1361. push_node(state, child);
  1362. }
  1363. static void node_replacechild(js_State *state)
  1364. {
  1365. unsigned int node = node_of(js_currentfunctiondata(state));
  1366. unsigned int child = arg_node(state, 1);
  1367. unsigned int old = arg_node(state, 2);
  1368. br_insert_before(D, node, child, old);
  1369. br_remove_child(D, node, old);
  1370. if (host.changed)
  1371. host.changed();
  1372. push_node(state, old);
  1373. }
  1374. static void node_clone(js_State *state)
  1375. {
  1376. unsigned int node = node_of(js_currentfunctiondata(state));
  1377. unsigned int h = br_clone_node(D, node + 1,
  1378. js_toboolean(state, 1));
  1379. push_node(state, h);
  1380. }
  1381. static void node_contains(js_State *state)
  1382. {
  1383. unsigned int node = node_of(js_currentfunctiondata(state));
  1384. unsigned int other = arg_node(state, 1);
  1385. unsigned int p = other;
  1386. while (1) {
  1387. if (p == node) {
  1388. js_pushboolean(state, 1);
  1389. return;
  1390. }
  1391. if (p == 0)
  1392. break;
  1393. p = D->nodes[p].parent;
  1394. }
  1395. js_pushboolean(state, 0);
  1396. }
  1397. static void node_haschildren(js_State *state)
  1398. {
  1399. unsigned int node = node_of(js_currentfunctiondata(state));
  1400. js_pushboolean(state, node < D->nnode && D->nodes[node].first != 0);
  1401. }
  1402. static void node_remove(js_State *state)
  1403. {
  1404. unsigned int node = node_of(js_currentfunctiondata(state));
  1405. unsigned int parent = node < D->nnode ? D->nodes[node].parent : 0;
  1406. (void)state;
  1407. if (parent)
  1408. br_remove_child(D, parent, node + 1);
  1409. if (host.changed)
  1410. host.changed();
  1411. }
  1412. static void node_matches(js_State *state)
  1413. {
  1414. unsigned int node = node_of(js_currentfunctiondata(state));
  1415. js_pushboolean(state, br_matches(D, node + 1,
  1416. js_tostring(state, 1)));
  1417. }
  1418. static void node_click(js_State *state)
  1419. {
  1420. unsigned int node = node_of(js_currentfunctiondata(state));
  1421. if (host.click)
  1422. host.click(node);
  1423. js_pushundefined(state);
  1424. }
  1425. static void node_getelementsbytagname(js_State *state)
  1426. {
  1427. unsigned int node = node_of(js_currentfunctiondata(state));
  1428. const char *name = js_tostring(state, 1);
  1429. unsigned int out[256];
  1430. unsigned int n = br_query_all(D, node + 1, name, out, 256);
  1431. js_newarray(state);
  1432. for (unsigned int i = 0; i < n; i++) {
  1433. push_node(state, out[i]);
  1434. js_setindex(state, -2, (int)i);
  1435. }
  1436. }
  1437. static void node_getelementsbyclassname(js_State *state)
  1438. {
  1439. unsigned int node = node_of(js_currentfunctiondata(state));
  1440. const char *name = js_tostring(state, 1);
  1441. char sel[256];
  1442. unsigned int n = 0, i = 0;
  1443. unsigned int out[256], count;
  1444. while (name[i] && n + 2 < sizeof(sel)) {
  1445. while (name[i] == ' ')
  1446. i++;
  1447. if (!name[i])
  1448. break;
  1449. sel[n++] = '.';
  1450. while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
  1451. sel[n++] = name[i++];
  1452. }
  1453. sel[n] = '\0';
  1454. count = br_query_all(D, node + 1, sel, out, 256);
  1455. js_newarray(state);
  1456. for (i = 0; i < count; i++) {
  1457. push_node(state, out[i]);
  1458. js_setindex(state, -2, (int)i);
  1459. }
  1460. }
  1461. static void node_queryselector(js_State *state)
  1462. {
  1463. unsigned int node = node_of(js_currentfunctiondata(state));
  1464. unsigned int out[1];
  1465. unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
  1466. out, 1);
  1467. if (n)
  1468. push_node(state, out[0]);
  1469. else
  1470. js_pushnull(state);
  1471. }
  1472. static void node_queryselectorall(js_State *state)
  1473. {
  1474. unsigned int node = node_of(js_currentfunctiondata(state));
  1475. unsigned int out[256];
  1476. unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
  1477. out, 256);
  1478. js_newarray(state);
  1479. for (unsigned int i = 0; i < n; i++) {
  1480. push_node(state, out[i]);
  1481. js_setindex(state, -2, (int)i);
  1482. }
  1483. }
  1484. static void node_getrect(js_State *state)
  1485. {
  1486. unsigned int node = node_of(js_currentfunctiondata(state));
  1487. int x = 0, y = 0, w = 0, h = 0;
  1488. if (host.rect)
  1489. host.rect(node, &x, &y, &w, &h);
  1490. js_newobject(state);
  1491. js_pushnumber(state, y); js_setproperty(state, -2, "top");
  1492. js_pushnumber(state, x); js_setproperty(state, -2, "left");
  1493. js_pushnumber(state, x + w); js_setproperty(state, -2, "right");
  1494. js_pushnumber(state, y + h); js_setproperty(state, -2, "bottom");
  1495. js_pushnumber(state, w); js_setproperty(state, -2, "width");
  1496. js_pushnumber(state, h); js_setproperty(state, -2, "height");
  1497. }
  1498. static void node_noop(js_State *state)
  1499. {
  1500. (void)state;
  1501. }
  1502. static void node_push_text(js_State *state, unsigned int node)
  1503. {
  1504. unsigned int cap = D->text_used + 1;
  1505. char *buf;
  1506. unsigned int len;
  1507. if (cap < 32)
  1508. cap = 32;
  1509. buf = (char *)malloc(cap);
  1510. if (!buf) {
  1511. js_pushstring(state, "");
  1512. return;
  1513. }
  1514. len = br_text_content(D, node, buf, cap);
  1515. js_pushlstring(state, buf, (int)len);
  1516. free(buf);
  1517. }
  1518. static unsigned int node_form_ancestor(unsigned int node)
  1519. {
  1520. while (node && node < D->nnode) {
  1521. if (D->nodes[node].type == BR_NODE_ELEM &&
  1522. D->nodes[node].tag == BR_TAG_FORM)
  1523. return node;
  1524. node = D->nodes[node].parent;
  1525. }
  1526. return 0;
  1527. }
  1528. static void push_form_elements(js_State *state, unsigned int form)
  1529. {
  1530. unsigned int out[128];
  1531. int n = 0, i;
  1532. if (host.form_elements)
  1533. n = host.form_elements(form, out, (int)(sizeof(out) /
  1534. sizeof(out[0])));
  1535. js_newarray(state);
  1536. for (i = 0; i < n; i++) {
  1537. const char *name;
  1538. push_node(state, out[i] + 1);
  1539. js_setindex(state, -2, i);
  1540. name = br_find_attr(D, out[i], "name");
  1541. if (name && name[0]) {
  1542. push_node(state, out[i] + 1);
  1543. js_setproperty(state, -2, name);
  1544. }
  1545. }
  1546. }
  1547. static void form_submit_js(js_State *state)
  1548. {
  1549. unsigned int node = node_of(js_currentfunctiondata(state));
  1550. if (host.form_submit)
  1551. host.form_submit(node);
  1552. }
  1553. static void form_reset_js(js_State *state)
  1554. {
  1555. unsigned int node = node_of(js_currentfunctiondata(state));
  1556. if (host.form_reset)
  1557. host.form_reset(node);
  1558. }
  1559. /* Userdata property hooks: every element wrapper carries node + 1. */
  1560. static int node_has(js_State *state, void *p, const char *name)
  1561. {
  1562. unsigned int node = node_of(p);
  1563. struct br_node *n;
  1564. char buf[64];
  1565. if (!D || node >= D->nnode)
  1566. return 0;
  1567. n = &D->nodes[node];
  1568. if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_FORM) {
  1569. if (!strcmp(name, "elements")) {
  1570. push_form_elements(state, node);
  1571. return 1;
  1572. }
  1573. if (!strcmp(name, "length")) {
  1574. unsigned int out[128];
  1575. int count = 0;
  1576. if (host.form_elements)
  1577. count = host.form_elements(node, out,
  1578. (int)(sizeof(out) / sizeof(out[0])));
  1579. js_pushnumber(state, count);
  1580. return 1;
  1581. }
  1582. if (!strcmp(name, "submit")) {
  1583. push_func_data(state, "submit", form_submit_js, 0,
  1584. node + 1);
  1585. return 1;
  1586. }
  1587. if (!strcmp(name, "reset")) {
  1588. push_func_data(state, "reset", form_reset_js, 0,
  1589. node + 1);
  1590. return 1;
  1591. }
  1592. }
  1593. if (!strcmp(name, "form")) {
  1594. unsigned int f;
  1595. if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_FORM)
  1596. f = 0;
  1597. else
  1598. f = node_form_ancestor(node);
  1599. if (f)
  1600. push_node(state, f + 1);
  1601. else
  1602. js_pushnull(state);
  1603. return 1;
  1604. }
  1605. if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
  1606. node_push_text(state, node);
  1607. return 1;
  1608. }
  1609. if (!strcmp(name, "nodeValue")) {
  1610. if (n->type == BR_NODE_TEXT || n->type == BR_NODE_COMMENT)
  1611. js_pushlstring(state, D->text + n->text_off,
  1612. (int)n->text_len);
  1613. else
  1614. js_pushnull(state);
  1615. return 1;
  1616. }
  1617. if (!strcmp(name, "nodeType")) {
  1618. js_pushnumber(state, node_type_value(node));
  1619. return 1;
  1620. }
  1621. if (!strcmp(name, "nodeName") || !strcmp(name, "tagName")) {
  1622. node_name(node, buf, sizeof(buf));
  1623. js_pushstring(state, buf);
  1624. return 1;
  1625. }
  1626. if (!strcmp(name, "parentNode") || !strcmp(name, "parentElement")) {
  1627. unsigned int p = n->parent;
  1628. push_node(state, p ? p + 1 : 0);
  1629. return 1;
  1630. }
  1631. if (!strcmp(name, "childNodes")) {
  1632. push_child_list(state, node, 0);
  1633. return 1;
  1634. }
  1635. if (!strcmp(name, "children")) {
  1636. push_child_list(state, node, 1);
  1637. return 1;
  1638. }
  1639. if (!strcmp(name, "firstChild")) {
  1640. push_node(state, n->first ? n->first + 1 : 0);
  1641. return 1;
  1642. }
  1643. if (!strcmp(name, "lastChild")) {
  1644. push_node(state, n->last ? n->last + 1 : 0);
  1645. return 1;
  1646. }
  1647. if (!strcmp(name, "firstElementChild") ||
  1648. !strcmp(name, "lastElementChild")) {
  1649. unsigned int c = n->first;
  1650. unsigned int found = 0;
  1651. if (!strcmp(name, "lastElementChild")) {
  1652. for (c = n->first; c; c = D->nodes[c].next)
  1653. if (D->nodes[c].type == BR_NODE_ELEM)
  1654. found = c;
  1655. } else {
  1656. while (c && D->nodes[c].type != BR_NODE_ELEM)
  1657. c = D->nodes[c].next;
  1658. found = c;
  1659. }
  1660. push_node(state, found ? found + 1 : 0);
  1661. return 1;
  1662. }
  1663. if (!strcmp(name, "nextSibling") || !strcmp(name, "nextElementSibling")) {
  1664. unsigned int c = n->next;
  1665. if (!strcmp(name, "nextElementSibling"))
  1666. while (c && D->nodes[c].type != BR_NODE_ELEM)
  1667. c = D->nodes[c].next;
  1668. push_node(state, c ? c + 1 : 0);
  1669. return 1;
  1670. }
  1671. if (!strcmp(name, "previousSibling") ||
  1672. !strcmp(name, "previousElementSibling")) {
  1673. unsigned int c = node_prev_sibling(node);
  1674. if (!strcmp(name, "previousElementSibling"))
  1675. while (c && D->nodes[c].type != BR_NODE_ELEM)
  1676. c = node_prev_sibling(c);
  1677. push_node(state, c ? c + 1 : 0);
  1678. return 1;
  1679. }
  1680. if (!strcmp(name, "childElementCount")) {
  1681. unsigned int c, count = 0;
  1682. for (c = n->first; c; c = D->nodes[c].next)
  1683. if (D->nodes[c].type == BR_NODE_ELEM)
  1684. count++;
  1685. js_pushnumber(state, count);
  1686. return 1;
  1687. }
  1688. if (n->tag == BR_TAG_A || n->tag == BR_TAG_AREA) {
  1689. if (!strcmp(name, "href") || !strcmp(name, "protocol") ||
  1690. !strcmp(name, "host") || !strcmp(name, "hostname") ||
  1691. !strcmp(name, "port") || !strcmp(name, "pathname") ||
  1692. !strcmp(name, "search") || !strcmp(name, "hash") ||
  1693. !strcmp(name, "origin")) {
  1694. push_element_url(state, node, name);
  1695. return 1;
  1696. }
  1697. }
  1698. if (!strcmp(name, "id")) { push_attr(state, node, "id", 0); return 1; }
  1699. if (!strcmp(name, "className")) {
  1700. push_attr(state, node, "class", 0);
  1701. return 1;
  1702. }
  1703. if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
  1704. if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
  1705. if (!strcmp(name, "value")) {
  1706. char buf[128];
  1707. if (host.form_value && host.form_value(node, buf, sizeof(buf)))
  1708. js_pushstring(state, buf);
  1709. else
  1710. push_attr(state, node, "value", 0);
  1711. return 1;
  1712. }
  1713. if (!strcmp(name, "type")) { push_attr(state, node, "type", 0); return 1; }
  1714. if (!strcmp(name, "name")) { push_attr(state, node, "name", 0); return 1; }
  1715. if (!strcmp(name, "alt")) { push_attr(state, node, "alt", 0); return 1; }
  1716. if (!strcmp(name, "title")) { push_attr(state, node, "title", 0); return 1; }
  1717. if (!strcmp(name, "checked")) {
  1718. int c = host.form_checked ? host.form_checked(node) : -1;
  1719. js_pushboolean(state, c >= 0 ? c : node_attr(node, name) != 0);
  1720. return 1;
  1721. }
  1722. if (!strcmp(name, "selectedIndex")) {
  1723. int c = host.form_selected ? host.form_selected(node) : -1;
  1724. js_pushnumber(state, c >= 0 ? c : 0);
  1725. return 1;
  1726. }
  1727. if (!strcmp(name, "selected")) {
  1728. int c = host.form_selected ? host.form_selected(node) : -1;
  1729. if (c >= 0)
  1730. js_pushboolean(state, c != 0);
  1731. else
  1732. js_pushboolean(state, node_attr(node, name) != 0);
  1733. return 1;
  1734. }
  1735. if (!strcmp(name, "disabled")) {
  1736. js_pushboolean(state, node_attr(node, name) != 0);
  1737. return 1;
  1738. }
  1739. if (!strcmp(name, "style")) {
  1740. push_style_object(state, node);
  1741. return 1;
  1742. }
  1743. if (!strcmp(name, "dataset") || !strcmp(name, "domStringMap")) {
  1744. push_dataset(state, node);
  1745. return 1;
  1746. }
  1747. if (!strcmp(name, "classList")) {
  1748. push_classlist(state, node);
  1749. return 1;
  1750. }
  1751. if (!strcmp(name, "attributes")) {
  1752. push_attributes(state, node);
  1753. return 1;
  1754. }
  1755. if (!strcmp(name, "innerHTML") || !strcmp(name, "outerHTML")) {
  1756. char key[32];
  1757. snprintf(key, sizeof(key), "html_%u", node + 1);
  1758. js_getregistry(state, key);
  1759. if (js_isundefined(state, -1)) {
  1760. js_pop(state, 1);
  1761. js_pushstring(state, "");
  1762. }
  1763. return 1;
  1764. }
  1765. if (!strcmp(name, "ownerDocument")) {
  1766. js_getglobal(state, "document");
  1767. return 1;
  1768. }
  1769. if (!strcmp(name, "getAttribute"))
  1770. { push_func_data(state, "getAttribute", node_getattr, 1, node + 1); return 1; }
  1771. if (!strcmp(name, "setAttribute"))
  1772. { push_func_data(state, "setAttribute", node_setattr, 2, node + 1); return 1; }
  1773. if (!strcmp(name, "removeAttribute"))
  1774. { push_func_data(state, "removeAttribute", node_removeattr, 1, node + 1); return 1; }
  1775. if (!strcmp(name, "hasAttribute"))
  1776. { push_func_data(state, "hasAttribute", node_hasattr, 1, node + 1); return 1; }
  1777. if (!strcmp(name, "appendChild"))
  1778. { push_func_data(state, "appendChild", node_appendchild, 1, node + 1); return 1; }
  1779. if (!strcmp(name, "insertBefore"))
  1780. { push_func_data(state, "insertBefore", node_insertbefore, 2, node + 1); return 1; }
  1781. if (!strcmp(name, "removeChild"))
  1782. { push_func_data(state, "removeChild", node_removechild, 1, node + 1); return 1; }
  1783. if (!strcmp(name, "replaceChild"))
  1784. { push_func_data(state, "replaceChild", node_replacechild, 2, node + 1); return 1; }
  1785. if (!strcmp(name, "cloneNode"))
  1786. { push_func_data(state, "cloneNode", node_clone, 1, node + 1); return 1; }
  1787. if (!strcmp(name, "contains"))
  1788. { push_func_data(state, "contains", node_contains, 1, node + 1); return 1; }
  1789. if (!strcmp(name, "hasChildNodes"))
  1790. { push_func_data(state, "hasChildNodes", node_haschildren, 0, node + 1); return 1; }
  1791. if (!strcmp(name, "remove"))
  1792. { push_func_data(state, "remove", node_remove, 0, node + 1); return 1; }
  1793. if (!strcmp(name, "matches") || !strcmp(name, "webkitMatchesSelector") ||
  1794. !strcmp(name, "mozMatchesSelector") ||
  1795. !strcmp(name, "msMatchesSelector"))
  1796. { push_func_data(state, name, node_matches, 1, node + 1); return 1; }
  1797. if (!strcmp(name, "getElementsByTagName"))
  1798. { push_func_data(state, "getElementsByTagName", node_getelementsbytagname, 1, node + 1); return 1; }
  1799. if (!strcmp(name, "getElementsByClassName"))
  1800. { push_func_data(state, "getElementsByClassName", node_getelementsbyclassname, 1, node + 1); return 1; }
  1801. if (!strcmp(name, "querySelector"))
  1802. { push_func_data(state, "querySelector", node_queryselector, 1, node + 1); return 1; }
  1803. if (!strcmp(name, "querySelectorAll"))
  1804. { push_func_data(state, "querySelectorAll", node_queryselectorall, 1, node + 1); return 1; }
  1805. if (!strcmp(name, "addEventListener"))
  1806. { push_func_data(state, "addEventListener", node_addlistener, 2, node + 1); return 1; }
  1807. if (!strcmp(name, "removeEventListener"))
  1808. { push_func_data(state, "removeEventListener", node_removelistener, 2, node + 1); return 1; }
  1809. if (!strcmp(name, "dispatchEvent"))
  1810. { push_func_data(state, "dispatchEvent", node_dispatchevent, 1, node + 1); return 1; }
  1811. if (!strcmp(name, "getBoundingClientRect"))
  1812. { push_func_data(state, "getBoundingClientRect", node_getrect, 0, node + 1); return 1; }
  1813. if (!strcmp(name, "getClientRects")) {
  1814. js_newarray(state);
  1815. return 1;
  1816. }
  1817. if (!strcmp(name, "offsetWidth") || !strcmp(name, "offsetHeight") ||
  1818. !strcmp(name, "clientWidth") || !strcmp(name, "clientHeight") ||
  1819. !strcmp(name, "scrollWidth") || !strcmp(name, "scrollHeight")) {
  1820. int x = 0, y = 0, w = 0, h = 0;
  1821. if (host.rect)
  1822. host.rect(node, &x, &y, &w, &h);
  1823. js_pushnumber(state, (!strcmp(name, "offsetHeight") ||
  1824. !strcmp(name, "clientHeight") ||
  1825. !strcmp(name, "scrollHeight")) ? h : w);
  1826. return 1;
  1827. }
  1828. if (!strcmp(name, "scrollTop") || !strcmp(name, "scrollLeft")) {
  1829. js_pushnumber(state, 0);
  1830. return 1;
  1831. }
  1832. if (!strcmp(name, "click")) {
  1833. push_func_data(state, "click", node_click, 0, node + 1);
  1834. return 1;
  1835. }
  1836. if (!strcmp(name, "focus") || !strcmp(name, "blur")) {
  1837. push_func_data(state, name, node_noop, 0, node + 1);
  1838. return 1;
  1839. }
  1840. return 0;
  1841. }
  1842. static void node_set_string(unsigned int node, const char *name,
  1843. const char *value)
  1844. {
  1845. br_set_attr(D, node, name, value, (unsigned int)strlen(value));
  1846. if (host.changed)
  1847. host.changed();
  1848. }
  1849. static int node_put(js_State *state, void *p, const char *name)
  1850. {
  1851. unsigned int node = node_of(p);
  1852. if (!D || node >= D->nnode)
  1853. return 0;
  1854. if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
  1855. const char *text = js_tostring(state, -1);
  1856. unsigned int len = (unsigned int)strlen(text);
  1857. if (br_set_text_content(D, node, text, len) != 0) {
  1858. if (host.status)
  1859. host.status("Page text arena is full", 1);
  1860. } else if (host.changed) {
  1861. host.changed();
  1862. }
  1863. return 1;
  1864. }
  1865. if (!strcmp(name, "nodeValue")) {
  1866. const char *text = js_tostring(state, -1);
  1867. if (br_set_text_node(D, node, text,
  1868. (unsigned int)strlen(text)) == 0 &&
  1869. host.changed)
  1870. host.changed();
  1871. return 1;
  1872. }
  1873. if (!strcmp(name, "id") || !strcmp(name, "className") ||
  1874. !strcmp(name, "href") || !strcmp(name, "src") ||
  1875. !strcmp(name, "value") || !strcmp(name, "type") ||
  1876. !strcmp(name, "name") || !strcmp(name, "alt") ||
  1877. !strcmp(name, "title")) {
  1878. const char *value = js_tostring(state, -1);
  1879. if (!strcmp(name, "value") && host.form_set_value &&
  1880. host.form_set_value(node, value))
  1881. return 1;
  1882. if (!strcmp(name, "className"))
  1883. node_set_string(node, "class", value);
  1884. else
  1885. node_set_string(node, name, value);
  1886. return 1;
  1887. }
  1888. if (!strcmp(name, "selectedIndex")) {
  1889. if (host.form_set_selected)
  1890. host.form_set_selected(node, js_tointeger(state, -1));
  1891. return 1;
  1892. }
  1893. if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
  1894. !strcmp(name, "selected")) {
  1895. int b = js_toboolean(state, -1);
  1896. if (!strcmp(name, "checked") && host.form_set_checked &&
  1897. host.form_set_checked(node, b))
  1898. return 1;
  1899. if (!strcmp(name, "selected") && host.form_set_selected &&
  1900. host.form_set_selected(node, b ? 1 : 0))
  1901. return 1;
  1902. if (b)
  1903. br_set_attr(D, node, name, "", 0);
  1904. else
  1905. br_remove_attr(D, node, name);
  1906. if (host.changed)
  1907. host.changed();
  1908. return 1;
  1909. }
  1910. if (!strcmp(name, "innerHTML")) {
  1911. const char *html = js_tostring(state, -1);
  1912. char key[32];
  1913. br_set_inner_html(D, node, html, (unsigned int)strlen(html));
  1914. snprintf(key, sizeof(key), "html_%u", node + 1);
  1915. js_pushstring(state, html);
  1916. js_setregistry(state, key);
  1917. if (host.changed)
  1918. host.changed();
  1919. return 1;
  1920. }
  1921. if (!strcmp(name, "outerHTML")) {
  1922. char key[32];
  1923. snprintf(key, sizeof(key), "html_%u", node + 1);
  1924. js_pushstring(state, js_tostring(state, -1));
  1925. js_setregistry(state, key);
  1926. return 1;
  1927. }
  1928. if (!strcmp(name, "style")) {
  1929. if (js_isstring(state, -1))
  1930. node_set_string(node, "style",
  1931. js_tostring(state, -1));
  1932. return 1;
  1933. }
  1934. return 0;
  1935. }
  1936. static int node_delete(js_State *state, void *p, const char *name)
  1937. {
  1938. (void)state;
  1939. (void)p;
  1940. (void)name;
  1941. return 0;
  1942. }
  1943. static void doc_getelementbyid(js_State *state)
  1944. {
  1945. const char *id = js_tostring(state, 1);
  1946. push_node(state, br_find_id(D, id));
  1947. }
  1948. static void console_log(js_State *state)
  1949. {
  1950. int i;
  1951. int n = js_gettop(state);
  1952. for (i = 1; i < n; i++) {
  1953. if (i > 1)
  1954. u_putc(' ');
  1955. u_puts(js_tostring(state, i));
  1956. }
  1957. u_putc('\n');
  1958. }
  1959. static void js_alert(js_State *state)
  1960. {
  1961. char msg[160];
  1962. unsigned int used = 0;
  1963. int i;
  1964. int n = js_gettop(state);
  1965. for (i = 1; i < n; i++) {
  1966. const char *s = js_tostring(state, i);
  1967. unsigned int k;
  1968. if (i > 1 && used + 1 < sizeof(msg))
  1969. msg[used++] = ' ';
  1970. for (k = 0; s[k] && used + 1 < sizeof(msg); k++)
  1971. msg[used++] = s[k];
  1972. }
  1973. msg[used] = '\0';
  1974. if (host.status)
  1975. host.status(msg, 0);
  1976. }
  1977. static void push_query_array(js_State *state, unsigned int root,
  1978. const char *selector)
  1979. {
  1980. unsigned int out[256];
  1981. unsigned int n = br_query_all(D, root, selector, out, 256);
  1982. unsigned int i;
  1983. js_newarray(state);
  1984. for (i = 0; i < n; i++) {
  1985. push_node(state, out[i]);
  1986. js_setindex(state, -2, (int)i);
  1987. }
  1988. }
  1989. static void doc_create_element(js_State *state)
  1990. {
  1991. const char *name = js_tostring(state, 1);
  1992. char lower[32];
  1993. unsigned int i;
  1994. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  1995. char c = name[i];
  1996. if (c >= 'A' && c <= 'Z')
  1997. c = (char)(c - 'A' + 'a');
  1998. lower[i] = c;
  1999. }
  2000. lower[i] = '\0';
  2001. push_node(state, br_create_element(D, lower, i));
  2002. }
  2003. static void doc_create_text(js_State *state)
  2004. {
  2005. const char *text = js_tostring(state, 1);
  2006. push_node(state, br_create_text(D, text, (unsigned int)strlen(text)));
  2007. }
  2008. static void doc_create_comment(js_State *state)
  2009. {
  2010. const char *text = js_tostring(state, 1);
  2011. push_node(state, br_create_comment(D, text, (unsigned int)strlen(text)));
  2012. }
  2013. static void doc_create_fragment(js_State *state)
  2014. {
  2015. push_node(state, br_create_fragment(D));
  2016. }
  2017. static void doc_create_attribute(js_State *state)
  2018. {
  2019. const char *name = js_tostring(state, 1);
  2020. js_newobject(state);
  2021. js_pushstring(state, name);
  2022. js_setproperty(state, -2, "name");
  2023. js_pushstring(state, "");
  2024. js_setproperty(state, -2, "value");
  2025. js_pushboolean(state, 1);
  2026. js_setproperty(state, -2, "specified");
  2027. }
  2028. static void doc_getelementsbytagname(js_State *state)
  2029. {
  2030. push_query_array(state, 0, js_tostring(state, 1));
  2031. }
  2032. static void doc_getelementsbyclassname(js_State *state)
  2033. {
  2034. const char *name = js_tostring(state, 1);
  2035. char sel[256];
  2036. unsigned int n = 0, i = 0;
  2037. while (name[i] && n + 2 < sizeof(sel)) {
  2038. while (name[i] == ' ')
  2039. i++;
  2040. if (!name[i])
  2041. break;
  2042. sel[n++] = '.';
  2043. while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
  2044. sel[n++] = name[i++];
  2045. }
  2046. sel[n] = '\0';
  2047. push_query_array(state, 0, sel);
  2048. }
  2049. static void doc_queryselector(js_State *state)
  2050. {
  2051. unsigned int out[1];
  2052. unsigned int n = br_query_all(D, 0, js_tostring(state, 1), out, 1);
  2053. if (n)
  2054. push_node(state, out[0]);
  2055. else
  2056. js_pushnull(state);
  2057. }
  2058. static void doc_queryselectorall(js_State *state)
  2059. {
  2060. push_query_array(state, 0, js_tostring(state, 1));
  2061. }
  2062. static void location_get(js_State *state)
  2063. {
  2064. js_pushstring(state, current_url ? current_url : "");
  2065. }
  2066. static void location_set(js_State *state)
  2067. {
  2068. const char *url = js_tostring(state, 1);
  2069. if (host.navigate)
  2070. host.navigate(url);
  2071. }
  2072. static void win_noop(js_State *state);
  2073. struct loc_parts {
  2074. const char *protocol, *host, *hostname, *port, *path, *search, *hash;
  2075. const char *origin;
  2076. unsigned int protocol_len, host_len, hostname_len, port_len;
  2077. unsigned int path_len, search_len, hash_len, origin_len;
  2078. };
  2079. static void loc_split(const char *u, struct loc_parts *p)
  2080. {
  2081. unsigned int n = (unsigned int)strlen(u), i;
  2082. const char *rest;
  2083. memset(p, 0, sizeof(*p));
  2084. p->path = u;
  2085. p->path_len = n;
  2086. p->origin = "null";
  2087. p->origin_len = 4;
  2088. for (i = 0; i < n; i++)
  2089. if (u[i] == ':')
  2090. break;
  2091. if (i + 2 < n && u[i] == ':' && u[i + 1] == '/' && u[i + 2] == '/') {
  2092. const char *hosts = u + i + 3, *hoste = hosts;
  2093. while (*hoste && *hoste != '/' && *hoste != '?' && *hoste != '#')
  2094. hoste++;
  2095. p->protocol = u;
  2096. p->protocol_len = i + 1;
  2097. p->host = hosts;
  2098. p->host_len = (unsigned int)(hoste - hosts);
  2099. p->hostname = hosts;
  2100. p->hostname_len = p->host_len;
  2101. for (i = 0; i < p->hostname_len; i++)
  2102. if (hosts[i] == ':') {
  2103. p->port = hosts + i + 1;
  2104. p->port_len = p->hostname_len - i - 1;
  2105. p->hostname_len = i;
  2106. break;
  2107. }
  2108. p->origin = u;
  2109. p->origin_len = (unsigned int)(hoste - u);
  2110. rest = hoste;
  2111. } else {
  2112. p->protocol = "file:";
  2113. p->protocol_len = 5;
  2114. rest = u;
  2115. }
  2116. {
  2117. const char *q = rest;
  2118. while (*q && *q != '?' && *q != '#')
  2119. q++;
  2120. p->path = rest;
  2121. p->path_len = (unsigned int)(q - rest);
  2122. if (*q == '?') {
  2123. const char *s = q;
  2124. while (*q && *q != '#')
  2125. q++;
  2126. p->search = s;
  2127. p->search_len = (unsigned int)(q - s);
  2128. }
  2129. if (*q == '#') {
  2130. p->hash = q;
  2131. p->hash_len = (unsigned int)strlen(q);
  2132. }
  2133. }
  2134. }
  2135. static void push_location(js_State *state)
  2136. {
  2137. struct loc_parts p;
  2138. loc_split(current_url ? current_url : "", &p);
  2139. js_newobject(state);
  2140. js_newcfunction(state, location_get, "get href", 0);
  2141. js_newcfunction(state, location_set, "set href", 1);
  2142. js_defaccessor(state, -3, "href", 0);
  2143. #define LOC_FIELD(field) \
  2144. do { \
  2145. js_pushlstring(state, p.field ? p.field : "", \
  2146. p.field ? (int)p.field##_len : 0); \
  2147. js_setproperty(state, -2, #field); \
  2148. } while (0)
  2149. LOC_FIELD(protocol);
  2150. LOC_FIELD(host);
  2151. LOC_FIELD(hostname);
  2152. LOC_FIELD(port);
  2153. js_pushlstring(state, p.path ? p.path : "",
  2154. p.path ? (int)p.path_len : 0);
  2155. js_setproperty(state, -2, "pathname");
  2156. LOC_FIELD(search);
  2157. LOC_FIELD(hash);
  2158. LOC_FIELD(origin);
  2159. #undef LOC_FIELD
  2160. js_newcfunction(state, win_noop, "reload", 0);
  2161. js_setproperty(state, -2, "reload");
  2162. js_newcfunction(state, win_noop, "assign", 1);
  2163. js_setproperty(state, -2, "assign");
  2164. js_newcfunction(state, win_noop, "replace", 1);
  2165. js_setproperty(state, -2, "replace");
  2166. js_newcfunction(state, win_noop, "toString", 0);
  2167. js_setproperty(state, -2, "toString");
  2168. }
  2169. /* ---- URL resolution for <a>/<area> URL properties ---- */
  2170. static int url_has_scheme(const char *s)
  2171. {
  2172. unsigned int i = 1;
  2173. if (!((s[0] >= 'a' && s[0] <= 'z') ||
  2174. (s[0] >= 'A' && s[0] <= 'Z')))
  2175. return 0;
  2176. while (s[i]) {
  2177. char c = s[i];
  2178. if (c == ':')
  2179. return 1;
  2180. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  2181. (c >= '0' && c <= '9') || c == '+' || c == '-' ||
  2182. c == '.'))
  2183. return 0;
  2184. i++;
  2185. }
  2186. return 0;
  2187. }
  2188. static void url_put(char *out, unsigned int cap, unsigned int *n,
  2189. const char *s, unsigned int len)
  2190. {
  2191. unsigned int i;
  2192. for (i = 0; i < len && *n + 1 < cap; i++)
  2193. out[(*n)++] = s[i];
  2194. }
  2195. /* Collapse //, /./ and /../ inside the path part of an absolute URL. */
  2196. static void url_normalize(char *url)
  2197. {
  2198. char tmp[512];
  2199. char *path;
  2200. unsigned int n = 0, i = 0;
  2201. path = strstr(url, "://");
  2202. if (path) {
  2203. path += 3;
  2204. while (*path && *path != '/')
  2205. path++;
  2206. } else {
  2207. path = url;
  2208. }
  2209. if (path[0] != '/')
  2210. return;
  2211. while (path[i]) {
  2212. unsigned int start;
  2213. while (path[i] == '/')
  2214. i++;
  2215. start = i;
  2216. while (path[i] && path[i] != '/')
  2217. i++;
  2218. if (i == start)
  2219. continue;
  2220. if (i - start == 1 && path[start] == '.')
  2221. continue;
  2222. if (i - start == 2 && path[start] == '.' &&
  2223. path[start + 1] == '.') {
  2224. while (n > 0 && tmp[n - 1] != '/')
  2225. n--;
  2226. if (n > 0)
  2227. n--;
  2228. continue;
  2229. }
  2230. if (n + 1 < sizeof(tmp))
  2231. tmp[n++] = '/';
  2232. while (start < i && n + 1 < sizeof(tmp))
  2233. tmp[n++] = path[start++];
  2234. }
  2235. if (n == 0)
  2236. tmp[n++] = '/';
  2237. tmp[n] = '\0';
  2238. memcpy(path, tmp, n + 1);
  2239. }
  2240. static void url_resolve(const char *base, const char *rel,
  2241. char *out, unsigned int cap)
  2242. {
  2243. struct loc_parts bp;
  2244. unsigned int n = 0, i;
  2245. if (cap == 0)
  2246. return;
  2247. if (!rel[0]) {
  2248. url_put(out, cap, &n, base, (unsigned int)strlen(base));
  2249. out[n] = '\0';
  2250. return;
  2251. }
  2252. if (url_has_scheme(rel)) {
  2253. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  2254. out[n] = '\0';
  2255. url_normalize(out);
  2256. return;
  2257. }
  2258. loc_split(base, &bp);
  2259. if (rel[0] == '/' && rel[1] == '/') {
  2260. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  2261. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  2262. } else if (rel[0] == '/') {
  2263. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  2264. url_put(out, cap, &n, "//", 2);
  2265. url_put(out, cap, &n, bp.host, bp.host_len);
  2266. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  2267. } else if (rel[0] == '#' || rel[0] == '?') {
  2268. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  2269. if (bp.host_len || (bp.protocol_len == 5 &&
  2270. bp.protocol[0] == 'f')) {
  2271. url_put(out, cap, &n, "//", 2);
  2272. url_put(out, cap, &n, bp.host, bp.host_len);
  2273. }
  2274. url_put(out, cap, &n, bp.path, bp.path_len);
  2275. if (rel[0] == '#')
  2276. url_put(out, cap, &n, bp.search, bp.search_len);
  2277. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  2278. } else {
  2279. const char *dir_end = bp.path;
  2280. for (i = 0; i < bp.path_len; i++)
  2281. if (bp.path[i] == '/')
  2282. dir_end = bp.path + i + 1;
  2283. if (dir_end == bp.path)
  2284. dir_end = bp.path + bp.path_len;
  2285. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  2286. if (bp.host_len || (bp.protocol_len == 5 &&
  2287. bp.protocol[0] == 'f')) {
  2288. url_put(out, cap, &n, "//", 2);
  2289. url_put(out, cap, &n, bp.host, bp.host_len);
  2290. }
  2291. url_put(out, cap, &n, bp.path,
  2292. (unsigned int)(dir_end - bp.path));
  2293. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  2294. }
  2295. out[n] = '\0';
  2296. url_normalize(out);
  2297. }
  2298. /* <a> URL properties are resolved against the page URL, like the DOM. */
  2299. static void push_element_url(js_State *state, unsigned int node,
  2300. const char *name)
  2301. {
  2302. char abs_url[512];
  2303. const char *href = br_find_attr(D, node, "href");
  2304. struct loc_parts p;
  2305. if (!href || !href[0]) {
  2306. js_pushstring(state, "");
  2307. return;
  2308. }
  2309. url_resolve(current_url ? current_url : "/", href,
  2310. abs_url, sizeof(abs_url));
  2311. loc_split(abs_url, &p);
  2312. if (!strcmp(name, "href"))
  2313. js_pushstring(state, abs_url);
  2314. else if (!strcmp(name, "protocol"))
  2315. js_pushlstring(state, p.protocol, (int)p.protocol_len);
  2316. else if (!strcmp(name, "origin"))
  2317. js_pushlstring(state, p.origin ? p.origin : "",
  2318. (int)p.origin_len);
  2319. else if (!strcmp(name, "host"))
  2320. js_pushlstring(state, p.host ? p.host : "", (int)p.host_len);
  2321. else if (!strcmp(name, "hostname"))
  2322. js_pushlstring(state, p.hostname ? p.hostname : "",
  2323. (int)p.hostname_len);
  2324. else if (!strcmp(name, "port"))
  2325. js_pushlstring(state, p.port ? p.port : "", (int)p.port_len);
  2326. else if (!strcmp(name, "pathname"))
  2327. js_pushlstring(state, p.path ? p.path : "", (int)p.path_len);
  2328. else if (!strcmp(name, "search"))
  2329. js_pushlstring(state, p.search ? p.search : "",
  2330. (int)p.search_len);
  2331. else
  2332. js_pushlstring(state, p.hash ? p.hash : "", (int)p.hash_len);
  2333. }
  2334. static void win_noop(js_State *state)
  2335. {
  2336. (void)state;
  2337. }
  2338. static void timer_add(js_State *state, int repeat)
  2339. {
  2340. int i;
  2341. unsigned int ms;
  2342. if (!js_iscallable(state, 1)) {
  2343. js_pushnumber(state, 0);
  2344. return;
  2345. }
  2346. ms = js_isdefined(state, 2) ?
  2347. (unsigned int)js_tointeger(state, 2) : 0;
  2348. if (repeat && ms == 0)
  2349. ms = 1;
  2350. for (i = 0; i < JS_TIMERS; i++)
  2351. if (!timers[i].used)
  2352. break;
  2353. if (i == JS_TIMERS) {
  2354. js_pushnumber(state, 0);
  2355. return;
  2356. }
  2357. js_copy(state, 1);
  2358. snprintf(timers[i].ref, sizeof(timers[i].ref), "%s",
  2359. js_ref(state));
  2360. timers[i].used = 1;
  2361. timers[i].repeat = repeat;
  2362. timers[i].interval = ms;
  2363. timers[i].deadline = u_ticks_ms() + ms;
  2364. js_pushnumber(state, i + 1);
  2365. }
  2366. static void win_settimeout(js_State *state)
  2367. {
  2368. timer_add(state, 0);
  2369. }
  2370. static void win_setinterval(js_State *state)
  2371. {
  2372. timer_add(state, 1);
  2373. }
  2374. static void timer_clear(js_State *state)
  2375. {
  2376. int id = js_tointeger(state, 1);
  2377. if (id >= 1 && id <= JS_TIMERS && timers[id - 1].used) {
  2378. js_unref(state, timers[id - 1].ref);
  2379. timers[id - 1].used = 0;
  2380. }
  2381. }
  2382. static void win_requestanimationframe(js_State *state)
  2383. {
  2384. timer_add(state, 0);
  2385. }
  2386. static void win_performance_now(js_State *state)
  2387. {
  2388. js_pushnumber(state, (double)u_ticks_ms());
  2389. }
  2390. struct computed_prop {
  2391. const char *js;
  2392. const char *css;
  2393. };
  2394. static const struct computed_prop computed_props[] = {
  2395. { "display", "display" }, { "position", "position" },
  2396. { "width", "width" }, { "height", "height" },
  2397. { "color", "color" }, { "backgroundColor", "background-color" },
  2398. { "fontSize", "font-size" }, { "fontWeight", "font-weight" },
  2399. { "fontStyle", "font-style" }, { "fontFamily", "font-family" },
  2400. { "textAlign", "text-align" }, { "float", "float" },
  2401. { "cssFloat", "float" }, { "clear", "clear" },
  2402. { "opacity", "opacity" }, { "visibility", "visibility" },
  2403. { "overflow", "overflow" }, { "whiteSpace", "white-space" },
  2404. { "textTransform", "text-transform" }, { "boxSizing", "box-sizing" },
  2405. { "lineHeight", "line-height" }, { "zIndex", "z-index" },
  2406. { "top", "top" }, { "left", "left" }, { "right", "right" },
  2407. { "bottom", "bottom" },
  2408. { "marginTop", "margin-top" }, { "marginRight", "margin-right" },
  2409. { "marginBottom", "margin-bottom" }, { "marginLeft", "margin-left" },
  2410. { "paddingTop", "padding-top" }, { "paddingRight", "padding-right" },
  2411. { "paddingBottom", "padding-bottom" },
  2412. { "paddingLeft", "padding-left" },
  2413. { "borderTopWidth", "border-top-width" },
  2414. { "borderRightWidth", "border-right-width" },
  2415. { "borderBottomWidth", "border-bottom-width" },
  2416. { "borderLeftWidth", "border-left-width" },
  2417. { "borderTopColor", "border-top-color" },
  2418. { "borderRightColor", "border-right-color" },
  2419. { "borderBottomColor", "border-bottom-color" },
  2420. { "borderLeftColor", "border-left-color" },
  2421. { "backgroundImage", "background-image" },
  2422. { "backgroundRepeat", "background-repeat" },
  2423. { "backgroundPosition", "background-position" },
  2424. { "backgroundSize", "background-size" },
  2425. { "transform", "transform" },
  2426. { 0, 0 }
  2427. };
  2428. static void computed_get(js_State *state)
  2429. {
  2430. unsigned int node = node_of(js_currentfunctiondata(state));
  2431. const char *prop = js_tostring(state, 1);
  2432. char buf[192];
  2433. if (host.computed && host.computed(node, prop, buf, sizeof(buf)))
  2434. js_pushstring(state, buf);
  2435. else
  2436. js_pushstring(state, "");
  2437. }
  2438. static void win_getcomputedstyle(js_State *state)
  2439. {
  2440. unsigned int node = 0;
  2441. char buf[192];
  2442. int i;
  2443. if (js_isuserdata(state, 1, "node"))
  2444. node = node_of(js_touserdata(state, 1, "node"));
  2445. js_newobject(state);
  2446. def_func_data(state, "getPropertyValue", computed_get, 1, node + 1);
  2447. for (i = 0; computed_props[i].js; i++) {
  2448. if (!host.computed ||
  2449. !host.computed(node, computed_props[i].css, buf,
  2450. sizeof(buf)))
  2451. continue;
  2452. js_pushstring(state, buf);
  2453. js_setproperty(state, -2, computed_props[i].js);
  2454. }
  2455. js_pushstring(state, "");
  2456. js_setproperty(state, -2, "cssText");
  2457. }
  2458. static void win_matchmedia(js_State *state)
  2459. {
  2460. const char *query = js_tostring(state, 1);
  2461. js_newobject(state);
  2462. js_pushboolean(state, 0);
  2463. js_setproperty(state, -2, "matches");
  2464. js_pushstring(state, query);
  2465. js_setproperty(state, -2, "media");
  2466. def_func(state, "addListener", win_noop, 1);
  2467. def_func(state, "removeListener", win_noop, 1);
  2468. def_func(state, "addEventListener", win_noop, 2);
  2469. def_func(state, "removeEventListener", win_noop, 2);
  2470. }
  2471. static void install_globals(js_State *state)
  2472. {
  2473. unsigned int h;
  2474. js_newobject(state);
  2475. def_func(state, "getElementById", doc_getelementbyid, 1);
  2476. def_func(state, "createElement", doc_create_element, 1);
  2477. def_func(state, "createTextNode", doc_create_text, 1);
  2478. def_func(state, "createComment", doc_create_comment, 1);
  2479. def_func(state, "createDocumentFragment", doc_create_fragment, 0);
  2480. def_func(state, "createAttribute", doc_create_attribute, 1);
  2481. def_func(state, "getElementsByTagName", doc_getelementsbytagname, 1);
  2482. def_func(state, "getElementsByClassName", doc_getelementsbyclassname, 1);
  2483. def_func(state, "querySelector", doc_queryselector, 1);
  2484. def_func(state, "querySelectorAll", doc_queryselectorall, 1);
  2485. def_func(state, "addEventListener", doc_addlistener, 2);
  2486. def_func(state, "removeEventListener", doc_removelistener, 2);
  2487. def_func(state, "createEvent", doc_createevent, 1);
  2488. h = br_find_tag(D, BR_TAG_HTML);
  2489. if (!h && D->nnode > 1)
  2490. h = D->nodes[0].first ? D->nodes[0].first + 1 : 0;
  2491. push_node(state, h);
  2492. js_setproperty(state, -2, "documentElement");
  2493. h = br_find_tag(D, BR_TAG_BODY);
  2494. push_node(state, h);
  2495. js_setproperty(state, -2, "body");
  2496. h = br_find_tag(D, BR_TAG_HEAD);
  2497. push_node(state, h);
  2498. js_setproperty(state, -2, "head");
  2499. js_pushnumber(state, 9);
  2500. js_setproperty(state, -2, "nodeType");
  2501. js_pushstring(state, "#document");
  2502. js_setproperty(state, -2, "nodeName");
  2503. js_pushnull(state);
  2504. js_setproperty(state, -2, "ownerDocument");
  2505. js_pushstring(state, "CSS1Compat");
  2506. js_setproperty(state, -2, "compatMode");
  2507. js_pushstring(state, D->title);
  2508. js_setproperty(state, -2, "title");
  2509. js_pushstring(state, "complete");
  2510. js_setproperty(state, -2, "readyState");
  2511. js_pushstring(state, "");
  2512. js_setproperty(state, -2, "cookie");
  2513. js_pushstring(state, current_url ? current_url : "");
  2514. js_setproperty(state, -2, "URL");
  2515. js_defglobal(state, "document", JS_DONTENUM);
  2516. js_newobject(state);
  2517. js_newcfunction(state, console_log, "log", 0);
  2518. js_setproperty(state, -2, "log");
  2519. js_defglobal(state, "console", JS_DONTENUM);
  2520. js_newcfunction(state, js_alert, "alert", 1);
  2521. js_defglobal(state, "alert", JS_DONTENUM);
  2522. js_newcfunction(state, event_call, "Event", 1);
  2523. js_defglobal(state, "Event", JS_DONTENUM);
  2524. js_newcfunction(state, custom_event_call, "CustomEvent", 1);
  2525. js_defglobal(state, "CustomEvent", JS_DONTENUM);
  2526. js_newcfunction(state, mouse_event_call, "MouseEvent", 1);
  2527. js_defglobal(state, "MouseEvent", JS_DONTENUM);
  2528. push_location(state);
  2529. js_defglobal(state, "location", JS_DONTENUM);
  2530. js_newobject(state);
  2531. js_pushstring(state, "Mozilla/5.0 (X11; Linux i686) NebOS/0.2");
  2532. js_setproperty(state, -2, "userAgent");
  2533. js_pushstring(state, "NebOS");
  2534. js_setproperty(state, -2, "platform");
  2535. js_pushstring(state, "en-US");
  2536. js_setproperty(state, -2, "language");
  2537. js_defglobal(state, "navigator", JS_DONTENUM);
  2538. js_newobject(state);
  2539. js_pushnumber(state, 1);
  2540. js_setproperty(state, -2, "length");
  2541. def_func(state, "pushState", win_noop, 3);
  2542. def_func(state, "replaceState", win_noop, 3);
  2543. def_func(state, "back", win_noop, 0);
  2544. def_func(state, "forward", win_noop, 0);
  2545. def_func(state, "go", win_noop, 1);
  2546. js_defglobal(state, "history", JS_DONTENUM);
  2547. def_global_func(state, "matchMedia", win_matchmedia, 1);
  2548. def_global_func(state, "getComputedStyle", win_getcomputedstyle, 1);
  2549. def_global_func(state, "addEventListener", win_addlistener, 2);
  2550. def_global_func(state, "removeEventListener", win_removelistener, 2);
  2551. def_global_func(state, "scrollTo", win_noop, 2);
  2552. def_global_func(state, "scrollBy", win_noop, 2);
  2553. def_global_func(state, "setTimeout", win_settimeout, 1);
  2554. def_global_func(state, "setInterval", win_setinterval, 1);
  2555. def_global_func(state, "clearTimeout", timer_clear, 1);
  2556. def_global_func(state, "clearInterval", timer_clear, 1);
  2557. def_global_func(state, "requestAnimationFrame", win_requestanimationframe, 1);
  2558. def_global_func(state, "cancelAnimationFrame", timer_clear, 1);
  2559. js_newobject(state);
  2560. def_func(state, "now", win_performance_now, 0);
  2561. js_newobject(state);
  2562. js_setproperty(state, -2, "timing");
  2563. js_defglobal(state, "performance", JS_DONTENUM);
  2564. js_pushnumber(state, host.width ? (int)host.width : 1024);
  2565. js_defglobal(state, "innerWidth", JS_DONTENUM);
  2566. js_pushnumber(state, host.height ? (int)host.height : 768);
  2567. js_defglobal(state, "innerHeight", JS_DONTENUM);
  2568. js_pushnumber(state, 0);
  2569. js_defglobal(state, "pageXOffset", JS_DONTENUM);
  2570. js_pushnumber(state, 0);
  2571. js_defglobal(state, "pageYOffset", JS_DONTENUM);
  2572. js_pushnumber(state, 0);
  2573. js_defglobal(state, "scrollX", JS_DONTENUM);
  2574. js_pushnumber(state, 0);
  2575. js_defglobal(state, "scrollY", JS_DONTENUM);
  2576. js_pushnumber(state, 1);
  2577. js_defglobal(state, "devicePixelRatio", JS_DONTENUM);
  2578. js_pushglobal(state);
  2579. js_defglobal(state, "window", JS_DONTENUM);
  2580. js_pushglobal(state);
  2581. js_defglobal(state, "globalThis", JS_DONTENUM);
  2582. js_getglobal(state, "document");
  2583. js_getglobal(state, "location");
  2584. js_setproperty(state, -2, "location");
  2585. js_pop(state, 1);
  2586. js_getglobal(state, "document");
  2587. js_pushglobal(state);
  2588. js_setproperty(state, -2, "defaultView");
  2589. js_pop(state, 1);
  2590. }
  2591. int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
  2592. {
  2593. if (!doc || !h)
  2594. return -1;
  2595. D = doc;
  2596. host = *h;
  2597. current_url = h->page_url;
  2598. panic_armed = 0;
  2599. memset(timers, 0, sizeof(timers));
  2600. memset(listeners, 0, sizeof(listeners));
  2601. memset(events, 0, sizeof(events));
  2602. memset(event_templates, 0, sizeof(event_templates));
  2603. /* Classic scripts are sloppy by default in browsers; strict mode is
  2604. * opt-in via a "use strict" directive. */
  2605. J = js_newstate(0, 0, 0);
  2606. if (!J)
  2607. return -1;
  2608. js_setreport(J, report_cb);
  2609. js_atpanic(J, panic_cb);
  2610. install_globals(J);
  2611. return 0;
  2612. }
  2613. void js_browser_stop(void)
  2614. {
  2615. if (J)
  2616. js_freestate(J);
  2617. J = 0;
  2618. D = 0;
  2619. current_url = 0;
  2620. panic_armed = 0;
  2621. memset(timers, 0, sizeof(timers));
  2622. }
  2623. int js_browser_active(void)
  2624. {
  2625. return J != 0;
  2626. }
  2627. static void report_top(js_State *state)
  2628. {
  2629. const char *msg;
  2630. js_getproperty(state, -1, "stack");
  2631. if (js_isstring(state, -1)) {
  2632. msg = js_tostring(state, -1);
  2633. if (host.report)
  2634. host.report(msg);
  2635. js_pop(state, 1);
  2636. } else {
  2637. js_pop(state, 1);
  2638. msg = js_trystring(state, -1, "JavaScript error");
  2639. if (host.report)
  2640. host.report(msg);
  2641. }
  2642. js_pop(state, 1);
  2643. }
  2644. void js_browser_tick(void)
  2645. {
  2646. unsigned int now;
  2647. int i, ran = 0;
  2648. if (!J)
  2649. return;
  2650. now = u_ticks_ms();
  2651. for (i = 0; i < JS_TIMERS; i++) {
  2652. if (!timers[i].used ||
  2653. (int)(now - timers[i].deadline) < 0)
  2654. continue;
  2655. js_setlimit(J, JS_RUN_LIMIT, 0);
  2656. js_getregistry(J, timers[i].ref);
  2657. if (!js_iscallable(J, -1)) {
  2658. js_pop(J, 1);
  2659. js_unref(J, timers[i].ref);
  2660. timers[i].used = 0;
  2661. continue;
  2662. }
  2663. js_pushundefined(J);
  2664. if (js_pcall(J, 0))
  2665. report_top(J);
  2666. else
  2667. js_pop(J, 1);
  2668. if (timers[i].used) {
  2669. if (timers[i].repeat)
  2670. timers[i].deadline = now + timers[i].interval;
  2671. else {
  2672. js_unref(J, timers[i].ref);
  2673. timers[i].used = 0;
  2674. }
  2675. }
  2676. if (++ran >= 4)
  2677. break;
  2678. }
  2679. }
  2680. int js_browser_run(const char *source, const char *url)
  2681. {
  2682. int r;
  2683. if (!J || !D)
  2684. return -1;
  2685. js_setlimit(J, JS_RUN_LIMIT, 0);
  2686. panic_armed = 1;
  2687. if (setjmp(panic_env)) {
  2688. panic_armed = 0;
  2689. if (host.report)
  2690. host.report("uncaught JavaScript exception");
  2691. /* The try/exception stack is unusable after a panic. Leak the
  2692. * state; the page heap reset on navigation reclaims it. */
  2693. J = 0;
  2694. D = 0;
  2695. return -1;
  2696. }
  2697. if (js_ploadstring(J, url ? url : "[inline]", source)) {
  2698. report_top(J);
  2699. panic_armed = 0;
  2700. return -1;
  2701. }
  2702. js_pushundefined(J);
  2703. r = js_pcall(J, 0);
  2704. if (r) {
  2705. report_top(J);
  2706. panic_armed = 0;
  2707. return -1;
  2708. }
  2709. js_pop(J, 1);
  2710. panic_armed = 0;
  2711. return 0;
  2712. }