Selaa lähdekoodia

Add browser float wrapping and live form state

Johannes Zellner 1 viikko sitten
vanhempi
sitoutus
cd973a81ad

+ 22 - 18
AGENTS.md

@@ -824,7 +824,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `@media (min-width/max-width)` blocks are evaluated against the browser
   `@media (min-width/max-width)` blocks are evaluated against the browser
   canvas width when the page is parsed; `@font-face`/`@import`/`@keyframes` are
   canvas width when the page is parsed; `@font-face`/`@import`/`@keyframes` are
   skipped.  The compatibility ladder is `testdata/browser/compat/`
   skipped.  The compatibility ladder is `testdata/browser/compat/`
-  (`make compat-serve`); pages 010–029 pass.  Inline
+  (`make compat-serve`); pages 010–035 and 030a pass.  Inline
   `<svg>` subtrees and closed `<details>` are skipped (not renderable), and
   `<svg>` subtrees and closed `<details>` are skipped (not renderable), and
   `<video>` gets a placeholder box.  The default page canvas follows
   `<video>` gets a placeholder box.  The default page canvas follows
   `ngui_theme_dark` (dark page, light text, `theme.panel` for `<pre>`);
   `ngui_theme_dark` (dark page, light text, `theme.panel` for `<pre>`);
@@ -866,7 +866,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `style` (`cssText`, property get/set and `getPropertyValue`/
   `style` (`cssText`, property get/set and `getPropertyValue`/
   `setProperty`/`removeProperty`), `innerHTML` (fragment parser in
   `setProperty`/`removeProperty`), `innerHTML` (fragment parser in
   `browser_html.c`), `textContent`/`nodeValue`, child/sibling/parent
   `browser_html.c`), `textContent`/`nodeValue`, child/sibling/parent
-  traversal, `matches` and `getBoundingClientRect`.  `<a>` elements expose
+  traversal, `matches` and `getBoundingClientRect` (real block/table/
+  absolute/image boxes; inline text boxes are still approximate).  `<a>` elements expose
   the DOM URL components (`href`/`protocol`/`host`/`hostname`/`port`/
   the DOM URL components (`href`/`protocol`/`host`/`hostname`/`port`/
   `pathname`/`search`/`hash`/`origin`) resolved against the page URL.
   `pathname`/`search`/`hash`/`origin`) resolved against the page URL.
   `document` exposes `createElement`/`createTextNode`/`createComment`/
   `document` exposes `createElement`/`createTextNode`/`createComment`/
@@ -888,9 +889,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   default; the vendored engine also carries small Annex B parser fixes for
   default; the vendored engine also carries small Annex B parser fixes for
   regex literals after control parentheses and identity escapes/braces.
   regex literals after control parentheses and identity escapes/braces.
   `addEventListener`/`removeEventListener`/`dispatchEvent` are no-ops (no
   `addEventListener`/`removeEventListener`/`dispatchEvent` are no-ops (no
-  event dispatch yet), and forms are not supported; flex/grid, basic tables,
-  inline SVG and CSS variables are supported (see known issue 22 for the
-  remaining CSS and DOM gaps).  `SYS_SPAWN`
+  event dispatch yet).  Form controls render, focus, edit, toggle and select
+  (text/password/textarea, checkbox/radio, buttons, select); GET submission
+  builds a query from successful controls, native reset works, and the DOM
+  `value`/`checked`/`selectedIndex` properties read/write the live control
+  state.  POST and JS `input`/`change`/`submit` events are not implemented.
+  Flex/grid, basic tables, inline SVG and CSS variables are supported (see
+  known issue 22 for the remaining CSS and DOM gaps).  `SYS_SPAWN`
   loads FAT ELFs through `kmalloc` now (the old 16 KB `fat_load_buf` is gone)
   loads FAT ELFs through `kmalloc` now (the old 16 KB `fat_load_buf` is gone)
   and wraps the load in `sys_fs_enter/exit`,
   and wraps the load in `sys_fs_enter/exit`,
   since the floppy needs IRQ 6.
   since the floppy needs IRQ 6.
@@ -1515,18 +1520,15 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 22. Browser limits: JavaScript runs inline and external classic scripts on
 22. Browser limits: JavaScript runs inline and external classic scripts on
     MuJS with the DOM surface described above (creation/mutation/selectors/
     MuJS with the DOM surface described above (creation/mutation/selectors/
     attributes/classList/dataset/style/innerHTML/timers).  Still missing:
     attributes/classList/dataset/style/innerHTML/timers).  Still missing:
-    event dispatch (`addEventListener` is a no-op), form submission,
-    `type="module"`, `defer`/`async`, `MutationObserver`/
+    event dispatch (`addEventListener` is a no-op), POST forms and JS form
+    events, `type="module"`, `defer`/`async`, `MutationObserver`/
     `IntersectionObserver`, `XMLHttpRequest`/`fetch`, `DOMParser`, canvas/2D
     `IntersectionObserver`, `XMLHttpRequest`/`fetch`, `DOMParser`, canvas/2D
     contexts and pseudo-class/element selectors.  `getComputedStyle` is a
     contexts and pseudo-class/element selectors.  `getComputedStyle` is a
     stub (it returns empty values), `outerHTML` returns only the string last
     stub (it returns empty values), `outerHTML` returns only the string last
     assigned to `innerHTML`, and `style`/`dataset` cover inline state rather
     assigned to `innerHTML`, and `style`/`dataset` cover inline state rather
     than computed style.  Libraries that need those APIs (jQuery plugins
     than computed style.  Libraries that need those APIs (jQuery plugins
     beyond Bootstrap's tooltip, Angular apps that use `$http`) still log
     beyond Bootstrap's tooltip, Angular apps that use `$http`) still log
-    errors even though the static page renders.  CSS also lacks table
-    `rowspan`, text wrap around floats and a real `clear` (the row-based
-    float model puts following blocks below the row), percentage margins/
-    padding (still resolved against the viewport), `@font-face`,
+    errors even though the static page renders.  CSS also lacks `@font-face`,
     `background-repeat`/`position`/`size` (a background image is stretched
     `background-repeat`/`position`/`size` (a background image is stretched
     to its box), the rest of `transform` (`translate` only), sticky beyond
     to its box), the rest of `transform` (`translate` only), sticky beyond
     the top edge, border-radius clipping of descendant content, gradients/
     the top edge, border-radius clipping of descendant content, gradients/
@@ -1534,8 +1536,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     solid colour), or CSS `color-mix`.  CSS custom properties (`var()`),
     solid colour), or CSS `color-mix`.  CSS custom properties (`var()`),
     flex rows with wrap/grow/gap/justify/align, two-track and
     flex rows with wrap/grow/gap/justify/align, two-track and
     `repeat(auto-fit, minmax(...))` grids, inline-block runs, basic
     `repeat(auto-fit, minmax(...))` grids, inline-block runs, basic
-    proportional table columns, table colspan/percentage columns and inline
-    SVG (`rect`, `line`, `path` with M/L/H/V, and `text`) are supported.
+    proportional table columns, table rowspan/colspan/percentage columns and
+    inline SVG (`rect`, `line`, `path` with M/L/H/V, and `text`) are
+    supported.
     `position:fixed`/`absolute` boxes are deferred and painted above the
     `position:fixed`/`absolute` boxes are deferred and painted above the
     flow; `z-index` sorts that deferred list; `position:relative` shifts
     flow; `z-index` sorts that deferred list; `position:relative` shifts
     painting without moving flow, and `position:sticky` clamps against the
     painting without moving flow, and `position:sticky` clamps against the
@@ -1547,16 +1550,17 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     is the basic Bootstrap-grid case: consecutive floated blocks are laid
     is the basic Bootstrap-grid case: consecutive floated blocks are laid
     side by side with percentage/px widths, left floats pack from the row's
     side by side with percentage/px widths, left floats pack from the row's
     left edge and right floats from its right edge, inline floats are
     left edge and right floats from its right edge, inline floats are
-    blockified (Bootstrap floats the navbar brand and its text links), there
-    is no `clear`, percentage margins are approximated as pixels, and a
-    float without a width uses a text-width estimate.  `@media` width
+    blockified (Bootstrap floats the navbar brand and its text links), inline
+    text following a float wraps beside it, `clear` advances past the active
+    float, and a float without a width uses a text-width estimate.  `@media` width
     blocks are evaluated against the canvas width at page-parse time, so a
     blocks are evaluated against the canvas width at page-parse time, so a
     window resize does not re-evaluate them until the next load, and
     window resize does not re-evaluate them until the next load, and
     pseudo-classes are never matched.  `font-size` maps to real pixel sizes
     pseudo-classes are never matched.  `font-size` maps to real pixel sizes
     (8..48), `font-weight` selects the bold face, `font-style: italic`
     (8..48), `font-weight` selects the bold face, `font-style: italic`
     selects the italic face, `font-family: monospace` selects Liberation
     selects the italic face, `font-family: monospace` selects Liberation
-    Mono, and nested `em`/`%` font sizes and `em`/`rem` widths resolve
-    against the parent/root; margin/padding `em` is still root-based.  Text
+    Mono, and nested `em`/`%` font sizes, `em`/`rem` widths, and
+    margin/padding percentages/`em` resolve against the containing
+    width/element font (root for `rem`).  Text
     is Latin-1 only (non-Latin-1 characters become `?`); there is no shaping,
     is Latin-1 only (non-Latin-1 characters become `?`); there is no shaping,
     bidi, `@font-face` or full-Unicode fallback.
     bidi, `@font-face` or full-Unicode fallback.
     Pages that need multi-row grid templates, `color-mix()` or full
     Pages that need multi-row grid templates, `color-mix()` or full

+ 299 - 28
browser.c

@@ -182,10 +182,13 @@ struct lay {
 	int left, right;
 	int left, right;
 	int y;
 	int y;
 	int line_x, line_y, line_h;
 	int line_x, line_y, line_h;
+	int line_left, line_right;
 	int run_x;
 	int run_x;
 	int align;
 	int align;
 	int depth;
 	int depth;
 	int space_pending;
 	int space_pending;
+	unsigned char float_active;
+	int float_left, float_right, float_bottom;
 	struct lay_run runs[96];
 	struct lay_run runs[96];
 	int nruns;
 	int nruns;
 };
 };
@@ -576,6 +579,148 @@ static int js_host_rect(unsigned int node, int *x, int *y, int *w, int *h)
 	return 1;
 	return 1;
 }
 }
 
 
+static void form_copy_out(const char *s, char *buf, int cap)
+{
+	int i = 0;
+
+	if (!buf || cap <= 0)
+		return;
+	while (s && s[i] && i + 1 < cap) {
+		buf[i] = s[i];
+		i++;
+	}
+	buf[i] = '\0';
+}
+
+static int js_host_form_value(unsigned int node, char *buf, int cap)
+{
+	struct form_state *fs;
+	int kind;
+
+	if (!form_is_control(node))
+		return 0;
+	kind = form_kind_of(node);
+	if (kind == FORM_CHECKBOX || kind == FORM_RADIO)
+		return 0;
+	if (kind == FORM_SELECT) {
+		char opt[64] = "";
+		unsigned int c, i = 0;
+
+		fs = form_get(node, 1);
+		if (!fs)
+			return 0;
+		form_option_text(node, fs->selected, opt, sizeof(opt));
+		for (c = doc.nodes[node].first; c; c = doc.nodes[c].next) {
+			if (doc.nodes[c].type != BR_NODE_ELEM ||
+			    doc.nodes[c].tag != BR_TAG_OPTION)
+				continue;
+			if (i == fs->selected) {
+				const char *v = br_value(&doc, c);
+				if (v && v[0])
+					form_copy_out(v, buf, cap);
+				else
+					form_copy_out(opt, buf, cap);
+				return 1;
+			}
+			i++;
+		}
+		form_copy_out(opt, buf, cap);
+		return 1;
+	}
+	fs = form_get(node, 1);
+	if (!fs)
+		return 0;
+	form_copy_out(fs->value, buf, cap);
+	return 1;
+}
+
+static int js_host_form_checked(unsigned int node)
+{
+	struct form_state *fs;
+	int kind;
+
+	if (!form_is_control(node))
+		return -1;
+	kind = form_kind_of(node);
+	if (kind != FORM_CHECKBOX && kind != FORM_RADIO)
+		return -1;
+	fs = form_get(node, 1);
+	return fs ? fs->checked : 0;
+}
+
+static int js_host_form_selected(unsigned int node)
+{
+	struct form_state *fs;
+
+	if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT)
+		return -1;
+	fs = form_get(node, 1);
+	return fs ? fs->selected : 0;
+}
+
+static int js_host_form_set_value(unsigned int node, const char *value)
+{
+	struct form_state *fs;
+	unsigned int n;
+
+	if (!form_is_control(node))
+		return 0;
+	fs = form_get(node, 1);
+	if (!fs)
+		return 0;
+	n = s_len(value ? value : "");
+	if (n > FORM_VALUE_MAX)
+		n = FORM_VALUE_MAX;
+	for (unsigned int i = 0; i < n; i++)
+		fs->value[i] = value[i];
+	fs->value[n] = '\0';
+	fs->len = (unsigned char)n;
+	fs->caret = (unsigned char)n;
+	layout_valid = 0;
+	ngui_invalidate(&ui);
+	return 1;
+}
+
+static int js_host_form_set_checked(unsigned int node, int checked)
+{
+	struct form_state *fs;
+	int kind;
+
+	if (!form_is_control(node))
+		return 0;
+	kind = form_kind_of(node);
+	if (kind != FORM_CHECKBOX && kind != FORM_RADIO)
+		return 0;
+	fs = form_get(node, 1);
+	if (!fs)
+		return 0;
+	fs->checked = (unsigned char)(checked != 0);
+	layout_valid = 0;
+	ngui_invalidate(&ui);
+	return 1;
+}
+
+static int js_host_form_set_selected(unsigned int node, int index)
+{
+	struct form_state *fs;
+	unsigned int n;
+
+	if (!form_is_control(node) || form_kind_of(node) != FORM_SELECT)
+		return 0;
+	fs = form_get(node, 1);
+	if (!fs)
+		return 0;
+	n = form_option_count(node);
+	if (index < 0)
+		index = 0;
+	if (n && (unsigned int)index >= n)
+		index = (int)n - 1;
+	fs->selected = (unsigned char)index;
+	layout_valid = 0;
+	ngui_invalidate(&ui);
+	return 1;
+}
+
 static int fetch_script_text(const char *url, unsigned char **out,
 static int fetch_script_text(const char *url, unsigned char **out,
 			     unsigned int *outlen, char *err,
 			     unsigned int *outlen, char *err,
 			     unsigned int errcap);
 			     unsigned int errcap);
@@ -611,6 +756,12 @@ static void run_page_scripts(void)
 	host.navigate = js_host_navigate;
 	host.navigate = js_host_navigate;
 	host.changed = js_host_changed;
 	host.changed = js_host_changed;
 	host.rect = js_host_rect;
 	host.rect = js_host_rect;
+	host.form_value = js_host_form_value;
+	host.form_checked = js_host_form_checked;
+	host.form_selected = js_host_form_selected;
+	host.form_set_value = js_host_form_set_value;
+	host.form_set_checked = js_host_form_set_checked;
+	host.form_set_selected = js_host_form_set_selected;
 	host.page_url = page_url;
 	host.page_url = page_url;
 	host.width = ui.w;
 	host.width = ui.w;
 	host.height = ui.h;
 	host.height = ui.h;
@@ -2738,16 +2889,37 @@ static int run_width(const struct lay_run *r)
 	return (int)r->w;
 	return (int)r->w;
 }
 }
 
 
+static void start_line(struct lay *l, const struct br_style *st)
+{
+	(void)st;
+	if (l->nruns == 0) {
+		l->line_y = l->y;
+		l->line_h = 0;
+	}
+	l->line_left = l->left;
+	l->line_right = l->right;
+	if (l->float_active && l->line_y < l->float_bottom) {
+		if (l->float_left > l->line_left)
+			l->line_left = l->float_left;
+		if (l->float_right < l->line_right)
+			l->line_right = l->float_right;
+	}
+	if (l->line_right < l->line_left + 8)
+		l->line_right = l->line_left + 8;
+	l->line_x = l->line_left;
+	l->run_x = l->line_left;
+}
+
 static void flush_line(struct lay *l)
 static void flush_line(struct lay *l)
 {
 {
 	int total, x, i, vis;
 	int total, x, i, vis;
 
 
-	if (!l->nruns && l->line_x == l->left) {
+	if (!l->nruns && l->line_x == l->line_left) {
 		l->line_h = 0;
 		l->line_h = 0;
 		return;
 		return;
 	}
 	}
-	total = l->line_x - l->left;
-	x = l->left;
+	total = l->line_x - l->line_left;
+	x = l->line_left;
 	vis = 1;
 	vis = 1;
 	if (l->draw) {
 	if (l->draw) {
 		int lh = l->line_h ? l->line_h : 16;
 		int lh = l->line_h ? l->line_h : 16;
@@ -2756,11 +2928,11 @@ static void flush_line(struct lay *l)
 		      l->line_y <= page_rect.y + page_rect.h;
 		      l->line_y <= page_rect.y + page_rect.h;
 	}
 	}
 	if (l->align == BR_ALIGN_CENTER)
 	if (l->align == BR_ALIGN_CENTER)
-		x += (l->right - l->left - total) / 2;
+		x += (l->line_right - l->line_left - total) / 2;
 	else if (l->align == BR_ALIGN_RIGHT)
 	else if (l->align == BR_ALIGN_RIGHT)
-		x = l->right - total;
-	if (x < l->left)
-		x = l->left;
+		x = l->line_right - total;
+	if (x < l->line_left)
+		x = l->line_left;
 	for (i = 0; i < l->nruns; i++) {
 	for (i = 0; i < l->nruns; i++) {
 		struct lay_run *r = &l->runs[i];
 		struct lay_run *r = &l->runs[i];
 		int w = run_width(r);
 		int w = run_width(r);
@@ -2921,9 +3093,13 @@ static void flush_line(struct lay *l)
 		x += w;
 		x += w;
 	}
 	}
 	l->y += l->line_h + 2;
 	l->y += l->line_h + 2;
+	if (l->float_active && l->y >= l->float_bottom)
+		l->float_active = 0;
 	l->nruns = 0;
 	l->nruns = 0;
 	l->line_x = l->left;
 	l->line_x = l->left;
 	l->run_x = l->left;
 	l->run_x = l->left;
+	l->line_left = l->left;
+	l->line_right = l->right;
 	l->line_h = 0;
 	l->line_h = 0;
 	l->space_pending = 0;
 	l->space_pending = 0;
 }
 }
@@ -2942,6 +3118,7 @@ static void add_run(struct lay *l, unsigned short off, unsigned short len,
 	if (l->nruns == 0) {
 	if (l->nruns == 0) {
 		l->line_y = l->y;
 		l->line_y = l->y;
 		l->line_h = 0;
 		l->line_h = 0;
+		start_line(l, st);
 	}
 	}
 	{
 	{
 		struct lay_run *r = &l->runs[l->nruns++];
 		struct lay_run *r = &l->runs[l->nruns++];
@@ -3009,11 +3186,14 @@ static void add_image_run(struct lay *l, unsigned int node,
 
 
 	if (idx >= 0 && idx < image_count && images[idx].state == 1)
 	if (idx >= 0 && idx < image_count && images[idx].state == 1)
 		bi = &images[idx];
 		bi = &images[idx];
-	if (l->space_pending && (l->nruns > 0 || l->line_x > l->left)) {
+	if (l->nruns == 0)
+		start_line(l, st);
+	if (l->space_pending &&
+	    (l->nruns > 0 || l->line_x > l->line_left)) {
 		int sp = (int)ngui_text_width_px(l->ui, " ", 1, st->font_px,
 		int sp = (int)ngui_text_width_px(l->ui, " ", 1, st->font_px,
 						 st->bold, st->italic,
 						 st->bold, st->italic,
 						 st->mono);
 						 st->mono);
-		if (l->line_x + sp <= l->right)
+		if (l->line_x + sp <= l->line_right)
 			l->line_x += sp;
 			l->line_x += sp;
 	}
 	}
 	l->space_pending = 0;
 	l->space_pending = 0;
@@ -3031,8 +3211,8 @@ static void add_image_run(struct lay *l, unsigned int node,
 		s_append(tmp, &n, "]", sizeof(tmp));
 		s_append(tmp, &n, "]", sizeof(tmp));
 		w = (int)ngui_text_width_px(l->ui, tmp, n, gs.font_px,
 		w = (int)ngui_text_width_px(l->ui, tmp, n, gs.font_px,
 					    gs.bold, gs.italic, gs.mono);
 					    gs.bold, gs.italic, gs.mono);
-		if (l->line_x + w > l->right &&
-		    (l->nruns > 0 || l->line_x > l->left))
+		if (l->line_x + w > l->line_right &&
+		    (l->nruns > 0 || l->line_x > l->line_left))
 			flush_line(l);
 			flush_line(l);
 		add_run(l, 0, 0, &gs, href, tmp);
 		add_run(l, 0, 0, &gs, href, tmp);
 		if (!l->draw) {
 		if (!l->draw) {
@@ -3049,7 +3229,7 @@ static void add_image_run(struct lay *l, unsigned int node,
 		}
 		}
 		return;
 		return;
 	}
 	}
-	maxw = l->right - l->left;
+	maxw = l->line_right - l->line_left;
 	if (maxw < 8)
 	if (maxw < 8)
 		maxw = 8;
 		maxw = 8;
 	{
 	{
@@ -3121,14 +3301,15 @@ static void add_image_run(struct lay *l, unsigned int node,
 		img_top[node] = (unsigned short)t;
 		img_top[node] = (unsigned short)t;
 		img_h[node] = (unsigned short)dh;
 		img_h[node] = (unsigned short)dh;
 	}
 	}
-	if (l->line_x + dw > l->right && (l->nruns > 0 ||
-					  l->line_x > l->left))
+	if (l->line_x + dw > l->line_right && (l->nruns > 0 ||
+						l->line_x > l->line_left))
 		flush_line(l);
 		flush_line(l);
 	if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
 	if (l->nruns >= (int)(sizeof(l->runs) / sizeof(l->runs[0])))
 		flush_line(l);
 		flush_line(l);
 	if (l->nruns == 0) {
 	if (l->nruns == 0) {
 		l->line_y = l->y;
 		l->line_y = l->y;
 		l->line_h = 0;
 		l->line_h = 0;
+		start_line(l, st);
 	}
 	}
 	{
 	{
 		struct lay_run *r = &l->runs[l->nruns++];
 		struct lay_run *r = &l->runs[l->nruns++];
@@ -3208,19 +3389,25 @@ static void emit_word(struct lay *l, const char *s, unsigned int start,
 		      unsigned int n, unsigned int base,
 		      unsigned int n, unsigned int base,
 		      const struct br_style *st, unsigned short href)
 		      const struct br_style *st, unsigned short href)
 {
 {
-	int avail = l->right - l->left;
+	int avail;
 	int w = text_width(l, s + start, n, st);
 	int w = text_width(l, s + start, n, st);
 	unsigned int off = start;
 	unsigned int off = start;
 
 
+	if (l->nruns == 0)
+		start_line(l, st);
+	avail = l->line_right - l->line_left;
 	if (st->nowrap) {
 	if (st->nowrap) {
 		if (n)
 		if (n)
 			add_run(l, (unsigned short)(base + start),
 			add_run(l, (unsigned short)(base + start),
 				(unsigned short)n, st, href, 0);
 				(unsigned short)n, st, href, 0);
 		return;
 		return;
 	}
 	}
-	if (l->line_x + w > l->right &&
-	    (l->nruns > 0 || l->line_x > l->left))
+	if (l->line_x + w > l->line_right &&
+	    (l->nruns > 0 || l->line_x > l->line_left)) {
 		flush_line(l);
 		flush_line(l);
+		start_line(l, st);
+		avail = l->line_right - l->line_left;
+	}
 	if (w <= avail) {
 	if (w <= avail) {
 		if (n)
 		if (n)
 			add_run(l, (unsigned short)(base + start),
 			add_run(l, (unsigned short)(base + start),
@@ -3234,8 +3421,11 @@ static void emit_word(struct lay *l, const char *s, unsigned int start,
 			(unsigned short)take, st, href, 0);
 			(unsigned short)take, st, href, 0);
 		off += take;
 		off += take;
 		n -= take;
 		n -= take;
-		if (n)
+		if (n) {
 			flush_line(l);
 			flush_line(l);
+			start_line(l, st);
+			avail = l->line_right - l->line_left;
+		}
 	}
 	}
 }
 }
 
 
@@ -3251,6 +3441,8 @@ static void emit_text(struct lay *l, const char *s, unsigned int len,
 		return;
 		return;
 	if (space_w <= 0)
 	if (space_w <= 0)
 		space_w = 8;
 		space_w = 8;
+	if (l->nruns == 0)
+		start_line(l, st);
 	base = (unsigned int)(s - doc.text);
 	base = (unsigned int)(s - doc.text);
 	if (st->uppercase) {
 	if (st->uppercase) {
 		char *p = (char *)s;
 		char *p = (char *)s;
@@ -3266,15 +3458,18 @@ static void emit_text(struct lay *l, const char *s, unsigned int len,
 
 
 			if (s[i] == '\n') {
 			if (s[i] == '\n') {
 				flush_line(l);
 				flush_line(l);
+				start_line(l, st);
 				i++;
 				i++;
 				continue;
 				continue;
 			}
 			}
 			if (s[i] == ' ') {
 			if (s[i] == ' ') {
-				if (l->line_x + space_w > l->right &&
-				    (l->nruns > 0 || l->line_x > l->left))
+				if (l->line_x + space_w > l->line_right &&
+				    (l->nruns > 0 || l->line_x > l->line_left)) {
 					flush_line(l);
 					flush_line(l);
-				else
+					start_line(l, st);
+				} else {
 					l->line_x += space_w;
 					l->line_x += space_w;
+				}
 				i++;
 				i++;
 				continue;
 				continue;
 			}
 			}
@@ -3298,8 +3493,9 @@ static void emit_text(struct lay *l, const char *s, unsigned int len,
 			i++;
 			i++;
 		n = i - start;
 		n = i - start;
 		if (l->space_pending &&
 		if (l->space_pending &&
-		    (l->nruns > 0 || l->line_x > l->left)) {
-			if (st->nowrap || l->line_x + space_w <= l->right)
+		    (l->nruns > 0 || l->line_x > l->line_left)) {
+			if (st->nowrap ||
+			    l->line_x + space_w <= l->line_right)
 				l->line_x += space_w;
 				l->line_x += space_w;
 		}
 		}
 		l->space_pending = 0;
 		l->space_pending = 0;
@@ -3332,6 +3528,8 @@ static void add_form_run(struct lay *l, unsigned int node,
 
 
 	if (!fs)
 	if (!fs)
 		return;
 		return;
+	if (l->nruns == 0)
+		start_line(l, st);
 	if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
 	if (kind == FORM_CHECKBOX || kind == FORM_RADIO) {
 		text = "";
 		text = "";
 		w = 16;
 		w = 16;
@@ -3375,18 +3573,19 @@ static void add_form_run(struct lay *l, unsigned int node,
 	} else if (kind == FORM_SELECT) {
 	} else if (kind == FORM_SELECT) {
 		w = st->width_kind == 1 ? st->width : 120;
 		w = st->width_kind == 1 ? st->width : 120;
 	}
 	}
-	if (w > l->right - l->left)
-		w = l->right - l->left;
+	if (w > l->line_right - l->line_left)
+		w = l->line_right - l->line_left;
 	if (w < 8)
 	if (w < 8)
 		w = 8;
 		w = 8;
-	if (l->line_x + w > l->right &&
-	    (l->nruns > 0 || l->line_x > l->left))
+	if (l->line_x + w > l->line_right &&
+	    (l->nruns > 0 || l->line_x > l->line_left))
 		flush_line(l);
 		flush_line(l);
 	if (l->nruns >= 96)
 	if (l->nruns >= 96)
 		return;
 		return;
 	if (l->nruns == 0) {
 	if (l->nruns == 0) {
 		l->line_y = l->y;
 		l->line_y = l->y;
 		l->line_h = 0;
 		l->line_h = 0;
+		start_line(l, st);
 	}
 	}
 	r = &l->runs[l->nruns++];
 	r = &l->runs[l->nruns++];
 	r->gap = (unsigned short)(l->line_x - l->run_x);
 	r->gap = (unsigned short)(l->line_x - l->run_x);
@@ -3945,6 +4144,8 @@ static void layout_block(struct lay *l, unsigned int node,
 	if (l->right < l->left + 8)
 	if (l->right < l->left + 8)
 		l->right = l->left + 8;
 		l->right = l->left + 8;
 	l->align = st->align;
 	l->align = st->align;
+	l->line_left = l->left;
+	l->line_right = l->right;
 	l->y = top + bt + pt;
 	l->y = top + bt + pt;
 
 
 	if (doc.nodes[node].tag == BR_TAG_HR) {
 	if (doc.nodes[node].tag == BR_TAG_HR) {
@@ -4038,6 +4239,11 @@ static void layout_block(struct lay *l, unsigned int node,
 			layout_children(l, node, st, depth + 1, link);
 			layout_children(l, node, st, depth + 1, link);
 	}
 	}
 	flush_line(l);
 	flush_line(l);
+	if (l->float_active) {
+		if (l->y < l->float_bottom)
+			l->y = l->float_bottom;
+		l->float_active = 0;
+	}
 	if (st->height_kind == 1) {
 	if (st->height_kind == 1) {
 		content_h = st->height;
 		content_h = st->height;
 		if (st->box_sizing)
 		if (st->box_sizing)
@@ -4225,10 +4431,12 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 	unsigned int n = first;
 	unsigned int n = first;
 	int row_left = l->left, row_right = l->right;
 	int row_left = l->left, row_right = l->right;
 	int row_top, row_bottom, xl, xr, any = 0;
 	int row_top, row_bottom, xl, xr, any = 0;
+	int fl_left = row_left, fl_right = row_right, fl_bottom;
 
 
 	flush_line(l);
 	flush_line(l);
 	row_top = l->y;
 	row_top = l->y;
 	row_bottom = l->y;
 	row_bottom = l->y;
+	fl_bottom = row_top;
 	xl = row_left;
 	xl = row_left;
 	xr = row_right;
 	xr = row_right;
 	while (n) {
 	while (n) {
@@ -4295,6 +4503,8 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 				l->left = row_left;
 				l->left = row_left;
 			l->right = xr;
 			l->right = xr;
 			xr = l->left;
 			xr = l->left;
+			if (xr < fl_right)
+				fl_right = xr;
 		} else {
 		} else {
 			if (any && xl + colw > xr) {
 			if (any && xl + colw > xr) {
 				row_top = row_bottom;
 				row_top = row_bottom;
@@ -4305,6 +4515,8 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 			l->left = xl;
 			l->left = xl;
 			l->right = xl + colw;
 			l->right = xl + colw;
 			xl += colw;
 			xl += colw;
+			if (xl > fl_left)
+				fl_left = xl;
 		}
 		}
 		if (l->right < l->left + 8)
 		if (l->right < l->left + 8)
 			l->right = l->left + 8;
 			l->right = l->left + 8;
@@ -4319,14 +4531,53 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 		layout_block(l, n, &cs, depth + 1, lk);
 		layout_block(l, n, &cs, depth + 1, lk);
 		if (l->y > row_bottom)
 		if (l->y > row_bottom)
 			row_bottom = l->y;
 			row_bottom = l->y;
+		if (l->y > fl_bottom)
+			fl_bottom = l->y;
 		any = 1;
 		any = 1;
 		n = nd->next;
 		n = nd->next;
 	}
 	}
 	l->left = row_left;
 	l->left = row_left;
 	l->right = row_right;
 	l->right = row_right;
-	l->y = row_bottom;
+	if (n && any) {
+		struct br_node *nd = &doc.nodes[n];
+		int inline_next = 0;
+
+		if (nd->type == BR_NODE_TEXT) {
+			inline_next = 1;
+		} else if (nd->type == BR_NODE_ELEM) {
+			struct br_style cs;
+
+			br_style_for(&doc, n, pst, &cs);
+			inline_next = cs.display != BR_DISPLAY_BLOCK &&
+				      cs.display != BR_DISPLAY_NONE &&
+				      !br_tag_is_block(nd->tag);
+		}
+		if (inline_next) {
+			if (l->float_active) {
+				if (l->float_left > fl_left)
+					fl_left = l->float_left;
+				if (l->float_right < fl_right)
+					fl_right = l->float_right;
+				if (l->float_bottom > fl_bottom)
+					fl_bottom = l->float_bottom;
+			}
+			l->float_active = 1;
+			l->float_left = fl_left;
+			l->float_right = fl_right;
+			l->float_bottom = fl_bottom;
+			l->y = row_top;
+		} else {
+			l->float_active = 0;
+			l->y = row_bottom;
+		}
+	} else {
+		l->float_active = 0;
+		l->y = row_bottom;
+	}
 	l->line_x = l->left;
 	l->line_x = l->left;
 	l->run_x = l->left;
 	l->run_x = l->left;
+	l->line_left = l->left;
+	l->line_right = l->right;
 	l->line_y = l->y;
 	l->line_y = l->y;
 	l->line_h = 0;
 	l->line_h = 0;
 	l->nruns = 0;
 	l->nruns = 0;
@@ -5588,6 +5839,20 @@ static void layout_children(struct lay *l, unsigned int parent,
 			n = nd->next;
 			n = nd->next;
 			continue;
 			continue;
 		}
 		}
+		if (cs.clear && l->float_active) {
+			flush_line(l);
+			if (l->y < l->float_bottom)
+				l->y = l->float_bottom;
+			l->float_active = 0;
+			l->line_x = l->left;
+			l->run_x = l->left;
+			l->line_left = l->left;
+			l->line_right = l->right;
+			l->line_y = l->y;
+			l->line_h = 0;
+			l->nruns = 0;
+			l->space_pending = 0;
+		}
 		if (nd->tag == BR_TAG_A && nd->href_off)
 		if (nd->tag == BR_TAG_A && nd->href_off)
 			lk = (unsigned short)n;
 			lk = (unsigned short)n;
 		if (cs.pos == 1 || cs.pos == 2) {
 		if (cs.pos == 1 || cs.pos == 2) {
@@ -5692,9 +5957,12 @@ static int measure_page(int left, int right)
 	l->y = 0;
 	l->y = 0;
 	l->line_x = left;
 	l->line_x = left;
 	l->run_x = left;
 	l->run_x = left;
+	l->line_left = left;
+	l->line_right = right;
 	l->line_y = 0;
 	l->line_y = 0;
 	l->line_h = 0;
 	l->line_h = 0;
 	l->align = BR_ALIGN_LEFT;
 	l->align = BR_ALIGN_LEFT;
+	l->float_active = 0;
 	l->depth = 0;
 	l->depth = 0;
 	l->nruns = 0;
 	l->nruns = 0;
 	l->space_pending = 0;
 	l->space_pending = 0;
@@ -5742,9 +6010,12 @@ static void render_page(int left, int right, int y0)
 	l->y = y0;
 	l->y = y0;
 	l->line_x = left;
 	l->line_x = left;
 	l->run_x = left;
 	l->run_x = left;
+	l->line_left = left;
+	l->line_right = right;
 	l->line_y = y0;
 	l->line_y = y0;
 	l->line_h = 0;
 	l->line_h = 0;
 	l->align = BR_ALIGN_LEFT;
 	l->align = BR_ALIGN_LEFT;
+	l->float_active = 0;
 	l->depth = 0;
 	l->depth = 0;
 	l->nruns = 0;
 	l->nruns = 0;
 	l->space_pending = 0;
 	l->space_pending = 0;

+ 17 - 1
browser_html.c

@@ -2233,7 +2233,7 @@ enum {
 	PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
 	PROP_MINW, PROP_MINW_KIND, PROP_BOX_SIZING, PROP_ML_AUTO, PROP_MR_AUTO,
 	PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
 	PROP_HEIGHT, PROP_HEIGHT_KIND, PROP_MINH, PROP_MINH_KIND,
 	PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
 	PROP_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
-	PROP_FLOAT, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
+	PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
 	PROP_JUSTIFY, PROP_ALIGN_ITEMS,
 	PROP_JUSTIFY, PROP_ALIGN_ITEMS,
 	PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
 	PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
 	PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
 	PROP_POS, PROP_LEFT, PROP_TOP, PROP_LEFT_SET, PROP_TOP_SET,
@@ -3830,6 +3830,18 @@ static void parse_decls(struct br_doc *d, const char *s,
 				 ci_eq_n(s + vstart, "right", 5))
 				 ci_eq_n(s + vstart, "right", 5))
 				f = 2;
 				f = 2;
 			decl_add(out, nout, PROP_FLOAT, f, important);
 			decl_add(out, nout, PROP_FLOAT, f, important);
+		} else if (str_eq(prop, "clear")) {
+			int c = 0;
+
+			if (contains_ci_len(s + vstart, vend - vstart, "both"))
+				c = 3;
+			else if (contains_ci_len(s + vstart, vend - vstart,
+						 "left"))
+				c = 1;
+			else if (contains_ci_len(s + vstart, vend - vstart,
+						 "right"))
+				c = 2;
+			decl_add(out, nout, PROP_CLEAR, c, important);
 		} else if (str_eq(prop, "flex-grow") ||
 		} else if (str_eq(prop, "flex-grow") ||
 			   str_eq(prop, "flex")) {
 			   str_eq(prop, "flex")) {
 			unsigned int q = vstart;
 			unsigned int q = vstart;
@@ -5613,6 +5625,9 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_FLOAT:
 	case PROP_FLOAT:
 		st->float_left = (unsigned char)dcl->val;
 		st->float_left = (unsigned char)dcl->val;
 		break;
 		break;
+	case PROP_CLEAR:
+		st->clear = (unsigned char)dcl->val;
+		break;
 	case PROP_FLEX_GROW:
 	case PROP_FLEX_GROW:
 		st->flex_grow = (unsigned char)dcl->val;
 		st->flex_grow = (unsigned char)dcl->val;
 		break;
 		break;
@@ -5721,6 +5736,7 @@ void br_style_for(struct br_doc *d, unsigned int node,
 	out->has_bg = 0;
 	out->has_bg = 0;
 	out->bg_image = 0;
 	out->bg_image = 0;
 	out->float_left = 0;
 	out->float_left = 0;
+	out->clear = 0;
 	out->list_item = 0;
 	out->list_item = 0;
 	out->font_unit = 0;
 	out->font_unit = 0;
 	for (i = 0; i < 4; i++) {
 	for (i = 0; i < 4; i++) {

+ 1 - 0
browser_html.h

@@ -99,6 +99,7 @@ struct br_style {
 	unsigned char display;      /* BR_DISPLAY_* */
 	unsigned char display;      /* BR_DISPLAY_* */
 	unsigned char vis_hidden, opacity_hidden, overflow_hidden;
 	unsigned char vis_hidden, opacity_hidden, overflow_hidden;
 	unsigned char float_left;   /* float: left/right */
 	unsigned char float_left;   /* float: left/right */
+	unsigned char clear;        /* 0 none, 1 left, 2 right, 3 both */
 	unsigned char flex_grow;    /* flex-grow, 0..100 */
 	unsigned char flex_grow;    /* flex-grow, 0..100 */
 	unsigned char flex_col;     /* flex-direction: column */
 	unsigned char flex_col;     /* flex-direction: column */
 	unsigned char flex_wrap;    /* flex-wrap: wrap */
 	unsigned char flex_wrap;    /* flex-wrap: wrap */

+ 5 - 5
docs/userland.html

@@ -365,11 +365,11 @@
 			drawing and scrolling.
 			drawing and scrolling.
 		</p>
 		</p>
 		<ul>
 		<ul>
-			<li><strong>HTML/CSS subset.</strong> External and inline stylesheets, tag/class/id and descendant/child selectors, colours, backgrounds (solid and a stretched <code>background-image</code>), real <code>font-size</code> (8–48 px), the <code>font</code> shorthand, bold/italic faces, <code>font-family: monospace</code>, margins, padding, per-side borders, alignment, explicit/percentage block widths with <code>min-width</code>/<code>max-width</code>/<code>min-height</code>/<code>max-height</code>, <code>box-sizing: border-box</code>, <code>margin: 0 auto</code>, nested <code>em</code>/<code>rem</code> sizes, basic floats, <code>position: relative</code>/<code>absolute</code>/<code>fixed</code>/<code>sticky</code>, <code>right</code>/<code>bottom</code>, <code>z-index</code>, <code>transform: translate</code>, flex rows with grow/wrap/justify/align, two-column grids, table colspan and percentage columns, <code>calc(percent ± px)</code> widths, <code>white-space: nowrap</code> and <code>@media</code> blocks evaluated at the browser canvas width when the page is parsed. Text is Latin-1; there is no shaping, bidi or <code>@font-face</code> yet.</li>
+			<li><strong>HTML/CSS subset.</strong> External and inline stylesheets, tag/class/id and descendant/child selectors, colours, backgrounds (solid and a stretched <code>background-image</code>), real <code>font-size</code> (8–48 px), the <code>font</code> shorthand, bold/italic faces, <code>font-family: monospace</code>, margins/padding (percentage sizes use the containing width; <code>em</code> uses the element font), per-side borders, alignment, explicit/percentage block widths with <code>min-width</code>/<code>max-width</code>/<code>min-height</code>/<code>max-height</code>, <code>box-sizing: border-box</code>, <code>margin: 0 auto</code>, nested <code>em</code>/<code>rem</code> sizes, basic floats, <code>position: relative</code>/<code>absolute</code>/<code>fixed</code>/<code>sticky</code>, <code>right</code>/<code>bottom</code>, <code>z-index</code>, <code>transform: translate</code>, flex rows with grow/wrap/justify/align, two-column grids, table rowspan/colspan and percentage columns, <code>calc(percent ± px)</code> widths, <code>white-space: nowrap</code> and <code>@media</code> blocks evaluated at the browser canvas width when the page is parsed. Text is Latin-1; there is no shaping, bidi or <code>@font-face</code> yet.</li>
 			<li><strong>JavaScript.</strong> Inline and external <code>&lt;script src&gt;</code> blocks run on the vendored MuJS engine in document order. External scripts use the same local/network fetch path as pages and CSS (512 KB cap) and are cached under <code>/share/bjs-*</code> for the current boot; the parsed page is painted before scripts run. Classic scripts are sloppy by default, and the engine carries Annex B fixes for regex literals after control parentheses and identity escapes/braces. Math, Date and JSON are the engine's; each page gets one state, and an 8M-instruction limit turns a runaway loop into a <code>script ran too long</code> error instead of freezing the browser.</li>
 			<li><strong>JavaScript.</strong> Inline and external <code>&lt;script src&gt;</code> blocks run on the vendored MuJS engine in document order. External scripts use the same local/network fetch path as pages and CSS (512 KB cap) and are cached under <code>/share/bjs-*</code> for the current boot; the parsed page is painted before scripts run. Classic scripts are sloppy by default, and the engine carries Annex B fixes for regex literals after control parentheses and identity escapes/braces. Math, Date and JSON are the engine's; each page gets one state, and an 8M-instruction limit turns a runaway loop into a <code>script ran too long</code> error instead of freezing the browser.</li>
-			<li><strong>DOM.</strong> Nodes are interned MuJS userdata wrappers, so <code>===</code> and library expando caches behave. Elements support <code>getAttribute</code>/<code>setAttribute</code>/<code>hasAttribute</code>, <code>appendChild</code>/<code>insertBefore</code>/<code>removeChild</code>/<code>replaceChild</code>/<code>cloneNode</code>/<code>remove</code>, <code>querySelector(All)</code>, <code>getElementsByTagName</code>, <code>getElementsByClassName</code>, <code>classList</code>, <code>dataset</code>, inline <code>style</code> (<code>cssText</code>, property get/set, <code>getPropertyValue</code>/<code>setProperty</code>/<code>removeProperty</code>), <code>innerHTML</code> (fragment parser), <code>textContent</code>/<code>nodeValue</code>, traversal and <code>matches</code>. <code>&lt;a&gt;</code> elements expose <code>href</code>/<code>protocol</code>/<code>host</code>/<code>hostname</code>/<code>port</code>/<code>pathname</code>/<code>search</code>/<code>hash</code>/<code>origin</code> resolved against the page URL. <code>document</code> has <code>createElement</code>, <code>createTextNode</code>, <code>createComment</code>, <code>createDocumentFragment</code>, <code>createAttribute</code>, <code>documentElement</code>/<code>body</code>/<code>head</code> and <code>readyState</code>; <code>navigator</code>, <code>history</code>, <code>matchMedia</code>, <code>getComputedStyle</code>, <code>setTimeout</code>/<code>setInterval</code>/<code>clearTimeout</code> and <code>requestAnimationFrame</code> exist, and due timers are pumped from the browser loop. jQuery 1.11, Bootstrap 3.2 tooltips and AngularJS 1.3 (with ngRoute/ngSanitize) initialize on a local test page.</li>
-			<li><strong>Not supported.</strong> Event dispatch (<code>addEventListener</code> is a no-op), forms, <code>type="module"</code>, <code>defer</code>/<code>async</code>, <code>MutationObserver</code>/<code>IntersectionObserver</code>, <code>XMLHttpRequest</code>/<code>fetch</code>, <code>DOMParser</code>, canvas/2D contexts and pseudo-class/element selectors. <code>getComputedStyle</code> is a stub; <code>outerHTML</code> only returns the string last assigned to <code>innerHTML</code>.</li>
-			<li><strong>CSS gaps.</strong> Table <code>rowspan</code>, text wrapping around floats and real <code>clear</code>, <code>@font-face</code>, percentage margins/padding (still resolved against the viewport), <code>background-repeat</code>/<code>position</code>/<code>size</code> (a background image is stretched to its box), the rest of <code>transform</code> (only <code>translate</code>), sticky beyond the top edge, pseudo-classes/elements, border-radius clipping of descendants, gradients/box-shadow/transitions and <code>color-mix</code>. Flex rows, card grids, basic tables, inline SVG and CSS variables are supported.</li>
+			<li><strong>DOM.</strong> Nodes are interned MuJS userdata wrappers, so <code>===</code> and library expando caches behave. Elements support <code>getAttribute</code>/<code>setAttribute</code>/<code>hasAttribute</code>, <code>appendChild</code>/<code>insertBefore</code>/<code>removeChild</code>/<code>replaceChild</code>/<code>cloneNode</code>/<code>remove</code>, <code>querySelector(All)</code>, <code>getElementsByTagName</code>, <code>getElementsByClassName</code>, <code>classList</code>, <code>dataset</code>, inline <code>style</code> (<code>cssText</code>, property get/set, <code>getPropertyValue</code>/<code>setProperty</code>/<code>removeProperty</code>), <code>innerHTML</code> (fragment parser), <code>textContent</code>/<code>nodeValue</code>, traversal, <code>matches</code> and <code>getBoundingClientRect</code> (real block/table/absolute/image boxes; inline text boxes are still approximate). Form controls expose live <code>value</code>/<code>checked</code>/<code>selectedIndex</code> properties backed by the browser's control state. <code>&lt;a&gt;</code> elements expose <code>href</code>/<code>protocol</code>/<code>host</code>/<code>hostname</code>/<code>port</code>/<code>pathname</code>/<code>search</code>/<code>hash</code>/<code>origin</code> resolved against the page URL. <code>document</code> has <code>createElement</code>, <code>createTextNode</code>, <code>createComment</code>, <code>createDocumentFragment</code>, <code>createAttribute</code>, <code>documentElement</code>/<code>body</code>/<code>head</code> and <code>readyState</code>; <code>navigator</code>, <code>history</code>, <code>matchMedia</code>, <code>getComputedStyle</code>, <code>setTimeout</code>/<code>setInterval</code>/<code>clearTimeout</code> and <code>requestAnimationFrame</code> exist, and due timers are pumped from the browser loop. jQuery 1.11, Bootstrap 3.2 tooltips and AngularJS 1.3 (with ngRoute/ngSanitize) initialize on a local test page.</li>
+			<li><strong>Not supported.</strong> Event dispatch (<code>addEventListener</code> is a no-op), POST forms and JS form events (<code>input</code>/<code>change</code>/<code>submit</code>), <code>type="module"</code>, <code>defer</code>/<code>async</code>, <code>MutationObserver</code>/<code>IntersectionObserver</code>, <code>XMLHttpRequest</code>/<code>fetch</code>, <code>DOMParser</code>, canvas/2D contexts and pseudo-class/element selectors. Basic controls render, focus, edit, toggle, select and submit a GET query (see the compatibility ladder); <code>getComputedStyle</code> is a stub and <code>outerHTML</code> only returns the string last assigned to <code>innerHTML</code>.</li>
+			<li><strong>CSS gaps.</strong> <code>@font-face</code>, <code>background-repeat</code>/<code>position</code>/<code>size</code> (a background image is stretched to its box), the rest of <code>transform</code> (only <code>translate</code>), sticky beyond the top edge, pseudo-classes/elements, border-radius clipping of descendants, gradients/box-shadow/transitions and <code>color-mix</code>. Flex rows, card grids, tables with rowspan/colspan, inline SVG and CSS variables are supported.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
 			<li><strong>Caps.</strong> 2 MB page body, 832 KB CSS, 6144 DOM nodes, 4608 rules, 16 images. Each image gets an <code>img_estimate()</code>-sized decode arena that is freed after the scaled bitmap is made; navigation calls <code>heap_reset()</code> and unmaps the page heap.</li>
 			<li><strong>Fetching.</strong> Each network fetch starts a one-shot helper process and a fresh TCP/TLS connection; images load one per loop within the viewport plus 160 px.</li>
 			<li><strong>Fetching.</strong> Each network fetch starts a one-shot helper process and a fresh TCP/TLS connection; images load one per loop within the viewport plus 160 px.</li>
 			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. Origin-level redirects (the common <code>http://host</code> → <code>https://host</code> → <code>https://www.host</code> chain) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
 			<li><strong>URL entry.</strong> A dotted name without a scheme that is not a known local file extension is treated as a hostname, so <code>cloudron.io</code> does not hit the local-file stat. Origin-level redirects (the common <code>http://host</code> → <code>https://host</code> → <code>https://www.host</code> chain) are cached per process and in <code>/share/borigins</code>; the first visit still follows them, later visits go straight to the canonical origin. Remote stylesheets are cached under <code>/share/bcss-*</code> for the current boot, so a reload or a fresh browser launch skips the CSS downloads.</li>
@@ -387,7 +387,7 @@
 			behaviour. Run <code>make compat-serve</code> and open
 			behaviour. Run <code>make compat-serve</code> and open
 			<code>http://&lt;host&gt;:8000/</code> on the board (or
 			<code>http://&lt;host&gt;:8000/</code> on the board (or
 			<code>http://10.0.2.2:8000/</code> in QEMU's user network). The
 			<code>http://10.0.2.2:8000/</code> in QEMU's user network). The
-			index records the current pass/fail status; pages 010–029 pass, while
+			index records the current pass/fail status; pages 010–035 and 030a pass, while
 			the README lists the remaining gaps.
 			the README lists the remaining gaps.
 		</p>
 		</p>
 	</section>
 	</section>

+ 48 - 4
js_browser.c

@@ -1147,13 +1147,41 @@ static int node_has(js_State *state, void *p, const char *name)
 	}
 	}
 	if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
 	if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
 	if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
 	if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
-	if (!strcmp(name, "value")) { push_attr(state, node, "value", 0); return 1; }
+	if (!strcmp(name, "value")) {
+		char buf[128];
+
+		if (host.form_value && host.form_value(node, buf, sizeof(buf)))
+			js_pushstring(state, buf);
+		else
+			push_attr(state, node, "value", 0);
+		return 1;
+	}
 	if (!strcmp(name, "type")) { push_attr(state, node, "type", 0); return 1; }
 	if (!strcmp(name, "type")) { push_attr(state, node, "type", 0); return 1; }
 	if (!strcmp(name, "name")) { push_attr(state, node, "name", 0); return 1; }
 	if (!strcmp(name, "name")) { push_attr(state, node, "name", 0); return 1; }
 	if (!strcmp(name, "alt")) { push_attr(state, node, "alt", 0); return 1; }
 	if (!strcmp(name, "alt")) { push_attr(state, node, "alt", 0); return 1; }
 	if (!strcmp(name, "title")) { push_attr(state, node, "title", 0); return 1; }
 	if (!strcmp(name, "title")) { push_attr(state, node, "title", 0); return 1; }
-	if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
-	    !strcmp(name, "selected")) {
+	if (!strcmp(name, "checked")) {
+		int c = host.form_checked ? host.form_checked(node) : -1;
+
+		js_pushboolean(state, c >= 0 ? c : node_attr(node, name) != 0);
+		return 1;
+	}
+	if (!strcmp(name, "selectedIndex")) {
+		int c = host.form_selected ? host.form_selected(node) : -1;
+
+		js_pushnumber(state, c >= 0 ? c : 0);
+		return 1;
+	}
+	if (!strcmp(name, "selected")) {
+		int c = host.form_selected ? host.form_selected(node) : -1;
+
+		if (c >= 0)
+			js_pushboolean(state, c != 0);
+		else
+			js_pushboolean(state, node_attr(node, name) != 0);
+		return 1;
+	}
+	if (!strcmp(name, "disabled")) {
 		js_pushboolean(state, node_attr(node, name) != 0);
 		js_pushboolean(state, node_attr(node, name) != 0);
 		return 1;
 		return 1;
 	}
 	}
@@ -1300,15 +1328,31 @@ static int node_put(js_State *state, void *p, const char *name)
 	    !strcmp(name, "name") || !strcmp(name, "alt") ||
 	    !strcmp(name, "name") || !strcmp(name, "alt") ||
 	    !strcmp(name, "title")) {
 	    !strcmp(name, "title")) {
 		const char *value = js_tostring(state, -1);
 		const char *value = js_tostring(state, -1);
+		if (!strcmp(name, "value") && host.form_set_value &&
+		    host.form_set_value(node, value))
+			return 1;
 		if (!strcmp(name, "className"))
 		if (!strcmp(name, "className"))
 			node_set_string(node, "class", value);
 			node_set_string(node, "class", value);
 		else
 		else
 			node_set_string(node, name, value);
 			node_set_string(node, name, value);
 		return 1;
 		return 1;
 	}
 	}
+	if (!strcmp(name, "selectedIndex")) {
+		if (host.form_set_selected)
+			host.form_set_selected(node, js_tointeger(state, -1));
+		return 1;
+	}
 	if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
 	if (!strcmp(name, "checked") || !strcmp(name, "disabled") ||
 	    !strcmp(name, "selected")) {
 	    !strcmp(name, "selected")) {
-		if (js_toboolean(state, -1))
+		int b = js_toboolean(state, -1);
+
+		if (!strcmp(name, "checked") && host.form_set_checked &&
+		    host.form_set_checked(node, b))
+			return 1;
+		if (!strcmp(name, "selected") && host.form_set_selected &&
+		    host.form_set_selected(node, b ? 1 : 0))
+			return 1;
+		if (b)
 			br_set_attr(D, node, name, "", 0);
 			br_set_attr(D, node, name, "", 0);
 		else
 		else
 			br_remove_attr(D, node, name);
 			br_remove_attr(D, node, name);

+ 7 - 0
js_browser.h

@@ -17,6 +17,13 @@ struct js_browser_host {
 	/* Viewport-relative border box for getBoundingClientRect(); returns 0
 	/* Viewport-relative border box for getBoundingClientRect(); returns 0
 	 * when the node has no laid-out box. */
 	 * when the node has no laid-out box. */
 	int (*rect)(unsigned int node, int *x, int *y, int *w, int *h);
 	int (*rect)(unsigned int node, int *x, int *y, int *w, int *h);
+	/* Live form state; return 1 when the node has a control state. */
+	int (*form_value)(unsigned int node, char *buf, int cap);
+	int (*form_checked)(unsigned int node);
+	int (*form_selected)(unsigned int node);
+	int (*form_set_value)(unsigned int node, const char *value);
+	int (*form_set_checked)(unsigned int node, int checked);
+	int (*form_set_selected)(unsigned int node, int index);
 	const char *page_url;   /* location.href for every script on the page */
 	const char *page_url;   /* location.href for every script on the page */
 	unsigned int width;     /* viewport size for window.inner* */
 	unsigned int width;     /* viewport size for window.inner* */
 	unsigned int height;
 	unsigned int height;

+ 16 - 0
testdata/browser/compat/032-float-wrap.html

@@ -0,0 +1,16 @@
+<!doctype html>
+<html><head><meta charset="utf-8"><title>032 float text wrap</title>
+<style>
+html,body{margin:0;padding:0;background:#fff;color:#202020;font-family:sans-serif;font-size:13px}
+body{padding:10px}h1{font-size:20px;margin:0 0 8px}p{margin:0 0 10px}
+.wrap{width:400px}.float{float:left;width:100px;height:100px;background:#cc3333}
+.text{font-size:13px;line-height:18px}.clear{clear:both;height:28px;background:#333;color:#fff;padding-left:4px}
+table{border-collapse:collapse}td{padding:0;vertical-align:top}
+</style></head><body>
+<h1>032 &middot; float text wrap and clear</h1>
+<p>Text must wrap to the right of the red float, then the dark bar clears both.</p>
+<div class="wrap">
+  <div class="float"></div>The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs. How vexingly quick daft zebras jump! The five boxing wizards jump quickly.
+  <div class="clear">cleared</div>
+</div>
+</body></html>

+ 29 - 0
testdata/browser/compat/035-form-js.html

@@ -0,0 +1,29 @@
+<!doctype html>
+<html><head><meta charset="utf-8"><title>035 form JS state</title>
+<style>
+html,body{margin:0;padding:0;background:#fff;color:#202020;font-family:sans-serif;font-size:13px}
+body{padding:10px}h1{font-size:20px;margin:0 0 8px}p{margin:0 0 10px}
+input,select{margin-right:8px;font-size:13px}#result{margin-top:12px;padding:8px;background:#ffffcc;color:#000;font-family:monospace;font-weight:bold}
+</style></head><body>
+<h1>035 &middot; form state from JavaScript</h1>
+<p>The script sets value/checked/selectedIndex and reads them back; the result must say PASS.</p>
+<form id="f">
+  <input id="i" type="text" name="q" value="hello">
+  <input id="c" type="checkbox" name="c">
+  <select id="s" name="s"><option value="a">One</option><option value="b">Two</option></select>
+</form>
+<div id="result">checking...</div>
+<script>
+setTimeout(function () {
+  var i = document.getElementById('i');
+  var c = document.getElementById('c');
+  var s = document.getElementById('s');
+  i.value = 'world';
+  c.checked = true;
+  s.selectedIndex = 1;
+  var ok = i.value === 'world' && c.checked === true && s.selectedIndex === 1;
+  document.getElementById('result').textContent = ok ? 'PASS: live form state' :
+    ('FAIL: i=' + i.value + ' c=' + c.checked + ' s=' + s.selectedIndex);
+}, 500);
+</script>
+</body></html>

+ 3 - 4
testdata/browser/compat/README

@@ -17,10 +17,9 @@ Serve this directory from the host and open it on the board:
   browser http://<host>:8000/            # board
   browser http://<host>:8000/            # board
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
 
 
-Status: 010-031, 033-034 and 030a pass.  Still missing from the ladder:
-float text wrap/clear, POST forms and JS form events, @font-face,
-background repeat/position/size, the rest of transform, sticky
-bottom/container clamping, and calc() beyond widths.
+Status: 010-035 and 030a pass.  Still missing from the ladder: POST forms
+and JS form events, @font-face, background repeat/position/size, the rest
+of transform, sticky bottom/container clamping, and calc() beyond widths.
 
 
 Rules for new pages:
 Rules for new pages:
   - one behaviour per page;
   - one behaviour per page;

+ 2 - 0
testdata/browser/compat/index.html

@@ -53,8 +53,10 @@ a { color: #0645ad; }
 <tr><td>030</td><td><a href="030-rowspan.html">table rowspan</a></td><td>rowspan=2 cell beside two rows</td><td class="pass">PASS</td></tr>
 <tr><td>030</td><td><a href="030-rowspan.html">table rowspan</a></td><td>rowspan=2 cell beside two rows</td><td class="pass">PASS</td></tr>
 <tr><td>030a</td><td><a href="030a-rect.html">getBoundingClientRect</a></td><td>block, absolute and image rectangles</td><td class="pass">PASS</td></tr>
 <tr><td>030a</td><td><a href="030a-rect.html">getBoundingClientRect</a></td><td>block, absolute and image rectangles</td><td class="pass">PASS</td></tr>
 <tr><td>031</td><td><a href="031-percent-padding.html">percentage padding</a></td><td>% padding/margins and em sizes</td><td class="pass">PASS</td></tr>
 <tr><td>031</td><td><a href="031-percent-padding.html">percentage padding</a></td><td>% padding/margins and em sizes</td><td class="pass">PASS</td></tr>
+<tr><td>032</td><td><a href="032-float-wrap.html">float wrap</a></td><td>text beside a float, clear: both</td><td class="pass">PASS</td></tr>
 <tr><td>033</td><td><a href="033-forms.html">form controls</a></td><td>text, password, checkbox, radio, select, textarea, buttons</td><td class="pass">PASS</td></tr>
 <tr><td>033</td><td><a href="033-forms.html">form controls</a></td><td>text, password, checkbox, radio, select, textarea, buttons</td><td class="pass">PASS</td></tr>
 <tr><td>034</td><td><a href="034-form-submit.html">form submit</a></td><td>GET submission and reset</td><td class="pass">PASS</td></tr>
 <tr><td>034</td><td><a href="034-form-submit.html">form submit</a></td><td>GET submission and reset</td><td class="pass">PASS</td></tr>
+<tr><td>035</td><td><a href="035-form-js.html">form JS state</a></td><td>live value / checked / selectedIndex</td><td class="pass">PASS</td></tr>
 </table>
 </table>
 </body>
 </body>
 </html>
 </html>