js_browser.c 70 KB

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