Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show 'Desktop Expanded menu' of twenty-twenty in AMP mode #7094

Merged
merged 2 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions includes/sanitizers/class-amp-core-theme-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected static function get_theme_features_config( $theme_slug, $args = [] ) {
'add_img_display_block_fix' => $args,
'add_twentytwenty_custom_logo_fix' => $args,
'add_twentytwenty_current_page_awareness' => [],
'show_twentytwenty_desktop_expanded_menu' => [],
];

$theme = wp_get_theme( 'twentytwenty' );
Expand Down Expand Up @@ -2189,6 +2190,26 @@ public function amend_twentytwentyone_sub_menu_toggles() {
}
}

/**
* Show "Desktop Expanded Menu" in AMP mode.
* Removes 'no-js' class from menu element.
*
* @return void
*/
public function show_twentytwenty_desktop_expanded_menu() {

$xpath = "//*[@class='header-navigation-wrapper']/div[ @class and contains( concat( ' ', normalize-space( @class ), ' ' ), ' header-toggles hide-no-js ' ) ]";
$expanded_menu = $this->dom->xpath->query( $xpath )->item( 0 );

if ( $expanded_menu instanceof DOMElement ) {
$class = $expanded_menu->getAttribute( Attribute::CLASS_ );
$expanded_menu->setAttribute(
Attribute::CLASS_,
str_replace( 'hide-no-js', '', $class )
);
}
}

/**
* Get the closest sub-menu within a menu item.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/php/test-class-amp-core-theme-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use AmpProject\Dom\Document;
use AmpProject\Dom\Element;
use AmpProject\Html\Tag;
use AmpProject\AmpWP\Tests\Helpers\MarkupComparison;
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
use AmpProject\AmpWP\Tests\TestCase;

Expand All @@ -21,6 +22,7 @@ class AMP_Core_Theme_Sanitizer_Test extends TestCase {

use PrivateAccess;
use LoadsCoreThemes;
use MarkupComparison;

public function set_up() {
parent::set_up();
Expand Down Expand Up @@ -719,4 +721,37 @@ public function test_add_twentytwentyone_dark_mode_toggle( $enabled ) {
$dom->saveHTML( $button )
);
}

/**
* Test data for $this->test_show_twentytwenty_desktop_expanded_menu()
*
* @return array Test data set.
*/
public function get_data_show_twentytwenty_desktop_expanded_menu() {

return [
'with-no-js-selector' => [
'input' => '<div class="header-navigation-wrapper"><div class="header-toggles hide-no-js"></div></div>',
'expected' => '<div class="header-navigation-wrapper"><div class="header-toggles "></div></div>',
],
'without-no-js-selector' => [
'input' => '<div class="header-navigation-wrapper"><div class="header-toggles"></div></div>',
'expected' => '<div class="header-navigation-wrapper"><div class="header-toggles"></div></div>',
],
];
}

/**
* @dataProvider get_data_show_twentytwenty_desktop_expanded_menu()
*
* @covers ::show_twentytwenty_desktop_expanded_menu()
*/
public function test_show_twentytwenty_desktop_expanded_menu( $input, $expected ) {
$dom = AMP_DOM_Utils::get_dom_from_content( $input );
$sanitizer = new AMP_Core_Theme_Sanitizer( $dom );

$this->call_private_method( $sanitizer, 'show_twentytwenty_desktop_expanded_menu' );

$this->assertEqualMarkup( $expected, AMP_DOM_Utils::get_content_from_dom( $dom ) );
}
}