Skip to content

Commit

Permalink
Prevent double escaping of image alt-text in Goldmar typographer
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 30, 2023
1 parent 3258569 commit 4f2f09a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
23 changes: 23 additions & 0 deletions markup/goldmark/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,29 @@ rightDoubleQuote = "»"
c.Assert(got, qt.Contains, "<p>A &laquo;quote&raquo; and &lsquo;another quote&rsquo; and a &laquo;quote with a &rsquo;nested&rsquo; quote&raquo; and a &lsquo;quote with a &laquo;nested&raquo; quote&rsquo; and an ellipsis&hellip;</p>\n")
}

// Issue #11045
func TestTypographerImageAltText(t *testing.T) {
c := qt.New(t)

content := `
!["They didn't even say 'hello'!" I exclaimed.](https://example.com/image.jpg)
`

confStr := `
[markup]
[markup.goldmark]
`

cfg := config.FromTOMLConfigString(confStr)
conf := testconfig.GetTestConfig(nil, cfg)

b := convert(c, conf, content)
got := string(b.Bytes())

c.Assert(got, qt.Contains, "&ldquo;They didn&rsquo;t even say &lsquo;hello&rsquo;!&rdquo; I exclaimed.")
}

func unsafeConf() config.AllProvider {
cfg := config.FromTOMLConfigString(`
[markup]
Expand Down
17 changes: 16 additions & 1 deletion markup/goldmark/render_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (r *hookedRenderer) renderImageDefault(w util.BufWriter, source []byte, nod
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
}
_, _ = w.WriteString(`" alt="`)
_, _ = w.Write(util.EscapeHTML(n.Text(source)))
_, _ = w.Write(nodeToHTMLText(n, source))
_ = w.WriteByte('"')
if n.Title != nil {
_, _ = w.WriteString(` title="`)
Expand Down Expand Up @@ -475,3 +475,18 @@ func (e *links) Extend(m goldmark.Markdown) {
util.Prioritized(newLinkRenderer(e.cfg), 100),
))
}

// Borrowed from Goldmark.
func nodeToHTMLText(n ast.Node, source []byte) []byte {
var buf bytes.Buffer
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
if s, ok := c.(*ast.String); ok && s.IsCode() {
buf.Write(s.Text(source))
} else if !c.HasChildren() {
buf.Write(util.EscapeHTML(c.Text(source)))
} else {
buf.Write(nodeToHTMLText(c, source))
}
}
return buf.Bytes()
}

0 comments on commit 4f2f09a

Please sign in to comment.