Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

sdk/ui: a field's placeholder and the other writable properties can be set; markup read back has no end tag on an input or a break

shinyobjectza1c8492e8f1d
diff --git a/sdk/ui/src/dom/element.ts b/sdk/ui/src/dom/element.ts
index 658ad395..ff11a90f 100644
--- a/sdk/ui/src/dom/element.ts
+++ b/sdk/ui/src/dom/element.ts
@@ -1,4 +1,5 @@
// Element: attributes, classes, geometry from the layout snapshot, focus and pointer capture.
+import { VOID } from "../html/parse";
import * as wire from "../wire";
import { ATTR_PROP, Comment_, DOMRect_, HTML_NS, editableChanged, forceLayout, setInnerHtml, Node_, S, body, computedOf, cssText, doc, geo, html, htmlDoc, installed, kebab, makeStyle, matchesSelector, mutated, nodeList, parseCss, parseSelector, pointerEventsOf, query, tellCapture, wireStyle } from "./all";
@@ -253,6 +254,7 @@ export class Element_ extends Node_ {
}
get outerHTML(): string {
const attrs = [...this._attrs].map(([k, v]) => ` ${k}="${escapeHtml(v)}"`).join("");
+ if (VOID.has(this.localName)) return `<${this.localName}${attrs}>`;
return `<${this.localName}${attrs}>${this.innerHTML}</${this.localName}>`;
}
/** the element replaced by what the markup makes (nothing, for one that isn't in a tree) */
diff --git a/sdk/ui/src/dom/html.ts b/sdk/ui/src/dom/html.ts
index 05339f95..439835c7 100644
--- a/sdk/ui/src/dom/html.ts
+++ b/sdk/ui/src/dom/html.ts
@@ -18,6 +18,9 @@ export class HTMLElement_ extends Element_ {
get lang() {
return this.getAttribute("lang") ?? "";
}
+ set lang(v: string) {
+ this.setAttribute("lang", String(v));
+ }
get dir() {
return this.getAttribute("dir") ?? "";
}
@@ -41,6 +44,9 @@ export class HTMLElement_ extends Element_ {
get draggable() {
return this.getAttribute("draggable") === "true";
}
+ set draggable(v: boolean) {
+ this.setAttribute("draggable", String(!!v));
+ }
get contentEditable() {
const a = this.getAttribute("contenteditable");
if (a === null) return "inherit";
@@ -59,6 +65,9 @@ export class HTMLElement_ extends Element_ {
get accessKey() {
return this.getAttribute("accesskey") ?? "";
}
+ set accessKey(v: string) {
+ this.setAttribute("accesskey", String(v));
+ }
get offsetParent(): Element_ | null {
if (!this.isConnected || computedOf(this).display === "none") return null;
for (let p = this.parentNode; p instanceof Element_; p = p.parentNode) {
@@ -97,6 +106,9 @@ export class HTMLElement_ extends Element_ {
get autofocus() {
return this.hasAttribute("autofocus");
}
+ set autofocus(v: boolean) {
+ this.toggleAttribute("autofocus", !!v);
+ }
}
function offset(el: Element_, axis: 0 | 1): number {
@@ -149,9 +161,15 @@ export class Control_ extends HTMLElement_ {
get required() {
return this.hasAttribute("required");
}
+ set required(v: boolean) {
+ this.toggleAttribute("required", !!v);
+ }
get readOnly() {
return this.hasAttribute("readonly");
}
+ set readOnly(v: boolean) {
+ this.toggleAttribute("readonly", !!v);
+ }
get form(): HTMLFormElement_ | null {
return this.closest("form") as HTMLFormElement_ | null;
}
@@ -204,12 +222,21 @@ export class Control_ extends HTMLElement_ {
get defaultChecked() {
return this.hasAttribute("checked");
}
+ set defaultChecked(v: boolean) {
+ this.toggleAttribute("checked", !!v);
+ }
get placeholder() {
return this.getAttribute("placeholder") ?? "";
}
+ set placeholder(v: string) {
+ this.setAttribute("placeholder", String(v));
+ }
get valueAsNumber() {
return this.value === "" ? NaN : Number(this.value);
}
+ set valueAsNumber(v: number) {
+ this.value = Number.isFinite(v) ? String(v) : "";
+ }
get files() {
return null;
}
@@ -288,6 +315,12 @@ export class HTMLSelectElement_ extends Control_ {
const o = query(this, "option", false) as HTMLOptionElement_[];
return o.indexOf(this.selectedOptions[0] as HTMLOptionElement_);
}
+ set selectedIndex(i: number) {
+ const o = (query(this, "option", false) as HTMLOptionElement_[])[i];
+ for (const x of query(this, "option", false) as HTMLOptionElement_[]) x._sel = x === o;
+ this._value = o?.value ?? "";
+ if (this._wired && this.__wid !== undefined) wire.set(this.__wid, "value", this.value);
+ }
}
export class HTMLOptionElement_ extends HTMLElement_ {
@@ -295,6 +328,9 @@ export class HTMLOptionElement_ extends HTMLElement_ {
get value() {
return this.getAttribute("value") ?? this.textContent;
}
+ set value(v: string) {
+ this.setAttribute("value", String(v));
+ }
get selected() {
return this._sel ?? this.hasAttribute("selected");
}
@@ -304,12 +340,21 @@ export class HTMLOptionElement_ extends HTMLElement_ {
get label() {
return this.getAttribute("label") ?? this.textContent;
}
+ set label(v: string) {
+ this.setAttribute("label", String(v));
+ }
get text() {
return this.textContent;
}
+ set text(v: string) {
+ this.textContent = String(v);
+ }
get disabled() {
return this.hasAttribute("disabled");
}
+ set disabled(v: boolean) {
+ this.toggleAttribute("disabled", !!v);
+ }
}
export class HTMLFormElement_ extends HTMLElement_ {
@@ -324,12 +369,21 @@ export class HTMLFormElement_ extends HTMLElement_ {
get noValidate() {
return this.hasAttribute("novalidate");
}
+ set noValidate(v: boolean) {
+ this.toggleAttribute("novalidate", !!v);
+ }
get action() {
return this.getAttribute("action") ?? "";
}
+ set action(v: string) {
+ this.setAttribute("action", String(v));
+ }
get method() {
return this.getAttribute("method") ?? "get";
}
+ set method(v: string) {
+ this.setAttribute("method", String(v));
+ }
submit() {}
requestSubmit(submitter?: Element_) {
submitForm(this, submitter ?? null);
@@ -353,12 +407,18 @@ export class HTMLAnchorElement_ extends HTMLElement_ {
get href() {
return this.getAttribute("href") ?? "";
}
+ set href(v: string) {
+ this.setAttribute("href", String(v));
+ }
}
export class HTMLLabelElement_ extends HTMLElement_ {
get htmlFor() {
return this.getAttribute("for") ?? "";
}
+ set htmlFor(v: string) {
+ this.setAttribute("for", String(v));
+ }
get control(): Element_ | null {
const f = this.htmlFor;
return f ? doc.getElementById(f) : (this.querySelector("input, select, textarea, button") ?? null);
diff --git a/sdk/ui/src/dom/media.ts b/sdk/ui/src/dom/media.ts
index 09488b26..9b2b6449 100644
--- a/sdk/ui/src/dom/media.ts
+++ b/sdk/ui/src/dom/media.ts
@@ -75,9 +75,15 @@ export class HTMLMediaElement_ extends HTMLElement_ {
get loop() {
return this._props.loop === true || this.hasAttribute("loop");
}
+ set loop(v: boolean) {
+ this.toggleAttribute("loop", !!v);
+ }
get autoplay() {
return this._props.autoPlay === true || this._props.autoplay === true || this.hasAttribute("autoplay");
}
+ set autoplay(v: boolean) {
+ this.toggleAttribute("autoplay", !!v);
+ }
play() {
if (!this._playing) {
// played to its end, it starts again from the start, as a browser's does, and currentTime is 0 from now: it was
diff --git a/sdk/ui/src/html/parse.ts b/sdk/ui/src/html/parse.ts
index 356d4cf2..7216e090 100644
--- a/sdk/ui/src/html/parse.ts
+++ b/sdk/ui/src/html/parse.ts
@@ -36,7 +36,8 @@ export type HNode = HElement | HText;
export type HText = { type: "text"; text: string };
export type HElement = { type: "el"; tag: string; attrs: [string, string][]; kids: HNode[]; content?: HElement };
-const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]);
+/** elements with no end tag */
+export const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]);
const RAW = new Set(["script", "style", "textarea", "title", "xmp"]);
// a start tag of one of these closes an open <p>
const CLOSES_P = new Set(["address", "article", "aside", "blockquote", "details", "div", "dl", "fieldset", "figcaption", "figure", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hr", "main", "menu", "nav", "ol", "p", "pre", "section", "table", "ul"]);
diff --git a/sdk/ui/test/dom.test.tsx b/sdk/ui/test/dom.test.tsx
index 27f7a539..22ba77ee 100644
--- a/sdk/ui/test/dom.test.tsx
+++ b/sdk/ui/test/dom.test.tsx
@@ -562,3 +562,28 @@ describe("outerHTML", () => {
expect(off.outerHTML).toBe("<u></u>");
});
});
+
+describe("writable properties", () => {
+ test("a property set on a field made by the page reaches its attribute, as Chrome's do", () => {
+ fresh();
+ let box: any;
+ mount(<div ref={(e: any) => (box = e)} />, 0, { dom: true });
+ const input: any = document.createElement("input");
+ box.append(input);
+ input.placeholder = "Search";
+ input.required = true;
+ input.readOnly = true;
+ input.readOnly = false;
+ expect(input.outerHTML).toBe('<input placeholder="Search" required="">');
+ const a: any = document.createElement("a");
+ a.href = "#/projects";
+ a.draggable = false;
+ expect([a.getAttribute("href"), a.getAttribute("draggable")]).toEqual(["#/projects", "false"]);
+ const label: any = document.createElement("label");
+ label.htmlFor = "q";
+ expect(label.getAttribute("for")).toBe("q");
+ box.innerHTML = "<select><option value='a'>A</option><option value='b'>B</option></select>";
+ box.firstChild.selectedIndex = 1;
+ expect([box.firstChild.value, box.firstChild.selectedIndex]).toEqual(["b", 1]);
+ });
+});