From 79f2b2feb84bd362b1dbb1395bb16e13c970262b Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 4 Jul 2021 10:26:04 +0100 Subject: [PATCH 1/2] 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 216a1f20a3ae3..7b9844bf7522a 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -401,7 +401,7 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) { } case html.ElementNode: if node.Data == "img" { - for _, attr := range node.Attr { + for i, attr := range node.Attr { if attr.Key != "src" { continue } @@ -414,6 +414,7 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) { 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 4d6344a720f2a..848c2a77e0026 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -384,6 +384,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 From d9f5fb5abb77ee138b87dafa57574b5b856b0624 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 4 Jul 2021 12:37:35 +0200 Subject: [PATCH 2/2] fix TestRender_RelativeImages --- modules/markup/html_test.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 848c2a77e0026..dddb3ade0d083 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -390,18 +390,9 @@ func TestRender_RelativeImages(t *testing.T) { 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) + buffer := markdown.RenderString(input, tree, localMetas) 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) + buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas) assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer)) }