diff --git a/doc/design/ext-web-go-rewrite.md b/doc/design/ext-web-go-rewrite.md index 6f1a2d4..028fa63 100644 --- a/doc/design/ext-web-go-rewrite.md +++ b/doc/design/ext-web-go-rewrite.md @@ -396,7 +396,7 @@ scripts/build-web-plugin.sh # go build -buildmode=c-shared -o …/libweb.so | **W-G8** | WS echo/broadcast + access_log | ✅ `web-ws-broadcast-*` | | **W-G9** | Postgres + Redis cache + file/S3 storage | ✅ `web-drivers-smoke` | | **W-G10** | proxy + invoke + nested plugin | ✅ `web-proxy-invoke` / hosting live | -| **W-G11** | 定制 C2–C4 shell/layout/style/nav | `web-c2`–`c4` | +| **W-G11** | 定制 C2–C4 shell/layout/style/nav | ✅ `web-c2`–`c4` | | **W-G12** | G 增强 WS rooms + Redis session/pubsub + API key 原语 | 单测 + anlian chat 切片 | | **W-G13** | 默认切换 Go `libweb`;归档 Rust;更新发版与 ext CLI | 全量 `tests/ext/web-*` 绿 | | **W-G14** | anlian Marqdo 重写主路径(可分仓或 `examples/anlian-mq/`) | §8 矩阵主路径可演示 | diff --git a/plugins/web/internal/render/render.go b/plugins/web/internal/render/render.go index 6b075c4..f1c3c0c 100644 --- a/plugins/web/internal/render/render.go +++ b/plugins/web/internal/render/render.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/marqdo/marqdo/plugins/web/internal/assets" "github.com/marqdo/marqdo/plugins/web/internal/db" "github.com/marqdo/marqdo/plugins/web/internal/table" ) @@ -560,6 +561,16 @@ func headHTML(page map[string]any, defaultTitle string) string { } } } + if head, ok := page["head"]; ok { + links := assets.HeadLinksFromJSON(head) + assetVersion := "" + if v, ok := page["asset_version"].(string); ok { + assetVersion = v + } else if v, ok := page["资源版本"].(string); ok { + assetVersion = v + } + s.WriteString(assets.RenderHeadLinksWithVersion(links, assetVersion)) + } return s.String() } diff --git a/plugins/web/internal/render/render_test.go b/plugins/web/internal/render/render_test.go index b87ecc7..61fbc85 100644 --- a/plugins/web/internal/render/render_test.go +++ b/plugins/web/internal/render/render_test.go @@ -29,6 +29,183 @@ func TestRenderPageIntroNoDB(t *testing.T) { } } +func TestShellOffStackedOmitsSidebarGrid(t *testing.T) { + page := map[string]any{ + "title": "t", + "shell_css": "off", + "layout": "stacked", + "sidebar": []any{ + map[string]any{"label": "A", "href": "/a"}, + }, + } + html := render.RenderPage(page, "", "") + if !strings.Contains(html, "layout-stacked") { + t.Fatalf("missing layout-stacked: %s", html) + } + if strings.Contains(html, "has-sidebar") { + t.Fatalf("unexpected has-sidebar: %s", html) + } + if strings.Contains(html, "grid-template-columns:14rem") { + t.Fatalf("unexpected sidebar grid: %s", html) + } +} + +func TestShellMinimalKeepsVarsNotCards(t *testing.T) { + page := map[string]any{"title": "t", "shell_css": "minimal"} + html := render.RenderPage(page, "", "") + if !strings.Contains(html, "--ink:") { + t.Fatalf("missing vars: %s", html) + } + if strings.Contains(html, ".content.cards article") { + t.Fatalf("unexpected cards css: %s", html) + } +} + +func TestLayoutBareSkipsAside(t *testing.T) { + page := map[string]any{ + "title": "t", + "layout": "bare", + "sidebar": []any{ + map[string]any{"label": "A", "href": "/a"}, + }, + "nav": []any{ + map[string]any{"label": "Home", "href": "/"}, + }, + } + html := render.RenderPage(page, "", "") + if !strings.Contains(html, "layout-bare") { + t.Fatalf("missing layout-bare: %s", html) + } + if strings.Contains(html, "Home<") { + t.Fatalf("missing Home: %s", html) + } + if strings.Contains(html, "/x") { + t.Fatalf("hide leaked: %s", html) + } +} + +func TestNavWhenAuthGuestRespectsLoggedIn(t *testing.T) { + nav := []any{ + map[string]any{"front": "Public", "back": "/"}, + map[string]any{"front": "Admin", "back": "/admin", "when": "auth"}, + map[string]any{"front": "Login", "back": "/login", "when": "guest"}, + } + guest := map[string]any{ + "title": "t", + "shell_css": "off", + "_logged_in": false, + "nav": nav, + } + g := render.RenderPage(guest, "", "") + if !strings.Contains(g, "/login") { + t.Fatalf("guest missing login: %s", g) + } + if strings.Contains(g, "/admin") { + t.Fatalf("guest leaked admin: %s", g) + } + user := map[string]any{ + "title": "t", + "shell_css": "off", + "_logged_in": true, + "_nav_user": "alice", + "nav": nav, + } + u := render.RenderPage(user, "", "") + if !strings.Contains(u, "/admin") { + t.Fatalf("user missing admin: %s", u) + } + if strings.Contains(u, "/login") { + t.Fatalf("user leaked login: %s", u) + } +} + +func TestNavMediaEmitsClassAndCSS(t *testing.T) { + page := map[string]any{ + "title": "t", + "shell_css": "off", + "nav": []any{ + map[string]any{"label": "Wide", "href": "/w", "media": "(min-width: 900px)"}, + map[string]any{"label": "Home", "href": "/"}, + }, + } + html := render.RenderPage(page, "", "") + if !strings.Contains(html, "nav-mq-0") { + t.Fatalf("missing nav-mq-0: %s", html) + } + if !strings.Contains(html, "@media not (min-width: 900px)") { + t.Fatalf("missing media css: %s", html) + } +} + +func TestHeadDeferVersionAndAssetVersion(t *testing.T) { + page := map[string]any{ + "title": "t", + "asset_version": "appv", + "head": []any{ + map[string]any{"rel": "script", "href": "/static/a.js", "defer": true, "version": "3"}, + map[string]any{"rel": "stylesheet", "href": "/static/t.css", "version": "1"}, + map[string]any{"rel": "script", "href": "/static/b.js", "defer": true}, + }, + } + html := render.RenderPage(page, "", "") + if !strings.Contains(html, `a.js?v=3`) { + t.Fatalf("missing script version: %s", html) + } + if !strings.Contains(html, "script defer") { + t.Fatalf("missing defer: %s", html) + } + if !strings.Contains(html, `t.css?v=1`) { + t.Fatalf("missing css version: %s", html) + } + if !strings.Contains(html, `b.js?v=appv`) { + t.Fatalf("missing asset_version fallback: %s", html) + } + syncPage := map[string]any{ + "title": "t", + "head": []any{ + map[string]any{"rel": "script", "href": "/static/sync.js"}, + }, + } + syncHTML := render.RenderPage(syncPage, "", "") + if !strings.Contains(syncHTML, `sync.js`) { + t.Fatalf("missing sync script: %s", syncHTML) + } + if strings.Contains(syncHTML, "script defer") { + t.Fatalf("unexpected defer on sync: %s", syncHTML) + } +} + func TestRenderPageNavChrome(t *testing.T) { page := map[string]any{ "title": "t", diff --git a/plugins/web/internal/style/style_test.go b/plugins/web/internal/style/style_test.go index 70bdc1d..40e43df 100644 --- a/plugins/web/internal/style/style_test.go +++ b/plugins/web/internal/style/style_test.go @@ -22,6 +22,22 @@ func TestStyleSimple(t *testing.T) { } } +func TestStyleStrictKeepsQuotedSlash(t *testing.T) { + css, err := style.Style(map[string]any{ + "name": "t", + "strict": true, + "table": []any{ + map[string]any{"选择器": ".box", "属性": "grid-column", "值": "1 / 5"}, + }, + }) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(css, "1 / 5") { + t.Fatalf("missing quoted value: %q", css) + } +} + func TestStyleStrictRejectsNumeric(t *testing.T) { _, err := style.Style(map[string]any{ "name": "x",