Skip to content

Commit

Permalink
Fix display of images in Twenty Twenty to be block not inline-block
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Mar 23, 2020
1 parent e6e86dc commit d976798
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions includes/sanitizers/class-amp-core-theme-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ protected static function get_theme_features_config( $theme_slug ) {
'add_twentytwenty_toggles' => [],
'add_nav_menu_styles' => [],
'add_twentytwenty_masthead_styles' => [],
'add_img_display_block_fix' => [],
'add_twentytwenty_current_page_awareness' => [],
];

Expand Down Expand Up @@ -719,6 +720,29 @@ static function() {
);
}

/**
* Add style rule with a selector of higher specificity than just `img` to make `amp-img` have `display:block` rather than `display:inline-block`.
*
* This is needed to override the AMP core stylesheet which has a more specific selector `.i-amphtml-layout-intrinsic` which
* is given a `display: line-block`; this display value prevents margins from collapsing with surrounding block elements,
* resulting in larger margins in AMP than expected.
*
* @since 1.5
*/
public static function add_img_display_block_fix() {
add_action(
'wp_enqueue_scripts',
static function() {
wp_add_inline_style(
get_template() . '-style',
// The selector is targeting an attribute that can never appear. It is purely present to increase specificity.
'amp-img:not([_]) { display: block }'
);
},
11
);
}

/**
* Add required styles for featured image header in Twenty Nineteen.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/php/test-class-amp-core-theme-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,19 @@ public function test_guess_modal_role( DOMElement $dom_element, $expected ) {

$this->assertEquals( $expected, $actual );
}

/**
* Tests add_img_display_block_fix.
*
* @covers AMP_Core_Theme_Sanitizer::add_img_display_block_fix()
*/
public function test_add_img_display_block_fix() {
$handle = get_template() . '-style';
wp_register_style( $handle, get_stylesheet_uri(), [], '0.1' );

$this->assertEmpty( wp_styles()->print_inline_style( $handle, false ) );
AMP_Core_Theme_Sanitizer::add_img_display_block_fix();
wp_enqueue_scripts();
$this->assertRegExp( '/amp-img.+display.+block/s', wp_styles()->print_inline_style( $handle, false ) );
}
}

0 comments on commit d976798

Please sign in to comment.