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

Consolidate Safari Rerenders into one script #49215

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
39 changes: 30 additions & 9 deletions lib/class-wp-duotone-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,42 @@ private static function get_css_custom_property_declaration( $filter_data ) {
* Safari renders elements incorrectly on first paint when the SVG filter comes after the content that it is filtering,
* so we force a repaint with a WebKit hack which solves the issue.
*
* @param string $selector The selector to apply the hack for.
* @param string $selectors The CSS selectors to apply the hack for.
*/
private static function safari_rerender_hack( $selector ) {
private static function safari_rerender_hack( $selectors ) {
/*
* Simply accessing el.offsetHeight flushes layout and style
* Accessing el.offsetHeight flushes layout and style
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed "Simply" for inclusion and brevity sake.

* changes in WebKit without having to wait for setTimeout.
*/
printf(
'<script>( function() { var el = document.querySelector( %s ); var display = el.style.display; el.style.display = "none"; el.offsetHeight; el.style.display = display; } )();</script>',
wp_json_encode( $selector )
'<script>
(
function() {
%s.forEach( selector => {
document.querySelectorAll( selector ).forEach( function( el ) {
if( ! el ) {
return;
}
var display = el.style.display;
el.style.display = "none";
el.offsetHeight;
el.style.display = display;
} );
} );
}
)();
</script>',
wp_json_encode( $selectors )
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

II think this will be easier to review if we leave it expanded, and then instead of printf we can use remove the whitespace when we output it with [normalize_whitespace](https://developer.wordpress.org/reference/functions/normalize_whitespace/) or similar.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think normalize_whitespace will work, I just used preg_replace.

);
}

/**
* Outputs all necessary SVG for duotone filters, CSS for classic themes, and safari rerendering hack
*/
public static function output_footer_assets() {

$selectors = array();

foreach ( self::$output as $filter_data ) {

// SVG will be output on the page later.
Expand All @@ -216,10 +235,12 @@ public static function output_footer_assets() {
wp_add_inline_style( 'core-block-supports', 'body{' . self::get_css_custom_property_declaration( $filter_data ) . '}' );
}

global $is_safari;
if ( $is_safari ) {
self::safari_rerender_hack( $filter_data['selector'] );
}
$selectors[] = $filter_data['selector'];
}

global $is_safari;
if ( $is_safari && ! empty( $selectors ) ) {
self::safari_rerender_hack( $selectors );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered using array_map or array_columns here, but collecting $selectors in the foreach is probably easier to grok.

}
}

Expand Down