Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restrict text in links to emojis.
  • Loading branch information
KN4CK3R committed Jun 21, 2021
commit 177bf45af663b306111ec80d8452497a9da80d6d
17 changes: 9 additions & 8 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
}

for _, node := range nodes {
visitNode(ctx, procs, node, true)
visitNode(ctx, procs, procs, node)
}

newNodes := make([]*html.Node, 0, len(nodes))
Expand Down Expand Up @@ -346,24 +346,22 @@ func postProcess(ctx *RenderContext, procs []processor, input io.Reader, output
return nil
}

func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText bool) {
func visitNode(ctx *RenderContext, procs, textProcs []processor, node *html.Node) {
// Add user-content- to IDs if they don't already have them
for idx, attr := range node.Attr {
if attr.Key == "id" && !(strings.HasPrefix(attr.Val, "user-content-") || blackfridayExtRegex.MatchString(attr.Val)) {
node.Attr[idx].Val = "user-content-" + attr.Val
}

if attr.Key == "class" && attr.Val == "emoji" {
visitText = false
textProcs = nil
}
}

// We ignore code, pre and already generated links.
// We ignore code and pre.
switch node.Type {
case html.TextNode:
if visitText {
textNode(ctx, procs, node)
}
textNode(ctx, textProcs, node)
case html.ElementNode:
if node.Data == "img" {
for _, attr := range node.Attr {
Expand All @@ -380,6 +378,9 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
attr.Val = util.URLJoin(prefix, attr.Val)
}
}
} else if node.Data == "a" {
// Restrict text in links to emojis
textProcs = emojiProcessors
} else if node.Data == "code" || node.Data == "pre" {
return
} else if node.Data == "i" {
Expand All @@ -405,7 +406,7 @@ func visitNode(ctx *RenderContext, procs []processor, node *html.Node, visitText
}
}
for n := node.FirstChild; n != nil; n = n.NextSibling {
visitNode(ctx, procs, n, visitText)
visitNode(ctx, procs, textProcs, n)
}
}
// ignore everything else
Expand Down
8 changes: 8 additions & 0 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,13 @@ func TestRenderSiblingImages_Issue12925(t *testing.T) {
res, err := RenderRawString(&markup.RenderContext{}, testcase)
assert.NoError(t, err)
assert.Equal(t, expected, res)
}

func TestRenderEmojiInLinks_Issue12331(t *testing.T) {
testcase := `[Link with emoji :moon: in text](https://gitea.io)`
expected := `<p><a href="https://gitea.io" rel="nofollow">Link with emoji <span class="emoji" aria-label="waxing gibbous moon">🌔</span> in text</a></p>
`
res, err := RenderString(&markup.RenderContext{}, testcase)
assert.NoError(t, err)
assert.Equal(t, expected, res)
}