Skip to content

Commit cf1bc10

Browse files
committedMay 28, 2024
wporg-plugins-2024: Fix FAQ anchor links.
See: #315 Props: dd32, ryelle git-svn-id: https://meta.svn.wordpress.org/sites/trunk@13750 74240141-8908-4e6f-9713-ba540dce6ec7
1 parent 66d51b3 commit cf1bc10

File tree

1 file changed

+20
-16
lines changed
  • wordpress.org/public_html/wp-content/themes/pub/wporg-plugins-2024/js

1 file changed

+20
-16
lines changed
 

‎wordpress.org/public_html/wp-content/themes/pub/wporg-plugins-2024/js/section-faq.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@
55
.each( function( index, question ) {
66
var $question = $( question ),
77
$button = $( '<button />' ),
8-
$h3 = $( '<h3 />' ),
9-
id = $question.attr( 'id' );
10-
11-
// If there is no ID, create our own.
12-
if ( ! id ) {
13-
id = encodeURIComponent( $question.text().toLowerCase().trim() );
14-
$question.attr( 'id', id );
15-
}
16-
17-
$button.attr( 'formaction', id ).on( 'click', function( event ) {
18-
event.preventDefault();
19-
window.location.hash = id;
20-
} );
8+
$h3 = $( '<h3 />' );
219

2210
$question.html( $h3.html( $button.text( $question.text() ) ) );
2311
} )
@@ -41,12 +29,28 @@
4129

4230
window.scrollTo( 0, $question.offset().top - scrollPaddingTop );
4331
}
32+
33+
if ( $question.prop( 'id' ) ) {
34+
window.location.hash = $question.prop( 'id' );
35+
}
4436
} );
4537

4638
if ( window.location.hash ) {
47-
jQuery(
39+
var uriHash = window.location.hash.substr(1),
40+
uriElement = document.getElementById( uriHash );
41+
42+
if ( ! uriElement ) {
4843
// Decode/Encode here is to work with any existing links that are not fully-encoded, the trim handles whitespace/newlines.
49-
document.getElementById( encodeURIComponent( decodeURIComponent( window.location.hash.substr(1) ).trim() ) )
50-
).trigger( 'click' );
44+
uriHash = encodeURIComponent( decodeURIComponent( uriHash ) ).trim();
45+
uriElement = document.getElementById( uriHash );
46+
47+
if ( ! uriElement ) {
48+
// Perform PHP-style URI encoding. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI#encoding_for_rfc3986
49+
uriHash = uriHash.replace( /[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}` );
50+
uriElement = document.getElementById( uriHash );
51+
}
52+
}
53+
54+
jQuery( uriElement ).trigger( 'click' );
5155
}
5256
} )( jQuery );

0 commit comments

Comments
 (0)
Please sign in to comment.