Add initial_value attribute & fix static special attribute handling (#1063)

* feat & fix: initial_value attribute, special attribute handling

* revision: rename js file

* fix: rustfmt

* fix: clippy

* fix: desktop not finding common.js

* fix: desktop failing to render

Missed a single ``this.SetAttributeInner`` as it was now ``setAttributeInner``
Javascript didn't even throw any errors about this smh

* revision: no need for match statement
This commit is contained in:
Miles Murgaw authored and GitHub committed 2023-06-04 11:29:34 -05:00
1 parent 0f684f0567
commit d09c92beda
9 files changed
+82 -18

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
export function setAttributeInner(node, field, value, ns) {
const name = field;
if (ns === "style") {
// ????? why do we need to do this
if (node.style === undefined) {
node.style = {};
}
node.style[name] = value;
} else if (ns != null && ns != undefined) {
node.setAttributeNS(ns, name, value);
} else {
switch (name) {
case "value":
if (value !== node.value) {
node.value = value;
}
break;
case "initial_value":
node.defaultValue = value;
break;
case "checked":
node.checked = value === "true" || value === true;
break;
case "selected":
node.selected = value === "true" || value === true;
break;
case "dangerous_inner_html":
node.innerHTML = value;
break;
default:
// https://github.com/facebook/react/blob/8b88ac2592c5f555f315f9440cbb665dd1e7457a/packages/react-dom/src/shared/DOMProperty.js#L352-L364
if (value === "false" && bool_attrs.hasOwnProperty(name)) {
node.removeAttribute(name);
} else {
node.setAttribute(name, value);
}
}
}
}
+9 -4
View File
@@ -1,3 +1,5 @@
import { setAttributeInner } from "./common.js";
class ListenerMap {
constructor(root) {
// bubbling events can listen at the root element
@@ -153,10 +155,10 @@ class Interpreter {
this.RemoveAttribute(id, field, ns);
} else {
const node = this.nodes[id];
this.SetAttributeInner(node, field, value, ns);
setAttributeInner(node, field, value, ns);
}
}
SetAttributeInner(node, field, value, ns) {
/*SetAttributeInner(node, field, value, ns) {
const name = field;
if (ns === "style") {
// ????? why do we need to do this
@@ -173,6 +175,9 @@ class Interpreter {
node.value = value;
}
break;
case "initial_value":
node.defaultValue = value;
break;
case "checked":
node.checked = value === "true" || value === true;
break;
@@ -191,7 +196,7 @@ class Interpreter {
}
}
}
}
}*/
RemoveAttribute(root, field, ns) {
const name = field;
const node = this.nodes[root];
@@ -291,7 +296,7 @@ class Interpreter {
for (let attr of node.attrs) {
if (attr.type == "Static") {
this.SetAttributeInner(el, attr.name, attr.value, attr.namespace);
setAttributeInner(el, attr.name, attr.value, attr.namespace);
}
}
+11
View File
@@ -1,4 +1,5 @@
pub static INTERPRETER_JS: &str = include_str!("./interpreter.js");
pub static COMMON_JS: &str = include_str!("./common.js");
#[cfg(feature = "sledgehammer")]
mod sledgehammer_bindings;
@@ -10,3 +11,13 @@ mod bindings;
#[cfg(feature = "web")]
pub use bindings::Interpreter;
// Common bindings for minimal usage.
#[cfg(feature = "minimal_bindings")]
pub mod minimal_bindings {
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
#[wasm_bindgen(module = "/src/common.js")]
extern "C" {
pub fn setAttributeInner(node: JsValue, name: &str, value: JsValue, ns: Option<&str>);
}
}
@@ -74,6 +74,9 @@ mod js {
node.value = value;
}
break;
case "initial_value":
node.defaultValue = value;
break;
case "checked":
node.checked = value === "true";
break;