From 32fd11395b7631cd226783a98b86e55192bd99ca Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 4 Jul 2021 10:26:04 +0100 Subject: [PATCH] Fix relative links in postprocessed images (#16334) If a pre-post-processed file contains relative img tags these need to be updated and joined correctly with the prefix. Finally, the node attributes need to be updated. Fix #16308 Signed-off-by: Andrew Thornton Co-authored-by: 6543 <6543@obermui.de> --- modules/markup/html.go | 3 ++- modules/markup/html_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 1e55629ab..7afd8114c 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -364,7 +364,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText } case html.ElementNode: if node.Data == "img" { - for _, attr := range node.Attr { + for i, attr := range node.Attr { if attr.Key != "src" { continue } @@ -377,6 +377,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText attr.Val = util.URLJoin(prefix, attr.Val) } + node.Attr[i] = attr } } else if node.Data == "a" { visitText = false diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 85418892e..a494c5bd1 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -425,6 +425,41 @@ func TestRender_ShortLinks(t *testing.T) { `

[[foobar]]

`) } +func TestRender_RelativeImages(t *testing.T) { + setting.AppURL = AppURL + setting.AppSubURL = AppSubURL + tree := util.URLJoin(AppSubURL, "src", "master") + + test := func(input, expected, expectedWiki string) { + buffer, err := markdown.RenderString(&RenderContext{ + URLPrefix: tree, + Metas: localMetas, + }, input) + assert.NoError(t, err) + assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer)) + buffer, err = markdown.RenderString(&RenderContext{ + URLPrefix: setting.AppSubURL, + Metas: localMetas, + IsWiki: true, + }, input) + assert.NoError(t, err) + assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer)) + } + + rawwiki := util.URLJoin(AppSubURL, "wiki", "raw") + mediatree := util.URLJoin(AppSubURL, "media", "master") + + test( + ``, + ``, + ``) + + test( + ``, + ``, + ``) +} + func Test_ParseClusterFuzz(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL