Преглед изворни кода

browser: fix unfocusable form fields and the external-link icon

Docusaurus' navbar search field showed the I-beam on the board but
ignored clicks.  doc_reset() cleared every node field except the cached
attribute bits, so a node slot kept BR_ATTR_DISABLED (and the open/inert
bits) from the previous page, and real hardware does not hand out zeroed
memory either.  The bits are now cleared on reset, node_append() zeroes
the whole node, and removing an attribute clears its cached bit
(`input.disabled = false` used to leave the field disabled).

Related form fixes found on the way:
- A field takes focus before `click` is dispatched; a cancelled click
  only skips the activation behaviour (as focus follows mousedown).
- Full form-state slots are recycled from controls that left the
  document (client-side routers replace content without navigating).
- The status bar no longer labels a hovered form control "Link".

The external-link icon after "Cloudron.io"/"Demo" drew only a corner:
a <use> without width/height now sizes its symbol to 100% of the
viewport instead of the symbol's viewBox units.

browser-host --keys gains {click:X,Y} for headless clicks.
Johannes Zellner пре 2 дана
родитељ
комит
a2c9570412

+ 1 - 1
AGENTS.md

@@ -138,7 +138,7 @@ make clean
   compat ladder without a display; `--keys 'clo{down}{enter}'` types into
   compat ladder without a display; `--keys 'clo{down}{enter}'` types into
   the headless window (100 ms per key from 300 ms after the first frame;
   the headless window (100 ms per key from 300 ms after the first frame;
   `{enter} {esc} {tab} {bs} {del} {up} {down} {left} {right} {ctrl-l}
   `{enter} {esc} {tab} {bs} {del} {up} {down} {left} {right} {ctrl-l}
-  {wait}`).  It needs the multilib `lib32-sdl3`,
+  {wait}`, and `{click:X,Y}` for a left click at canvas coordinates).  It needs the multilib `lib32-sdl3`,
   `lib32-wayland` and `lib32-libxkbcommon`; host sockets replace the NebOS TCP
   `lib32-wayland` and `lib32-libxkbcommon`; host sockets replace the NebOS TCP
   stack and the kernel/WM paths are not exercised.
   stack and the kernel/WM paths are not exercised.
 - QEMU guests default to 512 MB (`QEMU_MEM`, e.g. `make run QEMU_MEM=1G`),
 - QEMU guests default to 512 MB (`QEMU_MEM`, e.g. `make run QEMU_MEM=1G`),

+ 1 - 1
docs/development.html

@@ -239,7 +239,7 @@
 					<tr><td><code>make test-chat</code></td><td>Host tests (ASan/UBSan) for the chat client's JSON parser, cookie jar and the OIDC login page/reply/config parsers.</td></tr>
 					<tr><td><code>make test-chat</code></td><td>Host tests (ASan/UBSan) for the chat client's JSON parser, cookie jar and the OIDC login page/reply/config parsers.</td></tr>
 					<tr><td><code>make test-chat-e2e</code></td><td>Boots QEMU with an NE2000 on slirp, starts <code>tools/mock_chat.py</code> and drives <code>chat</code> through QMP: native login, send, live poll, read marker, older paging, browse/join, new DM, saved-session resume and sign-out.  <code>CHAT_E2E=--totp|--consent|--passkey|--wrong</code> runs a login variant.</td></tr>
 					<tr><td><code>make test-chat-e2e</code></td><td>Boots QEMU with an NE2000 on slirp, starts <code>tools/mock_chat.py</code> and drives <code>chat</code> through QMP: native login, send, live poll, read marker, older paging, browse/join, new DM, saved-session resume and sign-out.  <code>CHAT_E2E=--totp|--consent|--passkey|--wrong</code> runs a login variant.</td></tr>
 					<tr><td><code>make compat-serve</code></td><td>Serve the browser compatibility ladder from <code>testdata/browser/compat/</code> on port 8000 for the board or QEMU guest.</td></tr>
 					<tr><td><code>make compat-serve</code></td><td>Serve the browser compatibility ladder from <code>testdata/browser/compat/</code> on port 8000 for the board or QEMU guest.</td></tr>
-					<tr><td><code>make browser-host</code></td><td>Build the Linux SDL3 development harness (<code>browser-host</code> and its <code>host-curl</code> helper). It implements the ring-3 <code>SYS_*</code> ABI with host files, BSD sockets, <code>fork/exec</code> and an SDL3 window, so pages render and accept input without QEMU. Run <code>./browser-host [--size WxH] [--root DIR] URL|FILE</code>; <code>--headless --wait 3000 --shot out.ppm</code> writes the canvas without a display, and <code>--keys 'clo{down}{enter}'</code> types into it (one key per 100 ms; <code>{enter}</code>, <code>{esc}</code>, <code>{tab}</code>, <code>{bs}</code>, <code>{del}</code>, arrows, <code>{ctrl-l}</code>, <code>{wait}</code>). Needs the 32-bit SDL3/Wayland libraries (<code>lib32-sdl3</code>, <code>lib32-wayland</code>, <code>lib32-libxkbcommon</code>).</td></tr>
+					<tr><td><code>make browser-host</code></td><td>Build the Linux SDL3 development harness (<code>browser-host</code> and its <code>host-curl</code> helper). It implements the ring-3 <code>SYS_*</code> ABI with host files, BSD sockets, <code>fork/exec</code> and an SDL3 window, so pages render and accept input without QEMU. Run <code>./browser-host [--size WxH] [--root DIR] URL|FILE</code>; <code>--headless --wait 3000 --shot out.ppm</code> writes the canvas without a display, and <code>--keys 'clo{down}{enter}'</code> types into it (one key per 100 ms; <code>{enter}</code>, <code>{esc}</code>, <code>{tab}</code>, <code>{bs}</code>, <code>{del}</code>, arrows, <code>{ctrl-l}</code>, <code>{wait}</code>, and <code>{click:X,Y}</code> for a left click at canvas coordinates). Needs the 32-bit SDL3/Wayland libraries (<code>lib32-sdl3</code>, <code>lib32-wayland</code>, <code>lib32-libxkbcommon</code>).</td></tr>
 					<tr><td><code>make fonts</code></td><td>Regenerate the checked-in Arimo/Liberation Mono subsets with <code>pyftsubset</code>.</td></tr>
 					<tr><td><code>make fonts</code></td><td>Regenerate the checked-in Arimo/Liberation Mono subsets with <code>pyftsubset</code>.</td></tr>
 				</tbody>
 				</tbody>
 			</table>
 			</table>

+ 2 - 1
host/main.c

@@ -5,7 +5,8 @@
  *
  *
  * --keys types into a headless window, one key every 100 ms from 300 ms
  * --keys types into a headless window, one key every 100 ms from 300 ms
  * after the first frame: plain characters, plus {enter} {esc} {tab} {bs}
  * after the first frame: plain characters, plus {enter} {esc} {tab} {bs}
- * {del} {up} {down} {left} {right} {ctrl-l} and {wait} (a 1 s pause).
+ * {del} {up} {down} {left} {right} {ctrl-l}, {wait} (a 1 s pause) and
+ * {click:X,Y} (a left click at canvas coordinates X,Y).
  */
  */
 #include <signal.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdio.h>

+ 33 - 2
host/window_headless.c

@@ -108,9 +108,18 @@ void host_headless_pump(unsigned int timeout_ms)
 		usleep(timeout_ms * 1000u);
 		usleep(timeout_ms * 1000u);
 }
 }
 
 
+/* --keys `{click:X,Y}`: the pointer moves there and a left press is
+ * queued, its release on the next event poll. */
+static int ptr_x, ptr_y;
+static unsigned int ptr_buttons;
+static int click_pending;       /* 1 press queued, 2 release queued */
+
 int host_headless_pointer(struct win_pointer *out)
 int host_headless_pointer(struct win_pointer *out)
 {
 {
 	memset(out, 0, sizeof(*out));
 	memset(out, 0, sizeof(*out));
+	out->x = ptr_x;
+	out->y = ptr_y;
+	out->buttons = ptr_buttons;
 	out->focused = 1;
 	out->focused = 1;
 	out->inside = 1;
 	out->inside = 1;
 	return 0;
 	return 0;
@@ -118,8 +127,21 @@ int host_headless_pointer(struct win_pointer *out)
 
 
 int host_headless_event(struct win_event *out)
 int host_headless_event(struct win_event *out)
 {
 {
-	(void)out;
-	return 0;
+	if (!click_pending)
+		return 0;
+	out->x = ptr_x;
+	out->y = ptr_y;
+	if (click_pending == 1) {
+		out->type = WIN_EV_POINTER_DOWN;
+		ptr_buttons = 1;
+		click_pending = 2;
+	} else {
+		out->type = WIN_EV_POINTER_UP;
+		ptr_buttons = 0;
+		click_pending = 0;
+	}
+	out->buttons = ptr_buttons;
+	return 1;
 }
 }
 
 
 int host_headless_scroll(struct win_scroll *out)
 int host_headless_scroll(struct win_scroll *out)
@@ -154,6 +176,15 @@ int host_headless_key(void)
 	if (!*key_pos || now < key_due)
 	if (!*key_pos || now < key_due)
 		return 0;
 		return 0;
 	code = (unsigned char)*key_pos++;
 	code = (unsigned char)*key_pos++;
+	if (code == '{' && !strncmp(key_pos, "click:", 6)) {
+		const char *end = strchr(key_pos, '}');
+
+		if (sscanf(key_pos + 6, "%d,%d", &ptr_x, &ptr_y) == 2)
+			click_pending = 1;
+		key_pos = end ? end + 1 : key_pos + strlen(key_pos);
+		key_due = now + 300;
+		return 0;
+	}
 	if (code == '{') {
 	if (code == '{') {
 		const char *end = strchr(key_pos, '}');
 		const char *end = strchr(key_pos, '}');
 		unsigned int i;
 		unsigned int i;

+ 44 - 9
user/apps/browser/browser.c

@@ -400,6 +400,8 @@ static unsigned int form_option_text(unsigned int node, unsigned int idx,
 	return 0;
 	return 0;
 }
 }
 
 
+static int form_is_control(unsigned int node);
+
 static struct form_state *form_get(unsigned int node, int create)
 static struct form_state *form_get(unsigned int node, int create)
 {
 {
 	struct form_state *slot = 0;
 	struct form_state *slot = 0;
@@ -413,6 +415,22 @@ static struct form_state *form_get(unsigned int node, int create)
 			slot = &form_states[i];
 			slot = &form_states[i];
 		}
 		}
 	}
 	}
+	if (create && !slot) {
+		/* Full: a client-side router (Docusaurus' React Router)
+		 * replaces page content without a navigation, so slots of
+		 * controls that left the document pile up until no new
+		 * control can be drawn or focused.
+		 * Reuse those first, then slots of nodes that are no longer
+		 * controls. */
+		for (i = 0; i < FORM_MAX && !slot; i++)
+			if (form_states[i].node != form_focus &&
+			    br_node_detached(&doc, form_states[i].node))
+				slot = &form_states[i];
+		for (i = 0; i < FORM_MAX && !slot; i++)
+			if (form_states[i].node != form_focus &&
+			    !form_is_control(form_states[i].node))
+				slot = &form_states[i];
+	}
 	if (!create || !slot)
 	if (!create || !slot)
 		return 0;
 		return 0;
 	slot->node = node;
 	slot->node = node;
@@ -8282,10 +8300,14 @@ static void svg_draw_node(struct svgr_layer *L, unsigned int node,
 			double ow = svg_vp_w, oh = svg_vp_h;
 			double ow = svg_vp_w, oh = svg_vp_h;
 
 
 			if (svg_viewbox4(tgt, vb)) {
 			if (svg_viewbox4(tgt, vb)) {
-				double w = svg_attr_num(node, "width", vb[2],
-							ow);
-				double h = svg_attr_num(node, "height", vb[3],
-							oh);
+				/* A <use> without width/height sizes the
+				 * symbol to 100% of the viewport: Docusaurus'
+				 * 13.5px external-link icon references a
+				 * 24-unit symbol and showed only its corner. */
+				double w = svg_attr_num(node, "width",
+							ow > 0 ? ow : vb[2], ow);
+				double h = svg_attr_num(node, "height",
+							oh > 0 ? oh : vb[3], oh);
 				double vm[6];
 				double vm[6];
 
 
 				svg_viewport_matrix(tgt, vb, w, h, vm);
 				svg_viewport_matrix(tgt, vb, w, h, vm);
@@ -12454,6 +12476,10 @@ static void draw_status(struct ngui_rect r)
 	ngui_fill_rect(&ui, r, ui.theme.panel);
 	ngui_fill_rect(&ui, r, ui.theme.panel);
 	ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
 	ngui_fill_rect(&ui, (struct ngui_rect){r.x, r.y, r.w, 1},
 		       ui.theme.border);
 		       ui.theme.border);
+	/* A form control under the pointer is not a link. */
+	if (hover && !br_href(&doc, (unsigned int)hover) &&
+	    form_is_control((unsigned int)hover))
+		hover = 0;
 	if (hover) {
 	if (hover) {
 		const char *href = br_href(&doc, (unsigned int)hover);
 		const char *href = br_href(&doc, (unsigned int)hover);
 		char url[BR_URL_MAX];
 		char url[BR_URL_MAX];
@@ -12843,14 +12869,26 @@ static void form_click(unsigned int node)
 		return;
 		return;
 	if (form_focus != node)
 	if (form_focus != node)
 		form_blur();
 		form_blur();
-	if (!js_browser_event(node, "click", 1, 1))
-		return;
 	fs = form_get(node, 1);
 	fs = form_get(node, 1);
 	if (!fs)
 	if (!fs)
 		return;
 		return;
 	kind = form_kind_of(node);
 	kind = form_kind_of(node);
+	/* Focus moves on the press, before `click` is dispatched: a page
+	 * that cancels the click (a framework's root listener) suppresses
+	 * the activation behaviour below, never the focus (as in other
+	 * browsers, where focus follows mousedown). */
 	form_focus = node;
 	form_focus = node;
 	form_dirty = 0;
 	form_dirty = 0;
+	if (kind == FORM_TEXT || kind == FORM_PASSWORD ||
+	    kind == FORM_TEXTAREA)
+		fs->caret = fs->len;
+	layout_valid = 0;
+	ngui_invalidate(&ui);
+	if (!js_browser_event(node, "click", 1, 1))
+		return;
+	fs = form_get(node, 0);     /* the handler may have changed the DOM */
+	if (!fs)
+		return;
 	if (kind == FORM_CHECKBOX) {
 	if (kind == FORM_CHECKBOX) {
 		fs->checked = !fs->checked;
 		fs->checked = !fs->checked;
 		changed = 1;
 		changed = 1;
@@ -12889,9 +12927,6 @@ static void form_click(unsigned int node)
 	} else if (kind == FORM_RESET) {
 	} else if (kind == FORM_RESET) {
 		form_reset_event(form_ancestor(node));
 		form_reset_event(form_ancestor(node));
 		return;
 		return;
-	} else if (kind == FORM_TEXT || kind == FORM_PASSWORD ||
-		   kind == FORM_TEXTAREA) {
-		fs->caret = fs->len;
 	}
 	}
 	if (changed) {
 	if (changed) {
 		js_browser_event(node, "input", 1, 0);
 		js_browser_event(node, "input", 1, 0);

+ 38 - 2
user/apps/browser/browser_html.c

@@ -980,6 +980,11 @@ static void doc_reset(struct br_doc *d)
 		d->nodes[i].type = BR_NODE_ELEM;
 		d->nodes[i].type = BR_NODE_ELEM;
 		d->nodes[i].tag = BR_TAG_UNKNOWN;
 		d->nodes[i].tag = BR_TAG_UNKNOWN;
 		d->nodes[i].flags = 0;
 		d->nodes[i].flags = 0;
+		/* The cached disabled/open/inert bits must not survive into
+		 * the next page: a slot that held a disabled control made
+		 * Docusaurus' search field unfocusable after navigating (and
+		 * real hardware does not hand out zeroed memory). */
+		d->nodes[i].attr_flags = 0;
 		d->nodes[i].parent = 0;
 		d->nodes[i].parent = 0;
 		d->nodes[i].first = d->nodes[i].last = 0;
 		d->nodes[i].first = d->nodes[i].last = 0;
 		d->nodes[i].next = 0;
 		d->nodes[i].next = 0;
@@ -1002,6 +1007,8 @@ static void doc_reset(struct br_doc *d)
 	}
 	}
 }
 }
 
 
+static void node_zero(struct br_node *n);
+
 static unsigned int node_append(struct br_doc *d, unsigned int parent,
 static unsigned int node_append(struct br_doc *d, unsigned int parent,
 				unsigned char type, unsigned char tag)
 				unsigned char type, unsigned char tag)
 {
 {
@@ -1014,11 +1021,10 @@ static unsigned int node_append(struct br_doc *d, unsigned int parent,
 	}
 	}
 	idx = d->nnode++;
 	idx = d->nnode++;
 	n = &d->nodes[idx];
 	n = &d->nodes[idx];
+	node_zero(n);           /* every field, attr_flags included */
 	n->type = type;
 	n->type = type;
 	n->tag = tag;
 	n->tag = tag;
 	n->parent = (unsigned short)parent;
 	n->parent = (unsigned short)parent;
-	n->img_index = -1;
-	n->bg_img_index = -1;
 	if (d->nodes[parent].last)
 	if (d->nodes[parent].last)
 		d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
 		d->nodes[d->nodes[parent].last].next = (unsigned short)idx;
 	else
 	else
@@ -1116,6 +1122,11 @@ static int node_detached(struct br_doc *d, unsigned int node)
 	return 0;
 	return 0;
 }
 }
 
 
+int br_node_detached(struct br_doc *d, unsigned int node)
+{
+	return !d || node >= d->nnode || node_detached(d, node);
+}
+
 unsigned int br_find_id(struct br_doc *d, const char *id)
 unsigned int br_find_id(struct br_doc *d, const char *id)
 {
 {
 	unsigned int i;
 	unsigned int i;
@@ -1781,6 +1792,8 @@ int br_set_attr(struct br_doc *d, unsigned int node, const char *name,
 	return 0;
 	return 0;
 }
 }
 
 
+static void clear_attr_flag(struct br_node *n, const char *name);
+
 int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
 int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
 {
 {
 	struct br_node *n;
 	struct br_node *n;
@@ -1825,6 +1838,8 @@ int br_remove_attr(struct br_doc *d, unsigned int node, const char *name)
 	n->style_valid = 0;
 	n->style_valid = 0;
 	n->pe_state = 0;
 	n->pe_state = 0;
 	attr_note_known(d, n, name, "", 0);
 	attr_note_known(d, n, name, "", 0);
+	if (found)
+		clear_attr_flag(n, name);
 	return found ? 0 : -1;
 	return found ? 0 : -1;
 }
 }
 
 
@@ -2053,6 +2068,27 @@ static int parse_svg_viewbox(const char *s, unsigned int len,
 	return *w && *h;
 	return *w && *h;
 }
 }
 
 
+/* The inverse of note_attr_flag() for a removed attribute.  The cached
+ * bit used to outlive the attribute: `input.disabled = false` removed
+ * `disabled` but left BR_ATTR_DISABLED set, so a field a script disabled
+ * once (Docusaurus' search box while its index loads) never took focus
+ * again. */
+static void clear_attr_flag(struct br_node *n, const char *name)
+{
+	if (str_eq(name, "data-collapse-target"))
+		n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TARGET;
+	else if (str_eq(name, "data-collapse-trigger"))
+		n->attr_flags &= (unsigned char)~BR_ATTR_COLLAPSE_TRIGGER;
+	else if (str_eq(name, "is-open"))
+		n->attr_flags &= (unsigned char)~BR_ATTR_IS_OPEN;
+	else if (str_eq(name, "disabled"))
+		n->attr_flags &= (unsigned char)~BR_ATTR_DISABLED;
+	else if (str_eq(name, "open"))
+		n->attr_flags &= (unsigned char)~BR_ATTR_OPEN;
+	else if (str_eq(name, "inert"))
+		n->attr_flags &= (unsigned char)~BR_ATTR_INERT;
+}
+
 static void note_attr_flag(struct br_node *n, const char *name)
 static void note_attr_flag(struct br_node *n, const char *name)
 {
 {
 	if (str_eq(name, "data-collapse-target"))
 	if (str_eq(name, "data-collapse-target"))

+ 2 - 0
user/apps/browser/browser_html.h

@@ -398,6 +398,8 @@ void br_set_font_base(const char *base);
  * text nodes into out and returns the (possibly truncated) length. */
  * text nodes into out and returns the (possibly truncated) length. */
 unsigned int br_find_id(struct br_doc *d, const char *id);
 unsigned int br_find_id(struct br_doc *d, const char *id);
 unsigned int br_find_tag(struct br_doc *d, unsigned int tag);
 unsigned int br_find_tag(struct br_doc *d, unsigned int tag);
+/* 1 when the node is not in the document (removed, or never inserted). */
+int br_node_detached(struct br_doc *d, unsigned int node);
 unsigned int br_text_content(struct br_doc *d, unsigned int node,
 unsigned int br_text_content(struct br_doc *d, unsigned int node,
 			     char *out, unsigned int cap);
 			     char *out, unsigned int cap);
 int br_set_text_content(struct br_doc *d, unsigned int node,
 int br_set_text_content(struct br_doc *d, unsigned int node,