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

Conversation

jeryj
Copy link
Contributor

@jeryj jeryj commented Mar 20, 2023

Fixes #49170.

Why?

Previously every duotone selector had its own script tag on the page. This consolidates them into one script tag and loops through all the selectors to apply the hack. Also, previously it used document.querySelector for the rerender, which would only grab the first matched element when there may be many on the page.

How?

Collects all the selectors and the JS can now handle an array of strings rather than only one at a time.

Testing Instructions

Testing Instructions for Keyboard

Screenshots or screencast

Previously every duotone selector had its own script tag on the page. This consolidates them into one script tag and loops through all the selectors to apply the hack. Also, previously it used document.querySelector for the rerender, which would only grab the first matched element when there may be many on the page.
/*
* 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.

Comment on lines 198 to 215
'<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.


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.

@scruffian
Copy link
Contributor

I think this is looking good, but even when I removed the code altogether it still works in Safari, so I think we need better testing instructions! Maybe @ajlende knows...

@github-actions
Copy link

Flaky tests detected in 5b81ec9.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4478040074
📝 Reported issues:

@ajlende
Copy link
Contributor

ajlende commented Mar 21, 2023

@scruffian Safari may have fixed their bug. I have a codepen that I used for testing.

This is what I was seeing before.

image

And this is what I'm seeing now.

image

Do you know how many version of Safari we're supporting? If it's fixed in all of them, we can get rid of this code!

@ajlende
Copy link
Contributor

ajlende commented Mar 21, 2023

@scruffian and I see that we're supporting the last 2 safari versions, and both of those versions have the fix, so we're opening a PR to remove the hack.

@jeryj
Copy link
Contributor Author

jeryj commented Mar 21, 2023

Woo! Nice! I should have checked that first! Great catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consolidate Safari duotone re-render scripts
3 participants