Răsfoiți Sursa

Add browser calc margins, padding and box sizes

Johannes Zellner 1 săptămână în urmă
părinte
comite
e20b0b1ed4

+ 5 - 3
AGENTS.md

@@ -819,7 +819,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   alignment, margins, padding, per-side borders, `list-style: none`,
   `text-transform: uppercase`, `display`, explicit/percentage block
   `width`/`height` with `min`/`max` and `box-sizing: border-box`,
-  `margin: 0 auto`, `calc(percent ± px)`, `white-space: nowrap`,
+  `margin: 0 auto`, `calc(percent ± px)` in widths, margins/padding,
+  heights and min/max, `white-space: nowrap`,
   `position: relative`/`absolute`/`fixed`/`sticky`, `right`/`bottom`,
   `z-index`, `transform: translate`/`scale`/`rotate` with
   `transform-origin` (paint-time box and inline-image transforms), flex rows
@@ -830,7 +831,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `@font-face` loads one regular TTF per family from a local path or HTTP URL
   (the browser fetches remote faces before layout).  The compatibility
   ladder is `testdata/browser/compat/`
-  (`make compat-serve`); pages 010–040 and 030a pass (036a needs the
+  (`make compat-serve`); pages 010–041 and 030a pass (036a needs the
   POST echo server).  Inline
   `<svg>` subtrees and closed `<details>` are skipped (not renderable), and
   `<video>` gets a placeholder box.  The default page canvas follows
@@ -1564,7 +1565,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     bottom offsets and clamps to the containing block.  `prefers-color-scheme`
     media queries are ignored so the page's base palette is used.  Block
     `width`/`max-width`/`min-width`, content-box `height` with `min`/`max`,
-    `box-sizing: border-box`, `margin: 0 auto` and `calc(percent ± px)` work;
+    `box-sizing: border-box`, `margin: 0 auto` and `calc(percent ± px)`
+    work in widths, margins/padding, heights and min/max;
     percentage widths resolve against the containing block.  Float support
     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

+ 152 - 87
browser.c

@@ -2993,6 +2993,47 @@ static int css_mp_px(const struct br_style *st, int v, int unit, int base)
 	return v;
 }
 
+static int mp_px(const struct br_style *st, int side, int base)
+{
+	int raw;
+
+	switch (side) {
+	case 0: raw = st->mt; break;
+	case 1: raw = st->mr; break;
+	case 2: raw = st->mb; break;
+	case 3: raw = st->ml; break;
+	case 4: raw = st->pt; break;
+	case 5: raw = st->pr; break;
+	case 6: raw = st->pb; break;
+	default: raw = st->pl; break;
+	}
+	if (st->mp_unit[side] == 5)
+		return base * raw / 100 + st->mp_calc_px[side];
+	return css_mp_px(st, raw, st->mp_unit[side], base);
+}
+
+static int style_width_px(const struct br_style *st, int base)
+{
+	if (st->width_kind == 1)
+		return st->width;
+	if (st->width_kind == 2)
+		return base * (int)st->width / 100;
+	if (st->width_kind == 3)
+		return base * st->width_calc_pct / 100 + st->width_calc_px;
+	return 0;
+}
+
+static int style_height_px(const struct br_style *st, int base)
+{
+	if (st->height_kind == 1)
+		return st->height;
+	if (st->height_kind == 2)
+		return base * (int)st->height / 100;
+	if (st->height_kind == 3)
+		return base * st->height_calc_pct / 100 + st->height_calc_px;
+	return 0;
+}
+
 static int css_size(unsigned char kind, int val, int base)
 {
 	if (kind == 1)
@@ -3712,13 +3753,23 @@ static void add_image_run(struct lay *l, unsigned int node,
 
 		if (st->width_kind) {
 			css_w = 1;
-			dw = st->width_kind == 2 ? maxw * (int)st->width / 100 :
-						   (int)st->width;
+			if (st->width_kind == 3)
+				dw = maxw * st->width_calc_pct / 100 +
+				     st->width_calc_px;
+			else
+				dw = st->width_kind == 2 ?
+					maxw * (int)st->width / 100 :
+					(int)st->width;
 		}
 		if (st->height_kind) {
 			css_h = 1;
-			dh = st->height_kind == 2 ? maxw * (int)st->height / 100 :
-						    (int)st->height;
+			if (st->height_kind == 3)
+				dh = maxw * st->height_calc_pct / 100 +
+				     st->height_calc_px;
+			else
+				dh = st->height_kind == 2 ?
+					maxw * (int)st->height / 100 :
+					(int)st->height;
 		}
 		if (css_w && !css_h)
 			dh = ih ? ih * dw / iw : 0;
@@ -3726,7 +3777,10 @@ static void add_image_run(struct lay *l, unsigned int node,
 			dw = iw ? iw * dh / ih : 0;
 
 		if (st->max_width_kind) {
-			int mw = st->max_width_kind == 2 ?
+			int mw = st->max_width_kind == 3 ?
+				 maxw * st->max_width_calc_pct / 100 +
+				 st->max_width_calc_px :
+				 st->max_width_kind == 2 ?
 				 maxw * (int)st->max_width / 100 :
 				 (int)st->max_width;
 			if (mw > 0 && dw > mw) {
@@ -3780,8 +3834,7 @@ static void add_image_run(struct lay *l, unsigned int node,
 		/* Inline replaced elements honour their vertical margins; this
 		 * is how Bootstrap's negative logo margins centre the wordmark. */
 		{
-			int idy = css_mp_px(st, st->mt, st->mp_unit[0],
-					    l->right - l->left);
+			int idy = mp_px(st, 0, l->right - l->left);
 
 			if (idy < -2048)
 				idy = -2048;
@@ -3813,8 +3866,7 @@ static void add_image_run(struct lay *l, unsigned int node,
 	if (node < BR_MAX_NODES) {
 		layout_boxes[node].x = l->line_x;
 		layout_boxes[node].y = l->line_y +
-			css_mp_px(st, st->mt, st->mp_unit[0],
-				  l->right - l->left);
+			mp_px(st, 0, l->right - l->left);
 		layout_boxes[node].w = dw;
 		layout_boxes[node].h = dh;
 		layout_boxes[node].valid = 1;
@@ -4420,9 +4472,10 @@ static void layout_block(struct lay *l, unsigned int node,
 					  (st->left_set ? st->left : 0);
 			l->right = def_w >= 0 ? l->left + def_w :
 						page_rect.x + avail_w;
-			if (st->bottom_set && st->height_kind == 1)
+			if (st->bottom_set && st->height_kind)
 				l->y = page_rect.y + page_rect.h -
-				      st->bottom - st->height;
+				      st->bottom -
+				      style_height_px(st, page_rect.w);
 			else
 				l->y = page_rect.y +
 				      (st->top_set ? st->top : 0);
@@ -4437,10 +4490,10 @@ static void layout_block(struct lay *l, unsigned int node,
 						flow_right +
 						(st->left_set ? st->left : 0);
 			l->y = flow_y + (st->top_set ? st->top : 0);
-			if (st->bottom_set && st->height_kind == 1 &&
+			if (st->bottom_set && st->height_kind &&
 			    parent < BR_MAX_NODES && block_h[parent] > 0)
 				l->y = flow_y + block_h[parent] - st->bottom -
-				      st->height;
+				      style_height_px(st, old_right - old_left);
 		}
 		old_left = l->left;
 		old_right = l->right;
@@ -4470,18 +4523,18 @@ static void layout_block(struct lay *l, unsigned int node,
 	cbw = old_right - old_left;
 	if (cbw < 8)
 		cbw = 8;
-	l->y += css_mp_px(st, st->mt, st->mp_unit[0], cbw);
+	l->y += mp_px(st, 0, cbw);
 	top = l->y;
-	ml = css_mp_px(st, st->ml, st->mp_unit[3], cbw);
-	mr = css_mp_px(st, st->mr, st->mp_unit[1], cbw);
+	ml = mp_px(st, 3, cbw);
+	mr = mp_px(st, 1, cbw);
 	bl = st->border_w[3];
 	br = st->border_w[1];
-	pl = css_mp_px(st, st->pl, st->mp_unit[7], cbw);
-	pr = css_mp_px(st, st->pr, st->mp_unit[5], cbw);
+	pl = mp_px(st, 7, cbw);
+	pr = mp_px(st, 5, cbw);
 	bt = st->border_w[0];
 	bb = st->border_w[2];
-	pt = css_mp_px(st, st->pt, st->mp_unit[4], cbw);
-	pb = css_mp_px(st, st->pb, st->mp_unit[6], cbw);
+	pt = mp_px(st, 4, cbw);
+	pb = mp_px(st, 6, cbw);
 	deco_w = bl + br + pl + pr;
 	deco_h = bt + bb + pt + pb;
 
@@ -4503,14 +4556,20 @@ static void layout_block(struct lay *l, unsigned int node,
 	if (content_w < 0)
 		content_w = 0;
 	if (st->max_width_kind) {
-		int mw = css_size(st->max_width_kind, st->max_width, cbw);
+		int mw = st->max_width_kind == 3 ?
+			 cbw * st->max_width_calc_pct / 100 +
+			 st->max_width_calc_px :
+			 css_size(st->max_width_kind, st->max_width, cbw);
 		int max_box = st->box_sizing ? mw : mw + deco_w;
 
 		if (content_w + deco_w > max_box)
 			content_w = max_box - deco_w;
 	}
 	if (st->min_width_kind) {
-		int mw = css_size(st->min_width_kind, st->min_width, cbw);
+		int mw = st->min_width_kind == 3 ?
+			 cbw * st->min_width_calc_pct / 100 +
+			 st->min_width_calc_px :
+			 css_size(st->min_width_kind, st->min_width, cbw);
 		int min_box = st->box_sizing ? mw : mw + deco_w;
 
 		if (content_w + deco_w < min_box) {
@@ -4732,8 +4791,8 @@ static void layout_block(struct lay *l, unsigned int node,
 			l->y = l->float_bottom;
 		l->float_active = 0;
 	}
-	if (st->height_kind == 1) {
-		content_h = st->height;
+	if (st->height_kind) {
+		content_h = style_height_px(st, cbw);
 		if (st->box_sizing)
 			content_h -= deco_h;
 	} else {
@@ -4742,14 +4801,20 @@ static void layout_block(struct lay *l, unsigned int node,
 	if (content_h < 0)
 		content_h = 0;
 	if (st->max_height_kind) {
-		int mh = css_size(st->max_height_kind, st->max_height, cbw);
+		int mh = st->max_height_kind == 3 ?
+			 cbw * st->max_height_calc_pct / 100 +
+			 st->max_height_calc_px :
+			 css_size(st->max_height_kind, st->max_height, cbw);
 		int max_box = st->box_sizing ? mh : mh + deco_h;
 
 		if (content_h + deco_h > max_box)
 			content_h = max_box - deco_h;
 	}
 	if (st->min_height_kind) {
-		int mh = css_size(st->min_height_kind, st->min_height, cbw);
+		int mh = st->min_height_kind == 3 ?
+			 cbw * st->min_height_calc_pct / 100 +
+			 st->min_height_calc_px :
+			 css_size(st->min_height_kind, st->min_height, cbw);
 		int min_box = st->box_sizing ? mh : mh + deco_h;
 
 		if (content_h + deco_h < min_box)
@@ -4771,7 +4836,7 @@ static void layout_block(struct lay *l, unsigned int node,
 	l->left = old_left;
 	l->right = old_right;
 	l->align = old_align;
-	l->y += css_mp_px(st, st->mb, st->mp_unit[2], cbw);
+	l->y += mp_px(st, 2, cbw);
 	if (!l->draw) {
 		if (box_h > 65535)
 			box_h = 65535;
@@ -4875,16 +4940,18 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 					ih = (int)images[idx].img.h;
 				}
 			}
-			if (cs.width_kind == 1) {
-				w += cs.width;
-			} else if (cs.width_kind == 2) {
-				w += 64;
+			if (cs.width_kind) {
+				int ww = style_width_px(&cs, base);
+
+				w += ww > 0 ? ww : 64;
 			} else if (nd->width_attr) {
 				w += nd->width_attr;
 			} else if (nd->height_attr && iw && ih) {
 				w += iw * (int)nd->height_attr / ih;
-			} else if (cs.height_kind == 1 && iw && ih) {
-				w += iw * (int)cs.height / ih;
+			} else if (cs.height_kind && iw && ih) {
+				int hh = style_height_px(&cs, base);
+
+				w += hh > 0 ? iw * hh / ih : iw;
 			} else if (iw) {
 				w += iw;
 			} else {
@@ -4892,10 +4959,10 @@ static int intrinsic_width(struct ngui *ui, unsigned int node,
 			}
 			continue;
 		}
-		w += css_mp_px(&cs, cs.pl, cs.mp_unit[7], base) +
-		     css_mp_px(&cs, cs.pr, cs.mp_unit[5], base) +
-		     css_mp_px(&cs, cs.ml, cs.mp_unit[3], base) +
-		     css_mp_px(&cs, cs.mr, cs.mp_unit[1], base);
+		w += mp_px(&cs, 7, base) +
+		     mp_px(&cs, 5, base) +
+		     mp_px(&cs, 3, base) +
+		     mp_px(&cs, 1, base);
 		w += intrinsic_width(ui, c, &cs, depth + 1, base);
 		if (w > 4096)
 			break;
@@ -4948,18 +5015,18 @@ static unsigned int layout_float_group(struct lay *l, unsigned int first,
 		if (nd->tag == BR_TAG_A && nd->href_off)
 			lk = (unsigned short)n;
 		right = cs.float_left == 2;
-		if (cs.width_kind == 2)
-			colw = (row_right - row_left) * (int)cs.width / 100;
-		else if (cs.width_kind == 1)
+		if (cs.width_kind == 2 || cs.width_kind == 3) {
+			colw = style_width_px(&cs, row_right - row_left);
+		} else if (cs.width_kind == 1) {
 			colw = (int)cs.width;
-		else {
+		} else {
 			int base = row_right - row_left;
 
 			colw = intrinsic_width(l->ui, n, &cs, 0, base) +
-			       css_mp_px(&cs, cs.pl, cs.mp_unit[7], base) +
-			       css_mp_px(&cs, cs.pr, cs.mp_unit[5], base) +
-			       css_mp_px(&cs, cs.ml, cs.mp_unit[3], base) +
-			       css_mp_px(&cs, cs.mr, cs.mp_unit[1], base);
+			       mp_px(&cs, 7, base) +
+			       mp_px(&cs, 5, base) +
+			       mp_px(&cs, 3, base) +
+			       mp_px(&cs, 1, base);
 			if (colw < 8)
 				colw = 8;
 		}
@@ -5076,8 +5143,8 @@ static int flex_item_width(struct lay *l, unsigned int n,
 	(void)l;
 	int w;
 
-	if (cs->width_kind == 2)
-		w = avail * (int)cs->width / 100;
+	if (cs->width_kind == 2 || cs->width_kind == 3)
+		w = style_width_px(cs, avail);
 	else if (cs->width_kind == 1)
 		w = (int)cs->width;
 	else if (nd->tag == BR_TAG_IMG) {
@@ -5092,10 +5159,10 @@ static int flex_item_width(struct lay *l, unsigned int n,
 			w = 64;
 	} else {
 		w = intrinsic_width(l->ui, n, cs, 0, avail);
-		w += css_mp_px(cs, cs->pl, cs->mp_unit[7], avail);
-		w += css_mp_px(cs, cs->pr, cs->mp_unit[5], avail);
-		w += css_mp_px(cs, cs->ml, cs->mp_unit[3], avail);
-		w += css_mp_px(cs, cs->mr, cs->mp_unit[1], avail);
+		w += mp_px(cs, 7, avail);
+		w += mp_px(cs, 5, avail);
+		w += mp_px(cs, 3, avail);
+		w += mp_px(cs, 1, avail);
 	}
 	if (w < 24)
 		w = 24;
@@ -5304,8 +5371,12 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 			if (h > row_h)
 				row_h = h;
 		}
-		if (pst->height_kind == 1 && pst->height > row_h)
-			row_h = pst->height;
+		if (pst->height_kind) {
+			int hh = style_height_px(pst, avail);
+
+			if (hh > row_h)
+				row_h = hh;
+		}
 		for (k = 0; k < count; k++) {
 			struct br_style cs;
 			int node = (int)it[start + k].node;
@@ -5650,10 +5721,10 @@ static void table_columns(struct lay *l, unsigned int table,
 			if (span < 1)
 				break;
 			w = intrinsic_width(l->ui, c, &cs, 0, avail) +
-			    css_mp_px(&cs, cs.pl, cs.mp_unit[7], avail) +
-			    css_mp_px(&cs, cs.pr, cs.mp_unit[5], avail) +
-			    css_mp_px(&cs, cs.ml, cs.mp_unit[3], avail) +
-			    css_mp_px(&cs, cs.mr, cs.mp_unit[1], avail) +
+			    mp_px(&cs, 7, avail) +
+			    mp_px(&cs, 5, avail) +
+			    mp_px(&cs, 3, avail) +
+			    mp_px(&cs, 1, avail) +
 			    cs.border_w[0] + cs.border_w[1] +
 			    cs.border_w[2] + cs.border_w[3];
 			if (w < 24)
@@ -5692,14 +5763,10 @@ static void table_columns(struct lay *l, unsigned int table,
 				if (cs.display != BR_DISPLAY_NONE) {
 					w = intrinsic_width(l->ui, c, &cs,
 							    0, avail) +
-					    css_mp_px(&cs, cs.pl,
-						      cs.mp_unit[7], avail) +
-					    css_mp_px(&cs, cs.pr,
-						      cs.mp_unit[5], avail) +
-					    css_mp_px(&cs, cs.ml,
-						      cs.mp_unit[3], avail) +
-					    css_mp_px(&cs, cs.mr,
-						      cs.mp_unit[1], avail) +
+					    mp_px(&cs, 7, avail) +
+					    mp_px(&cs, 5, avail) +
+					    mp_px(&cs, 3, avail) +
+					    mp_px(&cs, 1, avail) +
 					    cs.border_w[0] + cs.border_w[1] +
 					    cs.border_w[2] + cs.border_w[3];
 					sum = 0;
@@ -5738,8 +5805,10 @@ static void table_columns(struct lay *l, unsigned int table,
 					if (cs.width_kind == 1) {
 						raw[col] = cs.width;
 						fixed[col] = 1;
-					} else if (cs.width_kind == 2) {
-						raw[col] = avail * cs.width / 100;
+					} else if (cs.width_kind == 2 ||
+						   cs.width_kind == 3) {
+						raw[col] = style_width_px(&cs,
+									 avail);
 						fixed[col] = 1;
 					}
 				}
@@ -5848,14 +5917,10 @@ static void layout_table_row(struct lay *l, unsigned int row,
 			br_style_for(&doc, cell[c], pst, &cs);
 			raw[c] = intrinsic_width(l->ui, cell[c], &cs, 0,
 						 avail) +
-				 css_mp_px(&cs, cs.pl, cs.mp_unit[7],
-					   avail) +
-				 css_mp_px(&cs, cs.pr, cs.mp_unit[5],
-					   avail) +
-				 css_mp_px(&cs, cs.ml, cs.mp_unit[3],
-					   avail) +
-				 css_mp_px(&cs, cs.mr, cs.mp_unit[1],
-					   avail);
+				 mp_px(&cs, 7, avail) +
+				 mp_px(&cs, 5, avail) +
+				 mp_px(&cs, 3, avail) +
+				 mp_px(&cs, 1, avail);
 			if (raw[c] < 24)
 				raw[c] = 24;
 			total += raw[c];
@@ -6056,10 +6121,10 @@ static void layout_table(struct lay *l, unsigned int table,
 		table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
 				 st, &cs);
 		w = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0, avail) +
-		    css_mp_px(&cs, cs.pl, cs.mp_unit[7], avail) +
-		    css_mp_px(&cs, cs.pr, cs.mp_unit[5], avail) +
-		    css_mp_px(&cs, cs.ml, cs.mp_unit[3], avail) +
-		    css_mp_px(&cs, cs.mr, cs.mp_unit[1], avail) +
+		    mp_px(&cs, 7, avail) +
+		    mp_px(&cs, 5, avail) +
+		    mp_px(&cs, 3, avail) +
+		    mp_px(&cs, 1, avail) +
 		    cs.border_w[0] + cs.border_w[1] +
 		    cs.border_w[2] + cs.border_w[3];
 		if (w < 24)
@@ -6082,10 +6147,10 @@ static void layout_table(struct lay *l, unsigned int table,
 		table_cell_style(rows[tbl_cells[i].row], tbl_cells[i].node,
 				 st, &cs);
 		w = intrinsic_width(l->ui, tbl_cells[i].node, &cs, 0, avail) +
-		    css_mp_px(&cs, cs.pl, cs.mp_unit[7], avail) +
-		    css_mp_px(&cs, cs.pr, cs.mp_unit[5], avail) +
-		    css_mp_px(&cs, cs.ml, cs.mp_unit[3], avail) +
-		    css_mp_px(&cs, cs.mr, cs.mp_unit[1], avail) +
+		    mp_px(&cs, 7, avail) +
+		    mp_px(&cs, 5, avail) +
+		    mp_px(&cs, 3, avail) +
+		    mp_px(&cs, 1, avail) +
 		    cs.border_w[0] + cs.border_w[1] +
 		    cs.border_w[2] + cs.border_w[3];
 		col = tbl_cells[i].col;
@@ -6109,8 +6174,8 @@ static void layout_table(struct lay *l, unsigned int table,
 		if (cs.width_kind == 1) {
 			raw[col] = cs.width;
 			fixed[col] = 1;
-		} else if (cs.width_kind == 2) {
-			raw[col] = avail * cs.width / 100;
+		} else if (cs.width_kind == 2 || cs.width_kind == 3) {
+			raw[col] = style_width_px(&cs, avail);
 			fixed[col] = 1;
 		}
 	}
@@ -6248,8 +6313,8 @@ static void layout_table(struct lay *l, unsigned int table,
 		cs.width_kind = 0;
 		cs.max_width_kind = 0;
 		cs.box_sizing = 0;
-		deco = css_mp_px(&cs, cs.pt, cs.mp_unit[4], avail) +
-		       css_mp_px(&cs, cs.pb, cs.mp_unit[6], avail) +
+		deco = mp_px(&cs, 4, avail) +
+		       mp_px(&cs, 6, avail) +
 		       cs.border_w[0] + cs.border_w[2];
 		cs.height_kind = 1;
 		cs.height = h > deco ? h - deco : 0;

+ 187 - 20
browser_html.c

@@ -2298,6 +2298,13 @@ enum {
 	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_MAXH, PROP_MAXH_KIND, PROP_LH_PCT, PROP_LH_PX, PROP_UPPER,
+	PROP_HEIGHT_CALC_PCT, PROP_HEIGHT_CALC_PX,
+	PROP_MINH_CALC_PCT, PROP_MINH_CALC_PX,
+	PROP_MAXH_CALC_PCT, PROP_MAXH_CALC_PX,
+	PROP_MINW_CALC_PCT, PROP_MINW_CALC_PX,
+	PROP_MAXW_CALC_PCT, PROP_MAXW_CALC_PX,
+	PROP_MT_CPX, PROP_MR_CPX, PROP_MB_CPX, PROP_ML_CPX,
+	PROP_PT_CPX, PROP_PR_CPX, PROP_PB_CPX, PROP_PL_CPX,
 	PROP_FLOAT, PROP_CLEAR, PROP_FLEX_GROW, PROP_FLEX_COL, PROP_FLEX_WRAP,
 	PROP_JUSTIFY, PROP_ALIGN_ITEMS,
 	PROP_GRID_NCOLS, PROP_GRID_GAP, PROP_GRID_SLOT, PROP_GRID_AUTO,
@@ -2682,6 +2689,28 @@ static int parse_margin_len(const char *s, unsigned int len,
 	return parse_css_len(s, len, pos, unit);
 }
 
+static int parse_calc(const char *s, unsigned int len, int *pct, int *px);
+
+/* calc(P% +/- Npx) for a margin/padding token; *pos advances past ')'. */
+static int parse_margin_calc(const char *s, unsigned int len,
+			     unsigned int *pos, int *pct, int *px)
+{
+	unsigned int i = *pos, j;
+
+	if (i + 5 > len || !ci_eq_n(s + i, "calc(", 5))
+		return 0;
+	i += 5;
+	j = i;
+	while (j < len && s[j] != ')')
+		j++;
+	if (j >= len)
+		return 0;
+	if (!parse_calc(s + i, j - i, pct, px))
+		return 0;
+	*pos = j + 1;
+	return 1;
+}
+
 /* calc(P% +/- Npx) for the common width/margin forms. */
 static int parse_calc(const char *s, unsigned int len, int *pct, int *px)
 {
@@ -4763,23 +4792,85 @@ static void parse_decls(struct br_doc *d, const char *s,
 			unsigned short v;
 			unsigned char kind;
 
-			if (str_eq(prop, "width") &&
-			    vend - vstart >= 5 &&
+			if (vend - vstart >= 5 &&
 			    ci_eq_n(s + vstart, "calc(", 5)) {
 				int pct, px;
 
 				if (parse_calc(s + vstart + 5,
 					       vend - vstart - 5, &pct, &px)) {
-					decl_add(out, nout, PROP_WIDTH, 0,
-						 important);
-					decl_add(out, nout, PROP_WIDTH_KIND, 3,
-						 important);
-					decl_add(out, nout,
-						 PROP_WIDTH_CALC_PCT, pct,
-						 important);
-					decl_add(out, nout,
-						 PROP_WIDTH_CALC_PX, px,
-						 important);
+					if (str_eq(prop, "width")) {
+						decl_add(out, nout, PROP_WIDTH,
+							 0, important);
+						decl_add(out, nout,
+							 PROP_WIDTH_KIND, 3,
+							 important);
+						decl_add(out, nout,
+							 PROP_WIDTH_CALC_PCT,
+							 pct, important);
+						decl_add(out, nout,
+							 PROP_WIDTH_CALC_PX,
+							 px, important);
+					} else if (str_eq(prop, "max-width")) {
+						decl_add(out, nout, PROP_MAXW,
+							 0, important);
+						decl_add(out, nout,
+							 PROP_MAXW_KIND, 3,
+							 important);
+						decl_add(out, nout,
+							 PROP_MAXW_CALC_PCT,
+							 pct, important);
+						decl_add(out, nout,
+							 PROP_MAXW_CALC_PX,
+							 px, important);
+					} else if (str_eq(prop, "min-width")) {
+						decl_add(out, nout, PROP_MINW,
+							 0, important);
+						decl_add(out, nout,
+							 PROP_MINW_KIND, 3,
+							 important);
+						decl_add(out, nout,
+							 PROP_MINW_CALC_PCT,
+							 pct, important);
+						decl_add(out, nout,
+							 PROP_MINW_CALC_PX,
+							 px, important);
+					} else if (str_eq(prop, "height")) {
+						decl_add(out, nout, PROP_HEIGHT,
+							 0, important);
+						decl_add(out, nout,
+							 PROP_HEIGHT_KIND, 3,
+							 important);
+						decl_add(out, nout,
+							 PROP_HEIGHT_CALC_PCT,
+							 pct, important);
+						decl_add(out, nout,
+							 PROP_HEIGHT_CALC_PX,
+							 px, important);
+					} else if (str_eq(prop, "max-height")) {
+						decl_add(out, nout, PROP_MAXH,
+							 0, important);
+						decl_add(out, nout,
+							 PROP_MAXH_KIND, 3,
+							 important);
+						decl_add(out, nout,
+							 PROP_MAXH_CALC_PCT,
+							 pct, important);
+						decl_add(out, nout,
+							 PROP_MAXH_CALC_PX,
+							 px, important);
+					} else {
+						decl_add(out, nout, PROP_MINH,
+							 0, important);
+						decl_add(out, nout,
+							 PROP_MINH_KIND, 3,
+							 important);
+						decl_add(out, nout,
+							 PROP_MINH_CALC_PCT,
+							 pct, important);
+						decl_add(out, nout,
+							 PROP_MINH_CALC_PX,
+							 px, important);
+					}
 				}
 				continue;
 			}
@@ -5100,16 +5191,22 @@ static void parse_decls(struct br_doc *d, const char *s,
 				}
 			}
 		} else if (str_eq(prop, "margin") || str_eq(prop, "padding")) {
-			int vals[4], units[4], nv = 0;
+			int vals[4], units[4], cpx[4] = { 0, 0, 0, 0 }, nv = 0;
 			unsigned int pos = 0;
 			int is_pad = prop[0] == 'p';
 			unsigned char base = is_pad ? PROP_PT : PROP_MT;
 
 			while (pos < vend - vstart && nv < 4) {
 				units[nv] = 0;
-				vals[nv] = parse_margin_len(s + vstart,
-							    vend - vstart,
-							    &pos, &units[nv]);
+				if (parse_margin_calc(s + vstart,
+						      vend - vstart, &pos,
+						      &vals[nv], &cpx[nv])) {
+					units[nv] = 5;
+				} else {
+					vals[nv] = parse_margin_len(
+						s + vstart, vend - vstart,
+						&pos, &units[nv]);
+				}
 				nv++;
 				while (pos < vend - vstart &&
 				       is_space(s[vstart + pos]))
@@ -5118,14 +5215,18 @@ static void parse_decls(struct br_doc *d, const char *s,
 			if (nv == 1) {
 				vals[1] = vals[2] = vals[3] = vals[0];
 				units[1] = units[2] = units[3] = units[0];
+				cpx[1] = cpx[2] = cpx[3] = cpx[0];
 			} else if (nv == 2) {
 				vals[2] = vals[0];
 				vals[3] = vals[1];
 				units[2] = units[0];
 				units[3] = units[1];
+				cpx[2] = cpx[0];
+				cpx[3] = cpx[1];
 			} else if (nv == 3) {
 				vals[3] = vals[1];
 				units[3] = units[1];
+				cpx[3] = cpx[1];
 			}
 			if (nv > 0) {
 				int k;
@@ -5147,6 +5248,12 @@ static void parse_decls(struct br_doc *d, const char *s,
 						 ((is_pad ? PROP_PT_U :
 							   PROP_MT_U) + k),
 						 u, important);
+					if (u == 5)
+						decl_add(out, nout,
+							 (unsigned char)
+							 ((is_pad ? PROP_PT_CPX :
+							   PROP_MT_CPX) + k),
+							 cpx[k], important);
 					if (!is_pad && (k == 1 || k == 3))
 						decl_add(out, nout,
 							 k == 1 ? PROP_MR_AUTO :
@@ -5164,11 +5271,19 @@ static void parse_decls(struct br_doc *d, const char *s,
 			   str_eq(prop, "padding-left")) {
 			unsigned int pos = 0;
 			int unit = 0;
-			int v = parse_margin_len(s + vstart, vend - vstart,
-						 &pos, &unit);
+			int v = 0, cpx = 0;
 			unsigned char p = PROP_MT;
 			const char *side;
-			int au = unit == 4;
+			int au;
+
+			if (parse_margin_calc(s + vstart, vend - vstart,
+					      &pos, &v, &cpx))
+				unit = 5;
+			else
+				v = parse_margin_len(s + vstart,
+						     vend - vstart, &pos,
+						     &unit);
+			au = unit == 4;
 
 			if (au)
 				v = 0;
@@ -5191,6 +5306,13 @@ static void parse_decls(struct br_doc *d, const char *s,
 						 (p - (prop[0] == 'p' ?
 						       PROP_PT : PROP_MT))),
 				 unit, important);
+			if (unit == 5)
+				decl_add(out, nout,
+					 (unsigned char)((prop[0] == 'p' ?
+						PROP_PT_CPX : PROP_MT_CPX) +
+						(p - (prop[0] == 'p' ?
+						      PROP_PT : PROP_MT))),
+					 cpx, important);
 			if (str_eq(side, "right"))
 				decl_add(out, nout, PROP_MR_AUTO, au, important);
 			else if (str_eq(side, "left"))
@@ -6224,6 +6346,14 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 		st->overflow_hidden = (unsigned char)(dcl->val != 0);
 		break;
 	case PROP_MT: st->mt = dcl->val; break;
+	case PROP_MT_CPX: st->mp_calc_px[0] = (signed short)dcl->val; break;
+	case PROP_MR_CPX: st->mp_calc_px[1] = (signed short)dcl->val; break;
+	case PROP_MB_CPX: st->mp_calc_px[2] = (signed short)dcl->val; break;
+	case PROP_ML_CPX: st->mp_calc_px[3] = (signed short)dcl->val; break;
+	case PROP_PT_CPX: st->mp_calc_px[4] = (signed short)dcl->val; break;
+	case PROP_PR_CPX: st->mp_calc_px[5] = (signed short)dcl->val; break;
+	case PROP_PB_CPX: st->mp_calc_px[6] = (signed short)dcl->val; break;
+	case PROP_PL_CPX: st->mp_calc_px[7] = (signed short)dcl->val; break;
 	case PROP_MR: st->mr = dcl->val; break;
 	case PROP_MB: st->mb = dcl->val; break;
 	case PROP_ML: st->ml = dcl->val; break;
@@ -6297,30 +6427,60 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_MAXW_KIND:
 		st->max_width_kind = (unsigned char)dcl->val;
 		break;
+	case PROP_MAXW_CALC_PCT:
+		st->max_width_calc_pct = (signed short)dcl->val;
+		break;
+	case PROP_MAXW_CALC_PX:
+		st->max_width_calc_px = (signed short)dcl->val;
+		break;
 	case PROP_MINW:
 		st->min_width = (unsigned short)dcl->val;
 		break;
 	case PROP_MINW_KIND:
 		st->min_width_kind = (unsigned char)dcl->val;
 		break;
+	case PROP_MINW_CALC_PCT:
+		st->min_width_calc_pct = (signed short)dcl->val;
+		break;
+	case PROP_MINW_CALC_PX:
+		st->min_width_calc_px = (signed short)dcl->val;
+		break;
 	case PROP_HEIGHT:
 		st->height = (unsigned short)dcl->val;
 		break;
 	case PROP_HEIGHT_KIND:
 		st->height_kind = (unsigned char)dcl->val;
 		break;
+	case PROP_HEIGHT_CALC_PCT:
+		st->height_calc_pct = (signed short)dcl->val;
+		break;
+	case PROP_HEIGHT_CALC_PX:
+		st->height_calc_px = (signed short)dcl->val;
+		break;
 	case PROP_MINH:
 		st->min_height = (unsigned short)dcl->val;
 		break;
 	case PROP_MINH_KIND:
 		st->min_height_kind = (unsigned char)dcl->val;
 		break;
+	case PROP_MINH_CALC_PCT:
+		st->min_height_calc_pct = (signed short)dcl->val;
+		break;
+	case PROP_MINH_CALC_PX:
+		st->min_height_calc_px = (signed short)dcl->val;
+		break;
 	case PROP_MAXH:
 		st->max_height = (unsigned short)dcl->val;
 		break;
 	case PROP_MAXH_KIND:
 		st->max_height_kind = (unsigned char)dcl->val;
 		break;
+	case PROP_MAXH_CALC_PCT:
+		st->max_height_calc_pct = (signed short)dcl->val;
+		break;
+	case PROP_MAXH_CALC_PX:
+		st->max_height_calc_px = (signed short)dcl->val;
+		break;
 	case PROP_LH_PCT:
 		st->line_h_pct = (unsigned char)dcl->val;
 		break;
@@ -6483,14 +6643,21 @@ void br_style_for(struct br_doc *d, unsigned int node,
 	out->width = out->max_width = out->min_width = out->height = 0;
 	out->min_height = out->max_height = 0;
 	out->width_calc_pct = out->width_calc_px = 0;
+	out->height_calc_pct = out->height_calc_px = 0;
+	out->min_height_calc_pct = out->min_height_calc_px = 0;
+	out->max_height_calc_pct = out->max_height_calc_px = 0;
+	out->min_width_calc_pct = out->min_width_calc_px = 0;
+	out->max_width_calc_pct = out->max_width_calc_px = 0;
 	out->width_kind = out->max_width_kind = out->min_width_kind = 0;
 	out->height_kind = out->min_height_kind = out->max_height_kind = 0;
 	out->box_sizing = 0;
 	out->ml_auto = out->mr_auto = 0;
 	out->mt = out->mr = out->mb = out->ml = 0;
 	out->pt = out->pr = out->pb = out->pl = 0;
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < 8; i++) {
 		out->mp_unit[i] = 0;
+		out->mp_calc_px[i] = 0;
+	}
 	out->flex_grow = out->flex_col = out->flex_wrap = 0;
 	out->justify = out->align_items = 0;
 	out->grid_ncols = out->grid_gap = out->grid_slot = 0;

+ 8 - 1
browser_html.h

@@ -148,18 +148,25 @@ struct br_style {
 	unsigned short min_width;
 	unsigned char height_kind;
 	unsigned short height;
+	signed short height_calc_pct, height_calc_px;
 	unsigned char min_height_kind;
 	unsigned short min_height;
+	signed short min_height_calc_pct, min_height_calc_px;
 	unsigned char max_height_kind;
 	unsigned short max_height;
+	signed short max_height_calc_pct, max_height_calc_px;
+	signed short min_width_calc_pct, min_width_calc_px;
+	signed short max_width_calc_pct, max_width_calc_px;
 	unsigned char box_sizing;   /* 0 content-box, 1 border-box */
 	unsigned char ml_auto, mr_auto;
 	unsigned char line_h_pct;   /* percent of font size, 0 = default */
 	unsigned short line_h_px;   /* absolute px, 0 = default */
 	signed short mt, mb, ml, mr;
 	signed short pt, pb, pl, pr;
-	/* mt, mr, mb, ml, pt, pr, pb, pl: 0 px, 1 %, 2 em, 3 rem, 4 auto */
+	/* mt, mr, mb, ml, pt, pr, pb, pl: 0 px, 1 %, 2 em, 3 rem, 4 auto,
+	 * 5 calc (the raw value is the percent, mp_calc_px the px part) */
 	unsigned char mp_unit[8];
+	signed short mp_calc_px[8];
 };
 
 struct br_node {

Fișier diff suprimat deoarece este prea mare
+ 1 - 1
docs/userland.html


+ 79 - 0
testdata/browser/compat/041-calc-boxes.html

@@ -0,0 +1,79 @@
+<!doctype html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>041 calc</title>
+<style>
+html, body { margin: 0; padding: 0; background: #ffffff; color: #202020; }
+body { font-family: sans-serif; font-size: 13px; padding: 10px; }
+h1 { font-size: 20px; margin: 0 0 6px 0; }
+p { margin: 0 0 10px 0; }
+.pane { float: left; width: 440px; margin-left: 10px; }
+.label { background: #eeeeee; padding: 4px 6px; margin-bottom: 4px; }
+.stage { width: 300px; background: #f4f4f4; border: 1px solid #999999; padding: 6px; }
+.box { background: #cc3333; color: #ffffff; margin-bottom: 8px; }
+.box1 { margin-left: calc(10% + 8px); padding: calc(5% + 4px);
+        height: calc(50% + 20px); }
+.ref1 { margin-left: 38px; padding: 19px; height: 170px; }
+.box2 { height: 10px; min-height: calc(20% + 10px); }
+.ref2 { height: 70px; }
+.box3 { max-width: calc(50% + 20px); height: 24px; }
+.ref3 { width: 170px; height: 24px; }
+.box4 { width: 50px; min-width: calc(30% + 10px); height: 24px; }
+.ref4 { width: 100px; height: 24px; }
+.box5 { height: 200px; max-height: calc(10% + 20px); }
+.ref5 { height: 50px; }
+#result { clear: both; margin-top: 14px; padding: 8px; background: #eeeeee;
+          color: #000000; font-family: monospace; font-weight: bold; }
+</style>
+</head>
+<body>
+<h1>041 &middot; calc in box metrics</h1>
+<p>Each A box must have the same box rectangle as its fixed-pixel B reference.</p>
+<div class="pane">
+  <div class="label">A &middot; calc()</div>
+  <div class="stage" id="sa">
+    <div class="box box1" id="a1">margin / padding / height</div>
+    <div class="box box2" id="a2"></div>
+    <div class="box box3" id="a3"></div>
+    <div class="box box4" id="a4"></div>
+    <div class="box box5" id="a5"></div>
+  </div>
+</div>
+<div class="pane">
+  <div class="label">B &middot; reference</div>
+  <div class="stage" id="sb">
+    <div class="box ref1" id="b1">margin / padding / height</div>
+    <div class="box ref2" id="b2"></div>
+    <div class="box ref3" id="b3"></div>
+    <div class="box ref4" id="b4"></div>
+    <div class="box ref5" id="b5"></div>
+  </div>
+</div>
+<div id="result">checking...</div>
+<script>
+setTimeout(function () {
+  var sa = document.getElementById('sa').getBoundingClientRect();
+  var sb = document.getElementById('sb').getBoundingClientRect();
+  var ok = true, details = [];
+  for (var i = 1; i <= 5; i++) {
+    var a = document.getElementById('a' + i).getBoundingClientRect();
+    var b = document.getElementById('b' + i).getBoundingClientRect();
+    var aw = Math.round(a.width), bw = Math.round(b.width);
+    var ah = Math.round(a.height), bh = Math.round(b.height);
+    var ax = Math.round(a.left - sa.left), bx = Math.round(b.left - sb.left);
+    if (Math.abs(aw - bw) > 1 || Math.abs(ah - bh) > 1 ||
+        Math.abs(ax - bx) > 1) {
+      ok = false;
+      details.push(i + ': A=' + aw + 'x' + ah + '@' + ax +
+                   ' B=' + bw + 'x' + bh + '@' + bx);
+    }
+  }
+  var out = document.getElementById('result');
+  out.textContent = ok ? 'PASS: calc margins, padding and box sizes' :
+    ('FAIL: ' + details.join('; '));
+  out.style.background = ok ? '#ccffcc' : '#ffcccc';
+}, 600);
+</script>
+</body>
+</html>

+ 3 - 3
testdata/browser/compat/README

@@ -17,13 +17,13 @@ Serve this directory from the host and open it on the board:
   browser http://<host>:8000/            # board
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
 
-Status: 010-040 and 030a pass.  036a needs the POST echo server:
+Status: 010-041 and 030a pass.  036a needs the POST echo server:
 
   python3 testdata/browser/compat/post_server.py
   browser /floppy1/036a.htm      # action is http://10.0.2.2:8765/echo
 
-Still missing from the ladder: 3D/perspective transforms, transformed
-text/descendants, and calc() beyond widths.
+Still missing from the ladder: 3D/perspective transforms and transformed
+text/descendants.
 
 Rules for new pages:
   - one behaviour per page;

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

@@ -63,6 +63,7 @@ a { color: #0645ad; }
 <tr><td>038</td><td><a href="038-background-props.html">background props</a></td><td>repeat, position, contain/cover and the shorthand URL</td><td class="pass">PASS</td></tr>
 <tr><td>039</td><td><a href="039-transform-scale-rotate.html">transform</a></td><td>scale, rotate, transform-origin on images and boxes</td><td class="pass">PASS</td></tr>
 <tr><td>040</td><td><a href="040-sticky-bottom.html">sticky edges</a></td><td>sticky top/bottom clamped to the containing block</td><td class="pass">PASS</td></tr>
+<tr><td>041</td><td><a href="041-calc-boxes.html">calc boxes</a></td><td>calc() in margins/padding, heights and min/max</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff