1/** 2 * WP-ShowHide's front end toggle. 3 * 4 * One delegated listener on the document rather than one per block: the 5 * shortcode can appear any number of times on a page, and blocks rendered 6 * after this file runs -- by a lazy-loaded comment list, an AJAX-paginated 7 * archive -- work without anything having to re-bind. 8 * 9 * Everything the handler needs is on the markup: the button carries its two 10 * labels in data-sh-more and data-sh-less, its state in aria-expanded, and the 11 * id of the element it controls in aria-controls. 12 */ 13( function() { 14 document.addEventListener( 'click', function( event ) { 15 // closest() is missing on a non-element target, which a click on a 16 // text node inside the button reports in some browsers. 17 const button = event.target.closest?.( '.sh-toggle' ); 18 19 if ( ! button ) { 20 return; 21 } 22 23 const wrap = button.closest( '.sh-link' ); 24 const content = document.getElementById( 25 button.getAttribute( 'aria-controls' ), 26 ); 27 const expanded = button.getAttribute( 'aria-expanded' ) === 'true'; 28 29 // Markup the plugin did not render, or content that has since been 30 // removed: leave it alone rather than throwing. 31 if ( ! wrap || ! content ) { 32 return; 33 } 34 35 button.setAttribute( 'aria-expanded', expanded ? 'false' : 'true' ); 36 button.textContent = expanded 37 ? button.dataset.shMore 38 : button.dataset.shLess; 39 content.hidden = expanded; 40 41 [ wrap, content ].forEach( function( element ) { 42 element.classList.toggle( 'sh-show', ! expanded ); 43 element.classList.toggle( 'sh-hide', expanded ); 44 } ); 45 46 // Fired on .sh-link and bubbling, so a theme can listen once on the 47 // document. The names predate the plugin's own hook prefix and are 48 // part of its public API, so they keep their spelling. 49 [ 50 expanded ? 'sh-link:less' : 'sh-link:more', 51 'sh-link:toggle', 52 ].forEach( function( name ) { 53 wrap.dispatchEvent( new CustomEvent( name, { bubbles: true } ) ); 54 } ); 55 } ); 56}() );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.