Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
9 changes: 7 additions & 2 deletions src/core/best-practices.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ export function run() {
});
if (bps.length) {
if (bpSummary) {
bpSummary.appendChild(html`<h1>Best Practices Summary</h1>`);
if (summaryItems) bpSummary.appendChild(summaryItems);
const existingHeading = bpSummary.querySelector(
":scope > :is(h1, h2, h3, h4, h5, h6)"
);
if (!existingHeading) {
bpSummary.prepend(html`<h1>Best Practices Summary</h1>`);
}
if (summaryItems) bpSummary.append(summaryItems);
}
} else if (bpSummary) {
const msg = `Using best practices summary (#bp-summary) but no best practices found.`;
Expand Down
25 changes: 25 additions & 0 deletions tests/spec/core/best-practices-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,29 @@ describe("Core — Best Practices", () => {
);
expect(bps.querySelectorAll("ul li")).toHaveSize(3);
});

it("does not duplicate heading when bp-summary already has one", async () => {
const body = `
<section id="bps">
<h2>Section</h2>
<span class='practicelab'>BP1</span>
<section id='bp-summary'>
<h2>Custom Heading</h2>
</section>
</section>
`;
const ops = {
config: makeBasicConfig(),
body,
};
const doc = await makeRSDoc(ops);
const bpSummary = doc.getElementById("bp-summary");
const headings = bpSummary.querySelectorAll("h1, h2, h3, h4, h5, h6");
expect(headings).toHaveSize(1);
expect(headings[0].textContent).toContain("Custom Heading");

const listItems = bpSummary.querySelectorAll("ul li");
expect(listItems).toHaveSize(1);
expect(listItems[0].textContent.trim()).toBe("Best Practice 1: BP1");
});
});
Loading