Fix trailing <br>s added in lists with EXTENSION_HARD_LINE_BREAK enabled

Previously, with EXTENSION_HARD_LINE_BREAK enabled, <li>s would contain unnecessary <br />s at the end of each item. It was especially noticeable by the vertical space this left at the end of an ordered or unordered list.

This fixes that by stripping trailing <br />s from individual list items while in the process of stripping trailing newlines.

One shortcoming of this patch: it does not fix the issue in *nested* lists.
pull/686/head
Matt Baer 2021-07-02 12:01:39 -04:00
parent e96880f42b
commit afc311c672
2 changed files with 68 additions and 3 deletions

View File

@ -1305,9 +1305,29 @@ gatherlines:
cookedBytes := cooked.Bytes()
parsedEnd := len(cookedBytes)
// strip trailing newlines
for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' {
parsedEnd--
if p.flags&EXTENSION_HARD_LINE_BREAK != 0 {
// strip trailing newlines and extra hard line breaks
const brLen = len("<br />")
for parsedEnd > 0 &&
(cookedBytes[parsedEnd-1] == '\n' ||
(parsedEnd >= brLen &&
cookedBytes[parsedEnd-brLen] == '<' &&
cookedBytes[parsedEnd-brLen+1] == 'b' &&
cookedBytes[parsedEnd-brLen+2] == 'r' &&
cookedBytes[parsedEnd-brLen+3] == ' ' &&
cookedBytes[parsedEnd-brLen+4] == '/' &&
cookedBytes[parsedEnd-brLen+5] == '>')) {
if cookedBytes[parsedEnd-1] == '\n' {
parsedEnd--
} else {
parsedEnd -= brLen
}
}
} else {
// strip trailing newlines
for parsedEnd > 0 && cookedBytes[parsedEnd-1] == '\n' {
parsedEnd--
}
}
p.r.ListItem(out, cookedBytes[:parsedEnd], *flags)

View File

@ -1636,6 +1636,51 @@ func TestListWithFencedCodeBlockNoExtensions(t *testing.T) {
doTestsBlock(t, tests, 0)
}
func TestListEXTENSION_HARD_LINE_BREAK(t *testing.T) {
// If there is a fenced code block in a list, and FencedCode is not set,
// lists should be processed normally.
var tests = []string{
`* One
* Two
Text`,
`<ul>
<li>One</li>
<li>Two</li>
</ul>
<p>Text</p>
`,
`* Double
line
* Single line
Text`,
`<ul>
<li>Double<br />
line</li>
<li>Single line</li>
</ul>
<p>Text</p>
`,
`1. One
2. Two
Text`,
`<ol>
<li>One</li>
<li>Two</li>
</ol>
<p>Text</p>
`,
}
doTestsBlock(t, tests, EXTENSION_HARD_LINE_BREAK)
}
func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) {
var tests = []string{
"% Some title\n" +