mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	* 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 <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de>
This commit is contained in:
		@@ -401,7 +401,7 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	case html.ElementNode:
 | 
						case html.ElementNode:
 | 
				
			||||||
		if node.Data == "img" {
 | 
							if node.Data == "img" {
 | 
				
			||||||
			for _, attr := range node.Attr {
 | 
								for i, attr := range node.Attr {
 | 
				
			||||||
				if attr.Key != "src" {
 | 
									if attr.Key != "src" {
 | 
				
			||||||
					continue
 | 
										continue
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
@@ -414,6 +414,7 @@ func (ctx *postProcessCtx) visitNode(node *html.Node, visitText bool) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
					attr.Val = util.URLJoin(prefix, attr.Val)
 | 
										attr.Val = util.URLJoin(prefix, attr.Val)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									node.Attr[i] = attr
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else if node.Data == "a" {
 | 
							} else if node.Data == "a" {
 | 
				
			||||||
			visitText = false
 | 
								visitText = false
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -384,6 +384,32 @@ func TestRender_ShortLinks(t *testing.T) {
 | 
				
			|||||||
		`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
 | 
							`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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 := markdown.RenderString(input, tree, localMetas)
 | 
				
			||||||
 | 
							assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
 | 
				
			||||||
 | 
							buffer = markdown.RenderWiki([]byte(input), setting.AppSubURL, localMetas)
 | 
				
			||||||
 | 
							assert.Equal(t, strings.TrimSpace(expectedWiki), strings.TrimSpace(buffer))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rawwiki := util.URLJoin(AppSubURL, "wiki", "raw")
 | 
				
			||||||
 | 
						mediatree := util.URLJoin(AppSubURL, "media", "master")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						test(
 | 
				
			||||||
 | 
							`<img src="Link">`,
 | 
				
			||||||
 | 
							`<img src="`+util.URLJoin(mediatree, "Link")+`"/>`,
 | 
				
			||||||
 | 
							`<img src="`+util.URLJoin(rawwiki, "Link")+`"/>`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						test(
 | 
				
			||||||
 | 
							`<img src="./icon.png">`,
 | 
				
			||||||
 | 
							`<img src="`+util.URLJoin(mediatree, "icon.png")+`"/>`,
 | 
				
			||||||
 | 
							`<img src="`+util.URLJoin(rawwiki, "icon.png")+`"/>`)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func Test_ParseClusterFuzz(t *testing.T) {
 | 
					func Test_ParseClusterFuzz(t *testing.T) {
 | 
				
			||||||
	setting.AppURL = AppURL
 | 
						setting.AppURL = AppURL
 | 
				
			||||||
	setting.AppSubURL = AppSubURL
 | 
						setting.AppSubURL = AppSubURL
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user