js_browser.c 74 KB

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