Skip to content

Commit 4adf1e3

Browse files
committed
use "a" tag for label directly when render
1 parent 0f28bc3 commit 4adf1e3

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

modules/htmlutil/html.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func HTMLFormat(s template.HTML, rawArgs ...any) template.HTML {
4242
// for most basic types (including template.HTML which is safe), just do nothing and use it
4343
case string:
4444
args[i] = template.HTMLEscapeString(v)
45+
case template.URL:
46+
args[i] = template.HTMLEscapeString(string(v))
4547
case fmt.Stringer:
4648
args[i] = template.HTMLEscapeString(v.String())
4749
default:

modules/htmlutil/html_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13+
type testStringer struct {
14+
}
15+
16+
func (t testStringer) String() string {
17+
return "FromStringMethod"
18+
}
1319
func TestHTMLFormat(t *testing.T) {
1420
assert.Equal(t, template.HTML("<a>&lt; < 1</a>"), HTMLFormat("<a>%s %s %d</a>", "<", template.HTML("<"), 1))
21+
assert.Equal(t, template.HTML("%!s(<nil>)"), HTMLFormat("%s", nil))
22+
assert.Equal(t, template.HTML("&lt;&gt;"), HTMLFormat("%s", template.URL("<>")))
23+
assert.Equal(t, template.HTML("FromStringMethod FromStringMethod"), HTMLFormat("%s %s", testStringer{}, &testStringer{}))
1524
}

modules/templates/util_render.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,23 @@ func (ut *RenderUtils) RenderIssueSimpleTitle(text string) template.HTML {
122122
return ret
123123
}
124124

125-
// RenderLabel renders a label
125+
func (ut *RenderUtils) RenderLabelWithLink(label *issues_model.Label, link any) template.HTML {
126+
var attrHref template.HTML
127+
switch link.(type) {
128+
case template.URL, string:
129+
attrHref = htmlutil.HTMLFormat(`href="%s"`, link)
130+
default:
131+
panic(fmt.Sprintf("unexpected type %T for link", link))
132+
}
133+
return ut.renderLabelWithTag(label, "a", attrHref)
134+
}
135+
126136
func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
137+
return ut.renderLabelWithTag(label, "span", "")
138+
}
139+
140+
// RenderLabel renders a label
141+
func (ut *RenderUtils) renderLabelWithTag(label *issues_model.Label, tagName, tagAttrs template.HTML) template.HTML {
127142
locale := ut.ctx.Value(translation.ContextKey).(translation.Locale)
128143
var extraCSSClasses string
129144
textColor := util.ContrastColor(label.Color)
@@ -137,8 +152,8 @@ func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
137152

138153
if labelScope == "" {
139154
// Regular label
140-
return htmlutil.HTMLFormat(`<div class="ui label %s" style="color: %s !important; background-color: %s !important;" data-tooltip-content title="%s"><span class="gt-ellipsis">%s</span></div>`,
141-
extraCSSClasses, textColor, label.Color, descriptionText, ut.RenderEmoji(label.Name))
155+
return htmlutil.HTMLFormat(`<%s %s class="ui label %s" style="color: %s !important; background-color: %s !important;" data-tooltip-content title="%s"><span class="gt-ellipsis">%s</span></%s>`,
156+
tagName, tagAttrs, extraCSSClasses, textColor, label.Color, descriptionText, ut.RenderEmoji(label.Name), tagName)
142157
}
143158

144159
// Scoped label
@@ -152,7 +167,7 @@ func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
152167
// Ensure we add the same amount of contrast also near 0 and 1.
153168
darken := contrast + math.Max(luminance+contrast-1.0, 0.0)
154169
lighten := contrast + math.Max(contrast-luminance, 0.0)
155-
// Compute factor to keep RGB values proportional.
170+
// Compute the factor to keep RGB values proportional.
156171
darkenFactor := math.Max(luminance-darken, 0.0) / math.Max(luminance, 1.0/255.0)
157172
lightenFactor := math.Min(luminance+lighten, 1.0) / math.Max(luminance, 1.0/255.0)
158173

@@ -173,26 +188,29 @@ func (ut *RenderUtils) RenderLabel(label *issues_model.Label) template.HTML {
173188

174189
if label.ExclusiveOrder > 0 {
175190
// <scope> | <label> | <order>
176-
return htmlutil.HTMLFormat(`<span class="ui label %s scope-parent" data-tooltip-content title="%s">`+
191+
return htmlutil.HTMLFormat(`<%s %s class="ui label %s scope-parent" data-tooltip-content title="%s">`+
177192
`<div class="ui label scope-left" style="color: %s !important; background-color: %s !important">%s</div>`+
178193
`<div class="ui label scope-middle" style="color: %s !important; background-color: %s !important">%s</div>`+
179194
`<div class="ui label scope-right">%d</div>`+
180-
`</span>`,
195+
`</%s>`,
196+
tagName, tagAttrs,
181197
extraCSSClasses, descriptionText,
182198
textColor, scopeColor, scopeHTML,
183199
textColor, itemColor, itemHTML,
184-
label.ExclusiveOrder)
200+
label.ExclusiveOrder,
201+
tagName)
185202
}
186203

187204
// <scope> | <label>
188-
return htmlutil.HTMLFormat(`<span class="ui label %s scope-parent" data-tooltip-content title="%s">`+
205+
return htmlutil.HTMLFormat(`<%s %s class="ui label %s scope-parent" data-tooltip-content title="%s">`+
189206
`<div class="ui label scope-left" style="color: %s !important; background-color: %s !important">%s</div>`+
190207
`<div class="ui label scope-right" style="color: %s !important; background-color: %s !important">%s</div>`+
191208
`</span>`,
209+
tagName, tagAttrs,
192210
extraCSSClasses, descriptionText,
193211
textColor, scopeColor, scopeHTML,
194212
textColor, itemColor, itemHTML,
195-
)
213+
tagName)
196214
}
197215

198216
// RenderEmoji renders html text with emoji post processors
@@ -235,7 +253,8 @@ func (ut *RenderUtils) RenderLabels(labels []*issues_model.Label, repoLink strin
235253
if label == nil {
236254
continue
237255
}
238-
htmlCode += fmt.Sprintf(`<a href="%s?labels=%d">%s</a>`, baseLink, label.ID, ut.RenderLabel(label))
256+
link := fmt.Sprintf("%s?labels=%d", baseLink, label.ID)
257+
htmlCode += string(ut.RenderLabelWithLink(label, template.URL(link)))
239258
}
240259
htmlCode += "</span>"
241260
return template.HTML(htmlCode)

modules/templates/util_render_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ func TestRenderLabels(t *testing.T) {
205205
issue = &issues.Issue{IsPull: true}
206206
expected = `/owner/repo/pulls?labels=123`
207207
assert.Contains(t, ut.RenderLabels([]*issues.Label{label}, "/owner/repo", issue), expected)
208+
209+
expectedLabel := `<a href="&lt;&gt;" class="ui label " style="color: #fff !important; background-color: label-color !important;" data-tooltip-content title=""><span class="gt-ellipsis">label-name</span></a>`
210+
assert.Equal(t, expectedLabel, string(ut.RenderLabelWithLink(label, "<>")))
211+
assert.Equal(t, expectedLabel, string(ut.RenderLabelWithLink(label, template.URL("<>"))))
208212
}
209213

210214
func TestUserMention(t *testing.T) {

templates/repo/issue/card.tmpl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@
6565
<div class="issue-card-bottom">
6666
{{/* the labels container must always be present, to help the assignees list right-aligned */}}
6767
<div class="issue-card-bottom-part labels-list">
68-
{{range .Labels}}
69-
<a target="_blank" href="{{$.Issue.Repo.Link}}/issues?labels={{.ID}}">{{ctx.RenderUtils.RenderLabel .}}</a>
68+
{{range $label := .Labels}}
69+
{{$link := print $.Issue.Repo.Link "/issues"}}
70+
{{$link = QueryBuild $link "labels" $label.ID}}
71+
{{ctx.RenderUtils.RenderLabelWithLink $label $link}}
7072
{{end}}
7173
</div>
7274
{{if .Assignees}}

web_src/css/modules/label.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ If the labels-list itself needs some layouts, use extra classes or "tw" helpers.
304304
padding-bottom: 0;
305305
}
306306

307-
.with-labels-list-inline .labels-list .ui.label + .ui.label,
308-
.with-labels-list-inline .labels-list a + a {
307+
.with-labels-list-inline .labels-list .ui.label + .ui.label {
309308
margin-left: 0.25em;
310309
}
311310

0 commit comments

Comments
 (0)