Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML Search: filter script and style elements from search result summary text. #12057

Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Bugs fixed
Patch by Bénédikt Tran.
* #11894: Do not add checksums to css files if building using the htmlhelp builder.
Patch by mkay.
* #12052: Remove ``<script>`` and ``<style>`` tags from the content of search result
summary snippets.
Patch by James Addison.

Testing
-------
Expand Down
2 changes: 2 additions & 0 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ const Search = {
htmlToText: (htmlString, anchor) => {
const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() });
jayaddison marked this conversation as resolved.
Show resolved Hide resolved
htmlElement.querySelectorAll("script").forEach((el) => { el.remove() });
htmlElement.querySelectorAll("style").forEach((el) => { el.remove() });
if (anchor) {
const anchorContent = htmlElement.querySelector(anchor);
if (anchorContent) return anchorContent.textContent;
Expand Down
41 changes: 27 additions & 14 deletions tests/js/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,33 @@ describe('Basic html theme search', function() {
describe("htmlToText", function() {

const testHTML = `<html>
<div class="body" role="main">
<section id="getting-started">
<h1>Getting Started</h1>
<p>Some text</p>
</section>
<section id="other-section">
<h1>Other Section</h1>
<p>Other text</p>
</section>
<section id="yet-another-section">
<h1>Yet Another Section</h1>
<p>More text</p>
</section>
</div>
<body>
<script src="directory/filename.js"></script>
<div class="body" role="main">
<script>
console.log('dynamic');
</script>
<style>
div.body p.centered {
text-align: center;
margin-top: 25px;
}
</style>
<!-- main content -->
<section id="getting-started">
<h1>Getting Started</h1>
<p>Some text</p>
</section>
<section id="other-section">
<h1>Other Section</h1>
<p>Other text</p>
</section>
<section id="yet-another-section">
<h1>Yet Another Section</h1>
<p>More text</p>
</section>
</div>
</body>
</html>`;

it("basic case", () => {
Expand Down