inline0.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. let m,p,ls,d,t,op,i,e,z,metaflags;
  2. export function setAttributeInner(node, field, value, ns) {
  3. const name = field;
  4. if (ns === "style") {
  5. // ????? why do we need to do this
  6. if (node.style === undefined) {
  7. node.style = {};
  8. }
  9. node.style[name] = value;
  10. } else if (!!ns) {
  11. node.setAttributeNS(ns, name, value);
  12. } else {
  13. switch (name) {
  14. case "value":
  15. if (value !== node.value) {
  16. node.value = value;
  17. }
  18. break;
  19. case "initial_value":
  20. node.defaultValue = value;
  21. break;
  22. case "checked":
  23. node.checked = truthy(value);
  24. break;
  25. case "initial_checked":
  26. node.defaultChecked = truthy(value);
  27. break;
  28. case "selected":
  29. node.selected = truthy(value);
  30. break;
  31. case "initial_selected":
  32. node.defaultSelected = truthy(value);
  33. break;
  34. case "dangerous_inner_html":
  35. node.innerHTML = value;
  36. break;
  37. default:
  38. // https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
  39. if (!truthy(value) && bool_attrs.hasOwnProperty(name)) {
  40. node.removeAttribute(name);
  41. } else {
  42. node.setAttribute(name, value);
  43. }
  44. }
  45. }
  46. }
  47. const bool_attrs = {
  48. allowfullscreen: true,
  49. allowpaymentrequest: true,
  50. async: true,
  51. autofocus: true,
  52. autoplay: true,
  53. checked: true,
  54. controls: true,
  55. default: true,
  56. defer: true,
  57. disabled: true,
  58. formnovalidate: true,
  59. hidden: true,
  60. ismap: true,
  61. itemscope: true,
  62. loop: true,
  63. multiple: true,
  64. muted: true,
  65. nomodule: true,
  66. novalidate: true,
  67. open: true,
  68. playsinline: true,
  69. readonly: true,
  70. required: true,
  71. reversed: true,
  72. selected: true,
  73. truespeed: true,
  74. webkitdirectory: true,
  75. };
  76. function truthy(val) {
  77. return val === "true" || val === true;
  78. }
  79. class ListenerMap {
  80. constructor(root) {
  81. // bubbling events can listen at the root element
  82. this.global = {};
  83. // non bubbling events listen at the element the listener was created at
  84. this.local = {};
  85. this.root = null;
  86. this.handler = null;
  87. }
  88. create(event_name, element, bubbles) {
  89. if (bubbles) {
  90. if (this.global[event_name] === undefined) {
  91. this.global[event_name] = {};
  92. this.global[event_name].active = 1;
  93. this.root.addEventListener(event_name, this.handler);
  94. } else {
  95. this.global[event_name].active++;
  96. }
  97. }
  98. else {
  99. const id = element.getAttribute("data-dioxus-id");
  100. if (!this.local[id]) {
  101. this.local[id] = {};
  102. }
  103. element.addEventListener(event_name, this.handler);
  104. }
  105. }
  106. remove(element, event_name, bubbles) {
  107. if (bubbles) {
  108. this.global[event_name].active--;
  109. if (this.global[event_name].active === 0) {
  110. this.root.removeEventListener(event_name, this.global[event_name].callback);
  111. delete this.global[event_name];
  112. }
  113. }
  114. else {
  115. const id = element.getAttribute("data-dioxus-id");
  116. delete this.local[id][event_name];
  117. if (this.local[id].length === 0) {
  118. delete this.local[id];
  119. }
  120. element.removeEventListener(event_name, this.handler);
  121. }
  122. }
  123. removeAllNonBubbling(element) {
  124. const id = element.getAttribute("data-dioxus-id");
  125. delete this.local[id];
  126. }
  127. }
  128. function LoadChild(ptr, len) {
  129. // iterate through each number and get that child
  130. node = stack[stack.length - 1];
  131. ptr_end = ptr + len;
  132. for (; ptr < ptr_end; ptr++) {
  133. end = m.getUint8(ptr);
  134. for (node = node.firstChild; end > 0; end--) {
  135. node = node.nextSibling;
  136. }
  137. }
  138. return node;
  139. }
  140. const listeners = new ListenerMap();
  141. let nodes = [];
  142. let stack = [];
  143. let root;
  144. const templates = {};
  145. let node, els, end, ptr_end, k;
  146. export function save_template(nodes, tmpl_id) {
  147. templates[tmpl_id] = nodes;
  148. }
  149. export function hydrate(ids) {
  150. console.log("hydrating", ids);
  151. const hydrateNodes = document.querySelectorAll('[data-node-hydration]');
  152. for (let i = 0; i < hydrateNodes.length; i++) {
  153. const hydrateNode = hydrateNodes[i];
  154. const hydration = hydrateNode.getAttribute('data-node-hydration');
  155. const split = hydration.split(',');
  156. const id = ids[parseInt(split[0])];
  157. nodes[id] = hydrateNode;
  158. console.log("hydrating node", hydrateNode, id);
  159. if (split.length > 1) {
  160. hydrateNode.listening = split.length - 1;
  161. hydrateNode.setAttribute('data-dioxus-id', id);
  162. for (let j = 1; j < split.length; j++) {
  163. const listener = split[j];
  164. const split2 = listener.split(':');
  165. const event_name = split2[0];
  166. const bubbles = split2[1] === '1';
  167. console.log("hydrating listener", event_name, bubbles);
  168. listeners.create(event_name, hydrateNode, bubbles);
  169. }
  170. }
  171. }
  172. const treeWalker = document.createTreeWalker(
  173. document.body,
  174. NodeFilter.SHOW_COMMENT,
  175. );
  176. let currentNode = treeWalker.nextNode();
  177. while (currentNode) {
  178. const id = currentNode.textContent;
  179. const split = id.split('node-id');
  180. if (split.length > 1) {
  181. console.log("hydrating text", currentNode.nextSibling, id);
  182. nodes[ids[parseInt(split[1])]] = currentNode.nextSibling;
  183. }
  184. currentNode = treeWalker.nextNode();
  185. }
  186. }
  187. export function get_node(id) {
  188. return nodes[id];
  189. }
  190. export function initialize(root, handler) {
  191. listeners.handler = handler;
  192. nodes = [root];
  193. stack = [root];
  194. listeners.root = root;
  195. }
  196. function AppendChildren(id, many){
  197. root = nodes[id];
  198. els = stack.splice(stack.length-many);
  199. for (k = 0; k < many; k++) {
  200. root.appendChild(els[k]);
  201. }
  202. }
  203. let u8buf,u8bufp;let u32buf,u32bufp;let s = "";let lsp,sp,sl; let c = new TextDecoder();const ns_cache = [];
  204. let ns_cache_cache_hit, ns_cache_cache_idx;
  205. function get_ns_cache() {
  206. ns_cache_cache_idx = u8buf[u8bufp++];
  207. if(ns_cache_cache_idx & 128){
  208. ns_cache_cache_hit=s.substring(sp,sp+=u8buf[u8bufp++]);
  209. ns_cache[ns_cache_cache_idx&4294967167]=ns_cache_cache_hit;
  210. return ns_cache_cache_hit;
  211. }
  212. else{
  213. return ns_cache[ns_cache_cache_idx&4294967167];
  214. }
  215. }const evt = [];
  216. let evt_cache_hit, evt_cache_idx;
  217. function get_evt() {
  218. evt_cache_idx = u8buf[u8bufp++];
  219. if(evt_cache_idx & 128){
  220. evt_cache_hit=s.substring(sp,sp+=u8buf[u8bufp++]);
  221. evt[evt_cache_idx&4294967167]=evt_cache_hit;
  222. return evt_cache_hit;
  223. }
  224. else{
  225. return evt[evt_cache_idx&4294967167];
  226. }
  227. }let u16buf,u16bufp;const attr = [];
  228. let attr_cache_hit, attr_cache_idx;
  229. function get_attr() {
  230. attr_cache_idx = u8buf[u8bufp++];
  231. if(attr_cache_idx & 128){
  232. attr_cache_hit=s.substring(sp,sp+=u8buf[u8bufp++]);
  233. attr[attr_cache_idx&4294967167]=attr_cache_hit;
  234. return attr_cache_hit;
  235. }
  236. else{
  237. return attr[attr_cache_idx&4294967167];
  238. }
  239. }
  240. let bubbles,ns,len,event_name,ptr,id,field,value;
  241. export function create(r){
  242. d=r;
  243. }
  244. export function update_memory(b){
  245. m=new DataView(b.buffer)
  246. }
  247. export function run(){
  248. metaflags=m.getUint32(d,true);
  249. if((metaflags>>>6)&1){
  250. ls=m.getUint32(d+6*4,true);
  251. }
  252. p=ls;
  253. if ((metaflags>>>5)&1){
  254. t = m.getUint32(d+5*4,true);
  255. u8buf=new Uint8Array(m.buffer,t,((m.buffer.byteLength-t)-(m.buffer.byteLength-t)%1)/1);
  256. }
  257. u8bufp=0;if ((metaflags>>>3)&1){
  258. t = m.getUint32(d+3*4,true);
  259. u32buf=new Uint32Array(m.buffer,t,((m.buffer.byteLength-t)-(m.buffer.byteLength-t)%4)/4);
  260. }
  261. u32bufp=0;if (metaflags&1){
  262. lsp = m.getUint32(d+1*4,true);
  263. }
  264. if ((metaflags>>>2)&1) {
  265. sl = m.getUint32(d+2*4,true);
  266. if ((metaflags>>>1)&1) {
  267. sp = lsp;
  268. s = "";
  269. e = sp + ((sl / 4) | 0) * 4;
  270. while (sp < e) {
  271. t = m.getUint32(sp, true);
  272. s += String.fromCharCode(
  273. t & 255,
  274. (t & 65280) >> 8,
  275. (t & 16711680) >> 16,
  276. t >> 24
  277. );
  278. sp += 4;
  279. }
  280. while (sp < lsp + sl) {
  281. s += String.fromCharCode(m.getUint8(sp++));
  282. }
  283. } else {
  284. s = c.decode(new DataView(m.buffer, lsp, sl));
  285. }
  286. }
  287. sp=0;if ((metaflags>>>4)&1){
  288. t = m.getUint32(d+4*4,true);
  289. u16buf=new Uint16Array(m.buffer,t,((m.buffer.byteLength-t)-(m.buffer.byteLength-t)%2)/2);
  290. }
  291. u16bufp=0;
  292. for(;;){
  293. op=m.getUint32(p,true);
  294. p+=4;
  295. z=0;
  296. while(z++<4){
  297. switch(op&255){
  298. case 0:{AppendChildren(root, stack.length-1);}break;case 1:{stack.push(nodes[u32buf[u32bufp++]]);}break;case 2:{AppendChildren(u32buf[u32bufp++], u16buf[u16bufp++]);}break;case 3:{stack.pop();}break;case 4:{root = nodes[u32buf[u32bufp++]]; els = stack.splice(stack.length-u16buf[u16bufp++]); if (root.listening) { listeners.removeAllNonBubbling(root); } root.replaceWith(...els);}break;case 5:{nodes[u32buf[u32bufp++]].after(...stack.splice(stack.length-u16buf[u16bufp++]));}break;case 6:{nodes[u32buf[u32bufp++]].before(...stack.splice(stack.length-u16buf[u16bufp++]));}break;case 7:{node = nodes[u32buf[u32bufp++]]; if (node !== undefined) { if (node.listening) { listeners.removeAllNonBubbling(node); } node.remove(); }}break;case 8:{stack.push(document.createTextNode(s.substring(sp,sp+=u32buf[u32bufp++])));}break;case 9:{node = document.createTextNode(s.substring(sp,sp+=u32buf[u32bufp++])); nodes[u32buf[u32bufp++]] = node; stack.push(node);}break;case 10:{node = document.createElement('pre'); node.hidden = true; stack.push(node); nodes[u32buf[u32bufp++]] = node;}break;case 11:event_name=get_evt();id=u32buf[u32bufp++];bubbles=u8buf[u8bufp++];node = nodes[id]; if(node.listening){node.listening += 1;}else{node.listening = 1;} node.setAttribute('data-dioxus-id', `${id}`); listeners.create(event_name, node, bubbles);break;case 12:{node = nodes[u32buf[u32bufp++]]; node.listening -= 1; node.removeAttribute('data-dioxus-id'); listeners.remove(node, get_evt(), u8buf[u8bufp++]);}break;case 13:{nodes[u32buf[u32bufp++]].textContent = s.substring(sp,sp+=u32buf[u32bufp++]);}break;case 14:{node = nodes[u32buf[u32bufp++]]; setAttributeInner(node, get_attr(), s.substring(sp,sp+=u32buf[u32bufp++]), get_ns_cache());}break;case 15:id=u32buf[u32bufp++];field=get_attr();ns=get_ns_cache();{
  299. node = nodes[id];
  300. if (!ns) {
  301. switch (field) {
  302. case "value":
  303. node.value = "";
  304. break;
  305. case "checked":
  306. node.checked = false;
  307. break;
  308. case "selected":
  309. node.selected = false;
  310. break;
  311. case "dangerous_inner_html":
  312. node.innerHTML = "";
  313. break;
  314. default:
  315. node.removeAttribute(field);
  316. break;
  317. }
  318. } else if (ns == "style") {
  319. node.style.removeProperty(name);
  320. } else {
  321. node.removeAttributeNS(ns, field);
  322. }
  323. }break;case 16:{nodes[u32buf[u32bufp++]] = LoadChild(u32buf[u32bufp++], u8buf[u8bufp++]);}break;case 17:ptr=u32buf[u32bufp++];len=u8buf[u8bufp++];value=s.substring(sp,sp+=u32buf[u32bufp++]);id=u32buf[u32bufp++];{
  324. node = LoadChild(ptr, len);
  325. if (node.nodeType == Node.TEXT_NODE) {
  326. node.textContent = value;
  327. } else {
  328. let text = document.createTextNode(value);
  329. node.replaceWith(text);
  330. node = text;
  331. }
  332. nodes[id] = node;
  333. }break;case 18:{els = stack.splice(stack.length - u16buf[u16bufp++]); node = LoadChild(u32buf[u32bufp++], u8buf[u8bufp++]); node.replaceWith(...els);}break;case 19:{node = templates[u16buf[u16bufp++]][u16buf[u16bufp++]].cloneNode(true); nodes[u32buf[u32bufp++]] = node; stack.push(node);}break;case 20:return true;
  334. }
  335. op>>>=8;
  336. }
  337. }
  338. }
  339. export function run_from_bytes(bytes){
  340. d = 0;
  341. update_memory(new Uint8Array(bytes))
  342. run()
  343. }