Skip to content

Commit d60b14d

Browse files
authored
fix(website): collapse long affected range event lists (#6102)
## Overview Affected range event lists can dominate vulnerability pages when a record contains hundreds of events. Show the first six events by default and provide an independent expand/collapse control for the rest of each range. Fixes #6064. ## Details - Share the event renderer between the tabbed and grouped package layouts. - Use native `details`/`summary` controls with the remaining event count, a collapse label, and a visible keyboard focus indicator. The control requires no JavaScript. - Preserve all event values, order, commit links, and unknown-introduced tooltips. Lists of six or fewer events remain fully visible. ### Screenshots The same package in `ROOT-APP-MAVEN-CVE-2026-5795`, rendered by the local Go development server with current OSV data: | Collapsed (first 6 events) | Expanded (all 11 events) | | --- | --- | | ![Collapsed event list with Show 5 more events](https://raw.githubusercontent.com/ZedingZhang/osv.dev/d183fac71fa51f42b294afbdd5f8ed7c1d436345/collapsed.png) | ![Expanded event list with Show fewer events](https://raw.githubusercontent.com/ZedingZhang/osv.dev/d183fac71fa51f42b294afbdd5f8ed7c1d436345/expanded.png) | ## Testing - `go test ./internal/website` passed, including new full-template rendering cases for 0, 1, 6, 7, and 20 events in both package layouts, multiple ranges, commit links, and tooltips. - `golangci-lint run --new-from-rev=HEAD ./internal/website` passed with 0 issues before committing. A full package lint reported existing formatting issues in unchanged files. - `pnpm run build` and `pnpm run build:prod` passed (Sass deprecation warnings; production build also reports bundle-size warnings). - Checked all three examples from the issue using the local Go development server and current OSV records. Verified Enter/Space expand/collapse, independent ranges, complete expanded contents, and a 390px viewport on the Discourse example. <!-- Developed with assistance from OpenAI Codex; implementation and rendered behavior were reviewed and tested. -->
1 parent c54e429 commit d60b14d

3 files changed

Lines changed: 192 additions & 38 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package website
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"html/template"
8+
"strings"
9+
"testing"
10+
11+
"github.com/ossf/osv-schema/bindings/go/osvschema"
12+
"golang.org/x/net/html"
13+
"google.golang.org/protobuf/encoding/protojson"
14+
)
15+
16+
// Render the complete content template to exercise both affected-package layouts.
17+
func TestRangeEventsTemplate(t *testing.T) {
18+
tmpl, err := template.New("vulnerability").Funcs(template.FuncMap{
19+
"hasPrefix": strings.HasPrefix,
20+
"add": func(a, b int) int { return a + b },
21+
"sub": func(a, b int) int { return a - b },
22+
}).ParseFiles("../../../website/frontend3/src/templates/vulnerability.html")
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
27+
for _, packageCount := range []int{1, 6} {
28+
for _, eventCount := range []int{0, 1, 6, 7, 20} {
29+
t.Run(fmt.Sprintf("packages=%d/events=%d", packageCount, eventCount), func(t *testing.T) {
30+
events := make([]map[string]string, 0, eventCount)
31+
for i := range eventCount {
32+
eventType := []string{"introduced", "fixed", "last_affected", "limit"}[i%4]
33+
events = append(events, map[string]string{eventType: fmt.Sprintf("event-%d", i)})
34+
}
35+
affected := make([]any, 0, packageCount)
36+
for i := range packageCount {
37+
affected = append(affected, map[string]any{
38+
"package": map[string]string{"ecosystem": "PyPI", "name": fmt.Sprintf("package-%d", i)},
39+
"ranges": []any{
40+
map[string]any{"type": "ECOSYSTEM", "events": events},
41+
// A short range in the same package must remain independent.
42+
map[string]any{"type": "GIT", "repo": "https://github.com/example/repo", "events": []any{
43+
map[string]string{"introduced": "0"},
44+
map[string]string{"fixed": "abcdef"},
45+
}},
46+
},
47+
})
48+
}
49+
data, err := json.Marshal(map[string]any{"id": "TEST-6064", "affected": affected})
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
vuln := &osvschema.Vulnerability{}
54+
if err := protojson.Unmarshal(data, vuln); err != nil {
55+
t.Fatal(err)
56+
}
57+
page := VulnerabilityPageData{Vulnerability: vuln}
58+
if page.ShouldCollapse() != (packageCount == 6) {
59+
t.Fatal("fixture does not exercise the expected package layout")
60+
}
61+
var output bytes.Buffer
62+
if err := tmpl.ExecuteTemplate(&output, "content", page); err != nil {
63+
t.Fatal(err)
64+
}
65+
doc, err := html.Parse(strings.NewReader(output.String()))
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
visible, hidden, controls := 0, 0, 0
70+
var visit func(*html.Node, bool)
71+
visit = func(n *html.Node, collapsed bool) {
72+
for _, attr := range n.Attr {
73+
if attr.Key != "class" {
74+
continue
75+
}
76+
if attr.Val == "events-section" {
77+
controls++
78+
collapsed = true
79+
for _, attr := range n.Attr {
80+
if attr.Key == "open" {
81+
t.Error("event lists should start collapsed")
82+
}
83+
}
84+
}
85+
if strings.Contains(attr.Val, "version-value") {
86+
if collapsed {
87+
hidden++
88+
} else {
89+
visible++
90+
}
91+
}
92+
}
93+
for c := n.FirstChild; c != nil; c = c.NextSibling {
94+
visit(c, collapsed)
95+
}
96+
}
97+
visit(doc, false)
98+
wantControls := 0
99+
if eventCount > 6 {
100+
wantControls = packageCount
101+
label := fmt.Sprintf("Show %d more events", eventCount-6)
102+
if eventCount == 7 {
103+
label = "Show 1 more event"
104+
}
105+
if strings.Count(output.String(), label+"</span>") != packageCount {
106+
t.Errorf("missing remaining-event label %q", label)
107+
}
108+
}
109+
if controls != wantControls || visible != packageCount*(min(eventCount, 6)+2) || hidden != packageCount*max(eventCount-6, 0) {
110+
t.Errorf("controls=%d, visible=%d, hidden=%d for %d events in %d packages", controls, visible, hidden, eventCount, packageCount)
111+
}
112+
for i := range eventCount {
113+
if strings.Count(output.String(), fmt.Sprintf("event-%d\n", i)) != packageCount {
114+
t.Errorf("event-%d missing or duplicated", i)
115+
}
116+
}
117+
if strings.Count(output.String(), `href="https://github.com/example/repo/commit/abcdef"`) != packageCount || strings.Count(output.String(), `class="tooltiptext"`) != packageCount {
118+
t.Error("commit links or unknown-introduced tooltips were lost")
119+
}
120+
})
121+
}
122+
}
123+
}

‎website/frontend3/src/styles.scss‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,37 @@ osv-tabs.vulnerability-packages[affordance="collapse"] {
11721172
gap: 4px;
11731173
}
11741174

1175+
.events-section {
1176+
>summary.events-header {
1177+
cursor: pointer;
1178+
padding: 8px 0;
1179+
font-family: $osv-heading-font-family;
1180+
1181+
&:hover {
1182+
text-decoration: underline;
1183+
}
1184+
1185+
&:focus-visible {
1186+
outline: 2px solid $osv-accent-color;
1187+
outline-offset: 2px;
1188+
}
1189+
}
1190+
1191+
.events-show-less {
1192+
display: none;
1193+
}
1194+
1195+
&[open]>summary {
1196+
.events-show-more {
1197+
display: none;
1198+
}
1199+
1200+
.events-show-less {
1201+
display: inline;
1202+
}
1203+
}
1204+
}
1205+
11751206
.specific {
11761207
overflow: auto;
11771208
}
@@ -3161,4 +3192,4 @@ html[data-theme="light"] .theme-toggle-btn {
31613192
.gl {
31623193
text-decoration: underline;
31633194
}
3164-
}
3195+
}

‎website/frontend3/src/templates/vulnerability.html‎

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -240,22 +240,7 @@ <h3 class="mdc-layout-grid__cell--span-3">Affected ranges <a href="https://ossf.
240240
{{ end }}
241241
<dt>Events</dt>
242242
<dd>
243-
<div class="mdc-layout-grid__inner events">
244-
{{ range .Events }}
245-
<div class="mdc-layout-grid__cell--span-3">{{ .Type }}</div>
246-
<div class="mdc-layout-grid__cell--span-9 version-value">
247-
{{ if .Link }}
248-
<a href="{{ .Link }}" rel="nofollow">{{ .Value }}</a>
249-
{{ else if .IsUnknown }}
250-
<div class="tooltip">{{ .Value }}
251-
<span class="tooltiptext">{{ .TooltipText }}</span>
252-
</div>
253-
{{ else }}
254-
{{ .Value }}
255-
{{ end }}
256-
</div>
257-
{{ end }}
258-
</div>
243+
{{ template "range-events" .Events }}
259244
</dd>
260245
{{ if .DatabaseSpecificJSON }}
261246
<dt>Database specific <a href="https://ossf.github.io/osv-schema/#affectedrangesdatabase_specific-field" target="_blank" rel="noopener noreferrer"></a></dt>
@@ -404,27 +389,7 @@ <h3 class="mdc-layout-grid__cell--span-3">
404389

405390
<dt>Events</dt>
406391
<dd>
407-
<div class="mdc-layout-grid__inner events">
408-
{{ range .Events }}
409-
<div class="mdc-layout-grid__cell--span-3">
410-
{{ .Type }}
411-
</div>
412-
<div class="mdc-layout-grid__cell--span-9 version-value">
413-
{{ if .Link }}
414-
<a href="{{ .Link }}" rel="nofollow">
415-
{{ .Value }}
416-
</a>
417-
{{ else if .IsUnknown }}
418-
<div class="tooltip">
419-
{{ .Value }}
420-
<span class="tooltiptext">{{ .TooltipText }}</span>
421-
</div>
422-
{{ else }}
423-
{{ .Value }}
424-
{{ end }}
425-
</div>
426-
{{ end }}
427-
</div>
392+
{{ template "range-events" .Events }}
428393
</dd>
429394

430395
{{ if .DatabaseSpecificJSON }}
@@ -560,3 +525,38 @@ <h3 class="mdc-layout-grid__cell--span-3">
560525
});
561526
</script>
562527
{{ end }}
528+
529+
{{ define "range-events" }}
530+
{{ $previewCount := 6 }}
531+
{{ if gt (len .) $previewCount }}
532+
{{ template "range-event-rows" (slice . 0 $previewCount) }}
533+
<details class="events-section">
534+
<summary class="events-header">
535+
<span class="events-show-more">Show {{ sub (len .) $previewCount }} more event{{ if ne (sub (len .) $previewCount) 1 }}s{{ end }}</span>
536+
<span class="events-show-less">Show fewer events</span>
537+
</summary>
538+
{{ template "range-event-rows" (slice . $previewCount) }}
539+
</details>
540+
{{ else }}
541+
{{ template "range-event-rows" . }}
542+
{{ end }}
543+
{{ end }}
544+
545+
{{ define "range-event-rows" }}
546+
<div class="mdc-layout-grid__inner events">
547+
{{ range . }}
548+
<div class="mdc-layout-grid__cell--span-3">{{ .Type }}</div>
549+
<div class="mdc-layout-grid__cell--span-9 version-value">
550+
{{ if .Link }}
551+
<a href="{{ .Link }}" rel="nofollow">{{ .Value }}</a>
552+
{{ else if .IsUnknown }}
553+
<div class="tooltip">{{ .Value }}
554+
<span class="tooltiptext">{{ .TooltipText }}</span>
555+
</div>
556+
{{ else }}
557+
{{ .Value }}
558+
{{ end }}
559+
</div>
560+
{{ end }}
561+
</div>
562+
{{ end }}

0 commit comments

Comments
 (0)