html: ensure <search> tag closes <p> and update tests

This change ensures that the <search> tag correctly closes an open <p> tag when encountered during parsing.

Changes:
- Added <search> to the list of elements that should close an open <p> tag in parse.go.
- Updated the second list in parse.go to ensure consistency.
- Updated html/atom/gen.go, table.go, and table_test.go accordingly.
- Modified parse_test.go to use strings.Builder instead of bytes.Buffer.
- Updated test error messages to follow Go’s conventions.
- Fixed an accidental colon in the comment in parse.go.

Change-Id: I5835da69f6bb0e14c483e55b7ae82915ae958dc1
Reviewed-on: https://go-review.googlesource.com/c/net/+/655457
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Pukki
2025-03-06 15:22:12 -03:00
committed by Gopher Robot
parent 09731f9bf9
commit 312450e473
5 changed files with 650 additions and 629 deletions

View File

@@ -540,6 +540,7 @@ var attributes = []string{
"scope",
"scoped",
"seamless",
"search",
"selected",
"shape",
"size",

File diff suppressed because it is too large Load Diff

View File

@@ -315,6 +315,7 @@ var testAtomList = []string{
"scoped",
"script",
"seamless",
"search",
"section",
"select",
"selected",

View File

@@ -924,7 +924,7 @@ func inBodyIM(p *parser) bool {
p.addElement()
p.im = inFramesetIM
return true
case a.Address, a.Article, a.Aside, a.Blockquote, a.Center, a.Details, a.Dialog, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Main, a.Menu, a.Nav, a.Ol, a.P, a.Section, a.Summary, a.Ul:
case a.Address, a.Article, a.Aside, a.Blockquote, a.Center, a.Details, a.Dialog, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Main, a.Menu, a.Nav, a.Ol, a.P, a.Search, a.Section, a.Summary, a.Ul:
p.popUntil(buttonScope, a.P)
p.addElement()
case a.H1, a.H2, a.H3, a.H4, a.H5, a.H6:
@@ -1136,7 +1136,7 @@ func inBodyIM(p *parser) bool {
return false
}
return true
case a.Address, a.Article, a.Aside, a.Blockquote, a.Button, a.Center, a.Details, a.Dialog, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Listing, a.Main, a.Menu, a.Nav, a.Ol, a.Pre, a.Section, a.Summary, a.Ul:
case a.Address, a.Article, a.Aside, a.Blockquote, a.Button, a.Center, a.Details, a.Dialog, a.Dir, a.Div, a.Dl, a.Fieldset, a.Figcaption, a.Figure, a.Footer, a.Header, a.Hgroup, a.Listing, a.Main, a.Menu, a.Nav, a.Ol, a.Pre, a.Search, a.Section, a.Summary, a.Ul:
p.popUntil(defaultScope, p.tok.DataAtom)
case a.Form:
if p.oe.contains(a.Template) {

View File

@@ -476,6 +476,23 @@ func TestParseFragmentForeignContentTemplates(t *testing.T) {
}
}
func TestSearchTagClosesP(t *testing.T) {
data := `<p>Unclosed paragraph<search>Search content</search>`
node, err := Parse(strings.NewReader(data))
if err != nil {
t.Fatalf("Error parsing HTML: %v", err)
}
var builder strings.Builder
Render(&builder, node)
output := builder.String()
expected := `<html><head></head><body><p>Unclosed paragraph</p><search>Search content</search></body></html>`
if output != expected {
t.Errorf("Parse(%q) = %q, want %q", data, output, expected)
}
}
func BenchmarkParser(b *testing.B) {
buf, err := os.ReadFile("testdata/go1.html")
if err != nil {