interpreter.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. export function main() {
  2. let root = window.document.getElementById("main");
  3. if (root != null) {
  4. window.interpreter = new Interpreter(root);
  5. window.ipc.postMessage(serializeIpcMessage("initialize"));
  6. }
  7. }
  8. export class Interpreter {
  9. constructor(root) {
  10. this.root = root;
  11. this.stack = [root];
  12. this.listeners = {};
  13. this.handlers = {};
  14. this.lastNodeWasText = false;
  15. this.nodes = [root];
  16. }
  17. top() {
  18. return this.stack[this.stack.length - 1];
  19. }
  20. pop() {
  21. return this.stack.pop();
  22. }
  23. PushRoot(root) {
  24. const node = this.nodes[root];
  25. this.stack.push(node);
  26. }
  27. AppendChildren(many) {
  28. let root = this.stack[this.stack.length - (1 + many)];
  29. let to_add = this.stack.splice(this.stack.length - many);
  30. for (let i = 0; i < many; i++) {
  31. root.appendChild(to_add[i]);
  32. }
  33. }
  34. ReplaceWith(root_id, m) {
  35. let root = this.nodes[root_id];
  36. let els = this.stack.splice(this.stack.length - m);
  37. root.replaceWith(...els);
  38. }
  39. InsertAfter(root, n) {
  40. let old = this.nodes[root];
  41. let new_nodes = this.stack.splice(this.stack.length - n);
  42. old.after(...new_nodes);
  43. }
  44. InsertBefore(root, n) {
  45. let old = this.nodes[root];
  46. let new_nodes = this.stack.splice(this.stack.length - n);
  47. old.before(...new_nodes);
  48. }
  49. Remove(root) {
  50. let node = this.nodes[root];
  51. if (node !== undefined) {
  52. node.remove();
  53. }
  54. }
  55. CreateTextNode(text, root) {
  56. const node = document.createTextNode(text);
  57. this.nodes[root] = node;
  58. this.stack.push(node);
  59. }
  60. CreateElement(tag, root) {
  61. const el = document.createElement(tag);
  62. this.nodes[root] = el;
  63. this.stack.push(el);
  64. }
  65. CreateElementNs(tag, root, ns) {
  66. let el = document.createElementNS(ns, tag);
  67. this.stack.push(el);
  68. this.nodes[root] = el;
  69. }
  70. CreatePlaceholder(root) {
  71. let el = document.createElement("pre");
  72. el.hidden = true;
  73. this.stack.push(el);
  74. this.nodes[root] = el;
  75. }
  76. NewEventListener(event_name, root, handler) {
  77. const element = this.nodes[root];
  78. element.setAttribute("data-dioxus-id", `${root}`);
  79. if (this.listeners[event_name] === undefined) {
  80. this.listeners[event_name] = 0;
  81. this.handlers[event_name] = handler;
  82. this.root.addEventListener(event_name, handler);
  83. } else {
  84. this.listeners[event_name]++;
  85. }
  86. }
  87. RemoveEventListener(root, event_name) {
  88. const element = this.nodes[root];
  89. element.removeAttribute(`data-dioxus-id`);
  90. this.listeners[event_name]--;
  91. if (this.listeners[event_name] === 0) {
  92. this.root.removeEventListener(event_name, this.handlers[event_name]);
  93. delete this.listeners[event_name];
  94. delete this.handlers[event_name];
  95. }
  96. }
  97. SetText(root, text) {
  98. this.nodes[root].textContent = text;
  99. }
  100. SetAttribute(root, field, value, ns) {
  101. const name = field;
  102. const node = this.nodes[root];
  103. if (ns === "style") {
  104. // @ts-ignore
  105. node.style[name] = value;
  106. } else if (ns != null || ns != undefined) {
  107. node.setAttributeNS(ns, name, value);
  108. } else {
  109. switch (name) {
  110. case "value":
  111. if (value !== node.value) {
  112. node.value = value;
  113. }
  114. break;
  115. case "checked":
  116. node.checked = value === "true";
  117. break;
  118. case "selected":
  119. node.selected = value === "true";
  120. break;
  121. case "dangerous_inner_html":
  122. node.innerHTML = value;
  123. break;
  124. default:
  125. // https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
  126. if (value === "false" && bool_attrs.hasOwnProperty(name)) {
  127. node.removeAttribute(name);
  128. } else {
  129. node.setAttribute(name, value);
  130. }
  131. }
  132. }
  133. }
  134. RemoveAttribute(root, field, ns) {
  135. const name = field;
  136. const node = this.nodes[root];
  137. if (ns !== null || ns !== undefined) {
  138. node.removeAttributeNS(ns, name);
  139. } else if (name === "value") {
  140. node.value = "";
  141. } else if (name === "checked") {
  142. node.checked = false;
  143. } else if (name === "selected") {
  144. node.selected = false;
  145. } else if (name === "dangerous_inner_html") {
  146. node.innerHTML = "";
  147. } else {
  148. node.removeAttribute(name);
  149. }
  150. }
  151. handleEdits(edits) {
  152. this.stack.push(this.root);
  153. for (let edit of edits) {
  154. this.handleEdit(edit);
  155. }
  156. }
  157. handleEdit(edit) {
  158. switch (edit.type) {
  159. case "PushRoot":
  160. this.PushRoot(edit.root);
  161. break;
  162. case "AppendChildren":
  163. this.AppendChildren(edit.many);
  164. break;
  165. case "ReplaceWith":
  166. this.ReplaceWith(edit.root, edit.m);
  167. break;
  168. case "InsertAfter":
  169. this.InsertAfter(edit.root, edit.n);
  170. break;
  171. case "InsertBefore":
  172. this.InsertBefore(edit.root, edit.n);
  173. break;
  174. case "Remove":
  175. this.Remove(edit.root);
  176. break;
  177. case "CreateTextNode":
  178. this.CreateTextNode(edit.text, edit.root);
  179. break;
  180. case "CreateElement":
  181. this.CreateElement(edit.tag, edit.root);
  182. break;
  183. case "CreateElementNs":
  184. this.CreateElementNs(edit.tag, edit.root, edit.ns);
  185. break;
  186. case "CreatePlaceholder":
  187. this.CreatePlaceholder(edit.root);
  188. break;
  189. case "RemoveEventListener":
  190. this.RemoveEventListener(edit.root, edit.event_name);
  191. break;
  192. case "NewEventListener":
  193. // this handler is only provided on desktop implementations since this
  194. // method is not used by the web implementation
  195. let handler = (event) => {
  196. let target = event.target;
  197. if (target != null) {
  198. let realId = target.getAttribute(`data-dioxus-id`);
  199. let shouldPreventDefault = target.getAttribute(
  200. `dioxus-prevent-default`
  201. );
  202. if (event.type === "click") {
  203. // todo call prevent default if it's the right type of event
  204. if (shouldPreventDefault !== `onclick`) {
  205. if (target.tagName === "A") {
  206. event.preventDefault();
  207. const href = target.getAttribute("href");
  208. if (href !== "" && href !== null && href !== undefined) {
  209. window.ipc.postMessage(
  210. serializeIpcMessage("browser_open", { href })
  211. );
  212. }
  213. }
  214. }
  215. // also prevent buttons from submitting
  216. if (target.tagName === "BUTTON") {
  217. event.preventDefault();
  218. }
  219. }
  220. // walk the tree to find the real element
  221. while (realId == null) {
  222. // we've reached the root we don't want to send an event
  223. if (target.parentElement === null) {
  224. return;
  225. }
  226. target = target.parentElement;
  227. realId = target.getAttribute(`data-dioxus-id`);
  228. }
  229. shouldPreventDefault = target.getAttribute(
  230. `dioxus-prevent-default`
  231. );
  232. let contents = serialize_event(event);
  233. if (shouldPreventDefault === `on${event.type}`) {
  234. event.preventDefault();
  235. }
  236. if (event.type === "submit") {
  237. event.preventDefault();
  238. }
  239. if (target.tagName === "FORM") {
  240. for (let x = 0; x < target.elements.length; x++) {
  241. let element = target.elements[x];
  242. let name = element.getAttribute("name");
  243. if (name != null) {
  244. if (element.getAttribute("type") === "checkbox") {
  245. // @ts-ignore
  246. contents.values[name] = element.checked ? "true" : "false";
  247. } else {
  248. // @ts-ignore
  249. contents.values[name] =
  250. element.value ?? element.textContent;
  251. }
  252. }
  253. }
  254. }
  255. if (realId === null) {
  256. return;
  257. }
  258. window.ipc.postMessage(
  259. serializeIpcMessage("user_event", {
  260. event: edit.event_name,
  261. mounted_dom_id: parseInt(realId),
  262. contents: contents,
  263. })
  264. );
  265. }
  266. };
  267. this.NewEventListener(edit.event_name, edit.root, handler);
  268. break;
  269. case "SetText":
  270. this.SetText(edit.root, edit.text);
  271. break;
  272. case "SetAttribute":
  273. this.SetAttribute(edit.root, edit.field, edit.value, edit.ns);
  274. break;
  275. case "RemoveAttribute":
  276. this.RemoveAttribute(edit.root, edit.name, edit.ns);
  277. break;
  278. }
  279. }
  280. }
  281. export function serialize_event(event) {
  282. switch (event.type) {
  283. case "copy":
  284. case "cut":
  285. case "past": {
  286. return {};
  287. }
  288. case "compositionend":
  289. case "compositionstart":
  290. case "compositionupdate": {
  291. let { data } = event;
  292. return {
  293. data,
  294. };
  295. }
  296. case "keydown":
  297. case "keypress":
  298. case "keyup": {
  299. let {
  300. charCode,
  301. key,
  302. altKey,
  303. ctrlKey,
  304. metaKey,
  305. keyCode,
  306. shiftKey,
  307. location,
  308. repeat,
  309. which,
  310. } = event;
  311. return {
  312. char_code: charCode,
  313. key: key,
  314. alt_key: altKey,
  315. ctrl_key: ctrlKey,
  316. meta_key: metaKey,
  317. key_code: keyCode,
  318. shift_key: shiftKey,
  319. location: location,
  320. repeat: repeat,
  321. which: which,
  322. locale: "locale",
  323. };
  324. }
  325. case "focus":
  326. case "blur": {
  327. return {};
  328. }
  329. case "change": {
  330. let target = event.target;
  331. let value;
  332. if (target.type === "checkbox" || target.type === "radio") {
  333. value = target.checked ? "true" : "false";
  334. } else {
  335. value = target.value ?? target.textContent;
  336. }
  337. return {
  338. value: value,
  339. values: {},
  340. };
  341. }
  342. case "input":
  343. case "invalid":
  344. case "reset":
  345. case "submit": {
  346. let target = event.target;
  347. let value = target.value ?? target.textContent;
  348. if (target.type === "checkbox") {
  349. value = target.checked ? "true" : "false";
  350. }
  351. return {
  352. value: value,
  353. values: {},
  354. };
  355. }
  356. case "click":
  357. case "contextmenu":
  358. case "doubleclick":
  359. case "dblclick":
  360. case "drag":
  361. case "dragend":
  362. case "dragenter":
  363. case "dragexit":
  364. case "dragleave":
  365. case "dragover":
  366. case "dragstart":
  367. case "drop":
  368. case "mousedown":
  369. case "mouseenter":
  370. case "mouseleave":
  371. case "mousemove":
  372. case "mouseout":
  373. case "mouseover":
  374. case "mouseup": {
  375. const {
  376. altKey,
  377. button,
  378. buttons,
  379. clientX,
  380. clientY,
  381. ctrlKey,
  382. metaKey,
  383. pageX,
  384. pageY,
  385. screenX,
  386. screenY,
  387. shiftKey,
  388. } = event;
  389. return {
  390. alt_key: altKey,
  391. button: button,
  392. buttons: buttons,
  393. client_x: clientX,
  394. client_y: clientY,
  395. ctrl_key: ctrlKey,
  396. meta_key: metaKey,
  397. page_x: pageX,
  398. page_y: pageY,
  399. screen_x: screenX,
  400. screen_y: screenY,
  401. shift_key: shiftKey,
  402. };
  403. }
  404. case "pointerdown":
  405. case "pointermove":
  406. case "pointerup":
  407. case "pointercancel":
  408. case "gotpointercapture":
  409. case "lostpointercapture":
  410. case "pointerenter":
  411. case "pointerleave":
  412. case "pointerover":
  413. case "pointerout": {
  414. const {
  415. altKey,
  416. button,
  417. buttons,
  418. clientX,
  419. clientY,
  420. ctrlKey,
  421. metaKey,
  422. pageX,
  423. pageY,
  424. screenX,
  425. screenY,
  426. shiftKey,
  427. pointerId,
  428. width,
  429. height,
  430. pressure,
  431. tangentialPressure,
  432. tiltX,
  433. tiltY,
  434. twist,
  435. pointerType,
  436. isPrimary,
  437. } = event;
  438. return {
  439. alt_key: altKey,
  440. button: button,
  441. buttons: buttons,
  442. client_x: clientX,
  443. client_y: clientY,
  444. ctrl_key: ctrlKey,
  445. meta_key: metaKey,
  446. page_x: pageX,
  447. page_y: pageY,
  448. screen_x: screenX,
  449. screen_y: screenY,
  450. shift_key: shiftKey,
  451. pointer_id: pointerId,
  452. width: width,
  453. height: height,
  454. pressure: pressure,
  455. tangential_pressure: tangentialPressure,
  456. tilt_x: tiltX,
  457. tilt_y: tiltY,
  458. twist: twist,
  459. pointer_type: pointerType,
  460. is_primary: isPrimary,
  461. };
  462. }
  463. case "select": {
  464. return {};
  465. }
  466. case "touchcancel":
  467. case "touchend":
  468. case "touchmove":
  469. case "touchstart": {
  470. const { altKey, ctrlKey, metaKey, shiftKey } = event;
  471. return {
  472. // changed_touches: event.changedTouches,
  473. // target_touches: event.targetTouches,
  474. // touches: event.touches,
  475. alt_key: altKey,
  476. ctrl_key: ctrlKey,
  477. meta_key: metaKey,
  478. shift_key: shiftKey,
  479. };
  480. }
  481. case "scroll": {
  482. return {};
  483. }
  484. case "wheel": {
  485. const { deltaX, deltaY, deltaZ, deltaMode } = event;
  486. return {
  487. delta_x: deltaX,
  488. delta_y: deltaY,
  489. delta_z: deltaZ,
  490. delta_mode: deltaMode,
  491. };
  492. }
  493. case "animationstart":
  494. case "animationend":
  495. case "animationiteration": {
  496. const { animationName, elapsedTime, pseudoElement } = event;
  497. return {
  498. animation_name: animationName,
  499. elapsed_time: elapsedTime,
  500. pseudo_element: pseudoElement,
  501. };
  502. }
  503. case "transitionend": {
  504. const { propertyName, elapsedTime, pseudoElement } = event;
  505. return {
  506. property_name: propertyName,
  507. elapsed_time: elapsedTime,
  508. pseudo_element: pseudoElement,
  509. };
  510. }
  511. case "abort":
  512. case "canplay":
  513. case "canplaythrough":
  514. case "durationchange":
  515. case "emptied":
  516. case "encrypted":
  517. case "ended":
  518. case "error":
  519. case "loadeddata":
  520. case "loadedmetadata":
  521. case "loadstart":
  522. case "pause":
  523. case "play":
  524. case "playing":
  525. case "progress":
  526. case "ratechange":
  527. case "seeked":
  528. case "seeking":
  529. case "stalled":
  530. case "suspend":
  531. case "timeupdate":
  532. case "volumechange":
  533. case "waiting": {
  534. return {};
  535. }
  536. case "toggle": {
  537. return {};
  538. }
  539. default: {
  540. return {};
  541. }
  542. }
  543. }
  544. function serializeIpcMessage(method, params = {}) {
  545. return JSON.stringify({ method, params });
  546. }
  547. const bool_attrs = {
  548. allowfullscreen: true,
  549. allowpaymentrequest: true,
  550. async: true,
  551. autofocus: true,
  552. autoplay: true,
  553. checked: true,
  554. controls: true,
  555. default: true,
  556. defer: true,
  557. disabled: true,
  558. formnovalidate: true,
  559. hidden: true,
  560. ismap: true,
  561. itemscope: true,
  562. loop: true,
  563. multiple: true,
  564. muted: true,
  565. nomodule: true,
  566. novalidate: true,
  567. open: true,
  568. playsinline: true,
  569. readonly: true,
  570. required: true,
  571. reversed: true,
  572. selected: true,
  573. truespeed: true,
  574. };