From 3dcfc072bbc984df6bad53a19fadfaa4a5af7812 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 3 Jul 2021 21:28:40 +0100 Subject: [PATCH 1/2] Fix relative links in postprocessed images 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 --- modules/markup/html.go | 3 ++- modules/markup/html_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index 1e55629ab5dd7..7afd8114c1b32 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 85418892ef7f1..969c01af6f278 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -425,6 +425,38 @@ 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, + }, 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)) + } + + mediatree := util.URLJoin(AppSubURL, "media", "master") + + url := util.URLJoin(mediatree, "Link") + urlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link") + + test( + ``, + ``, + ``) + +} + func Test_ParseClusterFuzz(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL From 7517ee6940cc3594b17320813faa9aaed527a3a9 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 4 Jul 2021 01:09:30 +0200 Subject: [PATCH 2/2] extend test --- modules/markup/html_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 969c01af6f278..a494c5bd1831b 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -433,6 +433,7 @@ func TestRender_RelativeImages(t *testing.T) { 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)) @@ -445,16 +446,18 @@ func TestRender_RelativeImages(t *testing.T) { assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer)) } + rawwiki := util.URLJoin(AppSubURL, "wiki", "raw") mediatree := util.URLJoin(AppSubURL, "media", "master") - url := util.URLJoin(mediatree, "Link") - urlWiki := util.URLJoin(AppSubURL, "wiki", "raw", "Link") - test( ``, - ``, - ``) + ``, + ``) + test( + ``, + ``, + ``) } func Test_ParseClusterFuzz(t *testing.T) {