Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit cea7e27

Browse files
authoredNov 17, 2023
fix(core/highlight-vars): scope highlighted var to .algorithm container (speced#4585)
1 parent faa96aa commit cea7e27

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed
 

‎src/core/highlight-vars.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22
/**
33
* Module core/highlight-vars
4-
* Highlights occurrences of a <var> within a section on click.
4+
* Highlights occurrences of a <var> within the algorithm or the encompassing section on click.
55
* Set `conf.highlightVars = true` to enable.
66
* Removes highlights from <var> if clicked anywhere else.
77
* All is done while keeping in mind that exported html stays clean
@@ -73,12 +73,13 @@ function getHighlightColor(target) {
7373

7474
function highlightVars(varElem) {
7575
const textContent = norm(varElem.textContent);
76-
const parent = varElem.closest("section");
76+
const parent = varElem.closest(".algorithm, section");
7777
const highlightColor = getHighlightColor(varElem);
7878

7979
const varsToHighlight = [...parent.querySelectorAll("var")].filter(
8080
el =>
81-
norm(el.textContent) === textContent && el.closest("section") === parent
81+
norm(el.textContent) === textContent &&
82+
el.closest(".algorithm, section") === parent
8283
);
8384

8485
// update availability of highlight color

‎tests/unit/core/highlight-vars-spec.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,29 @@ describe("Core - highlightVars", () => {
1414
<ol>
1515
<li><var id="section1-bar">bar</var>
1616
<li><var>a
17-
foo</foo>
17+
foo</var>
1818
</ol>
1919
</section>
2020
2121
<section id="section2">
2222
<p><var id="section2-foo">a foo</var></p>
2323
<ol>
24-
<li><var>a foo</foo>
24+
<li><var>a foo</var>
2525
<li><var id="section2-bar">bar</var>
2626
</ol>
2727
</section>
2828
29+
<section id="section3">
30+
<ol class="algorithm">
31+
<li><var id="section3-foo">a foo</var></li>
32+
<li><var>a foo</var>
33+
</ol>
34+
<ol class="algorithm">
35+
<li><var>a foo</var>
36+
</ol>
37+
</section>
38+
39+
2940
<section id="overmatch">
3041
<p><var id="level-1">foo</var></p>
3142
<p><var id="level-1-1">foo</var></p>
@@ -74,6 +85,13 @@ describe("Core - highlightVars", () => {
7485
expect(highlightedSec2).toHaveSize(2);
7586
});
7687

88+
it("highlights variables only in current algorithm container", async () => {
89+
const doc = await makeDoc();
90+
doc.getElementById("section3-foo").click();
91+
const highlightedSec3 = doc.querySelectorAll("#section3 var.respec-hl");
92+
expect(highlightedSec3).toHaveSize(2);
93+
});
94+
7795
it("doesn't overmatch outside its own section's vars", async () => {
7896
const doc = await makeDoc();
7997
expect(doc.querySelector("var.respec-hl")).toBeNull();

0 commit comments

Comments
 (0)