Backtestshugorun-20261009-153657#14044
Automatic summary may generate invalid HTML with blockquotes and lists
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the core reported case. When an automatic summary would end inside a blockquote or list, each extends the cutoff until the open container elements are closed. Both add unit and integration tests that fail before the change and pass after it. Patch A goes further, in ways that bear directly on the issue's goal that `.Summary` never emit unbalanced HTML: 1. **Unbalanced input.** When the input is malformed and no balanced position exists, `Summary()` appends end tags for any elements still open. Patch B returns the raw cut in that case; its own test expects `<div><p>foo</p>` with the `<div>` left unclosed. That is the same leak the issue describes. 2. **Manual `<!--more-->` divider.** Patch A handles a divider placed inside a blockquote or list. It closes the open elements in the summary, and in `ContentWithoutSummary` it drops the now-redundant end tags or re-opens the original start tags. Patch B leaves the divider path untouched, so a divider inside a blockquote still produces leaking HTML. 3. **Parsing.** Patch A uses the standard `golang.org/x/net/html` tokenizer. It handles case, attributes, comments and raw text robustly. Patch B hand-rolls a roughly 150-line custom tag scanner, which is more code to maintain and more room for edge-case bugs. Patch A's tests cover: - Markdown blockquotes, lists, and nested lists inside blockquotes. - AsciiDoc and reStructuredText wrappers. - The unbalanced fallback. - Dividers inside blockquotes and lists, including uppercase tags and attributes. Patch B also rewrites the existing benchmark and removes its correctness assertions on Content, Summary and ContentWithoutSummary. That is unrelated churn that weakens existing checks. Overall, Patch A is more correct and complete with comparable scope risk.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
1h 5m
Wall-clock time in the container
Full test suite
No new failures
2,249 passed · 95 skipped · 2,344 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
207vs 226 merged
Changed lines, ours vs merged
The issue
View on GitHub<!--
Please do not use the issue queue for questions or troubleshooting. Unless you are certain that your issue is a software defect, use the forum:
https://discourse.gohugo.io
-->
<!-- Please answer these questions before submitting your issue. Thanks! -->
What version of Hugo are you using (hugo version)?
<pre>
$ hugo version
hugo v0.151.0+extended+withdeploy darwin/arm64 BuildDate=2025-10-02T13:30:36Z VendorInfo=brew
</pre>
Does this issue reproduce with the latest release?
Yes
Issue
Using a blockquote that extends past the .Summary cutoff causes the content below {{ .Summary }} to be treated as part of the blockquote until the end of the parent tag. Reproduction repo
<img width="848" height="652" alt="Image" src="https://github.com/user-attachments/assets/212f747c-af3c-4af5-89fe-78fe9dc04ebd" />
Hugo's .Summary may lead to blockquote end tag be below cutoff point
Explanation
In most modern browsers this html code will be parsed and rendered without errors
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<main>
<blockquote>
<p>1</p>
</main>
</body>
</html>
Leading to shown in browser as a source
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<main>
<blockquote>
<p>1</p>
<!-- This blockquote end tag was automatically inserted -->
</blockquote>
</main>
</body>
</html>
This is due to these parsing rules
An end tag whose tag name is one of: "address", "article", "aside", "blockquote", "button", "center", "details", "dialog", "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "listing", "main", "menu", "nav", "ol", "pre", "search", "section", "select", "summary", "ul"
If the stack of open elements does not have an element in scope that is an HTML element with the same tag name as that of the token, then this is a parse error; ignore the token.
Otherwise, run these steps:
Generate implied end tags.
If the current node is not an HTML element with the same tag name as that of the token, then this is a parse error.
Pop elements from the stack of open elements until an HTML element with the same tag name as the token has been popped from the stack.
To Discuss
I think the best case scenario is for .Summary to handle generation of implied tags. But I am not sure how to do that myself.
For now I plan to provide a PR with fixes for a theme and docs, to avoid confusion and errors
The two patches
resources/page/page_markup.goindex 8f58fc2ab..1f863636e 100644@@ -25,6 +25,7 @@ import ( "github.com/gohugoio/hugo/markup/tableofcontents" "github.com/gohugoio/hugo/media" "github.com/gohugoio/hugo/tpl"+ "golang.org/x/net/html" ) type Content interface {@@ -112,9 +113,25 @@ func (s HtmlSummary) Content() string { } func (s HtmlSummary) Summary() string {+ var ss string if s.Divider.IsZero() {- return s.trimSpace(s.wrap(s.Value(s.SummaryLowHigh)))+ ss = s.Value(s.SummaryLowHigh)+ } else {+ ss = s.dividerSummary()+ }+ // The divider may be placed inside e.g. a blockquote or a list,+ // and the automatic summary may be cut inside unbalanced HTML,+ // so close any elements left open.+ open := openHTMLElements(ss)+ for i := len(open) - 1; i >= 0; i-- {+ ss += "</" + open[i].name + ">" }+ return s.trimSpace(s.wrap(ss))+}++// dividerSummary returns the summary part of the source when a divider is set,+// without any closing of open elements.+func (s HtmlSummary) dividerSummary() string { ss := s.source[s.SummaryLowHigh.Low:s.Divider.Low] if s.SummaryLowHigh.High > s.Divider.High { ss += s.source[s.Divider.High:s.SummaryLowHigh.High]@@ -122,7 +139,7 @@ func (s HtmlSummary) Summary() string { if !s.SummaryEndTag.IsZero() { ss += s.Value(s.SummaryEndTag) }- return s.trimSpace(s.wrap(ss))+ return ss } func (s HtmlSummary) ContentWithoutSummary() string {@@ -132,10 +149,38 @@ func (s HtmlSummary) ContentWithoutSummary() string { } return s.trimSpace(s.wrapLeft(s.source[s.SummaryLowHigh.High:])) }++ var rest string if s.SummaryEndTag.IsZero() {- return s.trimSpace(s.wrapLeft(s.source[s.Divider.High:]))+ rest = s.source[s.Divider.High:]+ } else {+ rest = s.source[s.SummaryEndTag.High:]+ }++ // The summary may have left elements open (closed in Summary).+ // Drop end tags of those directly following the summary,+ // and reopen the rest so their end tags have a matching start tag.+ open := openHTMLElements(s.dividerSummary())+ for len(open) > 0 {+ trimmed := strings.TrimLeftFunc(rest, unicode.IsSpace)+ z := html.NewTokenizer(strings.NewReader(trimmed))+ if z.Next() != html.EndTagToken {+ break+ }+ // Raw must be read before calling TagName, which may modify the underlying buffer.+ n := len(z.Raw())+ if name, _ := z.TagName(); string(name) != open[len(open)-1].name {+ break+ }+ rest = trimmed[n:]+ open = open[:len(open)-1]+ }+ var reopen strings.Builder+ for _, el := range open {+ reopen.WriteString(el.startTag) }- return s.trimSpace(s.wrapLeft(s.source[s.SummaryEndTag.High:]))++ return s.trimSpace(s.wrapLeft(reopen.String() + rest)) } func (s HtmlSummary) Truncated() bool {@@ -234,9 +279,16 @@ func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK boo } if count >= numWords {+ summaryHigh := j + closingIndex + len(ptag.tagName) + 3+ // The paragraph may be nested inside e.g. a blockquote or a list.+ // Make sure we don't cut the summary inside such an element.+ low := result.WrapperStart.High+ if end := endOfOpenHTMLElements(input[low:high], summaryHigh-low); end != -1 {+ summaryHigh = low + end+ } result.SummaryLowHigh = types.LowHigh[string]{- Low: result.WrapperStart.High,- High: j + closingIndex + len(ptag.tagName) + 3,+ Low: low,+ High: summaryHigh, } return }@@ -360,3 +412,76 @@ func expandSummaryDivider(s string, re tagReStartEnd, divider types.LowHigh[stri return divider, endMarkup }++// voidHTMLElements are elements that never have an end tag.+var voidHTMLElements = map[string]bool{+ "area": true, "base": true, "br": true, "col": true, "embed": true, "hr": true, "img": true,+ "input": true, "link": true, "meta": true, "param": true, "source": true, "track": true, "wbr": true,+}++type htmlElement struct {+ name string+ startTag string // the raw start tag, including attributes.+}++// htmlElementTracker tokenizes HTML and keeps track of the elements opened but not yet closed.+type htmlElementTracker struct {+ z *html.Tokenizer+ open []htmlElement+ offset int // position in the input right after the current token.+}++func newHTMLElementTracker(s string) *htmlElementTracker {+ return &htmlElementTracker{z: html.NewTokenizer(strings.NewReader(s))}+}++// next advances to the next token. It returns false when the input is exhausted+// (the input is read from a strings.Reader, so io.EOF is the only possible error).+func (t *htmlElementTracker) next() bool {+ tt := t.z.Next()+ if tt == html.ErrorToken {+ return false+ }+ // Raw must be copied before calling TagName, which may modify the underlying buffer.+ raw := string(t.z.Raw())+ t.offset += len(raw)++ switch tt {+ case html.StartTagToken:+ name, _ := t.z.TagName()+ if n := string(name); !voidHTMLElements[n] {+ t.open = append(t.open, htmlElement{name: n, startTag: raw})+ }+ case html.EndTagToken:+ name, _ := t.z.TagName()+ n := string(name)+ for i := len(t.open) - 1; i >= 0; i-- {+ if t.open[i].name == n {+ t.open = t.open[:i]+ break+ }+ }+ }+ return true+}++// openHTMLElements returns the elements opened but not closed in s, outermost first.+func openHTMLElements(s string) []htmlElement {+ t := newHTMLElementTracker(s)+ for t.next() {+ }+ return t.open+}++// endOfOpenHTMLElements returns the first position in s at or after pos+// where all elements opened in s are closed.+// It returns -1 if no such position exists, e.g. for malformed HTML.+func endOfOpenHTMLElements(s string, pos int) int {+ t := newHTMLElementTracker(s)+ for t.next() {+ if t.offset >= pos && len(t.open) == 0 {+ return t.offset+ }+ }+ return -1+}resources/page/page_markup_integration_test.goindex 412850b3a..05503b86d 100644@@ -245,6 +245,62 @@ Summary Truncated: {{ .Truncated }}| ) } +// Issue 14044.+func TestSummaryNestedBlockquotesAndLists(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+summaryLength=2+-- content/blockquote.md --+---+title: "Blockquote"+---+> First paragraph.+>+> Second paragraph.++Third paragraph.+-- content/list.md --+---+title: "List"+---+- First item.++- Second item.++Third paragraph.+-- content/manual.md --+---+title: "Manual"+---+> First paragraph.+>+> <!--more-->+>+> Second paragraph.++Third paragraph.+-- layouts/single.html --+Summary: {{ .Summary }}|+ContentWithoutSummary: {{ .ContentWithoutSummary }}|+`+ b := hugolib.Test(t, files)++ b.AssertFileContentExact("public/blockquote/index.html",+ "Summary: <blockquote>\n<p>First paragraph.</p>\n<p>Second paragraph.</p>\n</blockquote>|",+ "ContentWithoutSummary: <p>Third paragraph.</p>|",+ )+ b.AssertFileContentExact("public/list/index.html",+ "Summary: <ul>\n<li>\n<p>First item.</p>\n</li>\n<li>\n<p>Second item.</p>\n</li>\n</ul>|",+ "ContentWithoutSummary: <p>Third paragraph.</p>|",+ )+ b.AssertFileContentExact("public/manual/index.html",+ "Summary: <blockquote>\n<p>First paragraph.</p>\n</blockquote>|",+ "ContentWithoutSummary: <blockquote>\n<p>Second paragraph.</p>\n</blockquote>\n<p>Third paragraph.</p>|",+ )+}+ func TestPageMarkupWithoutSummaryRST(t *testing.T) { t.Parallel() if !rst.Supports() {resources/page/page_markup_test.goindex fc1102434..39fd9a603 100644@@ -40,6 +40,14 @@ func TestExtractSummaryFromHTML(t *testing.T) { {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>"}, {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>"}, {media.Builtin.MarkdownType, "<p>这是中文,全中文</p><p>a这是中文,全中文</p>", true, 5, "<p>这是中文,全中文</p>", "<p>a这是中文,全中文</p>"},+ // Issue 14044: the summary must not be cut inside a blockquote or a list.+ {media.Builtin.MarkdownType, "<blockquote>\n<p>First paragraph</p>\n<p>Second paragraph</p>\n</blockquote>\n<p>Third paragraph</p>", false, 2, "<blockquote>\n<p>First paragraph</p>\n<p>Second paragraph</p>\n</blockquote>", "<p>Third paragraph</p>"},+ {media.Builtin.MarkdownType, "<ul>\n<li>\n<p>First paragraph</p>\n</li>\n<li>\n<p>Second paragraph</p>\n</li>\n</ul>\n<p>Third paragraph</p>", false, 2, "<ul>\n<li>\n<p>First paragraph</p>\n</li>\n<li>\n<p>Second paragraph</p>\n</li>\n</ul>", "<p>Third paragraph</p>"},+ {media.Builtin.MarkdownType, "<ol>\n<li><p>First paragraph</p>\n<blockquote>\n<p>Second paragraph</p>\n</blockquote>\n</li>\n</ol>\n<p>Third paragraph</p>", false, 2, "<ol>\n<li><p>First paragraph</p>\n<blockquote>\n<p>Second paragraph</p>\n</blockquote>\n</li>\n</ol>", "<p>Third paragraph</p>"},+ {media.Builtin.AsciiDocType, "<div class=\"quoteblock\"><blockquote><div class=\"paragraph\"><p>First paragraph</p></div><div class=\"paragraph\"><p>Second paragraph</p></div></blockquote></div><div class=\"paragraph\"><p>Third paragraph</p></div>", false, 2, "<div class=\"quoteblock\"><blockquote><div class=\"paragraph\"><p>First paragraph</p></div><div class=\"paragraph\"><p>Second paragraph</p></div></blockquote></div>", "<div class=\"paragraph\"><p>Third paragraph</p></div>"},+ {media.Builtin.ReStructuredTextType, "<div class=\"document\"><blockquote><p>First paragraph</p><p>Second paragraph</p></blockquote><p>Third paragraph</p></div>", false, 2, "<div class=\"document\"><blockquote><p>First paragraph</p><p>Second paragraph</p></blockquote></div>", "<div class=\"document\"><p>Third paragraph</p></div>"},+ // Unbalanced HTML: fall back to cutting at the paragraph, closing open elements.+ {media.Builtin.MarkdownType, "<div><p>First paragraph</p><p>Second paragraph</p>", false, 2, "<div><p>First paragraph</p></div>", "<p>Second paragraph</p>"}, } for i, test := range tests {@@ -117,6 +125,12 @@ func TestExtractSummaryFromHTMLWithDivider(t *testing.T) { {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>"}, {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>"}, {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>"},++ // Issue 14044: divider inside a blockquote or a list.+ {media.Builtin.MarkdownType, "<blockquote>\n<p>First paragraph</p>\n<p>FOOO</p>\n<p>Second paragraph</p>\n</blockquote>", "<blockquote>\n<p>First paragraph</p>\n</blockquote>", "<blockquote><p>Second paragraph</p>\n</blockquote>", "<blockquote>\n<p>First paragraph</p>\n<p>Second paragraph</p>\n</blockquote>"},+ {media.Builtin.MarkdownType, "<blockquote class=\"q\">\n<p>First paragraphFOOO</p>\n</blockquote>\n<p>Second paragraph</p>", "<blockquote class=\"q\">\n<p>First paragraph</p></blockquote>", "<p>Second paragraph</p>", "<blockquote class=\"q\">\n<p>First paragraph</p>\n</blockquote>\n<p>Second paragraph</p>"},+ {media.Builtin.MarkdownType, "<BLOCKQUOTE>\n<p>First paragraph</p>\n<p>FOOO</p>\n</BLOCKQUOTE >\n<p>Second paragraph</p>", "<BLOCKQUOTE>\n<p>First paragraph</p>\n</blockquote>", "<p>Second paragraph</p>", "<BLOCKQUOTE>\n<p>First paragraph</p>\n</BLOCKQUOTE >\n<p>Second paragraph</p>"},+ {media.Builtin.MarkdownType, "<ul>\n<li>\n<p>First item</p>\n</li>\n<li>\n<p>Second itemFOOO</p>\n</li>\n<li>\n<p>Third item</p>\n</li>\n</ul>", "<ul>\n<li>\n<p>First item</p>\n</li>\n<li>\n<p>Second item</p></li></ul>", "<ul>\n<li>\n<p>Third item</p>\n</li>\n</ul>", "<ul>\n<li>\n<p>First item</p>\n</li>\n<li>\n<p>Second item</p>\n</li>\n<li>\n<p>Third item</p>\n</li>\n</ul>"}, } for i, test := range tests {
hugolib/page_test.goindex a50ff316b..88a6b22f3 100644@@ -788,6 +788,36 @@ Content: {{ .Content }}| "Content: <p>This is <strong>summary</strong>.") } +// See issue 14044.+func TestSummaryAutoBalancesContainerTags(t *testing.T) {+ t.Parallel()++ files := `+-- hugo.toml --+disableKinds = ['home','rss','section','sitemap','taxonomy','term']+summaryLength = 1+-- content/p1.md --+---+title: p1+---+> foo+-- content/p2.md --+---+title: p2+---+- item 1 line 1++ item 1 line 2+-- layouts/page.html --+|{{ .Summary }}|+`++ b := Test(t, files)++ b.AssertFileContent("public/p1/index.html", "|<blockquote>\n<p>foo</p>\n</blockquote>|")+ b.AssertFileContent("public/p2/index.html", "|<ul>\n<li>\n<p>item 1 line 1</p>\n<p>item 1 line 2</p>\n</li>\n</ul>|")+}+ // #2973 func TestSummaryWithHTMLTagsOnNextLine(t *testing.T) { htesting.SkipSlowTestUnlessCI(t)resources/page/page_markup.goindex 8f58fc2ab..572822b0b 100644@@ -234,9 +234,11 @@ func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK boo } if count >= numWords {+ summaryHigh := j + closingIndex + len(ptag.tagName) + 3+ summaryHigh = expandSummaryHighToBalancedHTML(input, result.WrapperStart.High, summaryHigh, high) result.SummaryLowHigh = types.LowHigh[string]{ Low: result.WrapperStart.High,- High: j + closingIndex + len(ptag.tagName) + 3,+ High: summaryHigh, } return }@@ -253,6 +255,167 @@ func ExtractSummaryFromHTML(mt media.Type, input string, numWords int, isCJK boo return } +// expandSummaryHighToBalancedHTML moves high forward until every element+// opened in input[low:high] is closed, so the summary does not end inside+// e.g. a blockquote or list item.+func expandSummaryHighToBalancedHTML(input string, low, high, maxHigh int) int {+ if low >= high || high >= maxHigh {+ return high+ }++ var buf [16]string+ stack := buf[:0]+ sc := htmlTagScanner{s: input, pos: low}++ for {+ name, end, ok := sc.next(high)+ if !ok {+ break+ }+ stack = pushOrPopHTMLStack(stack, name, end)+ }++ if len(stack) == 0 {+ return high+ }++ for {+ name, end, ok := sc.next(maxHigh)+ if !ok {+ return high+ }+ stack = pushOrPopHTMLStack(stack, name, end)+ if len(stack) == 0 {+ return sc.pos+ }+ }+}++func pushOrPopHTMLStack(stack []string, name string, end bool) []string {+ if end {+ for i := len(stack) - 1; i >= 0; i-- {+ if stack[i] == name {+ return stack[:i]+ }+ }+ return stack+ }+ if isVoidHTMLElement(name) {+ return stack+ }+ return append(stack, name)+}++// htmlTagScanner is a minimal, allocation-free scanner for start and end tags.+// It skips comments, quoted attribute values and raw text elements.+type htmlTagScanner struct {+ s string+ pos int+}++// next returns the lowercased name of the next tag before limit and+// whether it is an end tag. ok is false when no more tags are found.+func (sc *htmlTagScanner) next(limit int) (name string, end, ok bool) {+ for {+ i := strings.IndexByte(sc.s[sc.pos:limit], '<')+ if i == -1 {+ sc.pos = limit+ return "", false, false+ }+ sc.pos += i + 1+ if sc.pos >= limit {+ return "", false, false+ }++ end = false+ switch c := sc.s[sc.pos]; c {+ case '!', '?':+ if strings.HasPrefix(sc.s[sc.pos:limit], "!--") {+ sc.skipPast(limit, "-->")+ } else {+ sc.skipPast(limit, ">")+ }+ continue+ case '/':+ end = true+ sc.pos+++ }++ start := sc.pos+ for sc.pos < limit && isHTMLTagNameByte(sc.s[sc.pos]) {+ sc.pos+++ }+ if sc.pos == start {+ continue+ }+ name = strings.ToLower(sc.s[start:sc.pos])++ selfClosing := false+ for sc.pos < limit {+ c := sc.s[sc.pos]+ sc.pos+++ switch c {+ case '"', '\'':+ sc.skipPast(limit, string(c))+ case '/':+ selfClosing = true+ case '>':+ if selfClosing {+ continue+ }+ if !end && isRawTextHTMLElement(name) {+ sc.skipPastEndTag(limit, name)+ }+ return name, end, true+ default:+ selfClosing = false+ }+ }+ return name, end, true+ }+}++func (sc *htmlTagScanner) skipPast(limit int, sep string) {+ if i := strings.Index(sc.s[sc.pos:limit], sep); i == -1 {+ sc.pos = limit+ } else {+ sc.pos += i + len(sep)+ }+}++func (sc *htmlTagScanner) skipPastEndTag(limit int, name string) {+ for {+ sc.skipPast(limit, "</")+ if sc.pos >= limit {+ return+ }+ if rest := sc.s[sc.pos:limit]; len(rest) > len(name) && strings.EqualFold(rest[:len(name)], name) && !isHTMLTagNameByte(rest[len(name)]) {+ sc.pos -= 2+ return+ }+ }+}++func isHTMLTagNameByte(c byte) bool {+ return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-'+}++func isRawTextHTMLElement(name string) bool {+ switch name {+ case "script", "style", "textarea", "title", "iframe", "xmp", "noembed", "noframes":+ return true+ }+ return false+}++func isVoidHTMLElement(name string) bool {+ switch name {+ case "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr":+ return true+ }+ return false+}+ // ExtractSummaryFromHTMLWithDivider extracts a summary from the given HTML content with // a manual summary divider. func ExtractSummaryFromHTMLWithDivider(mt media.Type, input, divider string) (result HtmlSummary) {resources/page/page_markup_test.goindex fc1102434..ee9fb1bbe 100644@@ -38,6 +38,14 @@ func TestExtractSummaryFromHTML(t *testing.T) { {media.Builtin.MarkdownType, "<p>First paragraph</p>", false, 10, "<p>First paragraph</p>", ""}, {media.Builtin.MarkdownType, "<p>First paragraph</p><p>Second paragraph</p>", false, 2, "<p>First paragraph</p>", "<p>Second paragraph</p>"}, {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>"},+ {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>"},+ {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>"},+ {media.Builtin.HTMLType, "<div><p>foo</p></div><p>bar</p>", false, 1, "<div><p>foo</p></div>", "<p>bar</p>"},+ {media.Builtin.HTMLType, "<DIV><p>foo</p></DIV><p>bar</p>", false, 1, "<DIV><p>foo</p></DIV>", "<p>bar</p>"},+ {media.Builtin.HTMLType, "<div><!-- <span> --><p>foo</p></div><p>bar</p>", false, 1, "<div><!-- <span> --><p>foo</p></div>", "<p>bar</p>"},+ {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>"},+ {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>"},+ {media.Builtin.HTMLType, "<div><p>foo</p><p>bar</p>", false, 1, "<div><p>foo</p>", "<p>bar</p>"}, {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>"}, {media.Builtin.MarkdownType, "<p>这是中文,全中文</p><p>a这是中文,全中文</p>", true, 5, "<p>这是中文,全中文</p>", "<p>a这是中文,全中文</p>"}, }@@ -172,25 +180,18 @@ func TestIsProbablyHTMLToken(t *testing.T) { } func BenchmarkSummaryFromHTML(b *testing.B) {-- input := "<p>First paragraph</p><p>Second paragraph</p>"-- for b.Loop() {- summary := ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 2, false)- if s := summary.Content(); s != input {- b.Fatalf("unexpected content: %q", s)- }- if s := summary.ContentWithoutSummary(); s != "<p>Second paragraph</p>" {- b.Fatalf("unexpected content without summary: %q", s)- }- if s := summary.Summary(); s != "<p>First paragraph</p>" {- b.Fatalf("unexpected summary: %q", s)- }+ run := func(name, input string) {+ b.Run(name, func(b *testing.B) {+ for i := 0; i < b.N; i++ {+ _ = ExtractSummaryFromHTML(media.Builtin.MarkdownType, input, 2, false)+ }+ }) }+ run("basic", "<p>First paragraph</p><p>Second paragraph</p>")+ 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>") } func BenchmarkSummaryFromHTMLWithDivider(b *testing.B) {- input := "<p>First paragraph</p><p>FOOO</p><p>Second paragraph</p>" for b.Loop() {