From ee4c37b51bb32465ed3d608970e5918d493b370d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Kope=C4=87?= Date: Sun, 4 Oct 2026 13:31:18 +0200 Subject: [PATCH] chrome: serve Web Awesome under a version-stamped path, fix the brand ramp Three fixes found by driving a real page, not by reading the code. Its autoloader works out where to fetch components from by finding the ` cannot close + the tag it is embedded in. Invisible to JSON.parse. + """ + locale = self.translations.locale_for(request) + payload = self.translations.subset(keys, locale) + return json.dumps(payload, ensure_ascii=False).replace("<", "\\u003c") + # --- rendering ---------------------------------------------------------- def context(self, request: Request, **extra: object) -> dict[str, object]: diff --git a/chrome/static/tokens.css b/chrome/static/tokens.css index c4ad4cd..1eda7b8 100644 --- a/chrome/static/tokens.css +++ b/chrome/static/tokens.css @@ -27,6 +27,23 @@ --wa-color-brand-10: var(--wa-color-yellow-10); --wa-color-brand-05: var(--wa-color-yellow-05); + /* The "loud" brand surface - solid buttons, the active toggle, focus. + * + * Overridden rather than taken from the ramp. Web Awesome picks step 50 + * for a loud fill because that is the darkest point that still carries + * white text, which for yellow is #b45f04 - a brown. The services have + * always used bright yellow with DARK text on it, so the pair is set + * explicitly: step 80 for the fill, near-black for what sits on it. + * + * These rules are deliberately unlayered. Web Awesome's own theme lives + * in @layer wa-theme, and unlayered declarations beat layered ones, so + * this wins without !important and without raising specificity. + */ + --wa-color-brand-fill-loud: var(--wa-color-yellow-80); + --wa-color-brand-on-loud: var(--wa-color-neutral-10); + --wa-color-brand-border-loud: var(--wa-color-yellow-70); + --wa-color-focus: var(--wa-color-yellow-70); + /* Corner radius: the services settled on 14px long before WA arrived. */ --wa-border-radius-m: 10px; --wa-border-radius-l: 14px; diff --git a/chrome/templates/base.html b/chrome/templates/base.html index 03055b3..9f17274 100644 --- a/chrome/templates/base.html +++ b/chrome/templates/base.html @@ -16,7 +16,7 @@ import list to keep in sync with the markup. #} - + @@ -37,12 +37,28 @@ })(); - + {% block head %}{% endblock %} - + {# + Preact, hooks and htm are vendored here rather than per service, and + mapped so a page can `import { h } from "preact"`. hooks.module.js + carries a bare "preact" specifier internally, so the map is required, + not a convenience. See D-0016 for why there is no bundler. + #} + + + diff --git a/tests/test_chrome.py b/tests/test_chrome.py index 8de1806..ce57a8f 100644 --- a/tests/test_chrome.py +++ b/tests/test_chrome.py @@ -58,7 +58,7 @@ def test_page_extends_the_shell(client): html = client.get("/").text assert '

hello

' in html # the service's content assert "Test Service" in html # the shell's header - assert "wa/webawesome.loader.js" in html # Web Awesome is wired up + assert "webawesome.loader.js" in html # Web Awesome is wired up assert 'data-theme="system"' in html @@ -185,3 +185,57 @@ def test_unauthenticated_mode_must_be_asked_for_explicitly(): def test_webawesome_is_vendored(): assert WEBAWESOME_VERSION.count(".") == 2 + + +def test_preact_is_mapped_for_islands(client): + html = client.get("/").text + assert '"preact":' in html and "vendor/preact.module.js" in html + assert '"preact/hooks":' in html, "hooks has a bare 'preact' import; the map is required" + assert client.get("/chrome/vendor/preact.module.js").status_code == 200 + + +def test_strings_json_cannot_break_out_of_its_script_tag(tmp_path): + locales = tmp_path / "locales" + locales.mkdir() + (locales / "pl.json").write_text('{"evil": ""}', "utf-8") + app = FastAPI() + ui = Chrome(app, service="t", version="1", title="T", locales_dir=locales) + + class R: + cookies = {"lang": "pl"} + headers = {} + + blob = ui.strings_json(R(), ("evil",)) + assert "" not in blob + import json as _json + + assert _json.loads(blob)["evil"] == "" + + +def test_webawesome_loader_url_has_no_query_string(client): + """Its autoloader finds its own base path with src.endsWith(...). + + A `?v=` here makes every component 404 against the site root, which is + silent in the server log of a page that merely looks unstyled. + """ + import re + + html = client.get("/").text + src = re.search(r'src="([^"]*webawesome\.loader\.js[^"]*)"', html).group(1) + assert "?" not in src + assert client.get(src).status_code == 200 + + +def test_webawesome_path_carries_its_version(client): + from chrome import WEBAWESOME_VERSION + + html = client.get("/").text + assert f"/chrome/wa-{WEBAWESOME_VERSION}/" in html + assert f'data-webawesome="/chrome/wa-{WEBAWESOME_VERSION}/"' in html + + +def test_a_component_resolves_under_the_versioned_path(client): + from chrome import WEBAWESOME_VERSION + + r = client.get(f"/chrome/wa-{WEBAWESOME_VERSION}/components/button/button.js") + assert r.status_code == 200