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

Handle missing scheme in animated image rule #545

Conversation

pattonwebz
Copy link
Member

@pattonwebz pattonwebz commented Mar 25, 2024

The rule that handles detection of animated gifs and webp images was causing an error on sites when the image src was missing a scheme. This PR solves for that by creating a helper function to add a scheme and verify the URL is accessible before opening the file to analyse it's frames.

Fixes: #544

Comment on lines 917 to 923
$file = edac_url_add_scheme_if_not_existing( $filename );
$url_exists = edac_url_exists( $file );
// if the https version does not exist, try http.
if ( false === $url_exists ) {
$file = edac_url_add_scheme_if_not_existing( $filename, 'http' );
$url_exists = edac_url_exists( $file );
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I thought about this over the weekend and I want to just do away with the nested if logic here and instead assume it will always have only what the site protocal is since the page it loads on would dictate it. Thoughts on dropping the internal if here that falls back to http?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we can remove this logic check.

* @return string The filename unchanged if it doesn't look like a URL, or with a scheme added if it does.
*/
function edac_url_add_scheme_if_not_existing( string $file, string $site_protocol = 'https' ): string {
if ( str_starts_with( $file, $site_protocol ) || ! str_starts_with( $file, '//' ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we check if a protocol doesn't exist at all? Right now a URL like this isn't caught.

<img src="assets.rewardstyle.com/images/search/350.gif" />

The capability is supporting urls that are neither relative not protocol-relative. It's an edge case for sure but no harm in adding support for it.
…tive urls as well

Also adds an explicit list of valid schemes as well to support more edge cases
Copy link
Member

@SteveJonesDev SteveJonesDev left a comment

Choose a reason for hiding this comment

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

URL's like this assets.rewardstyle.com/images/search/350.gif are still being opened without a protocol.

Here is the warning:

[26-Mar-2024 16:39:23 UTC] PHP Warning:  fopen(rewardstyle.com/images/search/350.gif): Failed to open stream: No such file or directory in /Users/stevejones/Documents/Repositories/accessibility-checker/includes/helper-functions.php on line 950

…obust

Thanks to the newly added tests for catching my last change here as being a change that had downstream effects :)
…obust

Thanks to the newly added tests for catching my last change here as being a change that had downstream effects :)
…causes_error_when_image_lacks_scheme' into william/544/animage_image_check_causes_error_when_image_lacks_scheme

# Conflicts:
#	includes/helper-functions.php
@SteveJonesDev SteveJonesDev self-requested a review March 26, 2024 20:19
Copy link
Member

@SteveJonesDev SteveJonesDev left a comment

Choose a reason for hiding this comment

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

Good to merge.

@pattonwebz pattonwebz merged commit 8e16089 into develop Mar 26, 2024
16 checks passed
@pattonwebz pattonwebz deleted the william/544/animage_image_check_causes_error_when_image_lacks_scheme branch March 28, 2024 18:26
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.

gifs or webp with missing url scheme cause error when checking if they are animated
2 participants