helpers: Fix TrimShortHTML
authorSatowTakeshi <doublequotation@gmail.com>
Fri, 27 Mar 2020 16:36:50 +0000 (01:36 +0900)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 28 Mar 2020 10:10:25 +0000 (11:10 +0100)
Where some tags are siblings of p tag.

Fixes #7081

helpers/content.go
helpers/content_test.go

index e61888357efc2b17f35ebe2a2dec31cc321c2f22..5eeca88b669f86f4eca731d5686a9dd4c3095df4 100644 (file)
@@ -44,6 +44,7 @@ var (
        openingPTag        = []byte("<p>")
        closingPTag        = []byte("</p>")
        paragraphIndicator = []byte("<p")
+       closingIndicator   = []byte("</")
 )
 
 // ContentSpec provides functionality to render markdown content.
@@ -315,9 +316,13 @@ func (c *ContentSpec) TruncateWordsToWholeSentence(s string) (string, bool) {
 // 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)
index 7f82abc9da0496c7b3462b6a2897dfb511735039..86e5412c2e84f88b0d5f2854ac38b2ac7162aa00 100644 (file)
@@ -41,6 +41,7 @@ func TestTrimShortHTML(t *testing.T) {
                {[]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()