Skip to content

Commit

Permalink
Fix alt text stripping accents and single quotes (#2514)
Browse files Browse the repository at this point in the history
* Fix alt text stripping accents and single quotes

The altTextFrom method doesn't work well for non english characters and accents, it also uppercases every word which is weird

* Add tests and fix replace accents
  • Loading branch information
Tofandel authored May 28, 2024
1 parent 2768019 commit 33cad70
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
13 changes: 6 additions & 7 deletions src/Helpers/media_library_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Support\Facades\Storage;
use Aws\S3\S3Client;
use Aws\S3\PostObjectV4;
use Illuminate\Support\Str;

if (!function_exists('s3Endpoint')) {
/**
Expand Down Expand Up @@ -60,13 +61,11 @@ function bytesToHuman($bytes)
/**
* @param string $str
* @return bool|string
* @deprecated Use Str::ascii instead
*/
function replaceAccents($str)
{
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($str, 'ASCII', 'UTF-8');
}
return iconv('UTF-8', 'ASCII//TRANSLIT', $str);
return Str::ascii($str);
}
}

Expand All @@ -77,7 +76,7 @@ function replaceAccents($str)
*/
function sanitizeFilename($filename)
{
$sanitizedFilename = replaceAccents($filename);
$sanitizedFilename = Str::ascii($filename);

$invalid = array(
' ' => '-',
Expand All @@ -87,8 +86,8 @@ function sanitizeFilename($filename)

$sanitizedFilename = str_replace(array_keys($invalid), array_values($invalid), $sanitizedFilename);

$sanitizedFilename = preg_replace('#[^A-Za-z0-9-\. ]#', '', $sanitizedFilename); // Remove all non-alphanumeric except .
$sanitizedFilename = preg_replace('#\.(?=.*\.)#', '', $sanitizedFilename); // Remove all but last .
$sanitizedFilename = preg_replace('#[^A-Za-z0-9-. ]#', '', $sanitizedFilename); // Remove all non-alphanumeric except .
$sanitizedFilename = preg_replace('#\.(?=.*\.)#', '-', $sanitizedFilename); // Remove all but last .
$sanitizedFilename = preg_replace('#-+#', '-', $sanitizedFilename); // Replace any more than one - in a row
$sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end
$sanitizedFilename = strtolower($sanitizedFilename); // Lowercase
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function altTextFrom($filename)
{
$filename = pathinfo($filename, PATHINFO_FILENAME);
if (Str::endsWith($filename, '@2x')) {
$filename = substr($filename, 0, -2);
$filename = substr($filename, 0, -3);
}

return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename)));
return Str::ucfirst(preg_replace('/[-_]/', ' ', $filename));
}

public function canDeleteSafely()
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/Helpers/MediaLibraryHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace A17\Twill\Tests\Unit\Helpers;

use A17\Twill\Tests\Unit\TestCase;

class MediaLibraryHelpersTest extends TestCase
{
public function testReplaceAccents()
{
$this->assertEquals('aeeiou', replaceAccents('àéèïôû'));
}

public function testSanitizeFilename()
{
$this->assertEquals('happy-paques-xo-png.jpg', sanitizeFilename('Happy_Pâques - XO.png.jpg'));
}

}
18 changes: 18 additions & 0 deletions tests/unit/Models/MediaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace A17\Twill\Tests\Unit\Models;

use A17\Twill\Models\Media;
use A17\Twill\Tests\Unit\TestCase;

class MediaTest extends TestCase
{
public function testAltText()
{
$m = new Media();

$this->assertEquals("Happy Holidays", $m->altTextFrom('Happy_Holidays.jpg'));
$this->assertEquals("Happy Holidays", $m->altTextFrom('[email protected]'));
$this->assertEquals("J'aime la pièce", $m->altTextFrom('J\'aime-la-pièce.jpg'));
}
}

0 comments on commit 33cad70

Please sign in to comment.