diff --git a/src/Parser.spec.ts b/src/Parser.spec.ts index 28dad2b1..6d315ce3 100644 --- a/src/Parser.spec.ts +++ b/src/Parser.spec.ts @@ -179,6 +179,43 @@ describe("API", () => { p.write("<__proto__>"); }); + it("should implicitly close table sections when another section opens", () => { + const onopentagname = vi.fn(); + const onclosetag = vi.fn(); + + // must auto-close + new Parser({ onopentagname, onclosetag }).end( + "
F
B
", + ); + + expect(onclosetag).toHaveBeenCalledWith("tfoot", true); + const tfootClose = onclosetag.mock.calls.findIndex( + ([name]) => name === "tfoot", + ); + const tbodyOpen = onopentagname.mock.calls.findIndex( + ([name]) => name === "tbody", + ); + expect(tfootClose).toBeLessThan(tbodyOpen); + }); + + it("should implicitly close when opens", () => { + const onopentagname = vi.fn(); + const onclosetag = vi.fn(); + + new Parser({ onopentagname, onclosetag }).end( + "
B
H
", + ); + + expect(onclosetag).toHaveBeenCalledWith("tbody", true); + const tbodyClose = onclosetag.mock.calls.findIndex( + ([name]) => name === "tbody", + ); + const theadOpen = onopentagname.mock.calls.findIndex( + ([name]) => name === "thead", + ); + expect(tbodyClose).toBeLessThan(theadOpen); + }); + it("should implicitly close when opens", () => { const onclosetag = vi.fn(); const onopentagname = vi.fn(); @@ -190,10 +227,10 @@ describe("API", () => { // must auto-close , making them siblings per the HTML spec expect(onclosetag).toHaveBeenCalledWith("td", true); const tdClose = onclosetag.mock.calls.findIndex( - ([name]: [string]) => name === "td", + ([name]) => name === "td", ); const thOpen = onopentagname.mock.calls.findIndex( - ([name]: [string]) => name === "th", + ([name]) => name === "th", ); expect(tdClose).toBeLessThan(thOpen); }); diff --git a/src/Parser.ts b/src/Parser.ts index 875058dd..33050243 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -13,7 +13,7 @@ const formTags = new Set([ ]); const pTag = new Set(["p"]); const headingTags = new Set(["h1", "h2", "h3", "h4", "h5", "h6", "p"]); -const tableSectionTags = new Set(["thead", "tbody"]); +const tableSectionTags = new Set(["thead", "tbody", "tfoot", "tr", "td", "th"]); const ddtTags = new Set(["dd", "dt"]); const rtpTags = new Set(["rt", "rp"]); @@ -64,6 +64,7 @@ const openImpliesClose = new Map>([ ["ul", pTag], ["rt", rtpTags], ["rp", rtpTags], + ["thead", tableSectionTags], ["tbody", tableSectionTags], ["tfoot", tableSectionTags], ]);