فهرست منبع

Add browser background shorthand position and size

Parse the position token(s) before the slash and the size token(s) after it, ignoring slashes inside url()/quotes, so shorthand backgrounds keep their centering and cover/contain scaling. Adds ladder 053.
Johannes Zellner 1 هفته پیش
والد
کامیت
5524709b2e
4فایلهای تغییر یافته به همراه145 افزوده شده و 1 حذف شده
  1. 99 0
      browser_html.c
  2. 44 0
      testdata/browser/compat/053-background-shorthand.html
  3. 1 1
      testdata/browser/compat/README
  4. 1 0
      testdata/browser/compat/index.html

+ 99 - 0
browser_html.c

@@ -3356,6 +3356,101 @@ static void add_bg_size(struct br_decl *out, unsigned int *nout,
 	(void)kh;
 }
 
+/* Parse the `position / size` part of the background shorthand.  The image
+ * URL may contain slashes, so only a slash outside url(...)/quotes counts. */
+static void add_bg_shorthand_extra(struct br_decl *out, unsigned int *nout,
+				   const char *v, unsigned int len,
+				   int important)
+{
+	unsigned int i, slash = 0, depth = 0, p, first, count;
+	char q = 0;
+	signed short nv;
+	int vert;
+	unsigned char kind;
+
+	for (i = 0; i < len; i++) {
+		char c = v[i];
+
+		if (q) {
+			if (c == q)
+				q = 0;
+			continue;
+		}
+		if (c == '\'' || c == '"') {
+			q = c;
+			continue;
+		}
+		if (c == '(') {
+			depth++;
+			continue;
+		}
+		if (c == ')') {
+			if (depth)
+				depth--;
+			continue;
+		}
+		if (c == '/' && !depth) {
+			slash = i;
+			break;
+		}
+	}
+	if (!slash)
+		return;
+
+	/* Up to two position tokens immediately before the slash. */
+	p = slash;
+	first = slash;
+	count = 0;
+	while (count < 2) {
+		unsigned int e;
+
+		while (p > 0 && is_space(v[p - 1]))
+			p--;
+		e = p;
+		while (p > 0 && !is_space(v[p - 1]))
+			p--;
+		if (e == p)
+			break;
+		if (bg_keyword(v + p, e - p, &nv, &vert) ||
+		    bg_number(v + p, e - p, &nv, &kind)) {
+			first = p;
+			count++;
+		} else {
+			break;
+		}
+	}
+	if (count)
+		add_bg_position(out, nout, v + first, slash - first,
+				important);
+
+	/* Up to two size tokens after the slash. */
+	p = slash + 1;
+	while (p < len && is_space(v[p]))
+		p++;
+	first = p;
+	count = 0;
+	while (count < 2) {
+		unsigned int ts = p;
+
+		while (p < len && !is_space(v[p]))
+			p++;
+		if (p == ts)
+			break;
+		if ((ts + 5 <= p && ci_eq_n(v + ts, "cover", 5)) ||
+		    (ts + 7 <= p && ci_eq_n(v + ts, "contain", 7)) ||
+		    (ts + 4 <= p && ci_eq_n(v + ts, "auto", 4)) ||
+		    bg_number(v + ts, p - ts, &nv, &kind)) {
+			count++;
+			while (p < len && is_space(v[p]))
+				p++;
+		} else {
+			break;
+		}
+	}
+	if (count)
+		add_bg_size(out, nout, v + first, p - first, important);
+}
+
 static int parse_fixed_100(const char *s, unsigned int len,
 			   unsigned int *pos, signed short *out)
 {
@@ -4404,6 +4499,10 @@ static void parse_decls(struct br_doc *d, const char *s,
 			if (is_bg) {
 				add_bg_image_decl(out, nout, s + vstart,
 						  vend - vstart, important);
+				add_bg_shorthand_extra(out, nout,
+						       s + vstart,
+						       vend - vstart,
+						       important);
 				if (contains_ci_len(s + vstart, vend - vstart,
 						    "no-repeat"))
 					decl_add(out, nout, PROP_BG_REPEAT, 1,

+ 44 - 0
testdata/browser/compat/053-background-shorthand.html

@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>053 background shorthand position and size</title>
+<style>
+  body { background: #ffffff; color: #202020; font-family: sans-serif;
+         margin: 12px; font-size: 18px; }
+  .tile { width: 220px; height: 130px; border: 2px solid #202020;
+          margin: 8px 0; }
+  #a { background: #ffffff url(TILE.PNG) center/cover no-repeat; }
+  #b { background: url(TILE.PNG) left top/50px 30px no-repeat; }
+  #c { background: #204080 url(TILE.PNG) no-repeat right bottom / 100% 50%; }
+</style>
+</head>
+<body>
+<h1>053 &mdash; background shorthand</h1>
+<p>The position before the slash and the size after it must both survive the
+   shorthand parser. The image is <code>TILE.PNG</code>.</p>
+<div id="a" class="tile"></div>
+<div id="b" class="tile"></div>
+<div id="c" class="tile"></div>
+<div id="result" style="background: #c8f0c8; color: #000000; padding: 8px;
+     font-size: 18px;">checking...</div>
+<script>
+setTimeout(function () {
+  function P(id) { return getComputedStyle(document.getElementById(id)).backgroundPosition; }
+  function S(id) { return getComputedStyle(document.getElementById(id)).backgroundSize; }
+  var a = P('a') + ' / ' + S('a');
+  var b = P('b') + ' / ' + S('b');
+  var c = P('c') + ' / ' + S('c');
+  var ok = a === '50% 50% / cover' &&
+           b === '0% 0% / 50px 30px' &&
+           c === '100% 100% / 100% 50%';
+  var r = document.getElementById('result');
+  if (r) {
+    r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
+    r.textContent = (ok ? 'BACKGROUND-OK ' : 'BACKGROUND-FAIL ') +
+                    'a[' + a + '] b[' + b + '] c[' + c + ']';
+  }
+}, 300);
+</script>
+</body>
+</html>

+ 1 - 1
testdata/browser/compat/README

@@ -17,7 +17,7 @@ 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-052 and 030a pass.  036a needs the POST echo server:
+Status: 010-053 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

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

@@ -75,6 +75,7 @@ a { color: #0645ad; }
 <tr><td>050</td><td><a href="050-font-shorthand.html">font shorthand</a></td><td>size with a family token, bold/italic and line-height</td><td class="pass">PASS</td></tr>
 <tr><td>051</td><td><a href="051-list-decoration.html">list styles and text decoration</a></td><td>disc/circle/square/alpha/roman markers, line-through and overline</td><td class="pass">PASS</td></tr>
 <tr><td>052</td><td><a href="052-border-styles.html">border styles</a></td><td>solid, dashed, dotted and per-side borders</td><td class="pass">PASS</td></tr>
+<tr><td>053</td><td><a href="053-background-shorthand.html">background shorthand</a></td><td>position before the slash and size after it</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>