From eaea2f3b4543d9fbdc572c8f5f1a11e1fc2d664e Mon Sep 17 00:00:00 2001 From: ryelle Date: Fri, 4 Jun 2021 17:08:00 +0000 Subject: [PATCH] Twenty Twenty-One: Check for navigation element before using it. This prevents a javascript error in case the primary navigation has been removed, for example in a child theme. Props sushmak. Fixes #52773. Built from https://develop.svn.wordpress.org/trunk@51072 git-svn-id: http://core.svn.wordpress.org/trunk@50681 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../twentytwentyone/assets/js/primary-navigation.js | 10 ++++++++-- wp-includes/version.php | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js b/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js index ec559619a1..af9c6df128 100644 --- a/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js +++ b/wp-content/themes/twentytwentyone/assets/js/primary-navigation.js @@ -96,7 +96,13 @@ function twentytwentyoneExpandSubMenu( el ) { // jshint ignore:line */ var navMenu = function( id ) { var wrapper = document.body, // this is the element to which a CSS class is added when a mobile nav menu is open - mobileButton = document.getElementById( id + '-mobile-menu' ); + mobileButton = document.getElementById( id + '-mobile-menu' ), + navMenuEl = document.getElementById( 'site-navigation' ); + + // If there's no nav menu, none of this is necessary. + if ( ! navMenuEl ) { + return; + } if ( mobileButton ) { mobileButton.onclick = function() { @@ -167,7 +173,7 @@ function twentytwentyoneExpandSubMenu( el ) { // jshint ignore:line } } ); - document.getElementById( 'site-navigation' ).querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) { + navMenuEl.querySelectorAll( '.menu-wrapper > .menu-item-has-children' ).forEach( function( li ) { li.addEventListener( 'mouseenter', function() { this.querySelector( '.sub-menu-toggle' ).setAttribute( 'aria-expanded', 'true' ); twentytwentyoneSubmenuPosition( li ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 3b2a01ee06..f68cdbf4ac 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-51071'; +$wp_version = '5.8-alpha-51072'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.