Skip to content

Commit b779c13

Browse files
Refactor heading anchors script
Replace hash symbol with section symbol Also exclude headings that have a hidden/visuallyhidden ancestor, or themselves are hidden/visuallyhidden
1 parent 6b014ef commit b779c13

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

assets-src/js/main/heading-anchors.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/**
22
* Add anchor links to any H2 - H6 within the <main> element that:
33
* - are not children of a <nav> element
4-
* - do not have an ancestor with the data-anchor="no" attribute
5-
* - do not themselves have the data-anchor="no" attribute
4+
* - do not have an ancestor with the data-anchor="no" attribute, with
5+
* the `hidden` attribute or with the .visuallyhidden class
6+
* - do not themselves have the data-anchor="no" attribute, the `hidden`
7+
* attribute or the .visuallyhidden class
68
*
7-
* If a heading does not already possess an ID, use regular expressions
8-
* on the textContent of the heading to generate a string that is valid to
9+
* If a heading does not already possess an ID, use regular expressions on
10+
* the textContent of the heading to generate a string that is valid to
911
* use for both the heading ID and the anchor href. Supports non-Latin
1012
* scripts by matching any Unicode letter - \p{L} - or number - \p{N}. The
1113
* u flag enables Unicode matching, to support characters from any script.
@@ -29,13 +31,21 @@ let headingAnchors = function () {
2931
// Filter out headings that:
3032
// - Are not children of <nav>
3133
// - Do not have an ancestor with the data-anchor="no" attribute
34+
// - Do not have an ancestor with the `hidden` attribute
35+
// - Do not have an ancestor with the `.visuallyhidden` class
3236
// - Do not themselves have the data-anchor="no" attribute
37+
// - Do not themselves have the `hidden` attribute
38+
// - Do not themselves have the `.visuallyhidden` class
3339
let targetedHeadings = headingsArray.filter(function(heading) {
3440
let insideNav = heading.closest('nav') !== null;
3541
let parentHasDataAttribute = heading.closest('[data-anchor="no"]') !== null;
42+
let hiddenParent = heading.closest('[hidden]') !== null;
43+
let visuallyhiddenParent = heading.closest('.visuallyhidden') !== null;
3644
let hasDataAttribute = heading.getAttribute('data-anchor') === 'no';
45+
let isHidden = heading.getAttribute('hidden');
46+
let isVisuallyhidden = heading.classList.contains('visuallyhidden');
3747

38-
return !insideNav && !parentHasDataAttribute && !hasDataAttribute;
48+
return !insideNav && !parentHasDataAttribute && !hiddenParent && !visuallyhiddenParent && !hasDataAttribute && !isHidden && !isVisuallyhidden;
3949
});
4050

4151
if (targetedHeadings.length > 0) {
@@ -63,7 +73,7 @@ let headingAnchors = function () {
6373

6474
anchor.setAttribute('href', '#' + anchorHref);
6575
anchor.setAttribute('class', 'heading-anchor');
66-
anchor.innerHTML = '<span aria-hidden="true">#</span>'
76+
anchor.innerHTML = '<span aria-hidden="true">&sect;</span>'
6777
+'<span class="visuallyhidden">'
6878
+ translate.translate('anchor', languageCode) + '</span>';
6979
heading.appendChild(anchor);

public/dist/assets/js/main.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -915,11 +915,13 @@ __webpack_require__.r(__webpack_exports__);
915915
/**
916916
* Add anchor links to any H2 - H6 within the <main> element that:
917917
* - are not children of a <nav> element
918-
* - do not have an ancestor with the data-anchor="no" attribute
919-
* - do not themselves have the data-anchor="no" attribute
918+
* - do not have an ancestor with the data-anchor="no" attribute, with
919+
* the `hidden` attribute or with the .visuallyhidden class
920+
* - do not themselves have the data-anchor="no" attribute, the `hidden`
921+
* attribute or the .visuallyhidden class
920922
*
921-
* If a heading does not already possess an ID, use regular expressions
922-
* on the textContent of the heading to generate a string that is valid to
923+
* If a heading does not already possess an ID, use regular expressions on
924+
* the textContent of the heading to generate a string that is valid to
923925
* use for both the heading ID and the anchor href. Supports non-Latin
924926
* scripts by matching any Unicode letter - \p{L} - or number - \p{N}. The
925927
* u flag enables Unicode matching, to support characters from any script.
@@ -938,12 +940,20 @@ let headingAnchors = function () {
938940
// Filter out headings that:
939941
// - Are not children of <nav>
940942
// - Do not have an ancestor with the data-anchor="no" attribute
943+
// - Do not have an ancestor with the `hidden` attribute
944+
// - Do not have an ancestor with the `.visuallyhidden` class
941945
// - Do not themselves have the data-anchor="no" attribute
946+
// - Do not themselves have the `hidden` attribute
947+
// - Do not themselves have the `.visuallyhidden` class
942948
let targetedHeadings = headingsArray.filter(function (heading) {
943949
let insideNav = heading.closest('nav') !== null;
944950
let parentHasDataAttribute = heading.closest('[data-anchor="no"]') !== null;
951+
let hiddenParent = heading.closest('[hidden]') !== null;
952+
let visuallyhiddenParent = heading.closest('.visuallyhidden') !== null;
945953
let hasDataAttribute = heading.getAttribute('data-anchor') === 'no';
946-
return !insideNav && !parentHasDataAttribute && !hasDataAttribute;
954+
let isHidden = heading.getAttribute('hidden');
955+
let isVisuallyhidden = heading.classList.contains('visuallyhidden');
956+
return !insideNav && !parentHasDataAttribute && !hiddenParent && !visuallyhiddenParent && !hasDataAttribute && !isHidden && !isVisuallyhidden;
947957
});
948958
if (targetedHeadings.length > 0) {
949959
targetedHeadings.forEach(function (heading) {
@@ -964,7 +974,7 @@ let headingAnchors = function () {
964974
}
965975
anchor.setAttribute('href', '#' + anchorHref);
966976
anchor.setAttribute('class', 'heading-anchor');
967-
anchor.innerHTML = '<span aria-hidden="true">#</span>' + '<span class="visuallyhidden">' + _translations__WEBPACK_IMPORTED_MODULE_0__.translate.translate('anchor', languageCode) + '</span>';
977+
anchor.innerHTML = '<span aria-hidden="true">&sect;</span>' + '<span class="visuallyhidden">' + _translations__WEBPACK_IMPORTED_MODULE_0__.translate.translate('anchor', languageCode) + '</span>';
968978
heading.appendChild(anchor);
969979
});
970980
}

0 commit comments

Comments
 (0)