Making WordPress.org

Changeset 13750


Ignore:
Timestamp:
05/28/2024 12:45:57 AM (11 months ago)
Author:
dufresnesteven
Message:

wporg-plugins-2024: Fix FAQ anchor links.

See: https://github.com/WordPress/wordpress.org/pull/315
Props: dd32, ryelle

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins-2024/js/section-faq.js

    r11655 r13750  
    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() ) ) );
     
    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 );
Note: See TracChangeset for help on using the changeset viewer.