Skip to content

Commit 020075e

Browse files
mrsdizzietechknowlogick
mrsdizzie
authored andcommitted
Remove visitLinksForShortLinks features (#6257)
The visitLinksForShortLinks feature would look inside of an <a> tag and run shortLinkProcessorFull on any text, which attempts to create links out of potential 'short links' like [[test]] [[link|example]] etc... This makes no sense because you can't have nested links within an <a> tag. Specifically, the html5 standard says <a> tags can't include interactive content if they contain the href attribute: http://w3c.github.io/html/single-page.html#the-a-element And also defines an <a> element with a href attribute as interactive: http://w3c.github.io/html/single-page.html#interactive-content Therefore you can't really put a link inside of another link. In practice none of this works anyways since browsers won't render it, it would probably be broken if they tried, and it is causing a bug (#4946). No current tests rely on this behavior either. This removes the feature and also explicitly excludes the current visitNodeForShortLinks from looking in <a> tags.
1 parent ad86b84 commit 020075e

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

modules/markup/html.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ type postProcessCtx struct {
171171

172172
// processors used by this context.
173173
procs []processor
174-
175-
// if set to true, when an <a> is found, instead of just returning during
176-
// visitNode, it will recursively visit the node exclusively running
177-
// shortLinkProcessorFull with true.
178-
visitLinksForShortLinks bool
179174
}
180175

181176
// PostProcess does the final required transformations to the passed raw HTML
@@ -191,11 +186,10 @@ func PostProcess(
191186
) ([]byte, error) {
192187
// create the context from the parameters
193188
ctx := &postProcessCtx{
194-
metas: metas,
195-
urlPrefix: urlPrefix,
196-
isWikiMarkdown: isWikiMarkdown,
197-
procs: defaultProcessors,
198-
visitLinksForShortLinks: true,
189+
metas: metas,
190+
urlPrefix: urlPrefix,
191+
isWikiMarkdown: isWikiMarkdown,
192+
procs: defaultProcessors,
199193
}
200194
return ctx.postProcess(rawHTML)
201195
}
@@ -285,9 +279,6 @@ func (ctx *postProcessCtx) visitNode(node *html.Node) {
285279
ctx.textNode(node)
286280
case html.ElementNode:
287281
if node.Data == "a" || node.Data == "code" || node.Data == "pre" {
288-
if node.Data == "a" && ctx.visitLinksForShortLinks {
289-
ctx.visitNodeForShortLinks(node)
290-
}
291282
return
292283
}
293284
for n := node.FirstChild; n != nil; n = n.NextSibling {
@@ -302,7 +293,7 @@ func (ctx *postProcessCtx) visitNodeForShortLinks(node *html.Node) {
302293
case html.TextNode:
303294
shortLinkProcessorFull(ctx, node, true)
304295
case html.ElementNode:
305-
if node.Data == "code" || node.Data == "pre" {
296+
if node.Data == "code" || node.Data == "pre" || node.Data == "a" {
306297
return
307298
}
308299
for n := node.FirstChild; n != nil; n = n.NextSibling {

modules/markup/html_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,8 @@ func TestRender_ShortLinks(t *testing.T) {
222222
"[[some/path/Link #.jpg]]",
223223
`<p><a href="`+notencodedImgurl+`" rel="nofollow"><img src="`+notencodedImgurl+`"/></a></p>`,
224224
`<p><a href="`+notencodedImgurlWiki+`" rel="nofollow"><img src="`+notencodedImgurlWiki+`"/></a></p>`)
225+
test(
226+
"<p><a href=\"https://example.org\">[[foobar]]</a></p>",
227+
`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`,
228+
`<p><a href="https://example.org" rel="nofollow">[[foobar]]</a></p>`)
225229
}

0 commit comments

Comments
 (0)