vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Show a model from a document, and redraw the viewer
0be968244db3882594fa65032ff6b4cf59385bb9 · Keenan Nemetz · 11d ago · parent ffbcb4cc
Show a model from a document, and redraw the viewer A README that names a 3D model with an image path was emitting an <img> of a mesh. The raw route serves model/stl, no browser draws that, so the reader saw a broken image where the rack was supposed to be. A model image in a document now becomes the same viewer host the code browser writes, with the image's alt text as its label and the download link inside it for a reader without JavaScript or WebGL. Only a document with a place in a tree does this: an issue body has no raw route to fetch. The sanitiser admits exactly that host's shape, so the one classed element in a rendered document is the one the renderer wrote. The viewer also never redrew after OrbitControls moved the camera: it renders on demand, and only the button handlers called render. A drag or a wheel zoom changed the camera and left the old frame on screen, which is why the viewer looked static. Listen for the controls' change event, add zoom in/out buttons clamped to the wheel's own limits, and give the canvas a grab cursor. Docs now say a Markdown path names a model the same way it names a picture, and that the code browser's size cap cannot apply to a document rendered from its text alone.
server/web/markdown_test.go +70
| @@ -191,3 +191,73 @@ func TestInstanceIntroIsNotLinkedIntoARepository(t *testing.T) { | |||
| 191 | 191 | } | |
| 192 | 192 | } | |
| 193 | 193 | } | |
| 194 | |||
| 195 | // A README that names a 3D model is showing the model, not asking for an image. | ||
| 196 | // The bytes are a mesh, so an <img> is a broken image; the path becomes the same | ||
| 197 | // viewer host the code browser writes, with the download link a reader without | ||
| 198 | // JavaScript keeps. | ||
| 199 | func TestMarkdownRendersAModelAsAPreview(t *testing.T) { | ||
| 200 | t.Parallel() | ||
| 201 | w := &Web{} | ||
| 202 | doc := treeDoc{Repo: "demo", Ref: "main", Path: "README.md"} | ||
| 203 | got := string(w.renderTreeMarkdown(doc, "# Rack\n\n"+ | ||
| 204 | "\n\n"+ | ||
| 205 | "Inline  here.\n\n"+ | ||
| 206 | "\n")) | ||
| 207 | for _, want := range []string{ | ||
| 208 | `<span class="model-viewer" data-model="/demo/raw/main/assets/example-rack.stl" data-format="stl" data-title="An assembled rack">`, | ||
| 209 | `<span class="model-fallback"><a href="/demo/raw/main/assets/example-rack.stl"`, | ||
| 210 | "Download example-rack.stl</a>", | ||
| 211 | `data-model="/demo/raw/main/parts/wall.obj?raw=1" data-format="obj" data-title="a wall"`, | ||
| 212 | `<img src="/demo/raw/main/img/shot.png" alt="shot">`, | ||
| 213 | } { | ||
| 214 | if !strings.Contains(got, want) { | ||
| 215 | t.Fatalf("markdown is missing %q:\n%s", want, got) | ||
| 216 | } | ||
| 217 | } | ||
| 218 | if strings.Contains(got, "example-rack.stl\" alt=") { | ||
| 219 | t.Fatalf("a model was rendered as an image:\n%s", got) | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | // A body with no place in a tree has no raw route to fetch, so a model path in it | ||
| 224 | // stays the image it was written as rather than becoming a viewer that cannot | ||
| 225 | // load. The same is true of the instance introduction, whose assets are not | ||
| 226 | // served with a model's type. | ||
| 227 | func TestMarkdownKeepsAModelImageWithoutATree(t *testing.T) { | ||
| 228 | t.Parallel() | ||
| 229 | w := newTestWeb(t) | ||
| 230 | for _, got := range []string{ | ||
| 231 | string(w.renderTreeMarkdown(treeDoc{Repo: "demo"}, "\n")), | ||
| 232 | string(w.renderInstanceMarkdown("\n")), | ||
| 233 | } { | ||
| 234 | if !strings.Contains(got, "<img ") { | ||
| 235 | t.Fatalf("a document without a tree lost its image:\n%s", got) | ||
| 236 | } | ||
| 237 | if strings.Contains(got, "model-viewer") { | ||
| 238 | t.Fatalf("a document without a tree rendered a viewer:\n%s", got) | ||
| 239 | } | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | // The viewer host is the one classed element the renderer writes, so the policy | ||
| 244 | // has to admit it. It admits the shape and nothing else: a reader's own class is | ||
| 245 | // still stripped, and a viewer cannot be pointed off the instance. goldmark | ||
| 246 | // drops raw HTML before this runs, so this is the second line of defence. | ||
| 247 | func TestViewerPolicyAdmitsOnlyItsOwnShape(t *testing.T) { | ||
| 248 | t.Parallel() | ||
| 249 | got := sanitizer.Sanitize(`<div class="topnav">mine</div>` + | ||
| 250 | `<span class="model-viewer" data-model="//evil.invalid/rack.stl" data-format="exe">bad</span>` + | ||
| 251 | `<span class="model-viewer" data-model="/demo/raw/main/rack.stl" data-format="stl">fine</span>`) | ||
| 252 | if strings.Contains(got, `class="topnav"`) { | ||
| 253 | t.Fatalf("a class other than the viewer's survived:\n%s", got) | ||
| 254 | } | ||
| 255 | for _, unwanted := range []string{"evil.invalid", `data-format="exe"`} { | ||
| 256 | if strings.Contains(got, unwanted) { | ||
| 257 | t.Fatalf("the policy admitted %q:\n%s", unwanted, got) | ||
| 258 | } | ||
| 259 | } | ||
| 260 | if !strings.Contains(got, `data-model="/demo/raw/main/rack.stl" data-format="stl"`) { | ||
| 261 | t.Fatalf("the viewer host was dropped:\n%s", got) | ||
| 262 | } | ||
| 263 | } | ||