@@ -916,19 +916,14 @@ __webpack_require__.r(__webpack_exports__);
916
916
/* harmony import */ var _translations__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__ ( 3 ) ;
917
917
/**
918
918
* Add anchor links to any H2 - H6 within the <main> element that:
919
+ * - have an id
919
920
* - are not children of a <nav> element
920
921
* - do not have an ancestor with the data-anchor="no" attribute, with
921
922
* the `hidden` attribute or with the .visuallyhidden class
922
923
* - do not themselves have the data-anchor="no" attribute, the `hidden`
923
924
* attribute or the .visuallyhidden class
924
925
*
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.
932
927
*/
933
928
934
929
@@ -937,7 +932,7 @@ let headingAnchors = function () {
937
932
938
933
// Only add heading anchor links on "full" sites
939
934
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] ' ) ) ;
941
936
if ( headingsArray . length > 0 ) {
942
937
// Filter out headings that:
943
938
// - Are not children of <nav>
@@ -960,21 +955,7 @@ let headingAnchors = function () {
960
955
if ( targetedHeadings . length > 0 ) {
961
956
targetedHeadings . forEach ( function ( heading ) {
962
957
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 ) ;
978
959
anchor . setAttribute ( 'class' , 'heading-anchor' ) ;
979
960
anchor . innerHTML = '<span aria-hidden="true">§</span>' + '<span class="visuallyhidden">' + _translations__WEBPACK_IMPORTED_MODULE_0__ . translate . translate ( 'anchor' , languageCode ) + '</span>' ;
980
961
heading . append ( '\xa0' ) ;
0 commit comments