js_browser.c 76 KB

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