Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

// <tbody> must auto-close <tfoot>
new Parser({ onopentagname, onclosetag }).end(
"<table><tfoot><tr><td>F<tbody><tr><td>B</table>",
);

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 <tbody> when <thead> opens", () => {
const onopentagname = vi.fn();
const onclosetag = vi.fn();

new Parser({ onopentagname, onclosetag }).end(
"<table><tbody><tr><td>B<thead><tr><th>H</table>",
);

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 <td> when <th> opens", () => {
const onclosetag = vi.fn();
const onopentagname = vi.fn();
Expand All @@ -190,10 +227,10 @@ describe("API", () => {
// <th> must auto-close <td>, 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);
});
Expand Down
3 changes: 2 additions & 1 deletion src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);

Expand Down Expand Up @@ -64,6 +64,7 @@ const openImpliesClose = new Map<string, Set<string>>([
["ul", pTag],
["rt", rtpTags],
["rp", rtpTags],
["thead", tableSectionTags],
["tbody", tableSectionTags],
["tfoot", tableSectionTags],
]);
Expand Down
Loading