Skip to content

Commit a4f6c07

Browse files
BeowulfRbenteg
andcommitted
[v11.0/forgejo] fix(ui): add missing lazy load attribute to images (go-gitea#8246) (go-gitea#8283)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/8246 closes go-gitea#8076 (cherry picked from commit 7ab27a7) Co-authored-by: Bente Groh <[email protected]> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8283 Reviewed-by: Earl Warren <[email protected]>
1 parent ca07340 commit a4f6c07

File tree

10 files changed

+67
-59
lines changed

10 files changed

+67
-59
lines changed

modules/markup/markdown/markdown_test.go

Lines changed: 52 additions & 52 deletions
Large diffs are not rendered by default.

modules/markup/markdown/transform_image.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (g *ASTTransformer) transformImage(ctx *markup.RenderContext, v *ast.Image)
4444
for _, attr := range v.Attributes() {
4545
image.SetAttribute(attr.Name, attr.Value)
4646
}
47+
image.SetAttributeString("loading", []byte("lazy"))
4748
for child := v.FirstChild(); child != nil; {
4849
next := child.NextSibling()
4950
image.AppendChild(image, child)

modules/markup/sanitizer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ func createDefaultPolicy() *bluemonday.Policy {
108108
// Allow classes for emojis
109109
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^emoji$`)).OnElements("img")
110110

111+
// Allow attributes for images
112+
policy.AllowAttrs("loading").Matching(regexp.MustCompile(`^lazy$`)).OnElements("img")
113+
111114
// Allow icons, emojis, chroma syntax and keyword markup on span
112115
policy.AllowAttrs("class").Matching(regexp.MustCompile(`^((icon(\s+[\p{L}\p{N}_-]+)+)|(emoji)|(language-math display)|(language-math inline))$|^([a-z][a-z0-9]{0,2})$|^` + keywordClass + `$`)).OnElements("span")
113116
policy.AllowAttrs("data-alias").Matching(regexp.MustCompile(`^[a-zA-Z0-9-_+]+$`)).OnElements("span")

modules/markup/sanitizer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func Test_Sanitizer(t *testing.T) {
7575
// Emoji
7676
`<span class="emoji" aria-label="thumbs up" data-alias="+1">THUMBS UP</span>`, `<span class="emoji" aria-label="thumbs up" data-alias="+1">THUMBS UP</span>`,
7777
`<span class="emoji" aria-label="thumbs up" data-alias="(+!)">THUMBS UP</span>`, `<span class="emoji" aria-label="thumbs up">THUMBS UP</span>`,
78+
79+
// Images lazy loading
80+
`<img src="/image1" alt="image1" loading="lazy">`, `<img src="/image1" alt="image1" loading="lazy">`,
81+
`<img src="/image1" alt="image1" loading="eager">`, `<img src="/image1" alt="image1">`,
7882
}
7983

8084
for i := 0; i < len(testCases); i += 2 {

modules/templates/util_render_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ func TestRenderMarkdownToHtml(t *testing.T) {
192192
<a href="https://example.com" rel="nofollow">remote link</a>
193193
<a href="/src/file.bin" rel="nofollow">local link</a>
194194
<a href="https://example.com" rel="nofollow">remote link</a>
195-
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image"/></a>
196-
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image"/></a>
195+
<a href="/image.jpg" target="_blank" rel="nofollow noopener"><img src="/image.jpg" alt="local image" loading="lazy"/></a>
196+
<a href="https://example.com/image.jpg" target="_blank" rel="nofollow noopener"><img src="https://example.com/image.jpg" alt="remote image" loading="lazy"/></a>
197197
<a href="/image.jpg" rel="nofollow"><img src="/image.jpg" title="local image" alt=""/></a>
198198
<a href="https://example.com/image.jpg" rel="nofollow"><img src="https://example.com/image.jpg" title="remote link" alt=""/></a>
199199
<a href="https://example.com/user/repo/compare/88fc37a3c0a4dda553bdcfc80c178a58247f42fb...12fc37a3c0a4dda553bdcfc80c178a58247f42fb#hash" rel="nofollow"><code>88fc37a3c0...12fc37a3c0 (hash)</code></a>

templates/repo/issue/card.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{if $attachments}}
55
<div class="card-attachment-images">
66
{{range $attachments}}
7-
<img src="{{.DownloadURL}}" alt="{{.Name}}" />
7+
<img src="{{.DownloadURL}}" alt="{{.Name}}" loading="lazy"/>
88
{{end}}
99
</div>
1010
{{end}}

templates/repo/issue/view_content/attachments.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
{{if FilenameIsImage .Name}}
3232
{{if not (StringUtils.Contains (StringUtils.ToString $.RenderedContent) .UUID)}}
3333
<a target="_blank" rel="noopener noreferrer" href="{{.DownloadURL}}">
34-
<img alt="{{.Name}}" src="{{.DownloadURL}}" title="{{ctx.Locale.Tr "repo.issues.attachment.open_tab" .Name}}">
34+
<img alt="{{.Name}}" src="{{.DownloadURL}}" title="{{ctx.Locale.Tr "repo.issues.attachment.open_tab" .Name}}" loading="lazy">
3535
</a>
3636
{{end}}
3737
{{end}}

templates/repo/issue/view_content/comments.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@
605605
<div class="timeline-item-group">
606606
<div class="timeline-item event" id="{{.HashTag}}">
607607
<a class="timeline-avatar"{{if gt .Poster.ID 0}} href="{{.Poster.HomeLink}}"{{end}}>
608-
<img src="{{.Poster.AvatarLink $.Context}}" alt="" width="40" height="40">
608+
<img src="{{.Poster.AvatarLink $.Context}}" alt="" width="40" height="40" loading="lazy">
609609
</a>
610610
<span class="badge grey">{{svg "octicon-x" 16}}</span>
611611
<span class="text grey muted-links">

templates/user/dashboard/feeds.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
{{range $push.Commits}}
9292
{{$commitLink := printf "%s/commit/%s" $repoLink .Sha1}}
9393
<div class="flex-text-block">
94-
<img class="ui avatar" src="{{$push.AvatarLink $.Context .AuthorEmail}}" alt="" title="{{.AuthorName}}" width="16" height="16">
94+
<img class="ui avatar" src="{{$push.AvatarLink $.Context .AuthorEmail}}" alt="" title="{{.AuthorName}}" width="16" height="16" loading="lazy">
9595
<a class="ui sha label" href="{{$commitLink}}">{{ShortSha .Sha1}}</a>
9696
<span class="text truncate">
9797
{{RenderCommitMessage $.Context .Message ($repo.ComposeMetas ctx)}}

web_src/js/components/RepoContributors.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ export default {
391391
<div class="ui top attached header tw-flex tw-flex-1">
392392
<b class="ui right">#{{ index + 1 }}</b>
393393
<a :href="contributor.home_link">
394-
<img class="ui avatar tw-align-middle" height="40" width="40" :src="contributor.avatar_link" alt="">
394+
<img class="ui avatar tw-align-middle" height="40" width="40" :src="contributor.avatar_link" alt="" loading="lazy">
395395
</a>
396396
<div class="tw-ml-2">
397397
<a v-if="contributor.home_link !== ''" :href="contributor.home_link"><h4>{{ contributor.name }}</h4></a>

0 commit comments

Comments
 (0)