mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
[search] filter script and style elements from search result summary text. (#12057)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
parent
b2069fb64f
commit
bf0bec3b4b
@ -109,6 +109,9 @@ Bugs fixed
|
|||||||
Patch by Bénédikt Tran.
|
Patch by Bénédikt Tran.
|
||||||
* #11894: Do not add checksums to css files if building using the htmlhelp builder.
|
* #11894: Do not add checksums to css files if building using the htmlhelp builder.
|
||||||
Patch by mkay.
|
Patch by mkay.
|
||||||
|
* #12052: Remove ``<script>`` and ``<style>`` tags from the content of search result
|
||||||
|
summary snippets.
|
||||||
|
Patch by James Addison.
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
-------
|
-------
|
||||||
|
@ -162,7 +162,9 @@ const Search = {
|
|||||||
|
|
||||||
htmlToText: (htmlString, anchor) => {
|
htmlToText: (htmlString, anchor) => {
|
||||||
const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
|
const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
|
||||||
htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() });
|
for (const removalQuery of [".headerlinks", "script", "style"]) {
|
||||||
|
htmlElement.querySelectorAll(removalQuery).forEach((el) => { el.remove() });
|
||||||
|
}
|
||||||
if (anchor) {
|
if (anchor) {
|
||||||
const anchorContent = htmlElement.querySelector(`[role="main"] ${anchor}`);
|
const anchorContent = htmlElement.querySelector(`[role="main"] ${anchor}`);
|
||||||
if (anchorContent) return anchorContent.textContent;
|
if (anchorContent) return anchorContent.textContent;
|
||||||
|
@ -61,20 +61,33 @@ describe('Basic html theme search', function() {
|
|||||||
describe("htmlToText", function() {
|
describe("htmlToText", function() {
|
||||||
|
|
||||||
const testHTML = `<html>
|
const testHTML = `<html>
|
||||||
<div class="body" role="main">
|
<body>
|
||||||
<section id="getting-started">
|
<script src="directory/filename.js"></script>
|
||||||
<h1>Getting Started</h1>
|
<div class="body" role="main">
|
||||||
<p>Some text</p>
|
<script>
|
||||||
</section>
|
console.log('dynamic');
|
||||||
<section id="other-section">
|
</script>
|
||||||
<h1>Other Section</h1>
|
<style>
|
||||||
<p>Other text</p>
|
div.body p.centered {
|
||||||
</section>
|
text-align: center;
|
||||||
<section id="yet-another-section">
|
margin-top: 25px;
|
||||||
<h1>Yet Another Section</h1>
|
}
|
||||||
<p>More text</p>
|
</style>
|
||||||
</section>
|
<!-- main content -->
|
||||||
</div>
|
<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>`;
|
</html>`;
|
||||||
|
|
||||||
it("basic case", () => {
|
it("basic case", () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user