Browse Source

browser: real getBoundingClientRect boxes for inline spans

Inline elements and inline-blocks that flow in the text line had no
layout box, so getBoundingClientRect() reported width 0 (compat page 037
failed its own metrics check while rendering correctly).

Each line run now records its innermost inline box (lay.box_owner,
set by layout_inline_box()), and flush_line() unions the placed runs
into layout_boxes[], one line box tall per line, growing enclosing
spans too.  A pass/flush id distinguishes a re-layout from a span that
continues on the next line.  The box also makes the span a click
target.  037 now passes headless.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Johannes Zellner 2 ngày trước cách đây
mục cha
commit
4bc60ea887
3 tập tin đã thay đổi với 93 bổ sung và 2 xóa
  1. 4 1
      AGENTS.md
  2. 0 0
      docs/userland.html
  3. 89 1
      user/apps/browser/browser.c

+ 4 - 1
AGENTS.md

@@ -1853,7 +1853,10 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `setProperty`/`removeProperty`), `innerHTML` (fragment parser in
   `browser_html.c`), `textContent`/`nodeValue`, child/sibling/parent
   traversal, `matches` and `getBoundingClientRect` (real block/table/
-  absolute/image boxes; inline text boxes are still approximate).  `<a>` elements expose
+  absolute/image boxes; an inline span, or an inline-block that stays in
+  the text line, is the union of its placed line runs, built in
+  `flush_line()` from each run's owning box, one line-box tall per line;
+  the box also makes it a click target).  `<a>` elements expose
   the DOM URL components (`href`/`protocol`/`host`/`hostname`/`port`/
   `pathname`/`search`/`hash`/`origin`) resolved against the page URL.
   `document` exposes `createElement`/`createTextNode`/`createComment`/

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
docs/userland.html


+ 89 - 1
user/apps/browser/browser.c

@@ -162,6 +162,17 @@ struct br_layout_box {
 	unsigned char valid;
 };
 static struct br_layout_box layout_boxes[BR_MAX_NODES];
+/* Inline boxes are built from their line runs in flush_line(): a box is
+ * replaced when a new layout pass (or a re-layout at the same or an
+ * earlier line) reaches it, and grown when its runs continue on the same
+ * or a later line of the same pass.  inline_box_parent[] is the enclosing
+ * inline box (node + 1), so nested spans grow their ancestors too. */
+static unsigned int inline_box_pass_id;
+static unsigned int inline_box_flush_id;
+static unsigned int inline_box_pass[BR_MAX_NODES];
+static unsigned int inline_box_flush[BR_MAX_NODES];
+static int inline_box_y[BR_MAX_NODES];
+static unsigned short inline_box_parent[BR_MAX_NODES];
 
 /* Images are loaded one per main-loop iteration so the parsed page stays
  * interactive while a slow network/TLS fetch or 4K decode is in progress. */
@@ -267,6 +278,10 @@ struct lay_run {
 	unsigned short pb_bg;
 	short pb_img;
 	short pb_x, pb_y, pb_w, pb_h;
+	/* Innermost inline element (node + 1) whose box this run belongs
+	 * to, 0 = none; flush_line() unions the placed runs into
+	 * layout_boxes[] so getBoundingClientRect() sees inline boxes. */
+	unsigned short box;
 };
 
 struct lay {
@@ -288,6 +303,7 @@ struct lay {
 	int float_left, float_right, float_bottom;
 	struct lay_run runs[96];
 	int nruns;
+	unsigned short box_owner;  /* node + 1 of the open inline box */
 };
 
 struct hit {
@@ -6926,10 +6942,51 @@ static void start_line(struct lay *l, const struct br_style *st)
 	l->run_x = l->line_left;
 }
 
+static void inline_box_add(const struct lay *l, unsigned int owner, int x,
+			   int w)
+{
+	int y = l->line_y;
+	int h = l->line_h ? l->line_h : 16;
+	int depth = 0;
+
+	while (owner && owner <= BR_MAX_NODES && depth++ < 16) {
+		unsigned int node = owner - 1;
+		struct br_layout_box *b = &layout_boxes[node];
+
+		if (b->valid && inline_box_pass[node] == inline_box_pass_id &&
+		    (inline_box_flush[node] == inline_box_flush_id ||
+		     inline_box_y[node] < y)) {
+			int x1 = b->x + b->w, y1 = b->y + b->h;
+
+			if (x < b->x)
+				b->x = x;
+			if (y < b->y)
+				b->y = y;
+			if (x + w > x1)
+				x1 = x + w;
+			if (y + h > y1)
+				y1 = y + h;
+			b->w = x1 - b->x;
+			b->h = y1 - b->y;
+		} else {
+			b->x = x;
+			b->y = y;
+			b->w = w;
+			b->h = h;
+			b->valid = 1;
+		}
+		inline_box_pass[node] = inline_box_pass_id;
+		inline_box_flush[node] = inline_box_flush_id;
+		inline_box_y[node] = y;
+		owner = inline_box_parent[node];
+	}
+}
+
 static void flush_line(struct lay *l)
 {
 	int total, x, i, vis;
 
+	inline_box_flush_id++;
 	if (!l->nruns && l->line_x == l->line_left) {
 		l->line_h = 0;
 		return;
@@ -7232,6 +7289,8 @@ static void flush_line(struct lay *l)
 				}
 			}
 		}
+		if (r->box)
+			inline_box_add(l, r->box, x, w);
 		x += w;
 	}
 	l->y += l->line_h + 2;
@@ -7267,6 +7326,7 @@ static void add_run(struct lay *l, unsigned int off, unsigned short len,
 
 		if (gap < 0)
 			gap = 0;
+		r->box = l->box_owner;
 		r->gap = (unsigned short)gap;
 		r->off = off;
 		r->len = len;
@@ -7474,6 +7534,7 @@ static void add_image_run(struct lay *l, unsigned int node,
 
 		if (gap < 0)
 			gap = 0;
+		r->box = l->box_owner;
 		r->gap = (unsigned short)gap;
 		r->off = 0;
 		r->len = 0;
@@ -7564,6 +7625,7 @@ static void add_svg_run(struct lay *l, unsigned int node,
 	l->line_x += ml;
 	r = &l->runs[l->nruns++];
 	memset(r, 0, sizeof(*r));
+	r->box = l->box_owner;
 	r->gap = (unsigned short)(l->line_x > l->run_x ?
 				  l->line_x - l->run_x : 0);
 	r->off = node;
@@ -7964,6 +8026,7 @@ static void add_form_run(struct lay *l, unsigned int node,
 	}
 	l->line_x += ml;
 	r = &l->runs[l->nruns++];
+	r->box = l->box_owner;
 	r->gap = (unsigned short)(l->line_x - l->run_x);
 	r->off = r->len = 0;
 	r->color = st->color;
@@ -11858,6 +11921,7 @@ static void emit_pseudo_box(struct lay *l, const struct br_style *parent,
 		l->line_x += ml;
 	r = &l->runs[l->nruns++];
 	memset(r, 0, sizeof(*r));
+	r->box = l->box_owner;
 	r->gap = (unsigned short)(l->line_x > l->run_x ?
 				  l->line_x - l->run_x : 0);
 	r->img = RUN_PBOX;
@@ -12036,6 +12100,23 @@ static int layout_out_of_flow(struct lay *l, unsigned int n,
 	return 1;
 }
 
+/* An element laid out in the text line (an inline span, or an inline-block
+ * that flows like one): its runs are tagged with it, so flush_line() can
+ * record its box for getBoundingClientRect() and click targeting. */
+static void layout_inline_box(struct lay *l, unsigned int node,
+			      const struct br_style *cs, int depth,
+			      unsigned short link)
+{
+	unsigned short saved = l->box_owner;
+
+	if (node < BR_MAX_NODES) {
+		inline_box_parent[node] = saved;
+		l->box_owner = (unsigned short)(node + 1);
+	}
+	layout_children(l, node, cs, depth + 1, link);
+	l->box_owner = saved;
+}
+
 static void layout_children(struct lay *l, unsigned int parent,
 			    const struct br_style *pst, int depth,
 			    unsigned short link)
@@ -12135,7 +12216,7 @@ static void layout_children(struct lay *l, unsigned int parent,
 		}
 		if (cs.display == BR_DISPLAY_INLINE_BLOCK &&
 		    inline_block_flows(n, &cs, 0)) {
-			layout_children(l, n, &cs, depth + 1, lk);
+			layout_inline_box(l, n, &cs, depth, lk);
 			n = nd->next;
 			continue;
 		}
@@ -12158,6 +12239,9 @@ static void layout_children(struct lay *l, unsigned int parent,
 		    cs.display == BR_DISPLAY_FLEX ||
 		    br_tag_is_block(nd->tag))
 			layout_block(l, n, &cs, depth + 1, lk);
+		else if (cs.display == BR_DISPLAY_INLINE &&
+			 nd->tag != BR_TAG_TD && nd->tag != BR_TAG_TH)
+			layout_inline_box(l, n, &cs, depth, lk);
 		else {
 			layout_children(l, n, &cs, depth + 1, lk);
 			if (nd->tag == BR_TAG_TD || nd->tag == BR_TAG_TH)
@@ -12187,6 +12271,8 @@ static int measure_page(int left, int right)
 		layout_boxes[i].valid = 0;
 	}
 	memset(li_marker, 0, sizeof(li_marker));
+	inline_box_pass_id++;
+	l->box_owner = 0;
 	l->draw = 0;
 	l->ui = &ui;
 	l->left = left;
@@ -12246,6 +12332,8 @@ static void render_page(int left, int right, int y0)
 {
 	struct lay *l = &layout_state;
 	struct br_style base = base_style();
+	inline_box_pass_id++;
+	l->box_owner = 0;
 	l->draw = 1;
 	l->ui = &ui;
 	l->left = left;

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác