js_browser.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200
  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_cb(js_State *state, const char *message)
  33. {
  34. (void)state;
  35. if (host.report)
  36. host.report(message);
  37. }
  38. static void panic_cb(js_State *state)
  39. {
  40. (void)state;
  41. if (panic_armed)
  42. longjmp(panic_env, 1);
  43. if (host.report)
  44. host.report("uncaught JavaScript exception");
  45. }
  46. static unsigned int node_of(void *p)
  47. {
  48. return (unsigned int)(uintptr_t)p - 1;
  49. }
  50. static int node_has(js_State *state, void *p, const char *name);
  51. static int node_put(js_State *state, void *p, const char *name);
  52. static int node_delete(js_State *state, void *p, const char *name);
  53. static void push_element_url(js_State *state, unsigned int node,
  54. const char *name);
  55. /* One wrapper per DOM node: the same element must have the same object
  56. * identity on every access, or library caches (jQuery expando data, Sizzle)
  57. * silently split into disconnected copies. */
  58. static void push_node(js_State *state, unsigned int handle)
  59. {
  60. char key[24];
  61. if (!handle) {
  62. js_pushnull(state);
  63. return;
  64. }
  65. snprintf(key, sizeof(key), "jsonnode_%u", handle);
  66. js_getregistry(state, key);
  67. if (!js_isundefined(state, -1))
  68. return;
  69. js_pop(state, 1);
  70. /* js_newuserdatax() pops a prototype from the top. */
  71. js_pushnull(state);
  72. js_newuserdatax(state, "node", (void *)(uintptr_t)handle,
  73. node_has, node_put, node_delete, 0);
  74. js_copy(state, -1);
  75. js_setregistry(state, key);
  76. }
  77. static unsigned int arg_node(js_State *state, int idx)
  78. {
  79. if (!js_isuserdata(state, idx, "node"))
  80. js_typeerror(state, "not a DOM node");
  81. return (unsigned int)(uintptr_t)js_touserdata(state, idx, "node");
  82. }
  83. static void def_func_data(js_State *state, const char *name,
  84. js_CFunction fn, int length, unsigned int handle)
  85. {
  86. js_newcfunctionx(state, fn, name, length,
  87. (void *)(uintptr_t)handle, 0);
  88. js_setproperty(state, -2, name);
  89. }
  90. static void def_func(js_State *state, const char *name,
  91. js_CFunction fn, int length)
  92. {
  93. js_newcfunction(state, fn, name, length);
  94. js_setproperty(state, -2, name);
  95. }
  96. static void def_global_func(js_State *state, const char *name,
  97. js_CFunction fn, int length)
  98. {
  99. js_newcfunction(state, fn, name, length);
  100. js_defglobal(state, name, JS_DONTENUM);
  101. }
  102. static void push_func_data(js_State *state, const char *name,
  103. js_CFunction fn, int length, unsigned int handle)
  104. {
  105. js_newcfunctionx(state, fn, name, length,
  106. (void *)(uintptr_t)handle, 0);
  107. }
  108. static const char *node_attr(unsigned int node, const char *name)
  109. {
  110. return br_find_attr(D, node, name);
  111. }
  112. static void push_attr(js_State *state, unsigned int node, const char *name,
  113. int null_if_missing)
  114. {
  115. const char *s = node_attr(node, name);
  116. if (s)
  117. js_pushstring(state, s);
  118. else if (null_if_missing)
  119. js_pushnull(state);
  120. else
  121. js_pushstring(state, "");
  122. }
  123. static unsigned int node_name(unsigned int node, char *buf, unsigned int cap)
  124. {
  125. struct br_node *n;
  126. const char *s;
  127. unsigned int i = 0;
  128. if (!D || node >= D->nnode || cap == 0)
  129. return 0;
  130. n = &D->nodes[node];
  131. if (n->type == BR_NODE_TEXT)
  132. s = "#text";
  133. else if (n->type == BR_NODE_COMMENT)
  134. s = "#comment";
  135. else if (n->type == BR_NODE_FRAGMENT)
  136. s = "#document-fragment";
  137. else if (n->tag == BR_TAG_UNKNOWN && n->name_off)
  138. s = D->attrs + n->name_off;
  139. else
  140. s = br_tag_name(n->tag);
  141. while (s && *s && i + 1 < cap) {
  142. char c = *s++;
  143. if (c >= 'a' && c <= 'z')
  144. c = (char)(c - 'a' + 'A');
  145. buf[i++] = c;
  146. }
  147. buf[i] = '\0';
  148. return i;
  149. }
  150. static unsigned int node_type_value(unsigned int node)
  151. {
  152. struct br_node *n;
  153. if (!D || node >= D->nnode)
  154. return 0;
  155. n = &D->nodes[node];
  156. if (n->type == BR_NODE_TEXT)
  157. return 3;
  158. if (n->type == BR_NODE_COMMENT)
  159. return 8;
  160. if (n->type == BR_NODE_FRAGMENT)
  161. return 11;
  162. return 1;
  163. }
  164. static void push_child_list(js_State *state, unsigned int node, int elements)
  165. {
  166. struct br_node *n;
  167. unsigned int c, i = 0;
  168. js_newarray(state);
  169. if (!D || node >= D->nnode)
  170. return;
  171. n = &D->nodes[node];
  172. for (c = n->first; c; c = D->nodes[c].next) {
  173. if (elements && D->nodes[c].type != BR_NODE_ELEM)
  174. continue;
  175. push_node(state, c + 1);
  176. js_setindex(state, -2, (int)i++);
  177. }
  178. }
  179. static unsigned int node_prev_sibling(unsigned int node)
  180. {
  181. unsigned int p, c, prev = 0;
  182. if (!D || node >= D->nnode)
  183. return 0;
  184. p = D->nodes[node].parent;
  185. for (c = D->nodes[p].first; c && c != node; c = D->nodes[c].next)
  186. prev = c;
  187. return prev;
  188. }
  189. /* ---- inline style and dataset objects ---- */
  190. static int ci_eq_n(const char *a, const char *b, unsigned int n)
  191. {
  192. unsigned int i;
  193. for (i = 0; i < n; i++) {
  194. char ca = a[i], cb = b[i];
  195. if (ca >= 'A' && ca <= 'Z')
  196. ca = (char)(ca - 'A' + 'a');
  197. if (cb >= 'A' && cb <= 'Z')
  198. cb = (char)(cb - 'A' + 'a');
  199. if (ca != cb)
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. /* camelCase JS property -> CSS property ("marginTop" -> "margin-top"). */
  205. static void css_name(const char *name, char *out, unsigned int cap)
  206. {
  207. unsigned int n = 0, i;
  208. if (!strcmp(name, "cssFloat")) {
  209. if (cap > 6)
  210. memcpy(out, "float", 6);
  211. else if (cap)
  212. out[0] = '\0';
  213. return;
  214. }
  215. for (i = 0; name[i] && n + 2 < cap; i++) {
  216. char c = name[i];
  217. if (c >= 'A' && c <= 'Z') {
  218. if (n == 0 || out[n - 1] != '-')
  219. out[n++] = '-';
  220. out[n++] = (char)(c - 'A' + 'a');
  221. } else {
  222. out[n++] = c;
  223. }
  224. }
  225. if (cap)
  226. out[n] = '\0';
  227. }
  228. /* camelCase dataset property -> attribute name ("fooBar" -> "data-foo-bar"). */
  229. static void data_name(const char *name, char *out, unsigned int cap)
  230. {
  231. unsigned int n = 0, i;
  232. for (i = 0; i < 5 && n + 1 < cap; i++)
  233. out[n++] = "data-"[i];
  234. for (i = 0; name[i] && n + 2 < cap; i++) {
  235. char c = name[i];
  236. if (c >= 'A' && c <= 'Z') {
  237. out[n++] = '-';
  238. c = (char)(c - 'A' + 'a');
  239. }
  240. out[n++] = c;
  241. }
  242. if (cap)
  243. out[n] = '\0';
  244. }
  245. static int style_lookup(const char *style, const char *prop, char *out,
  246. unsigned int cap)
  247. {
  248. unsigned int i = 0, nl = (unsigned int)strlen(prop);
  249. while (style && style[i]) {
  250. unsigned int ns, ne, vs, ve, a, b, k;
  251. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  252. i++;
  253. if (!style[i])
  254. break;
  255. ns = i;
  256. while (style[i] && style[i] != ':' && style[i] != ';')
  257. i++;
  258. ne = i;
  259. if (style[i] != ':') {
  260. if (style[i] == ';')
  261. i++;
  262. continue;
  263. }
  264. i++;
  265. while (style[i] == ' ' || style[i] == '\t')
  266. i++;
  267. vs = i;
  268. while (style[i] && style[i] != ';')
  269. i++;
  270. ve = i;
  271. a = ns;
  272. b = ne;
  273. while (a < b && (style[a] == ' ' || style[a] == '\t'))
  274. a++;
  275. while (b > a && (style[b - 1] == ' ' || style[b - 1] == '\t'))
  276. b--;
  277. while (ve > vs && (style[ve - 1] == ' ' || style[ve - 1] == '\t'))
  278. ve--;
  279. if (b - a == nl && ci_eq_n(style + a, prop, nl)) {
  280. for (k = 0; k < ve - vs && k + 1 < cap; k++)
  281. out[k] = style[vs + k];
  282. out[k < cap ? k : cap - 1] = '\0';
  283. return 1;
  284. }
  285. }
  286. return 0;
  287. }
  288. static unsigned int style_count(const char *style)
  289. {
  290. unsigned int i = 0, n = 0;
  291. while (style && style[i]) {
  292. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  293. i++;
  294. if (!style[i])
  295. break;
  296. while (style[i] && style[i] != ';')
  297. i++;
  298. n++;
  299. }
  300. return n;
  301. }
  302. static unsigned int style_span(const char *style, unsigned int i)
  303. {
  304. while (style[i] == ' ' || style[i] == ';' || style[i] == '\t')
  305. i++;
  306. return i;
  307. }
  308. static void style_update(unsigned int node, const char *prop,
  309. const char *value, int remove)
  310. {
  311. const char *old = br_find_attr(D, node, "style");
  312. char arr[512];
  313. unsigned int n = 0, i = 0, nl = (unsigned int)strlen(prop);
  314. int replaced = 0;
  315. if (remove && !old)
  316. return;
  317. while (old && old[i] && n + 2 < sizeof(arr)) {
  318. unsigned int start = style_span(old, i);
  319. unsigned int ns, ne, ve, a, b, k;
  320. i = start;
  321. if (!old[i])
  322. break;
  323. ns = i;
  324. while (old[i] && old[i] != ':' && old[i] != ';')
  325. i++;
  326. ne = i;
  327. if (old[i] == ':') {
  328. i++;
  329. while (old[i] == ' ' || old[i] == '\t')
  330. i++;
  331. while (old[i] && old[i] != ';')
  332. i++;
  333. }
  334. ve = i;
  335. a = ns;
  336. b = ne;
  337. while (a < b && (old[a] == ' ' || old[a] == '\t'))
  338. a++;
  339. while (b > a && (old[b - 1] == ' ' || old[b - 1] == '\t'))
  340. b--;
  341. if (b - a == nl && ci_eq_n(old + a, prop, nl)) {
  342. replaced = 1;
  343. if (remove || !value || !value[0])
  344. continue;
  345. }
  346. for (k = ns; k < ve && n + 1 < sizeof(arr); k++)
  347. arr[n++] = old[k];
  348. if (n + 1 < sizeof(arr))
  349. arr[n++] = ';';
  350. }
  351. if (replaced && (remove || !value || !value[0])) {
  352. arr[n] = '\0';
  353. br_set_attr(D, node, "style", arr, n);
  354. if (host.changed)
  355. host.changed();
  356. return;
  357. }
  358. if (value && value[0] && n + nl + strlen(value) + 3 < sizeof(arr)) {
  359. for (i = 0; i < nl; i++)
  360. arr[n++] = prop[i];
  361. arr[n++] = ':';
  362. for (i = 0; value[i]; i++)
  363. arr[n++] = value[i];
  364. arr[n++] = ';';
  365. }
  366. arr[n] = '\0';
  367. br_set_attr(D, node, "style", arr, n);
  368. if (host.changed)
  369. host.changed();
  370. }
  371. static void style_getproperty(js_State *state)
  372. {
  373. unsigned int node = node_of(js_currentfunctiondata(state));
  374. char val[256];
  375. if (style_lookup(br_find_attr(D, node, "style"),
  376. js_tostring(state, 1), val, sizeof(val)))
  377. js_pushstring(state, val);
  378. else
  379. js_pushstring(state, "");
  380. }
  381. static void style_setproperty(js_State *state)
  382. {
  383. unsigned int node = node_of(js_currentfunctiondata(state));
  384. style_update(node, js_tostring(state, 1),
  385. js_tostring(state, 2), 0);
  386. }
  387. static void style_removeproperty(js_State *state)
  388. {
  389. unsigned int node = node_of(js_currentfunctiondata(state));
  390. const char *prop = js_tostring(state, 1);
  391. char old[256];
  392. int had = style_lookup(br_find_attr(D, node, "style"), prop,
  393. old, sizeof(old));
  394. style_update(node, prop, "", 1);
  395. if (had)
  396. js_pushstring(state, old);
  397. else
  398. js_pushstring(state, "");
  399. }
  400. static int style_has(js_State *state, void *p, const char *name)
  401. {
  402. unsigned int node = node_of(p);
  403. const char *style;
  404. char prop[64], val[256];
  405. if (!D || node >= D->nnode)
  406. return 0;
  407. style = br_find_attr(D, node, "style");
  408. if (!strcmp(name, "cssText")) {
  409. js_pushstring(state, style ? style : "");
  410. return 1;
  411. }
  412. if (!strcmp(name, "length")) {
  413. js_pushnumber(state, style_count(style));
  414. return 1;
  415. }
  416. if (!strcmp(name, "getPropertyValue")) {
  417. push_func_data(state, name, style_getproperty, 1, node + 1);
  418. return 1;
  419. }
  420. if (!strcmp(name, "setProperty")) {
  421. push_func_data(state, name, style_setproperty, 2, node + 1);
  422. return 1;
  423. }
  424. if (!strcmp(name, "removeProperty")) {
  425. push_func_data(state, name, style_removeproperty, 1, node + 1);
  426. return 1;
  427. }
  428. css_name(name, prop, sizeof(prop));
  429. if (style_lookup(style, prop, val, sizeof(val)))
  430. js_pushstring(state, val);
  431. else
  432. js_pushstring(state, "");
  433. return 1;
  434. }
  435. static int style_put(js_State *state, void *p, const char *name)
  436. {
  437. unsigned int node = node_of(p);
  438. const char *value;
  439. char prop[64];
  440. if (!D || node >= D->nnode)
  441. return 0;
  442. value = js_tostring(state, -1);
  443. if (!strcmp(name, "cssText")) {
  444. br_set_attr(D, node, "style", value, (unsigned int)strlen(value));
  445. if (host.changed)
  446. host.changed();
  447. return 1;
  448. }
  449. css_name(name, prop, sizeof(prop));
  450. style_update(node, prop, value, 0);
  451. return 1;
  452. }
  453. static void push_style_object(js_State *state, unsigned int node)
  454. {
  455. char key[32];
  456. snprintf(key, sizeof(key), "sty_%u", node + 1);
  457. js_getregistry(state, key);
  458. if (!js_isundefined(state, -1))
  459. return;
  460. js_pop(state, 1);
  461. js_pushnull(state);
  462. js_newuserdatax(state, "style", (void *)(uintptr_t)(node + 1),
  463. style_has, style_put, 0, 0);
  464. js_copy(state, -1);
  465. js_setregistry(state, key);
  466. }
  467. static int dataset_has(js_State *state, void *p, const char *name)
  468. {
  469. unsigned int node = node_of(p);
  470. char attr[64];
  471. const char *value;
  472. if (!D || node >= D->nnode)
  473. return 0;
  474. data_name(name, attr, sizeof(attr));
  475. value = br_find_attr(D, node, attr);
  476. if (value)
  477. js_pushstring(state, value);
  478. else
  479. js_pushundefined(state);
  480. return 1;
  481. }
  482. static int dataset_put(js_State *state, void *p, const char *name)
  483. {
  484. unsigned int node = node_of(p);
  485. char attr[64];
  486. const char *value = js_tostring(state, -1);
  487. if (!D || node >= D->nnode)
  488. return 0;
  489. data_name(name, attr, sizeof(attr));
  490. br_set_attr(D, node, attr, value, (unsigned int)strlen(value));
  491. if (host.changed)
  492. host.changed();
  493. return 1;
  494. }
  495. static int dataset_delete(js_State *state, void *p, const char *name)
  496. {
  497. unsigned int node = node_of(p);
  498. char attr[64];
  499. (void)state;
  500. if (!D || node >= D->nnode)
  501. return 0;
  502. data_name(name, attr, sizeof(attr));
  503. br_remove_attr(D, node, attr);
  504. if (host.changed)
  505. host.changed();
  506. return 1;
  507. }
  508. static void push_dataset(js_State *state, unsigned int node)
  509. {
  510. char key[32];
  511. snprintf(key, sizeof(key), "ds_%u", node + 1);
  512. js_getregistry(state, key);
  513. if (!js_isundefined(state, -1))
  514. return;
  515. js_pop(state, 1);
  516. js_pushnull(state);
  517. js_newuserdatax(state, "dataset", (void *)(uintptr_t)(node + 1),
  518. dataset_has, dataset_put, dataset_delete, 0);
  519. js_copy(state, -1);
  520. js_setregistry(state, key);
  521. }
  522. static void node_class_set(unsigned int node, const char *classes)
  523. {
  524. br_set_attr(D, node, "class", classes,
  525. (unsigned int)strlen(classes));
  526. if (host.changed)
  527. host.changed();
  528. }
  529. static int class_has(const char *classes, const char *name)
  530. {
  531. unsigned int i = 0;
  532. if (!classes)
  533. return 0;
  534. while (classes[i]) {
  535. unsigned int start;
  536. while (classes[i] == ' ' || classes[i] == '\t')
  537. i++;
  538. start = i;
  539. while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
  540. i++;
  541. if (i - start == strlen(name) &&
  542. !strncmp(classes + start, name, i - start))
  543. return 1;
  544. }
  545. return 0;
  546. }
  547. static void class_add(unsigned int node, const char *name, int add)
  548. {
  549. const char *old = node_attr(node, "class");
  550. char buf[256];
  551. unsigned int n = 0, i = 0;
  552. if (add) {
  553. if (class_has(old, name))
  554. return;
  555. if (old && old[0]) {
  556. while (old[i] && n + 1 < sizeof(buf))
  557. buf[n++] = old[i++];
  558. if (n + 1 < sizeof(buf))
  559. buf[n++] = ' ';
  560. }
  561. i = 0;
  562. while (name[i] && n + 1 < sizeof(buf))
  563. buf[n++] = name[i++];
  564. } else {
  565. if (!old)
  566. return;
  567. while (old[i]) {
  568. unsigned int start;
  569. while (old[i] == ' ' || old[i] == '\t')
  570. i++;
  571. start = i;
  572. while (old[i] && old[i] != ' ' && old[i] != '\t')
  573. i++;
  574. if (i - start == strlen(name) &&
  575. !strncmp(old + start, name, i - start))
  576. continue;
  577. if (n && n + 1 < sizeof(buf))
  578. buf[n++] = ' ';
  579. {
  580. unsigned int k;
  581. for (k = start; k < i && n + 1 < sizeof(buf);
  582. k++)
  583. buf[n++] = old[k];
  584. }
  585. }
  586. }
  587. buf[n] = '\0';
  588. node_class_set(node, buf);
  589. }
  590. static void node_classlist_add(js_State *state)
  591. {
  592. class_add(node_of(js_currentfunctiondata(state)),
  593. js_tostring(state, 1), 1);
  594. }
  595. static void node_classlist_remove(js_State *state)
  596. {
  597. class_add(node_of(js_currentfunctiondata(state)),
  598. js_tostring(state, 1), 0);
  599. }
  600. static void node_classlist_toggle(js_State *state)
  601. {
  602. unsigned int node = node_of(js_currentfunctiondata(state));
  603. const char *name = js_tostring(state, 1);
  604. int has = class_has(node_attr(node, "class"), name);
  605. int want = !has;
  606. if (js_isdefined(state, 2))
  607. want = js_toboolean(state, 2);
  608. if (want != has)
  609. class_add(node, name, want);
  610. js_pushboolean(state, want);
  611. }
  612. static void node_classlist_contains(js_State *state)
  613. {
  614. unsigned int node = node_of(js_currentfunctiondata(state));
  615. js_pushboolean(state, class_has(node_attr(node, "class"),
  616. js_tostring(state, 1)));
  617. }
  618. static unsigned int class_count(const char *classes)
  619. {
  620. unsigned int i = 0, n = 0;
  621. if (!classes)
  622. return 0;
  623. while (classes[i]) {
  624. while (classes[i] == ' ' || classes[i] == '\t')
  625. i++;
  626. if (!classes[i])
  627. break;
  628. n++;
  629. while (classes[i] && classes[i] != ' ' && classes[i] != '\t')
  630. i++;
  631. }
  632. return n;
  633. }
  634. static void push_attributes(js_State *state, unsigned int node)
  635. {
  636. struct br_node *n;
  637. const char *p, *end;
  638. unsigned int count = 0;
  639. js_newobject(state);
  640. if (!D || node >= D->nnode)
  641. return;
  642. n = &D->nodes[node];
  643. p = D->attrs + n->extra_off;
  644. end = p + n->extra_len;
  645. while (p + 1 < end) {
  646. const char *an = p;
  647. const char *av;
  648. p += strlen(p) + 1;
  649. if (p >= end)
  650. break;
  651. av = p;
  652. p += strlen(p) + 1;
  653. js_newobject(state);
  654. js_pushstring(state, an);
  655. js_setproperty(state, -2, "name");
  656. js_pushstring(state, av);
  657. js_setproperty(state, -2, "value");
  658. js_pushboolean(state, 1);
  659. js_setproperty(state, -2, "specified");
  660. /* Array-like: attributes[i] and attributes[name] both work. */
  661. js_copy(state, -1);
  662. js_setproperty(state, -3, an);
  663. js_setindex(state, -2, (int)count);
  664. count++;
  665. }
  666. js_pushnumber(state, count);
  667. js_setproperty(state, -2, "length");
  668. }
  669. static void push_classlist(js_State *state, unsigned int node)
  670. {
  671. js_newobject(state);
  672. def_func_data(state, "add", node_classlist_add, 1, node + 1);
  673. def_func_data(state, "remove", node_classlist_remove, 1, node + 1);
  674. def_func_data(state, "toggle", node_classlist_toggle, 1, node + 1);
  675. def_func_data(state, "contains", node_classlist_contains, 1, node + 1);
  676. js_pushnumber(state, class_count(node_attr(node, "class")));
  677. js_setproperty(state, -2, "length");
  678. }
  679. static void node_getattr(js_State *state)
  680. {
  681. unsigned int node = node_of(js_currentfunctiondata(state));
  682. push_attr(state, node, js_tostring(state, 1), 1);
  683. }
  684. static void node_setattr(js_State *state)
  685. {
  686. unsigned int node = node_of(js_currentfunctiondata(state));
  687. const char *name = js_tostring(state, 1);
  688. const char *value = js_tostring(state, 2);
  689. char lower[64];
  690. unsigned int i;
  691. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  692. char c = name[i];
  693. if (c >= 'A' && c <= 'Z')
  694. c = (char)(c - 'A' + 'a');
  695. lower[i] = c;
  696. }
  697. lower[i] = '\0';
  698. br_set_attr(D, node, lower, value, (unsigned int)strlen(value));
  699. if (host.changed)
  700. host.changed();
  701. }
  702. static void node_removeattr(js_State *state)
  703. {
  704. unsigned int node = node_of(js_currentfunctiondata(state));
  705. const char *name = js_tostring(state, 1);
  706. char lower[64];
  707. unsigned int i;
  708. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  709. char c = name[i];
  710. if (c >= 'A' && c <= 'Z')
  711. c = (char)(c - 'A' + 'a');
  712. lower[i] = c;
  713. }
  714. lower[i] = '\0';
  715. br_remove_attr(D, node, lower);
  716. if (host.changed)
  717. host.changed();
  718. }
  719. static void node_hasattr(js_State *state)
  720. {
  721. unsigned int node = node_of(js_currentfunctiondata(state));
  722. js_pushboolean(state, node_attr(node, js_tostring(state, 1)) != 0);
  723. }
  724. static void node_appendchild(js_State *state)
  725. {
  726. unsigned int node = node_of(js_currentfunctiondata(state));
  727. unsigned int child = arg_node(state, 1);
  728. if (br_append_child(D, node, child) != 0) {
  729. js_pushnull(state);
  730. return;
  731. }
  732. if (host.changed)
  733. host.changed();
  734. push_node(state, child);
  735. }
  736. static void node_insertbefore(js_State *state)
  737. {
  738. unsigned int node = node_of(js_currentfunctiondata(state));
  739. unsigned int child = arg_node(state, 1);
  740. unsigned int before = 0;
  741. if (!js_isnull(state, 2) && !js_isundefined(state, 2))
  742. before = arg_node(state, 2);
  743. if (br_insert_before(D, node, child, before) != 0) {
  744. js_pushnull(state);
  745. return;
  746. }
  747. if (host.changed)
  748. host.changed();
  749. push_node(state, child);
  750. }
  751. static void node_removechild(js_State *state)
  752. {
  753. unsigned int node = node_of(js_currentfunctiondata(state));
  754. unsigned int child = arg_node(state, 1);
  755. if (br_remove_child(D, node, child) != 0) {
  756. js_pushnull(state);
  757. return;
  758. }
  759. if (host.changed)
  760. host.changed();
  761. push_node(state, child);
  762. }
  763. static void node_replacechild(js_State *state)
  764. {
  765. unsigned int node = node_of(js_currentfunctiondata(state));
  766. unsigned int child = arg_node(state, 1);
  767. unsigned int old = arg_node(state, 2);
  768. br_insert_before(D, node, child, old);
  769. br_remove_child(D, node, old);
  770. if (host.changed)
  771. host.changed();
  772. push_node(state, old);
  773. }
  774. static void node_clone(js_State *state)
  775. {
  776. unsigned int node = node_of(js_currentfunctiondata(state));
  777. unsigned int h = br_clone_node(D, node + 1,
  778. js_toboolean(state, 1));
  779. push_node(state, h);
  780. }
  781. static void node_contains(js_State *state)
  782. {
  783. unsigned int node = node_of(js_currentfunctiondata(state));
  784. unsigned int other = arg_node(state, 1);
  785. unsigned int p = other;
  786. while (1) {
  787. if (p == node) {
  788. js_pushboolean(state, 1);
  789. return;
  790. }
  791. if (p == 0)
  792. break;
  793. p = D->nodes[p].parent;
  794. }
  795. js_pushboolean(state, 0);
  796. }
  797. static void node_haschildren(js_State *state)
  798. {
  799. unsigned int node = node_of(js_currentfunctiondata(state));
  800. js_pushboolean(state, node < D->nnode && D->nodes[node].first != 0);
  801. }
  802. static void node_remove(js_State *state)
  803. {
  804. unsigned int node = node_of(js_currentfunctiondata(state));
  805. unsigned int parent = node < D->nnode ? D->nodes[node].parent : 0;
  806. (void)state;
  807. if (parent)
  808. br_remove_child(D, parent, node + 1);
  809. if (host.changed)
  810. host.changed();
  811. }
  812. static void node_matches(js_State *state)
  813. {
  814. unsigned int node = node_of(js_currentfunctiondata(state));
  815. js_pushboolean(state, br_matches(D, node + 1,
  816. js_tostring(state, 1)));
  817. }
  818. static void node_getelementsbytagname(js_State *state)
  819. {
  820. unsigned int node = node_of(js_currentfunctiondata(state));
  821. const char *name = js_tostring(state, 1);
  822. unsigned int out[256];
  823. unsigned int n = br_query_all(D, node + 1, name, out, 256);
  824. js_newarray(state);
  825. for (unsigned int i = 0; i < n; i++) {
  826. push_node(state, out[i]);
  827. js_setindex(state, -2, (int)i);
  828. }
  829. }
  830. static void node_getelementsbyclassname(js_State *state)
  831. {
  832. unsigned int node = node_of(js_currentfunctiondata(state));
  833. const char *name = js_tostring(state, 1);
  834. char sel[256];
  835. unsigned int n = 0, i = 0;
  836. unsigned int out[256], count;
  837. while (name[i] && n + 2 < sizeof(sel)) {
  838. while (name[i] == ' ')
  839. i++;
  840. if (!name[i])
  841. break;
  842. sel[n++] = '.';
  843. while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
  844. sel[n++] = name[i++];
  845. }
  846. sel[n] = '\0';
  847. count = br_query_all(D, node + 1, sel, out, 256);
  848. js_newarray(state);
  849. for (i = 0; i < count; i++) {
  850. push_node(state, out[i]);
  851. js_setindex(state, -2, (int)i);
  852. }
  853. }
  854. static void node_queryselector(js_State *state)
  855. {
  856. unsigned int node = node_of(js_currentfunctiondata(state));
  857. unsigned int out[1];
  858. unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
  859. out, 1);
  860. if (n)
  861. push_node(state, out[0]);
  862. else
  863. js_pushnull(state);
  864. }
  865. static void node_queryselectorall(js_State *state)
  866. {
  867. unsigned int node = node_of(js_currentfunctiondata(state));
  868. unsigned int out[256];
  869. unsigned int n = br_query_all(D, node + 1, js_tostring(state, 1),
  870. out, 256);
  871. js_newarray(state);
  872. for (unsigned int i = 0; i < n; i++) {
  873. push_node(state, out[i]);
  874. js_setindex(state, -2, (int)i);
  875. }
  876. }
  877. static void node_getrect(js_State *state)
  878. {
  879. unsigned int node = node_of(js_currentfunctiondata(state));
  880. int x = 0, y = 0, w = 0, h = 0;
  881. if (host.rect)
  882. host.rect(node, &x, &y, &w, &h);
  883. js_newobject(state);
  884. js_pushnumber(state, y); js_setproperty(state, -2, "top");
  885. js_pushnumber(state, x); js_setproperty(state, -2, "left");
  886. js_pushnumber(state, x + w); js_setproperty(state, -2, "right");
  887. js_pushnumber(state, y + h); js_setproperty(state, -2, "bottom");
  888. js_pushnumber(state, w); js_setproperty(state, -2, "width");
  889. js_pushnumber(state, h); js_setproperty(state, -2, "height");
  890. }
  891. static void node_noop(js_State *state)
  892. {
  893. (void)state;
  894. }
  895. static void node_push_text(js_State *state, unsigned int node)
  896. {
  897. unsigned int cap = D->text_used + 1;
  898. char *buf;
  899. unsigned int len;
  900. if (cap < 32)
  901. cap = 32;
  902. buf = (char *)malloc(cap);
  903. if (!buf) {
  904. js_pushstring(state, "");
  905. return;
  906. }
  907. len = br_text_content(D, node, buf, cap);
  908. js_pushlstring(state, buf, (int)len);
  909. free(buf);
  910. }
  911. /* Userdata property hooks: every element wrapper carries node + 1. */
  912. static int node_has(js_State *state, void *p, const char *name)
  913. {
  914. unsigned int node = node_of(p);
  915. struct br_node *n;
  916. char buf[64];
  917. if (!D || node >= D->nnode)
  918. return 0;
  919. n = &D->nodes[node];
  920. if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
  921. node_push_text(state, node);
  922. return 1;
  923. }
  924. if (!strcmp(name, "nodeValue")) {
  925. if (n->type == BR_NODE_TEXT || n->type == BR_NODE_COMMENT)
  926. js_pushlstring(state, D->text + n->text_off,
  927. (int)n->text_len);
  928. else
  929. js_pushnull(state);
  930. return 1;
  931. }
  932. if (!strcmp(name, "nodeType")) {
  933. js_pushnumber(state, node_type_value(node));
  934. return 1;
  935. }
  936. if (!strcmp(name, "nodeName") || !strcmp(name, "tagName")) {
  937. node_name(node, buf, sizeof(buf));
  938. js_pushstring(state, buf);
  939. return 1;
  940. }
  941. if (!strcmp(name, "parentNode") || !strcmp(name, "parentElement")) {
  942. unsigned int p = n->parent;
  943. push_node(state, p ? p + 1 : 0);
  944. return 1;
  945. }
  946. if (!strcmp(name, "childNodes")) {
  947. push_child_list(state, node, 0);
  948. return 1;
  949. }
  950. if (!strcmp(name, "children")) {
  951. push_child_list(state, node, 1);
  952. return 1;
  953. }
  954. if (!strcmp(name, "firstChild")) {
  955. push_node(state, n->first ? n->first + 1 : 0);
  956. return 1;
  957. }
  958. if (!strcmp(name, "lastChild")) {
  959. push_node(state, n->last ? n->last + 1 : 0);
  960. return 1;
  961. }
  962. if (!strcmp(name, "firstElementChild") ||
  963. !strcmp(name, "lastElementChild")) {
  964. unsigned int c = n->first;
  965. unsigned int found = 0;
  966. if (!strcmp(name, "lastElementChild")) {
  967. for (c = n->first; c; c = D->nodes[c].next)
  968. if (D->nodes[c].type == BR_NODE_ELEM)
  969. found = c;
  970. } else {
  971. while (c && D->nodes[c].type != BR_NODE_ELEM)
  972. c = D->nodes[c].next;
  973. found = c;
  974. }
  975. push_node(state, found ? found + 1 : 0);
  976. return 1;
  977. }
  978. if (!strcmp(name, "nextSibling") || !strcmp(name, "nextElementSibling")) {
  979. unsigned int c = n->next;
  980. if (!strcmp(name, "nextElementSibling"))
  981. while (c && D->nodes[c].type != BR_NODE_ELEM)
  982. c = D->nodes[c].next;
  983. push_node(state, c ? c + 1 : 0);
  984. return 1;
  985. }
  986. if (!strcmp(name, "previousSibling") ||
  987. !strcmp(name, "previousElementSibling")) {
  988. unsigned int c = node_prev_sibling(node);
  989. if (!strcmp(name, "previousElementSibling"))
  990. while (c && D->nodes[c].type != BR_NODE_ELEM)
  991. c = node_prev_sibling(c);
  992. push_node(state, c ? c + 1 : 0);
  993. return 1;
  994. }
  995. if (!strcmp(name, "childElementCount")) {
  996. unsigned int c, count = 0;
  997. for (c = n->first; c; c = D->nodes[c].next)
  998. if (D->nodes[c].type == BR_NODE_ELEM)
  999. count++;
  1000. js_pushnumber(state, count);
  1001. return 1;
  1002. }
  1003. if (n->tag == BR_TAG_A || n->tag == BR_TAG_AREA) {
  1004. if (!strcmp(name, "href") || !strcmp(name, "protocol") ||
  1005. !strcmp(name, "host") || !strcmp(name, "hostname") ||
  1006. !strcmp(name, "port") || !strcmp(name, "pathname") ||
  1007. !strcmp(name, "search") || !strcmp(name, "hash") ||
  1008. !strcmp(name, "origin")) {
  1009. push_element_url(state, node, name);
  1010. return 1;
  1011. }
  1012. }
  1013. if (!strcmp(name, "id")) { push_attr(state, node, "id", 0); return 1; }
  1014. if (!strcmp(name, "className")) {
  1015. push_attr(state, node, "class", 0);
  1016. return 1;
  1017. }
  1018. if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
  1019. if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
  1020. if (!strcmp(name, "value")) {
  1021. char buf[128];
  1022. if (host.form_value && host.form_value(node, buf, sizeof(buf)))
  1023. js_pushstring(state, buf);
  1024. else
  1025. push_attr(state, node, "value", 0);
  1026. return 1;
  1027. }
  1028. if (!strcmp(name, "type")) { push_attr(state, node, "type", 0); return 1; }
  1029. if (!strcmp(name, "name")) { push_attr(state, node, "name", 0); return 1; }
  1030. if (!strcmp(name, "alt")) { push_attr(state, node, "alt", 0); return 1; }
  1031. if (!strcmp(name, "title")) { push_attr(state, node, "title", 0); return 1; }
  1032. if (!strcmp(name, "checked")) {
  1033. int c = host.form_checked ? host.form_checked(node) : -1;
  1034. js_pushboolean(state, c >= 0 ? c : node_attr(node, name) != 0);
  1035. return 1;
  1036. }
  1037. if (!strcmp(name, "selectedIndex")) {
  1038. int c = host.form_selected ? host.form_selected(node) : -1;
  1039. js_pushnumber(state, c >= 0 ? c : 0);
  1040. return 1;
  1041. }
  1042. if (!strcmp(name, "selected")) {
  1043. int c = host.form_selected ? host.form_selected(node) : -1;
  1044. if (c >= 0)
  1045. js_pushboolean(state, c != 0);
  1046. else
  1047. js_pushboolean(state, node_attr(node, name) != 0);
  1048. return 1;
  1049. }
  1050. if (!strcmp(name, "disabled")) {
  1051. js_pushboolean(state, node_attr(node, name) != 0);
  1052. return 1;
  1053. }
  1054. if (!strcmp(name, "style")) {
  1055. push_style_object(state, node);
  1056. return 1;
  1057. }
  1058. if (!strcmp(name, "dataset") || !strcmp(name, "domStringMap")) {
  1059. push_dataset(state, node);
  1060. return 1;
  1061. }
  1062. if (!strcmp(name, "classList")) {
  1063. push_classlist(state, node);
  1064. return 1;
  1065. }
  1066. if (!strcmp(name, "attributes")) {
  1067. push_attributes(state, node);
  1068. return 1;
  1069. }
  1070. if (!strcmp(name, "innerHTML") || !strcmp(name, "outerHTML")) {
  1071. char key[32];
  1072. snprintf(key, sizeof(key), "html_%u", node + 1);
  1073. js_getregistry(state, key);
  1074. if (js_isundefined(state, -1)) {
  1075. js_pop(state, 1);
  1076. js_pushstring(state, "");
  1077. }
  1078. return 1;
  1079. }
  1080. if (!strcmp(name, "ownerDocument")) {
  1081. js_getglobal(state, "document");
  1082. return 1;
  1083. }
  1084. if (!strcmp(name, "getAttribute"))
  1085. { push_func_data(state, "getAttribute", node_getattr, 1, node + 1); return 1; }
  1086. if (!strcmp(name, "setAttribute"))
  1087. { push_func_data(state, "setAttribute", node_setattr, 2, node + 1); return 1; }
  1088. if (!strcmp(name, "removeAttribute"))
  1089. { push_func_data(state, "removeAttribute", node_removeattr, 1, node + 1); return 1; }
  1090. if (!strcmp(name, "hasAttribute"))
  1091. { push_func_data(state, "hasAttribute", node_hasattr, 1, node + 1); return 1; }
  1092. if (!strcmp(name, "appendChild"))
  1093. { push_func_data(state, "appendChild", node_appendchild, 1, node + 1); return 1; }
  1094. if (!strcmp(name, "insertBefore"))
  1095. { push_func_data(state, "insertBefore", node_insertbefore, 2, node + 1); return 1; }
  1096. if (!strcmp(name, "removeChild"))
  1097. { push_func_data(state, "removeChild", node_removechild, 1, node + 1); return 1; }
  1098. if (!strcmp(name, "replaceChild"))
  1099. { push_func_data(state, "replaceChild", node_replacechild, 2, node + 1); return 1; }
  1100. if (!strcmp(name, "cloneNode"))
  1101. { push_func_data(state, "cloneNode", node_clone, 1, node + 1); return 1; }
  1102. if (!strcmp(name, "contains"))
  1103. { push_func_data(state, "contains", node_contains, 1, node + 1); return 1; }
  1104. if (!strcmp(name, "hasChildNodes"))
  1105. { push_func_data(state, "hasChildNodes", node_haschildren, 0, node + 1); return 1; }
  1106. if (!strcmp(name, "remove"))
  1107. { push_func_data(state, "remove", node_remove, 0, node + 1); return 1; }
  1108. if (!strcmp(name, "matches") || !strcmp(name, "webkitMatchesSelector") ||
  1109. !strcmp(name, "mozMatchesSelector") ||
  1110. !strcmp(name, "msMatchesSelector"))
  1111. { push_func_data(state, name, node_matches, 1, node + 1); return 1; }
  1112. if (!strcmp(name, "getElementsByTagName"))
  1113. { push_func_data(state, "getElementsByTagName", node_getelementsbytagname, 1, node + 1); return 1; }
  1114. if (!strcmp(name, "getElementsByClassName"))
  1115. { push_func_data(state, "getElementsByClassName", node_getelementsbyclassname, 1, node + 1); return 1; }
  1116. if (!strcmp(name, "querySelector"))
  1117. { push_func_data(state, "querySelector", node_queryselector, 1, node + 1); return 1; }
  1118. if (!strcmp(name, "querySelectorAll"))
  1119. { push_func_data(state, "querySelectorAll", node_queryselectorall, 1, node + 1); return 1; }
  1120. if (!strcmp(name, "addEventListener"))
  1121. { push_func_data(state, "addEventListener", node_noop, 2, node + 1); return 1; }
  1122. if (!strcmp(name, "removeEventListener"))
  1123. { push_func_data(state, "removeEventListener", node_noop, 2, node + 1); return 1; }
  1124. if (!strcmp(name, "dispatchEvent"))
  1125. { push_func_data(state, "dispatchEvent", node_noop, 1, node + 1); return 1; }
  1126. if (!strcmp(name, "getBoundingClientRect"))
  1127. { push_func_data(state, "getBoundingClientRect", node_getrect, 0, node + 1); return 1; }
  1128. if (!strcmp(name, "getClientRects")) {
  1129. js_newarray(state);
  1130. return 1;
  1131. }
  1132. if (!strcmp(name, "offsetWidth") || !strcmp(name, "offsetHeight") ||
  1133. !strcmp(name, "clientWidth") || !strcmp(name, "clientHeight") ||
  1134. !strcmp(name, "scrollWidth") || !strcmp(name, "scrollHeight")) {
  1135. int x = 0, y = 0, w = 0, h = 0;
  1136. if (host.rect)
  1137. host.rect(node, &x, &y, &w, &h);
  1138. js_pushnumber(state, (!strcmp(name, "offsetHeight") ||
  1139. !strcmp(name, "clientHeight") ||
  1140. !strcmp(name, "scrollHeight")) ? h : w);
  1141. return 1;
  1142. }
  1143. if (!strcmp(name, "scrollTop") || !strcmp(name, "scrollLeft")) {
  1144. js_pushnumber(state, 0);
  1145. return 1;
  1146. }
  1147. if (!strcmp(name, "click") || !strcmp(name, "focus") ||
  1148. !strcmp(name, "blur")) {
  1149. push_func_data(state, name, node_noop, 0, node + 1);
  1150. return 1;
  1151. }
  1152. return 0;
  1153. }
  1154. static void node_set_string(unsigned int node, const char *name,
  1155. const char *value)
  1156. {
  1157. br_set_attr(D, node, name, value, (unsigned int)strlen(value));
  1158. if (host.changed)
  1159. host.changed();
  1160. }
  1161. static int node_put(js_State *state, void *p, const char *name)
  1162. {
  1163. unsigned int node = node_of(p);
  1164. if (!D || node >= D->nnode)
  1165. return 0;
  1166. if (!strcmp(name, "textContent") || !strcmp(name, "innerText")) {
  1167. const char *text = js_tostring(state, -1);
  1168. unsigned int len = (unsigned int)strlen(text);
  1169. if (br_set_text_content(D, node, text, len) != 0) {
  1170. if (host.status)
  1171. host.status("Page text arena is full", 1);
  1172. } else if (host.changed) {
  1173. host.changed();
  1174. }
  1175. return 1;
  1176. }
  1177. if (!strcmp(name, "nodeValue")) {
  1178. const char *text = js_tostring(state, -1);
  1179. if (br_set_text_node(D, node, text,
  1180. (unsigned int)strlen(text)) == 0 &&
  1181. host.changed)
  1182. host.changed();
  1183. return 1;
  1184. }
  1185. if (!strcmp(name, "id") || !strcmp(name, "className") ||
  1186. !strcmp(name, "href") || !strcmp(name, "src") ||
  1187. !strcmp(name, "value") || !strcmp(name, "type") ||
  1188. !strcmp(name, "name") || !strcmp(name, "alt") ||
  1189. !strcmp(name, "title")) {
  1190. const char *value = js_tostring(state, -1);
  1191. if (!strcmp(name, "value") && host.form_set_value &&
  1192. host.form_set_value(node, value))
  1193. return 1;
  1194. if (!strcmp(name, "className"))
  1195. node_set_string(node, "class", value);
  1196. else
  1197. node_set_string(node, name, value);
  1198. return 1;
  1199. }
  1200. if (!strcmp(name, "selectedIndex")) {
  1201. if (host.form_set_selected)
  1202. host.form_set_selected(node, js_tointeger(state, -1));
  1203. return 1;
  1204. }
  1205. if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
  1206. !strcmp(name, "selected")) {
  1207. int b = js_toboolean(state, -1);
  1208. if (!strcmp(name, "checked") && host.form_set_checked &&
  1209. host.form_set_checked(node, b))
  1210. return 1;
  1211. if (!strcmp(name, "selected") && host.form_set_selected &&
  1212. host.form_set_selected(node, b ? 1 : 0))
  1213. return 1;
  1214. if (b)
  1215. br_set_attr(D, node, name, "", 0);
  1216. else
  1217. br_remove_attr(D, node, name);
  1218. if (host.changed)
  1219. host.changed();
  1220. return 1;
  1221. }
  1222. if (!strcmp(name, "innerHTML")) {
  1223. const char *html = js_tostring(state, -1);
  1224. char key[32];
  1225. br_set_inner_html(D, node, html, (unsigned int)strlen(html));
  1226. snprintf(key, sizeof(key), "html_%u", node + 1);
  1227. js_pushstring(state, html);
  1228. js_setregistry(state, key);
  1229. if (host.changed)
  1230. host.changed();
  1231. return 1;
  1232. }
  1233. if (!strcmp(name, "outerHTML")) {
  1234. char key[32];
  1235. snprintf(key, sizeof(key), "html_%u", node + 1);
  1236. js_pushstring(state, js_tostring(state, -1));
  1237. js_setregistry(state, key);
  1238. return 1;
  1239. }
  1240. if (!strcmp(name, "style")) {
  1241. if (js_isstring(state, -1))
  1242. node_set_string(node, "style",
  1243. js_tostring(state, -1));
  1244. return 1;
  1245. }
  1246. return 0;
  1247. }
  1248. static int node_delete(js_State *state, void *p, const char *name)
  1249. {
  1250. (void)state;
  1251. (void)p;
  1252. (void)name;
  1253. return 0;
  1254. }
  1255. static void doc_getelementbyid(js_State *state)
  1256. {
  1257. const char *id = js_tostring(state, 1);
  1258. push_node(state, br_find_id(D, id));
  1259. }
  1260. static void console_log(js_State *state)
  1261. {
  1262. int i;
  1263. int n = js_gettop(state);
  1264. for (i = 1; i < n; i++) {
  1265. if (i > 1)
  1266. u_putc(' ');
  1267. u_puts(js_tostring(state, i));
  1268. }
  1269. u_putc('\n');
  1270. }
  1271. static void js_alert(js_State *state)
  1272. {
  1273. char msg[160];
  1274. unsigned int used = 0;
  1275. int i;
  1276. int n = js_gettop(state);
  1277. for (i = 1; i < n; i++) {
  1278. const char *s = js_tostring(state, i);
  1279. unsigned int k;
  1280. if (i > 1 && used + 1 < sizeof(msg))
  1281. msg[used++] = ' ';
  1282. for (k = 0; s[k] && used + 1 < sizeof(msg); k++)
  1283. msg[used++] = s[k];
  1284. }
  1285. msg[used] = '\0';
  1286. if (host.status)
  1287. host.status(msg, 0);
  1288. }
  1289. static void push_query_array(js_State *state, unsigned int root,
  1290. const char *selector)
  1291. {
  1292. unsigned int out[256];
  1293. unsigned int n = br_query_all(D, root, selector, out, 256);
  1294. unsigned int i;
  1295. js_newarray(state);
  1296. for (i = 0; i < n; i++) {
  1297. push_node(state, out[i]);
  1298. js_setindex(state, -2, (int)i);
  1299. }
  1300. }
  1301. static void doc_create_element(js_State *state)
  1302. {
  1303. const char *name = js_tostring(state, 1);
  1304. char lower[32];
  1305. unsigned int i;
  1306. for (i = 0; name[i] && i + 1 < sizeof(lower); i++) {
  1307. char c = name[i];
  1308. if (c >= 'A' && c <= 'Z')
  1309. c = (char)(c - 'A' + 'a');
  1310. lower[i] = c;
  1311. }
  1312. lower[i] = '\0';
  1313. push_node(state, br_create_element(D, lower, i));
  1314. }
  1315. static void doc_create_text(js_State *state)
  1316. {
  1317. const char *text = js_tostring(state, 1);
  1318. push_node(state, br_create_text(D, text, (unsigned int)strlen(text)));
  1319. }
  1320. static void doc_create_comment(js_State *state)
  1321. {
  1322. const char *text = js_tostring(state, 1);
  1323. push_node(state, br_create_comment(D, text, (unsigned int)strlen(text)));
  1324. }
  1325. static void doc_create_fragment(js_State *state)
  1326. {
  1327. push_node(state, br_create_fragment(D));
  1328. }
  1329. static void doc_create_attribute(js_State *state)
  1330. {
  1331. const char *name = js_tostring(state, 1);
  1332. js_newobject(state);
  1333. js_pushstring(state, name);
  1334. js_setproperty(state, -2, "name");
  1335. js_pushstring(state, "");
  1336. js_setproperty(state, -2, "value");
  1337. js_pushboolean(state, 1);
  1338. js_setproperty(state, -2, "specified");
  1339. }
  1340. static void doc_getelementsbytagname(js_State *state)
  1341. {
  1342. push_query_array(state, 0, js_tostring(state, 1));
  1343. }
  1344. static void doc_getelementsbyclassname(js_State *state)
  1345. {
  1346. const char *name = js_tostring(state, 1);
  1347. char sel[256];
  1348. unsigned int n = 0, i = 0;
  1349. while (name[i] && n + 2 < sizeof(sel)) {
  1350. while (name[i] == ' ')
  1351. i++;
  1352. if (!name[i])
  1353. break;
  1354. sel[n++] = '.';
  1355. while (name[i] && name[i] != ' ' && n + 1 < sizeof(sel))
  1356. sel[n++] = name[i++];
  1357. }
  1358. sel[n] = '\0';
  1359. push_query_array(state, 0, sel);
  1360. }
  1361. static void doc_queryselector(js_State *state)
  1362. {
  1363. unsigned int out[1];
  1364. unsigned int n = br_query_all(D, 0, js_tostring(state, 1), out, 1);
  1365. if (n)
  1366. push_node(state, out[0]);
  1367. else
  1368. js_pushnull(state);
  1369. }
  1370. static void doc_queryselectorall(js_State *state)
  1371. {
  1372. push_query_array(state, 0, js_tostring(state, 1));
  1373. }
  1374. static void doc_noop(js_State *state)
  1375. {
  1376. (void)state;
  1377. }
  1378. static void location_get(js_State *state)
  1379. {
  1380. js_pushstring(state, current_url ? current_url : "");
  1381. }
  1382. static void location_set(js_State *state)
  1383. {
  1384. const char *url = js_tostring(state, 1);
  1385. if (host.navigate)
  1386. host.navigate(url);
  1387. }
  1388. static void win_noop(js_State *state);
  1389. struct loc_parts {
  1390. const char *protocol, *host, *hostname, *port, *path, *search, *hash;
  1391. const char *origin;
  1392. unsigned int protocol_len, host_len, hostname_len, port_len;
  1393. unsigned int path_len, search_len, hash_len, origin_len;
  1394. };
  1395. static void loc_split(const char *u, struct loc_parts *p)
  1396. {
  1397. unsigned int n = (unsigned int)strlen(u), i;
  1398. const char *rest;
  1399. memset(p, 0, sizeof(*p));
  1400. p->path = u;
  1401. p->path_len = n;
  1402. p->origin = "null";
  1403. p->origin_len = 4;
  1404. for (i = 0; i < n; i++)
  1405. if (u[i] == ':')
  1406. break;
  1407. if (i + 2 < n && u[i] == ':' && u[i + 1] == '/' && u[i + 2] == '/') {
  1408. const char *hosts = u + i + 3, *hoste = hosts;
  1409. while (*hoste && *hoste != '/' && *hoste != '?' && *hoste != '#')
  1410. hoste++;
  1411. p->protocol = u;
  1412. p->protocol_len = i + 1;
  1413. p->host = hosts;
  1414. p->host_len = (unsigned int)(hoste - hosts);
  1415. p->hostname = hosts;
  1416. p->hostname_len = p->host_len;
  1417. for (i = 0; i < p->hostname_len; i++)
  1418. if (hosts[i] == ':') {
  1419. p->port = hosts + i + 1;
  1420. p->port_len = p->hostname_len - i - 1;
  1421. p->hostname_len = i;
  1422. break;
  1423. }
  1424. p->origin = u;
  1425. p->origin_len = (unsigned int)(hoste - u);
  1426. rest = hoste;
  1427. } else {
  1428. p->protocol = "file:";
  1429. p->protocol_len = 5;
  1430. rest = u;
  1431. }
  1432. {
  1433. const char *q = rest;
  1434. while (*q && *q != '?' && *q != '#')
  1435. q++;
  1436. p->path = rest;
  1437. p->path_len = (unsigned int)(q - rest);
  1438. if (*q == '?') {
  1439. const char *s = q;
  1440. while (*q && *q != '#')
  1441. q++;
  1442. p->search = s;
  1443. p->search_len = (unsigned int)(q - s);
  1444. }
  1445. if (*q == '#') {
  1446. p->hash = q;
  1447. p->hash_len = (unsigned int)strlen(q);
  1448. }
  1449. }
  1450. }
  1451. static void push_location(js_State *state)
  1452. {
  1453. struct loc_parts p;
  1454. loc_split(current_url ? current_url : "", &p);
  1455. js_newobject(state);
  1456. js_newcfunction(state, location_get, "get href", 0);
  1457. js_newcfunction(state, location_set, "set href", 1);
  1458. js_defaccessor(state, -3, "href", 0);
  1459. #define LOC_FIELD(field) \
  1460. do { \
  1461. js_pushlstring(state, p.field ? p.field : "", \
  1462. p.field ? (int)p.field##_len : 0); \
  1463. js_setproperty(state, -2, #field); \
  1464. } while (0)
  1465. LOC_FIELD(protocol);
  1466. LOC_FIELD(host);
  1467. LOC_FIELD(hostname);
  1468. LOC_FIELD(port);
  1469. js_pushlstring(state, p.path ? p.path : "",
  1470. p.path ? (int)p.path_len : 0);
  1471. js_setproperty(state, -2, "pathname");
  1472. LOC_FIELD(search);
  1473. LOC_FIELD(hash);
  1474. LOC_FIELD(origin);
  1475. #undef LOC_FIELD
  1476. js_newcfunction(state, win_noop, "reload", 0);
  1477. js_setproperty(state, -2, "reload");
  1478. js_newcfunction(state, win_noop, "assign", 1);
  1479. js_setproperty(state, -2, "assign");
  1480. js_newcfunction(state, win_noop, "replace", 1);
  1481. js_setproperty(state, -2, "replace");
  1482. js_newcfunction(state, win_noop, "toString", 0);
  1483. js_setproperty(state, -2, "toString");
  1484. }
  1485. /* ---- URL resolution for <a>/<area> URL properties ---- */
  1486. static int url_has_scheme(const char *s)
  1487. {
  1488. unsigned int i = 1;
  1489. if (!((s[0] >= 'a' && s[0] <= 'z') ||
  1490. (s[0] >= 'A' && s[0] <= 'Z')))
  1491. return 0;
  1492. while (s[i]) {
  1493. char c = s[i];
  1494. if (c == ':')
  1495. return 1;
  1496. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  1497. (c >= '0' && c <= '9') || c == '+' || c == '-' ||
  1498. c == '.'))
  1499. return 0;
  1500. i++;
  1501. }
  1502. return 0;
  1503. }
  1504. static void url_put(char *out, unsigned int cap, unsigned int *n,
  1505. const char *s, unsigned int len)
  1506. {
  1507. unsigned int i;
  1508. for (i = 0; i < len && *n + 1 < cap; i++)
  1509. out[(*n)++] = s[i];
  1510. }
  1511. /* Collapse //, /./ and /../ inside the path part of an absolute URL. */
  1512. static void url_normalize(char *url)
  1513. {
  1514. char tmp[512];
  1515. char *path;
  1516. unsigned int n = 0, i = 0;
  1517. path = strstr(url, "://");
  1518. if (path) {
  1519. path += 3;
  1520. while (*path && *path != '/')
  1521. path++;
  1522. } else {
  1523. path = url;
  1524. }
  1525. if (path[0] != '/')
  1526. return;
  1527. while (path[i]) {
  1528. unsigned int start;
  1529. while (path[i] == '/')
  1530. i++;
  1531. start = i;
  1532. while (path[i] && path[i] != '/')
  1533. i++;
  1534. if (i == start)
  1535. continue;
  1536. if (i - start == 1 && path[start] == '.')
  1537. continue;
  1538. if (i - start == 2 && path[start] == '.' &&
  1539. path[start + 1] == '.') {
  1540. while (n > 0 && tmp[n - 1] != '/')
  1541. n--;
  1542. if (n > 0)
  1543. n--;
  1544. continue;
  1545. }
  1546. if (n + 1 < sizeof(tmp))
  1547. tmp[n++] = '/';
  1548. while (start < i && n + 1 < sizeof(tmp))
  1549. tmp[n++] = path[start++];
  1550. }
  1551. if (n == 0)
  1552. tmp[n++] = '/';
  1553. tmp[n] = '\0';
  1554. memcpy(path, tmp, n + 1);
  1555. }
  1556. static void url_resolve(const char *base, const char *rel,
  1557. char *out, unsigned int cap)
  1558. {
  1559. struct loc_parts bp;
  1560. unsigned int n = 0, i;
  1561. if (cap == 0)
  1562. return;
  1563. if (!rel[0]) {
  1564. url_put(out, cap, &n, base, (unsigned int)strlen(base));
  1565. out[n] = '\0';
  1566. return;
  1567. }
  1568. if (url_has_scheme(rel)) {
  1569. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1570. out[n] = '\0';
  1571. url_normalize(out);
  1572. return;
  1573. }
  1574. loc_split(base, &bp);
  1575. if (rel[0] == '/' && rel[1] == '/') {
  1576. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1577. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1578. } else if (rel[0] == '/') {
  1579. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1580. url_put(out, cap, &n, "//", 2);
  1581. url_put(out, cap, &n, bp.host, bp.host_len);
  1582. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1583. } else if (rel[0] == '#' || rel[0] == '?') {
  1584. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1585. if (bp.host_len || (bp.protocol_len == 5 &&
  1586. bp.protocol[0] == 'f')) {
  1587. url_put(out, cap, &n, "//", 2);
  1588. url_put(out, cap, &n, bp.host, bp.host_len);
  1589. }
  1590. url_put(out, cap, &n, bp.path, bp.path_len);
  1591. if (rel[0] == '#')
  1592. url_put(out, cap, &n, bp.search, bp.search_len);
  1593. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1594. } else {
  1595. const char *dir_end = bp.path;
  1596. for (i = 0; i < bp.path_len; i++)
  1597. if (bp.path[i] == '/')
  1598. dir_end = bp.path + i + 1;
  1599. if (dir_end == bp.path)
  1600. dir_end = bp.path + bp.path_len;
  1601. url_put(out, cap, &n, bp.protocol, bp.protocol_len);
  1602. if (bp.host_len || (bp.protocol_len == 5 &&
  1603. bp.protocol[0] == 'f')) {
  1604. url_put(out, cap, &n, "//", 2);
  1605. url_put(out, cap, &n, bp.host, bp.host_len);
  1606. }
  1607. url_put(out, cap, &n, bp.path,
  1608. (unsigned int)(dir_end - bp.path));
  1609. url_put(out, cap, &n, rel, (unsigned int)strlen(rel));
  1610. }
  1611. out[n] = '\0';
  1612. url_normalize(out);
  1613. }
  1614. /* <a> URL properties are resolved against the page URL, like the DOM. */
  1615. static void push_element_url(js_State *state, unsigned int node,
  1616. const char *name)
  1617. {
  1618. char abs_url[512];
  1619. const char *href = br_find_attr(D, node, "href");
  1620. struct loc_parts p;
  1621. if (!href || !href[0]) {
  1622. js_pushstring(state, "");
  1623. return;
  1624. }
  1625. url_resolve(current_url ? current_url : "/", href,
  1626. abs_url, sizeof(abs_url));
  1627. loc_split(abs_url, &p);
  1628. if (!strcmp(name, "href"))
  1629. js_pushstring(state, abs_url);
  1630. else if (!strcmp(name, "protocol"))
  1631. js_pushlstring(state, p.protocol, (int)p.protocol_len);
  1632. else if (!strcmp(name, "origin"))
  1633. js_pushlstring(state, p.origin ? p.origin : "",
  1634. (int)p.origin_len);
  1635. else if (!strcmp(name, "host"))
  1636. js_pushlstring(state, p.host ? p.host : "", (int)p.host_len);
  1637. else if (!strcmp(name, "hostname"))
  1638. js_pushlstring(state, p.hostname ? p.hostname : "",
  1639. (int)p.hostname_len);
  1640. else if (!strcmp(name, "port"))
  1641. js_pushlstring(state, p.port ? p.port : "", (int)p.port_len);
  1642. else if (!strcmp(name, "pathname"))
  1643. js_pushlstring(state, p.path ? p.path : "", (int)p.path_len);
  1644. else if (!strcmp(name, "search"))
  1645. js_pushlstring(state, p.search ? p.search : "",
  1646. (int)p.search_len);
  1647. else
  1648. js_pushlstring(state, p.hash ? p.hash : "", (int)p.hash_len);
  1649. }
  1650. static void win_noop(js_State *state)
  1651. {
  1652. (void)state;
  1653. }
  1654. static void timer_add(js_State *state, int repeat)
  1655. {
  1656. int i;
  1657. unsigned int ms;
  1658. if (!js_iscallable(state, 1)) {
  1659. js_pushnumber(state, 0);
  1660. return;
  1661. }
  1662. ms = js_isdefined(state, 2) ?
  1663. (unsigned int)js_tointeger(state, 2) : 0;
  1664. if (repeat && ms == 0)
  1665. ms = 1;
  1666. for (i = 0; i < JS_TIMERS; i++)
  1667. if (!timers[i].used)
  1668. break;
  1669. if (i == JS_TIMERS) {
  1670. js_pushnumber(state, 0);
  1671. return;
  1672. }
  1673. js_copy(state, 1);
  1674. snprintf(timers[i].ref, sizeof(timers[i].ref), "%s",
  1675. js_ref(state));
  1676. timers[i].used = 1;
  1677. timers[i].repeat = repeat;
  1678. timers[i].interval = ms;
  1679. timers[i].deadline = u_ticks_ms() + ms;
  1680. js_pushnumber(state, i + 1);
  1681. }
  1682. static void win_settimeout(js_State *state)
  1683. {
  1684. timer_add(state, 0);
  1685. }
  1686. static void win_setinterval(js_State *state)
  1687. {
  1688. timer_add(state, 1);
  1689. }
  1690. static void timer_clear(js_State *state)
  1691. {
  1692. int id = js_tointeger(state, 1);
  1693. if (id >= 1 && id <= JS_TIMERS && timers[id - 1].used) {
  1694. js_unref(state, timers[id - 1].ref);
  1695. timers[id - 1].used = 0;
  1696. }
  1697. }
  1698. static void win_requestanimationframe(js_State *state)
  1699. {
  1700. timer_add(state, 0);
  1701. }
  1702. static void win_performance_now(js_State *state)
  1703. {
  1704. js_pushnumber(state, (double)u_ticks_ms());
  1705. }
  1706. static void win_getproperty(js_State *state)
  1707. {
  1708. (void)state;
  1709. js_pushstring(state, "");
  1710. }
  1711. static void win_getcomputedstyle(js_State *state)
  1712. {
  1713. (void)state;
  1714. js_newobject(state);
  1715. def_func(state, "getPropertyValue", win_getproperty, 1);
  1716. js_pushstring(state, "");
  1717. js_setproperty(state, -2, "cssText");
  1718. }
  1719. static void win_matchmedia(js_State *state)
  1720. {
  1721. const char *query = js_tostring(state, 1);
  1722. js_newobject(state);
  1723. js_pushboolean(state, 0);
  1724. js_setproperty(state, -2, "matches");
  1725. js_pushstring(state, query);
  1726. js_setproperty(state, -2, "media");
  1727. def_func(state, "addListener", win_noop, 1);
  1728. def_func(state, "removeListener", win_noop, 1);
  1729. def_func(state, "addEventListener", win_noop, 2);
  1730. def_func(state, "removeEventListener", win_noop, 2);
  1731. }
  1732. static void install_globals(js_State *state)
  1733. {
  1734. unsigned int h;
  1735. js_newobject(state);
  1736. def_func(state, "getElementById", doc_getelementbyid, 1);
  1737. def_func(state, "createElement", doc_create_element, 1);
  1738. def_func(state, "createTextNode", doc_create_text, 1);
  1739. def_func(state, "createComment", doc_create_comment, 1);
  1740. def_func(state, "createDocumentFragment", doc_create_fragment, 0);
  1741. def_func(state, "createAttribute", doc_create_attribute, 1);
  1742. def_func(state, "getElementsByTagName", doc_getelementsbytagname, 1);
  1743. def_func(state, "getElementsByClassName", doc_getelementsbyclassname, 1);
  1744. def_func(state, "querySelector", doc_queryselector, 1);
  1745. def_func(state, "querySelectorAll", doc_queryselectorall, 1);
  1746. def_func(state, "addEventListener", doc_noop, 2);
  1747. def_func(state, "removeEventListener", doc_noop, 2);
  1748. h = br_find_tag(D, BR_TAG_HTML);
  1749. if (!h && D->nnode > 1)
  1750. h = D->nodes[0].first ? D->nodes[0].first + 1 : 0;
  1751. push_node(state, h);
  1752. js_setproperty(state, -2, "documentElement");
  1753. h = br_find_tag(D, BR_TAG_BODY);
  1754. push_node(state, h);
  1755. js_setproperty(state, -2, "body");
  1756. h = br_find_tag(D, BR_TAG_HEAD);
  1757. push_node(state, h);
  1758. js_setproperty(state, -2, "head");
  1759. js_pushnumber(state, 9);
  1760. js_setproperty(state, -2, "nodeType");
  1761. js_pushstring(state, "#document");
  1762. js_setproperty(state, -2, "nodeName");
  1763. js_pushnull(state);
  1764. js_setproperty(state, -2, "ownerDocument");
  1765. js_pushstring(state, "CSS1Compat");
  1766. js_setproperty(state, -2, "compatMode");
  1767. js_pushstring(state, D->title);
  1768. js_setproperty(state, -2, "title");
  1769. js_pushstring(state, "complete");
  1770. js_setproperty(state, -2, "readyState");
  1771. js_pushstring(state, "");
  1772. js_setproperty(state, -2, "cookie");
  1773. js_pushstring(state, current_url ? current_url : "");
  1774. js_setproperty(state, -2, "URL");
  1775. js_defglobal(state, "document", JS_DONTENUM);
  1776. js_newobject(state);
  1777. js_newcfunction(state, console_log, "log", 0);
  1778. js_setproperty(state, -2, "log");
  1779. js_defglobal(state, "console", JS_DONTENUM);
  1780. js_newcfunction(state, js_alert, "alert", 1);
  1781. js_defglobal(state, "alert", JS_DONTENUM);
  1782. push_location(state);
  1783. js_defglobal(state, "location", JS_DONTENUM);
  1784. js_newobject(state);
  1785. js_pushstring(state, "Mozilla/5.0 (X11; Linux i686) NebOS/0.2");
  1786. js_setproperty(state, -2, "userAgent");
  1787. js_pushstring(state, "NebOS");
  1788. js_setproperty(state, -2, "platform");
  1789. js_pushstring(state, "en-US");
  1790. js_setproperty(state, -2, "language");
  1791. js_defglobal(state, "navigator", JS_DONTENUM);
  1792. js_newobject(state);
  1793. js_pushnumber(state, 1);
  1794. js_setproperty(state, -2, "length");
  1795. def_func(state, "pushState", win_noop, 3);
  1796. def_func(state, "replaceState", win_noop, 3);
  1797. def_func(state, "back", win_noop, 0);
  1798. def_func(state, "forward", win_noop, 0);
  1799. def_func(state, "go", win_noop, 1);
  1800. js_defglobal(state, "history", JS_DONTENUM);
  1801. def_global_func(state, "matchMedia", win_matchmedia, 1);
  1802. def_global_func(state, "getComputedStyle", win_getcomputedstyle, 1);
  1803. def_global_func(state, "addEventListener", win_noop, 2);
  1804. def_global_func(state, "removeEventListener", win_noop, 2);
  1805. def_global_func(state, "scrollTo", win_noop, 2);
  1806. def_global_func(state, "scrollBy", win_noop, 2);
  1807. def_global_func(state, "setTimeout", win_settimeout, 1);
  1808. def_global_func(state, "setInterval", win_setinterval, 1);
  1809. def_global_func(state, "clearTimeout", timer_clear, 1);
  1810. def_global_func(state, "clearInterval", timer_clear, 1);
  1811. def_global_func(state, "requestAnimationFrame", win_requestanimationframe, 1);
  1812. def_global_func(state, "cancelAnimationFrame", timer_clear, 1);
  1813. js_newobject(state);
  1814. def_func(state, "now", win_performance_now, 0);
  1815. js_newobject(state);
  1816. js_setproperty(state, -2, "timing");
  1817. js_defglobal(state, "performance", JS_DONTENUM);
  1818. js_pushnumber(state, host.width ? (int)host.width : 1024);
  1819. js_defglobal(state, "innerWidth", JS_DONTENUM);
  1820. js_pushnumber(state, host.height ? (int)host.height : 768);
  1821. js_defglobal(state, "innerHeight", JS_DONTENUM);
  1822. js_pushnumber(state, 0);
  1823. js_defglobal(state, "pageXOffset", JS_DONTENUM);
  1824. js_pushnumber(state, 0);
  1825. js_defglobal(state, "pageYOffset", JS_DONTENUM);
  1826. js_pushnumber(state, 0);
  1827. js_defglobal(state, "scrollX", JS_DONTENUM);
  1828. js_pushnumber(state, 0);
  1829. js_defglobal(state, "scrollY", JS_DONTENUM);
  1830. js_pushnumber(state, 1);
  1831. js_defglobal(state, "devicePixelRatio", JS_DONTENUM);
  1832. js_pushglobal(state);
  1833. js_defglobal(state, "window", JS_DONTENUM);
  1834. js_pushglobal(state);
  1835. js_defglobal(state, "globalThis", JS_DONTENUM);
  1836. js_getglobal(state, "document");
  1837. js_getglobal(state, "location");
  1838. js_setproperty(state, -2, "location");
  1839. js_pop(state, 1);
  1840. js_getglobal(state, "document");
  1841. js_pushglobal(state);
  1842. js_setproperty(state, -2, "defaultView");
  1843. js_pop(state, 1);
  1844. }
  1845. int js_browser_start(struct br_doc *doc, const struct js_browser_host *h)
  1846. {
  1847. if (!doc || !h)
  1848. return -1;
  1849. D = doc;
  1850. host = *h;
  1851. current_url = h->page_url;
  1852. panic_armed = 0;
  1853. memset(timers, 0, sizeof(timers));
  1854. /* Classic scripts are sloppy by default in browsers; strict mode is
  1855. * opt-in via a "use strict" directive. */
  1856. J = js_newstate(0, 0, 0);
  1857. if (!J)
  1858. return -1;
  1859. js_setreport(J, report_cb);
  1860. js_atpanic(J, panic_cb);
  1861. install_globals(J);
  1862. return 0;
  1863. }
  1864. void js_browser_stop(void)
  1865. {
  1866. if (J)
  1867. js_freestate(J);
  1868. J = 0;
  1869. D = 0;
  1870. current_url = 0;
  1871. panic_armed = 0;
  1872. memset(timers, 0, sizeof(timers));
  1873. }
  1874. int js_browser_active(void)
  1875. {
  1876. return J != 0;
  1877. }
  1878. static void report_top(js_State *state)
  1879. {
  1880. const char *msg;
  1881. js_getproperty(state, -1, "stack");
  1882. if (js_isstring(state, -1)) {
  1883. msg = js_tostring(state, -1);
  1884. if (host.report)
  1885. host.report(msg);
  1886. js_pop(state, 1);
  1887. } else {
  1888. js_pop(state, 1);
  1889. msg = js_trystring(state, -1, "JavaScript error");
  1890. if (host.report)
  1891. host.report(msg);
  1892. }
  1893. js_pop(state, 1);
  1894. }
  1895. void js_browser_tick(void)
  1896. {
  1897. unsigned int now;
  1898. int i, ran = 0;
  1899. if (!J)
  1900. return;
  1901. now = u_ticks_ms();
  1902. for (i = 0; i < JS_TIMERS; i++) {
  1903. if (!timers[i].used ||
  1904. (int)(now - timers[i].deadline) < 0)
  1905. continue;
  1906. js_setlimit(J, JS_RUN_LIMIT, 0);
  1907. js_getregistry(J, timers[i].ref);
  1908. if (!js_iscallable(J, -1)) {
  1909. js_pop(J, 1);
  1910. js_unref(J, timers[i].ref);
  1911. timers[i].used = 0;
  1912. continue;
  1913. }
  1914. js_pushundefined(J);
  1915. if (js_pcall(J, 0))
  1916. report_top(J);
  1917. else
  1918. js_pop(J, 1);
  1919. if (timers[i].used) {
  1920. if (timers[i].repeat)
  1921. timers[i].deadline = now + timers[i].interval;
  1922. else {
  1923. js_unref(J, timers[i].ref);
  1924. timers[i].used = 0;
  1925. }
  1926. }
  1927. if (++ran >= 4)
  1928. break;
  1929. }
  1930. }
  1931. int js_browser_run(const char *source, const char *url)
  1932. {
  1933. int r;
  1934. if (!J || !D)
  1935. return -1;
  1936. js_setlimit(J, JS_RUN_LIMIT, 0);
  1937. panic_armed = 1;
  1938. if (setjmp(panic_env)) {
  1939. panic_armed = 0;
  1940. if (host.report)
  1941. host.report("uncaught JavaScript exception");
  1942. /* The try/exception stack is unusable after a panic. Leak the
  1943. * state; the page heap reset on navigation reclaims it. */
  1944. J = 0;
  1945. D = 0;
  1946. return -1;
  1947. }
  1948. if (js_ploadstring(J, url ? url : "[inline]", source)) {
  1949. report_top(J);
  1950. panic_armed = 0;
  1951. return -1;
  1952. }
  1953. js_pushundefined(J);
  1954. r = js_pcall(J, 0);
  1955. if (r) {
  1956. report_top(J);
  1957. panic_armed = 0;
  1958. return -1;
  1959. }
  1960. js_pop(J, 1);
  1961. panic_armed = 0;
  1962. return 0;
  1963. }