js_browser.c 73 KB

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