Skip to content

Commit ec2bf23

Browse files
authored
Merge pull request #160 from w3c/heading-anchors-id-only
Only create anchors for headings that have an id
2 parents 85dd661 + 1c54057 commit ec2bf23

File tree

3 files changed

+10
-52
lines changed

3 files changed

+10
-52
lines changed

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
/**
22
* Add anchor links to any H2 - H6 within the <main> element that:
3+
* - have an id
34
* - are not children of a <nav> element
45
* - do not have an ancestor with the data-anchor="no" attribute, with
56
* the `hidden` attribute or with the .visuallyhidden class
67
* - do not themselves have the data-anchor="no" attribute, the `hidden`
78
* attribute or the .visuallyhidden class
89
*
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
11-
* use for both the heading ID and the anchor href. Supports non-Latin
12-
* scripts by matching any Unicode letter - \p{L} - or number - \p{N}. The
13-
* u flag enables Unicode matching, to support characters from any script.
14-
*
15-
* Otherwise, generate the anchor using the existing heading ID value.
10+
* Generate the anchor using the existing heading ID value.
1611
*/
1712

1813
import {translate} from './translations';
@@ -24,7 +19,7 @@ let headingAnchors = function () {
2419
// Only add heading anchor links on "full" sites
2520
if (languageCode === 'en' || languageCode === 'ja' || languageCode === 'zh-hans') {
2621

27-
let headingsArray= Array.from(document.querySelectorAll('main h2, main h3, main h4, main h5, main h6'));
22+
let headingsArray= Array.from(document.querySelectorAll('main h2[id], main h3[id], main h4[id], main h5[id], main h6[id]'));
2823

2924
if (headingsArray.length > 0) {
3025

@@ -52,26 +47,8 @@ let headingAnchors = function () {
5247
targetedHeadings.forEach(function(heading) {
5348

5449
let anchor = document.createElement('a');
55-
let anchorHref;
56-
57-
// If the heading already has an ID, use this for constructing the anchor
58-
if (heading.getAttribute('id')) {
59-
anchorHref = heading.id;
60-
} else {
61-
// If the heading does not already have an ID, generate anchor href from the heading text. Steps are:
62-
// - Remove leading/trailing spaces
63-
// - Use RegEx to remove invalid characters but keep all Unicode letters/numbers
64-
// - Use RegEx to replace spaces with hyphens
65-
// - convert to lowercase as per URL policy
66-
anchorHref = heading.textContent
67-
.trim()
68-
.replace(/[^\p{L}\p{N}\s-]/gu, '')
69-
.replace(/\s+/g, '-')
70-
.toLowerCase();
71-
heading.id = anchorHref;
72-
}
73-
74-
anchor.setAttribute('href', '#' + anchorHref);
50+
51+
anchor.setAttribute('href', '#' + heading.id);
7552
anchor.setAttribute('class', 'heading-anchor');
7653
anchor.innerHTML = '<span aria-hidden="true">&sect;</span>'
7754
+'<span class="visuallyhidden">'

public/dist/assets/js/main.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -916,19 +916,14 @@ __webpack_require__.r(__webpack_exports__);
916916
/* harmony import */ var _translations__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
917917
/**
918918
* Add anchor links to any H2 - H6 within the <main> element that:
919+
* - have an id
919920
* - are not children of a <nav> element
920921
* - do not have an ancestor with the data-anchor="no" attribute, with
921922
* the `hidden` attribute or with the .visuallyhidden class
922923
* - do not themselves have the data-anchor="no" attribute, the `hidden`
923924
* attribute or the .visuallyhidden class
924925
*
925-
* If a heading does not already possess an ID, use regular expressions on
926-
* the textContent of the heading to generate a string that is valid to
927-
* use for both the heading ID and the anchor href. Supports non-Latin
928-
* scripts by matching any Unicode letter - \p{L} - or number - \p{N}. The
929-
* u flag enables Unicode matching, to support characters from any script.
930-
*
931-
* Otherwise, generate the anchor using the existing heading ID value.
926+
* Generate the anchor using the existing heading ID value.
932927
*/
933928

934929

@@ -937,7 +932,7 @@ let headingAnchors = function () {
937932

938933
// Only add heading anchor links on "full" sites
939934
if (languageCode === 'en' || languageCode === 'ja' || languageCode === 'zh-hans') {
940-
let headingsArray = Array.from(document.querySelectorAll('main h2, main h3, main h4, main h5, main h6'));
935+
let headingsArray = Array.from(document.querySelectorAll('main h2[id], main h3[id], main h4[id], main h5[id], main h6[id]'));
941936
if (headingsArray.length > 0) {
942937
// Filter out headings that:
943938
// - Are not children of <nav>
@@ -960,21 +955,7 @@ let headingAnchors = function () {
960955
if (targetedHeadings.length > 0) {
961956
targetedHeadings.forEach(function (heading) {
962957
let anchor = document.createElement('a');
963-
let anchorHref;
964-
965-
// If the heading already has an ID, use this for constructing the anchor
966-
if (heading.getAttribute('id')) {
967-
anchorHref = heading.id;
968-
} else {
969-
// If the heading does not already have an ID, generate anchor href from the heading text. Steps are:
970-
// - Remove leading/trailing spaces
971-
// - Use RegEx to remove invalid characters but keep all Unicode letters/numbers
972-
// - Use RegEx to replace spaces with hyphens
973-
// - convert to lowercase as per URL policy
974-
anchorHref = heading.textContent.trim().replace(/[^\p{L}\p{N}\s-]/gu, '').replace(/\s+/g, '-').toLowerCase();
975-
heading.id = anchorHref;
976-
}
977-
anchor.setAttribute('href', '#' + anchorHref);
958+
anchor.setAttribute('href', '#' + heading.id);
978959
anchor.setAttribute('class', 'heading-anchor');
979960
anchor.innerHTML = '<span aria-hidden="true">&sect;</span>' + '<span class="visuallyhidden">' + _translations__WEBPACK_IMPORTED_MODULE_0__.translate.translate('anchor', languageCode) + '</span>';
980961
heading.append('\xa0');

0 commit comments

Comments
 (0)