Debian Codegraph @

resources/page/page_markup_test.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package page
15
16 import (
17 "strings"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/common/types"
22 "github.com/gohugoio/hugo/media"
23 )
24
25 func TestExtractSummaryFromHTML(t *testing.T) {
26 c := qt.New(t)
27
28 tests := []struct {
29 mt media.Type
30 input string
31 isCJK bool
32 numWords int
33 expectSummary string
34 expectContentWithoutSummary string
35 }{
36 {media.Builtin.ReStructuredTextType, "<div class=\"document\">\n\n\n<p>Simple Page</p>\n</div>", false, 70, "<div class=\"document\">\n\n\n<p>Simple Page</p>\n</div>", ""},
37 {media.Builtin.ReStructuredTextType, "<div class=\"document\"><p>First paragraph</p><p>Second paragraph</p></div>", false, 2, `<div class="document"><p>First paragraph</p></div>`, "<div class=\"document\"><p>Second paragraph</p></div>"},
38 {media.Builtin.MarkdownType, "<p>First paragraph</p>", false, 10, "<p>First paragraph</p>", ""},
39 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraph</p>", false, 2, "<p>First paragraph</p>", "<p>Second paragraph</p>"},
40 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p>", false, 3, "<p>First paragraph</p><p>Second paragraph</p>", "<p>Third paragraph</p>"},
41 {media.Builtin.MarkdownType, "<blockquote>\n<p>foo</p>\n</blockquote>\n<p>bar</p>", false, 1, "<blockquote>\n<p>foo</p>\n</blockquote>", "<p>bar</p>"},
42 {media.Builtin.MarkdownType, "<ul>\n<li>\n<p>item 1 line 1</p>\n<p>item 1 line 2</p>\n</li>\n</ul>\n<p>bar</p>", false, 1, "<ul>\n<li>\n<p>item 1 line 1</p>\n<p>item 1 line 2</p>\n</li>\n</ul>", "<p>bar</p>"},
43 {media.Builtin.HTMLType, "<div><p>foo</p></div><p>bar</p>", false, 1, "<div><p>foo</p></div>", "<p>bar</p>"},
44 {media.Builtin.HTMLType, "<DIV><p>foo</p></DIV><p>bar</p>", false, 1, "<DIV><p>foo</p></DIV>", "<p>bar</p>"},
45 {media.Builtin.HTMLType, "<div><!-- <span> --><p>foo</p></div><p>bar</p>", false, 1, "<div><!-- <span> --><p>foo</p></div>", "<p>bar</p>"},
46 {media.Builtin.HTMLType, `<div><p title="a>b"><img src="x"/>foo</p></div><p>bar</p>`, false, 1, `<div><p title="a>b"><img src="x"/>foo</p></div>`, "<p>bar</p>"},
47 {media.Builtin.HTMLType, "<div><script>var a = '<div>';</script><p>foo</p></div><p>bar</p>", false, 1, "<div><script>var a = '<div>';</script><p>foo</p></div>", "<p>bar</p>"},
48 {media.Builtin.HTMLType, "<div><p>foo</p><p>bar</p>", false, 1, "<div><p>foo</p>", "<p>bar</p>"},
49 {media.Builtin.AsciiDocType, "<div><p>First paragraph</p></div><div><p>Second paragraph</p></div>", false, 2, "<div><p>First paragraph</p></div>", "<div><p>Second paragraph</p></div>"},
50 {media.Builtin.MarkdownType, "<p>这是中文,全中文</p><p>a这是中文,全中文</p>", true, 5, "<p>这是中文,全中文</p>", "<p>a这是中文,全中文</p>"},
51 }
52
53 for i, test := range tests {
54 summary := ExtractSummaryFromHTML(test.mt, test.input, test.numWords, test.isCJK)
55 c.Assert(summary.Summary(), qt.Equals, test.expectSummary, qt.Commentf("Summary %d", i))
56 c.Assert(summary.ContentWithoutSummary(), qt.Equals, test.expectContentWithoutSummary, qt.Commentf("ContentWithoutSummary %d", i))
57 }
58 }
59
60 // See https://discourse.gohugo.io/t/automatic-summarys-summarylength-seems-broken-in-the-case-of-plainify/51466/4
61 // Also issue 12837
62 func TestExtractSummaryFromHTMLLotsOfHTMLInSummary(t *testing.T) {
63 c := qt.New(t)
64
65 input := `
66 <p>
67 <div>
68 <picture>
69 <img src="imgs/1.jpg" alt="1"/>
70 </picture>
71 <picture>
72 <img src="imgs/2.jpg" alt="2"/>
73 </picture>
74 <picture>
75 <img src="imgs/3.jpg" alt="3"/>
76 </picture>
77 <picture>
78 <img src="imgs/4.jpg" alt="4"/>
79 </picture>
80 <picture>
81 <img src="imgs/5.jpg" alt="5"/>
82 </picture>
83 </div>
84 </p>
85 <p>
86 This is a story about a cat.
87 </p>
88 <p>
89 The cat was white and fluffy.
90 </p>
91 <p>
92 And it liked milk.
93 </p>
94 `
95
96 summary := ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 10, false)
97 c.Assert(strings.HasSuffix(summary.Summary(), "<p>\nThis is a story about a cat.\n</p>\n<p>\nThe cat was white and fluffy.\n</p>"), qt.IsTrue)
98 }
99
100 func TestExtractSummaryFromHTMLWithDivider(t *testing.T) {
101 c := qt.New(t)
102
103 const divider = "FOOO"
104
105 tests := []struct {
106 mt media.Type
107 input string
108 expectSummary string
109 expectContentWithoutSummary string
110 expectContent string
111 }{
112 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>FOOO</p><p>Second paragraph</p>", "<p>First paragraph</p>", "<p>Second paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p>"},
113 {media.Builtin.MarkdownType, "<p>First paragraph</p>\n<p>FOOO</p>\n<p>Second paragraph</p>", "<p>First paragraph</p>", "<p>Second paragraph</p>", "<p>First paragraph</p>\n<p>Second paragraph</p>"},
114 {media.Builtin.MarkdownType, "<p>FOOO</p>\n<p>First paragraph</p>", "", "<p>First paragraph</p>", "<p>First paragraph</p>"},
115 {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraphFOOO</p><p>Third paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p>", "<p>Third paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p><p>Third paragraph</p>"},
116 {media.Builtin.MarkdownType, "<p>这是中文,全中文FOOO</p><p>a这是中文,全中文</p>", "<p>这是中文,全中文</p>", "<p>a这是中文,全中文</p>", "<p>这是中文,全中文</p><p>a这是中文,全中文</p>"},
117 {media.Builtin.MarkdownType, `<p>a <strong>b</strong>` + "\v" + ` c</p>` + "\n<p>FOOO</p>", "<p>a <strong>b</strong>\v c</p>", "", "<p>a <strong>b</strong>\v c</p>"},
118
119 {media.Builtin.HTMLType, "<p>First paragraph</p>FOOO<p>Second paragraph</p>", "<p>First paragraph</p>", "<p>Second paragraph</p>", "<p>First paragraph</p><p>Second paragraph</p>"},
120
121 {media.Builtin.ReStructuredTextType, "<div class=\"document\">\n\n\n<p>This is summary.</p>\n<p>FOOO</p>\n<p>This is content.</p>\n</div>", "<div class=\"document\">\n\n\n<p>This is summary.</p>\n</div>", "<div class=\"document\"><p>This is content.</p>\n</div>", "<div class=\"document\">\n\n\n<p>This is summary.</p>\n<p>This is content.</p>\n</div>"},
122 {media.Builtin.ReStructuredTextType, "<div class=\"document\"><p>First paragraphFOOO</p><p>Second paragraph</p></div>", "<div class=\"document\"><p>First paragraph</p></div>", "<div class=\"document\"><p>Second paragraph</p></div>", `<div class="document"><p>First paragraph</p><p>Second paragraph</p></div>`},
123
124 {media.Builtin.AsciiDocType, "<div class=\"paragraph\"><p>Summary Next Line</p></div><div class=\"paragraph\"><p>FOOO</p></div><div class=\"paragraph\"><p>Some more text</p></div>", "<div class=\"paragraph\"><p>Summary Next Line</p></div>", "<div class=\"paragraph\"><p>Some more text</p></div>", "<div class=\"paragraph\"><p>Summary Next Line</p></div><div class=\"paragraph\"><p>Some more text</p></div>"},
125 {media.Builtin.AsciiDocType, "<div class=\"paragraph\">\n<p>Summary Next Line</p>\n</div>\n<div class=\"paragraph\">\n<p>FOOO</p>\n</div>\n<div class=\"paragraph\">\n<p>Some more text</p>\n</div>\n", "<div class=\"paragraph\">\n<p>Summary Next Line</p>\n</div>", "<div class=\"paragraph\">\n<p>Some more text</p>\n</div>", "<div class=\"paragraph\">\n<p>Summary Next Line</p>\n</div>\n<div class=\"paragraph\">\n<p>Some more text</p>\n</div>"},
126 {media.Builtin.AsciiDocType, "<div><p>FOOO</p></div><div><p>First paragraph</p></div>", "", "<div><p>First paragraph</p></div>", "<div><p>First paragraph</p></div>"},
127 {media.Builtin.AsciiDocType, "<div><p>First paragraphFOOO</p></div><div><p>Second paragraph</p></div>", "<div><p>First paragraph</p></div>", "<div><p>Second paragraph</p></div>", "<div><p>First paragraph</p></div><div><p>Second paragraph</p></div>"},
128 }
129
130 for i, test := range tests {
131 summary := ExtractSummaryFromHTMLWithDivider(test.mt, test.input, divider)
132 c.Assert(summary.Summary(), qt.Equals, test.expectSummary, qt.Commentf("Summary %d", i))
133 c.Assert(summary.ContentWithoutSummary(), qt.Equals, test.expectContentWithoutSummary, qt.Commentf("ContentWithoutSummary %d", i))
134 c.Assert(summary.Content(), qt.Equals, test.expectContent, qt.Commentf("Content %d", i))
135 }
136 }
137
138 func TestExpandDivider(t *testing.T) {
139 c := qt.New(t)
140
141 for i, test := range []struct {
142 input string
143 divider string
144 ptag tagReStartEnd
145 expect string
146 expectEndMarkup string
147 }{
148 {"<p>First paragraph</p>\n<p>FOOO</p>\n<p>Second paragraph</p>", "FOOO", startEndP, "<p>FOOO</p>\n", ""},
149 {"<div class=\"paragraph\">\n<p>FOOO</p>\n</div>", "FOOO", startEndDiv, "<div class=\"paragraph\">\n<p>FOOO</p>\n</div>", ""},
150 {"<div><p>FOOO</p></div><div><p>Second paragraph</p></div>", "FOOO", startEndDiv, "<div><p>FOOO</p></div>", ""},
151 {"<div><p>First paragraphFOOO</p></div><div><p>Second paragraph</p></div>", "FOOO", startEndDiv, "FOOO", "</p></div>"},
152 {" <p> abc FOOO </p> ", "FOOO", startEndP, "FOOO", " </p>"},
153 {" <p> FOOO </p> ", "FOOO", startEndP, "<p> FOOO </p>", ""},
154 {" <p>\n \nFOOO </p> ", "FOOO", startEndP, "<p>\n \nFOOO </p>", ""},
155 {" <div> FOOO </div> ", "FOOO", startEndDiv, "<div> FOOO </div>", ""},
156 } {
157
158 l := types.LowHigh[string]{Low: strings.Index(test.input, test.divider), High: strings.Index(test.input, test.divider) + len(test.divider)}
159 e, t := expandSummaryDivider(test.input, test.ptag, l)
160 c.Assert(test.input[e.Low:e.High], qt.Equals, test.expect, qt.Commentf("[%d] Test.expect %q", i, test.input))
161 c.Assert(test.input[t.Low:t.High], qt.Equals, test.expectEndMarkup, qt.Commentf("[%d] Test.expectEndMarkup %q", i, test.input))
162 }
163 }
164
165 func TestIsProbablyHTMLToken(t *testing.T) {
166 c := qt.New(t)
167
168 for i, test := range []struct {
169 input string
170 expect bool
171 }{
172 {"<p>", true},
173 {"<p", true},
174 {"width=\"32\"", true},
175 {"width='32'", true},
176 {"<p>Æøå", false},
177 } {
178 c.Assert(isProbablyHTMLToken(test.input), qt.Equals, test.expect, qt.Commentf("[%d] Test.expect %q", i, test.input))
179 }
180 }
181
182 func BenchmarkSummaryFromHTML(b *testing.B) {
183 run := func(name, input string) {
184 b.Run(name, func(b *testing.B) {
185 for i := 0; i < b.N; i++ {
186 _ = ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 2, false)
187 }
188 })
189 }
190 run("basic", "<p>First paragraph</p><p>Second paragraph</p>")
191 run("list", "<ul>\n<li>\n<p>item 1 line 1</p>\n<p>item 1 line 2</p>\n</li>\n</ul>\n<p>bar</p>")
192 }
193
194 func BenchmarkSummaryFromHTMLWithDivider(b *testing.B) {
195 input := "<p>First paragraph</p><p>FOOO</p><p>Second paragraph</p>"
196
197 for b.Loop() {
198 summary := ExtractSummaryFromHTMLWithDivider(media.Builtin.MarkdownType, input, "FOOO")
199 if s := summary.Content(); s != "<p>First paragraph</p><p>Second paragraph</p>" {
200 b.Fatalf("unexpected content: %q", s)
201 }
202 if s := summary.ContentWithoutSummary(); s != "<p>Second paragraph</p>" {
203 b.Fatalf("unexpected content without summary: %q", s)
204 }
205 if s := summary.Summary(); s != "<p>First paragraph</p>" {
206 b.Fatalf("unexpected summary: %q", s)
207 }
208 }
209 }