From c227fb7cc9e30d6a4330cdad34b0b2a53b014d93 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 9 May 2022 18:02:30 -0700 Subject: web: add support for child DOM elements and text nodes --- web/static/dartboat.js | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'web/static') diff --git a/web/static/dartboat.js b/web/static/dartboat.js index eb1b8fa..e664a8d 100644 --- a/web/static/dartboat.js +++ b/web/static/dartboat.js @@ -55,18 +55,29 @@ function elemScrollToBottom(sel) { e.scrollTop = e.scrollHeight; } -function elemAppendElemv(sel, elemc, elemv, off_ns, off_name, off_content, - off_n_attrs, off_attr_names, off_attr_vals) { - const target = $(UTF8ToString(sel)); - +function targetAppendElemv(target, elemc, elemv, + off_type, off_ns, off_name, off_content, + off_n_attrs, off_attr_names, off_attr_vals, + off_n_children, off_children) { for (let ptr = elemv; ptr < elemv + 4*elemc; ptr += 4) { const struct = HEAP32[ptr>>2]; - const nsptr = HEAP32[(struct + off_ns)>>2]; - const name = UTF8ToString(HEAP32[(struct + off_name)>>2]); - const e = nsptr ? - document.createElementNS(UTF8ToString(nsptr), name) : - document.createElement(name); + let e; + const type = UTF8ToString(HEAP32[(struct + off_type)>>2]); + const contentptr = HEAP32[(struct + off_content)>>2]; + const content = contentptr && UTF8ToString(contentptr); + if (type === "text") { + e = document.createTextNode(content); + } else { + const nsptr = HEAP32[(struct + off_ns)>>2]; + const name = UTF8ToString( + HEAP32[(struct + off_name)>>2]); + e = nsptr ? + document.createElementNS( + UTF8ToString(nsptr), name) : + document.createElement(name); + if (content) e.textContent = content; + } const n_attrs = HEAP32[(struct + off_n_attrs)>>2]; if (n_attrs) { @@ -81,13 +92,20 @@ function elemAppendElemv(sel, elemc, elemv, off_ns, off_name, off_content, HEAP32[(attr_vals + i*4)>>2])); } - const content = HEAP32[(struct + off_content)>>2]; - if (content) e.textContent = UTF8ToString(content); + const n_children = HEAP32[(struct + off_n_children)>>2]; + if (n_children) + targetAppendElemv(e, n_children, + HEAP32[(struct + off_children)>>2], + ...Array.prototype.slice.call(arguments, 3)); target.appendChild(e); } } +function elemAppendElemv(sel, ...args) { + targetAppendElemv($(UTF8ToString(sel)), ...args); +} + function promptHandle(command, data) { const str_c = toCString(command); const str_d = data && toCString(data); -- cgit v1.2.3-70-g09d2