Quellcode durchsuchen

Fix browser script nodes, page height and table layout

Keep script/style elements in the DOM so analytics snippets can insert siblings, ignore html/body percentage heights when measuring the scroll range, honour legacy table width attributes and one-column tables, reset table-cell text alignment, and support a:link colours. Adds ladder 047 and 048.
Johannes Zellner vor 1 Woche
Ursprung
Commit
7f285e09ff

+ 37 - 2
browser.c

@@ -5197,6 +5197,20 @@ static void layout_block(struct lay *l, unsigned int node,
 			w = 0;
 		content_w = st->box_sizing ? w - deco_w : w;
 		definite_w = 1;
+	} else if ((doc.nodes[node].tag == BR_TAG_TABLE ||
+		    doc.nodes[node].tag == BR_TAG_TD ||
+		    doc.nodes[node].tag == BR_TAG_TH) &&
+		   doc.nodes[node].width_attr) {
+		/* Legacy table/cell width attributes: percentages resolve
+		 * against the containing block, bare numbers are pixels. */
+		int w = doc.nodes[node].width_pct ?
+			cbw * doc.nodes[node].width_attr / 100 :
+			doc.nodes[node].width_attr;
+
+		if (w < 0)
+			w = 0;
+		content_w = st->box_sizing ? w - deco_w : w;
+		definite_w = 1;
 	} else {
 		content_w = cbw - ml - mr - deco_w;
 	}
@@ -5451,11 +5465,18 @@ static void layout_block(struct lay *l, unsigned int node,
 			l->y = l->float_bottom;
 		l->float_active = 0;
 	}
-	if (st->height_kind) {
+	if (st->height_kind &&
+	    !((doc.nodes[node].tag == BR_TAG_HTML ||
+	       doc.nodes[node].tag == BR_TAG_BODY) &&
+	      st->height_kind == 2)) {
 		content_h = style_height_px(st, cbw);
 		if (st->box_sizing)
 			content_h -= deco_h;
 	} else {
+		/* Percentage heights on html/body resolve against the
+		 * viewport, but their content still defines the document
+		 * scroll height.  Treating them as auto avoids collapsing
+		 * the measured page to the containing block width. */
 		content_h = l->y - (top + bt + pt);
 	}
 	if (content_h < 0)
@@ -5470,7 +5491,10 @@ static void layout_block(struct lay *l, unsigned int node,
 		if (content_h + deco_h > max_box)
 			content_h = max_box - deco_h;
 	}
-	if (st->min_height_kind) {
+	if (st->min_height_kind &&
+	    !((doc.nodes[node].tag == BR_TAG_HTML ||
+	       doc.nodes[node].tag == BR_TAG_BODY) &&
+	      st->min_height_kind == 2)) {
 		int mh = st->min_height_kind == 3 ?
 			 cbw * st->min_height_calc_pct / 100 +
 			 st->min_height_calc_px :
@@ -6590,6 +6614,7 @@ static void layout_table_row(struct lay *l, unsigned int row,
 	if (cols < 2 || total <= 0) {
 		int c;
 
+		total = 0;
 		for (c = 0; c < ncell; c++) {
 			struct br_style cs;
 
@@ -6604,6 +6629,16 @@ static void layout_table_row(struct lay *l, unsigned int row,
 				raw[c] = 24;
 			total += raw[c];
 		}
+		/* A one-column table still has to fill its own width; the
+		 * intrinsic fallback above otherwise leaves the cell at its
+		 * text width (HN's outer table and nested story table). */
+		if (total > 0 && total < avail) {
+			int c;
+
+			for (c = 0; c < ncell; c++)
+				raw[c] = raw[c] * avail / total;
+			total = avail;
+		}
 	}
 	prefix[0] = row_left;
 	for (n = 0; n < (unsigned int)cols && n < 12; n++)

+ 74 - 5
browser_html.c

@@ -1744,7 +1744,8 @@ static int match_compound(struct br_doc *d, unsigned int node,
 	struct br_node *n = &d->nodes[node];
 	unsigned int i = 0;
 
-	while (i < len && sel[i] != '.' && sel[i] != '#' && sel[i] != '[')
+	while (i < len && sel[i] != '.' && sel[i] != '#' &&
+	       sel[i] != '[' && sel[i] != ':')
 		i++;
 	if (i > 0 && !(i == 1 && sel[0] == '*')) {
 		char tag[32];
@@ -1769,7 +1770,7 @@ static int match_compound(struct br_doc *d, unsigned int node,
 			i++;
 			start = i;
 			while (i < len && sel[i] != '.' && sel[i] != '#' &&
-			       sel[i] != '[' && sel[i] != ' ')
+			       sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
 				i++;
 			if (!class_contains(br_find_attr(d, node, "class"),
 					    sel + start, i - start))
@@ -1780,7 +1781,7 @@ static int match_compound(struct br_doc *d, unsigned int node,
 			i++;
 			start = i;
 			while (i < len && sel[i] != '.' && sel[i] != '#' &&
-			       sel[i] != '[' && sel[i] != ' ')
+			       sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
 				i++;
 			{
 				const char *id = br_find_attr(d, node, "id");
@@ -1835,6 +1836,23 @@ static int match_compound(struct br_doc *d, unsigned int node,
 						      sel + vs, ve - vs, op))
 					return 0;
 			}
+		} else if (sel[i] == ':') {
+			unsigned int start;
+
+			i++;
+			start = i;
+			while (i < len && sel[i] != '.' && sel[i] != '#' &&
+			       sel[i] != '[' && sel[i] != ':' && sel[i] != ' ')
+				i++;
+			/* Every link is treated as unvisited: there is no
+			 * history state, so a:link matches and a:visited does
+			 * not.  Other pseudo-classes (hover/focus/active) are
+			 * deliberately unmatched instead of applying their
+			 * styles permanently. */
+			if (i - start != 4 ||
+			    !ci_eq_n(sel + start, "link", 4) ||
+			    n->tag != BR_TAG_A || !n->href_off)
+				return 0;
 		} else {
 			return 0;
 		}
@@ -5657,6 +5675,13 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 							k += 5;
 							continue;
 						}
+						if (k + 5 <= e &&
+						    ci_eq_n(s + k, ":link", 5) &&
+						    (k + 5 == e ||
+						     !is_ident(s[k + 5]))) {
+							k += 5;
+							continue;
+						}
 						if (k + 5 <= e &&
 						    ci_eq_n(s + k, ":not(", 5)) {
 							unsigned int n = k + 5;
@@ -6026,6 +6051,14 @@ static int compound_match(struct br_doc *d, unsigned int node, const char *s,
 					return 0;
 				i += 5;
 				got = 1;
+			} else if (i + 5 <= len && ci_eq_n(s + i, ":link", 5) &&
+				   (i + 5 == len || !is_ident(s[i + 5]))) {
+				/* No history state: every link is unvisited. */
+				if (d->nodes[node].tag != BR_TAG_A ||
+				    !d->nodes[node].href_off)
+					return 0;
+				i += 5;
+				got = 1;
 			} else if (i + 5 <= len && ci_eq_n(s + i, ":not(", 5)) {
 				unsigned int st = i + 5, e = st;
 				int depth = 1;
@@ -6762,6 +6795,10 @@ void br_style_for(struct br_doc *d, unsigned int node,
 		break;
 	case BR_TAG_TD: case BR_TAG_TH:
 		out->pr = 12;
+		/* A cell establishes left-aligned content even inside a legacy
+		 * <center> (HN's outer page wrapper); author CSS/align below
+		 * can still override this. */
+		out->align = BR_ALIGN_LEFT;
 		break;
 	case BR_TAG_VIDEO:
 		out->display = BR_DISPLAY_BLOCK;
@@ -6770,16 +6807,26 @@ void br_style_for(struct br_doc *d, unsigned int node,
 		break;
 	}
 
-	/* Legacy bgcolor attributes (tables/cells/body) are presentational
-	 * hints; author CSS still wins because it is applied below. */
+	/* Legacy bgcolor/align attributes (tables/cells/body) are
+	 * presentational hints; author CSS still wins because it is applied
+	 * below. */
 	{
 		unsigned short bgc;
 		const char *bga = br_find_attr(d, node, "bgcolor");
+		const char *ala = br_find_attr(d, node, "align");
 
 		if (bga && parse_color(bga, s_len(bga), &bgc)) {
 			out->bg = bgc;
 			out->has_bg = 1;
 		}
+		if (ala) {
+			if (same_string(ala, "right"))
+				out->align = BR_ALIGN_RIGHT;
+			else if (same_string(ala, "center"))
+				out->align = BR_ALIGN_CENTER;
+			else if (same_string(ala, "left"))
+				out->align = BR_ALIGN_LEFT;
+		}
 	}
 
 	/* presentational <font color>: author CSS below still wins */
@@ -7175,6 +7222,7 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 			if (tag == BR_TAG_SCRIPT) {
 				int p = find_ci(html, len, "</script", 8, i);
 				unsigned int end = p >= 0 ? (unsigned int)p : len;
+				unsigned int idx;
 
 				if (script_is_javascript(html + attr_start,
 							 attr_end - attr_start)) {
@@ -7191,6 +7239,16 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 						d->scripts_truncated = 1;
 					}
 				}
+				/* Keep the element in the DOM even though its body is
+				 * executed separately: scripts (Piwik, analytics)
+				 * insert a sibling after getElementsByTagName('script')
+				 * [0], and the element must exist for that to work. */
+				idx = node_append(d, stack[sp], BR_NODE_ELEM,
+						  (unsigned char)tag);
+				if (idx)
+					parse_attrs(d, &d->nodes[idx],
+						    html + attr_start,
+						    attr_end - attr_start);
 				i = p >= 0 ? (unsigned int)p + 8 : len;
 				while (i < len && html[i] != '>')
 					i++;
@@ -7220,6 +7278,8 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 				int p = find_ci(html, len, "</style", 7, i);
 				unsigned int end = p >= 0 ? (unsigned int)p : len;
 				unsigned int cend = end;
+				unsigned int idx;
+
 				while (cend > i && is_space(html[cend - 1]))
 					cend--;
 				if (css_len + (cend - i) + 2 < BR_CSS_SIZE) {
@@ -7227,6 +7287,15 @@ int br_parse(struct br_doc *d, const char *html, unsigned int len)
 						css_store[css_len++] = html[k];
 					css_store[css_len++] = '\n';
 				}
+				/* The CSS is collected above, but keep a display:none
+				 * element so getElementsByTagName('style')/parentNode
+				 * behave like a real document. */
+				idx = node_append(d, stack[sp], BR_NODE_ELEM,
+						  (unsigned char)tag);
+				if (idx)
+					parse_attrs(d, &d->nodes[idx],
+						    html + attr_start,
+						    attr_end - attr_start);
 				i = p >= 0 ? (unsigned int)p + 7 : len;
 				while (i < len && html[i] != '>')
 					i++;

+ 56 - 0
testdata/browser/compat/047-script-dom-height.html

@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html lang="en" style="height: 100%">
+<head>
+<meta charset="utf-8">
+<title>047 script DOM and 100% height</title>
+<style>
+  html { height: 100%; }
+  body { min-height: 100%; margin: 0; background: #ffffff; color: #202020;
+         font-family: sans-serif; }
+  .spacer { height: 1700px; padding: 16px; }
+  footer { height: 150px; background: #204080; color: #ffffff;
+           font-size: 22px; padding: 14px; }
+  .ok { background: #c8f0c8; color: #000000; padding: 8px; font-size: 18px; }
+  .fail { background: #ffc8c8; color: #000000; padding: 8px; font-size: 18px; }
+</style>
+</head>
+<body>
+<div class="spacer">
+  <h1>047 &mdash; script DOM and 100% height</h1>
+  <p>This page is taller than the window. Press <b>End</b>: the blue footer
+     must appear. The old layout measured the page against its width, so End
+     stopped here instead of at the bottom.</p>
+  <div id="result" class="fail">script check pending</div>
+</div>
+<footer>
+  <div id="footer-text">SCROLL-FAIL</div>
+  <div id="script-result">SCRIPT-PENDING</div>
+</footer>
+<script>
+setTimeout(function () {
+  var scripts = document.getElementsByTagName('script');
+  var styles = document.getElementsByTagName('style');
+  var ok = scripts.length >= 1 && scripts[0] && scripts[0].parentNode;
+  ok = ok && styles.length >= 1 && styles[0] && styles[0].parentNode;
+
+  var r = document.getElementById('result');
+  if (r) {
+    r.className = ok ? 'ok' : 'fail';
+    r.textContent = ok ? 'SCRIPT-OK (' + scripts.length + ' script element)'
+                       : 'SCRIPT-FAIL';
+  }
+
+  var sr = document.getElementById('script-result');
+  if (sr) sr.textContent = ok ? 'SCRIPT-OK' : 'SCRIPT-FAIL';
+
+  var body = document.body.getBoundingClientRect();
+  var foot = document.querySelector('footer').getBoundingClientRect();
+  var ft = document.getElementById('footer-text');
+  if (ft) {
+    ft.textContent = 'SCROLL-OK body=' + Math.round(body.height) +
+                     ' footer=' + Math.round(foot.top);
+  }
+}, 300);
+</script>
+</body>
+</html>

+ 35 - 0
testdata/browser/compat/048-link-pseudo.html

@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>048 link pseudo-class</title>
+<style>
+  body { background: #ffffff; color: #202020; font-family: sans-serif;
+         margin: 12px; }
+  a:link { color: #000000; text-decoration: none; }
+  a:visited { color: #ff0000; }
+  a:hover { color: #00aa00; }
+</style>
+</head>
+<body>
+<h1>048 &mdash; a:link colour</h1>
+<p>Without pseudo-class support the engine uses the dark theme accent, so the
+   link below is blue; <code>a:link</code> must make it black.  The red
+   visited and green hover rules must not stick permanently.</p>
+<p><a id="link" href="#target">BLACK LINK TEXT</a></p>
+<div id="result" style="background: #ffc8c8; color: #000000; padding: 8px;
+     font-size: 18px;">checking...</div>
+<script>
+setTimeout(function () {
+  var a = document.getElementById('link');
+  var c = getComputedStyle(a);
+  var ok = c.color === 'rgb(0, 0, 0)';
+  var r = document.getElementById('result');
+  if (r) {
+    r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
+    r.textContent = (ok ? 'LINK-OK ' : 'LINK-FAIL ') + c.color;
+  }
+}, 300);
+</script>
+</body>
+</html>

+ 4 - 1
testdata/browser/compat/README

@@ -17,11 +17,14 @@ 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-046 and 030a pass.  036a needs the POST echo server:
+Status: 010-048 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
 
+047 is a two-part check: the top shows SCRIPT-OK, and after pressing End the
+blue footer must show SCROLL-OK (it proves the 100%-height page is scrollable).
+
 Still missing from the ladder: 3D/perspective transforms and transformed
 text/descendants.
 

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

@@ -69,6 +69,8 @@ a { color: #0645ad; }
 <tr><td>044</td><td><a href="044-overflow-hidden.html">overflow hidden</a></td><td>a block larger than its parent is clipped</td><td class="pass">PASS</td></tr>
 <tr><td>045</td><td><a href="045-bgcolor.html">legacy bgcolor</a></td><td>table/cell bgcolor matches CSS backgrounds</td><td class="pass">PASS</td></tr>
 <tr><td>046</td><td><a href="046-flex-footer-query.html">flex footer and queries</a></td><td>space-evenly columns, padded one-line button, query selectors</td><td class="pass">PASS</td></tr>
+<tr><td>047</td><td><a href="047-script-dom-height.html">script DOM and 100% height</a></td><td>script/style elements exist in the DOM; html/body 100% height still scrolls to the footer</td><td class="pass">PASS</td></tr>
+<tr><td>048</td><td><a href="048-link-pseudo.html">link pseudo-class</a></td><td><code>a:link</code> colour applies; visited/hover do not stick</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>