shithub: hugo

Download patch

ref: 9c9987535f98714c8a4ec98903f54233735ef0e4
parent: 4a39564efe7b02a685598ae9dbae95e2326c0230
author: SatowTakeshi <doublequotation@gmail.com>
date: Fri Mar 27 21:36:50 EDT 2020

helpers: Fix TrimShortHTML

Where some tags are siblings of p tag.

Fixes #7081

--- a/helpers/content.go
+++ b/helpers/content.go
@@ -44,6 +44,7 @@
 	openingPTag        = []byte("<p>")
 	closingPTag        = []byte("</p>")
 	paragraphIndicator = []byte("<p")
+	closingIndicator   = []byte("</")
 )
 
 // ContentSpec provides functionality to render markdown content.
@@ -315,9 +316,13 @@
 // where said tags are the only <p> tags in the input and enclose the content
 // of the input (whitespace excluded).
 func (c *ContentSpec) TrimShortHTML(input []byte) []byte {
-	first := bytes.Index(input, paragraphIndicator)
-	last := bytes.LastIndex(input, paragraphIndicator)
-	if first == last {
+	firstOpeningP := bytes.Index(input, paragraphIndicator)
+	lastOpeningP := bytes.LastIndex(input, paragraphIndicator)
+
+	lastClosingP := bytes.LastIndex(input, closingPTag)
+	lastClosing := bytes.LastIndex(input, closingIndicator)
+
+	if firstOpeningP == lastOpeningP && lastClosingP == lastClosing {
 		input = bytes.TrimSpace(input)
 		input = bytes.TrimPrefix(input, openingPTag)
 		input = bytes.TrimSuffix(input, closingPTag)
--- a/helpers/content_test.go
+++ b/helpers/content_test.go
@@ -41,6 +41,7 @@
 		{[]byte("\n  \n \t  <p> \t Whitespace\nHTML  \n\t </p>\n\t"), []byte("Whitespace\nHTML")},
 		{[]byte("<p>Multiple</p><p>paragraphs</p>"), []byte("<p>Multiple</p><p>paragraphs</p>")},
 		{[]byte("<p>Nested<p>paragraphs</p></p>"), []byte("<p>Nested<p>paragraphs</p></p>")},
+		{[]byte("<p>Hello</p>\n<ul>\n<li>list1</li>\n<li>list2</li>\n</ul>"), []byte("<p>Hello</p>\n<ul>\n<li>list1</li>\n<li>list2</li>\n</ul>")},
 	}
 
 	c := newTestContentSpec()