This repository was archived by the owner on Jun 7, 2024. It is now read-only.
File tree 2 files changed +24
-5
lines changed
2 files changed +24
-5
lines changed Original file line number Diff line number Diff line change 1
1
// @ts -check
2
2
/**
3
3
* 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.
5
5
* Set `conf.highlightVars = true` to enable.
6
6
* Removes highlights from <var> if clicked anywhere else.
7
7
* All is done while keeping in mind that exported html stays clean
@@ -73,12 +73,13 @@ function getHighlightColor(target) {
73
73
74
74
function highlightVars ( varElem ) {
75
75
const textContent = norm ( varElem . textContent ) ;
76
- const parent = varElem . closest ( "section" ) ;
76
+ const parent = varElem . closest ( ".algorithm, section" ) ;
77
77
const highlightColor = getHighlightColor ( varElem ) ;
78
78
79
79
const varsToHighlight = [ ...parent . querySelectorAll ( "var" ) ] . filter (
80
80
el =>
81
- norm ( el . textContent ) === textContent && el . closest ( "section" ) === parent
81
+ norm ( el . textContent ) === textContent &&
82
+ el . closest ( ".algorithm, section" ) === parent
82
83
) ;
83
84
84
85
// update availability of highlight color
Original file line number Diff line number Diff line change @@ -14,18 +14,29 @@ describe("Core - highlightVars", () => {
14
14
<ol>
15
15
<li><var id="section1-bar">bar</var>
16
16
<li><var>a
17
- foo</foo >
17
+ foo</var >
18
18
</ol>
19
19
</section>
20
20
21
21
<section id="section2">
22
22
<p><var id="section2-foo">a foo</var></p>
23
23
<ol>
24
- <li><var>a foo</foo >
24
+ <li><var>a foo</var >
25
25
<li><var id="section2-bar">bar</var>
26
26
</ol>
27
27
</section>
28
28
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
+
29
40
<section id="overmatch">
30
41
<p><var id="level-1">foo</var></p>
31
42
<p><var id="level-1-1">foo</var></p>
@@ -74,6 +85,13 @@ describe("Core - highlightVars", () => {
74
85
expect ( highlightedSec2 ) . toHaveSize ( 2 ) ;
75
86
} ) ;
76
87
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
+
77
95
it ( "doesn't overmatch outside its own section's vars" , async ( ) => {
78
96
const doc = await makeDoc ( ) ;
79
97
expect ( doc . querySelector ( "var.respec-hl" ) ) . toBeNull ( ) ;
You can’t perform that action at this time.
0 commit comments