Skip to content

Commit 33cad70

Browse files
authored
Fix alt text stripping accents and single quotes (#2514)
* 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
1 parent 2768019 commit 33cad70

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/Helpers/media_library_helpers.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Support\Facades\Storage;
44
use Aws\S3\S3Client;
55
use Aws\S3\PostObjectV4;
6+
use Illuminate\Support\Str;
67

78
if (!function_exists('s3Endpoint')) {
89
/**
@@ -60,13 +61,11 @@ function bytesToHuman($bytes)
6061
/**
6162
* @param string $str
6263
* @return bool|string
64+
* @deprecated Use Str::ascii instead
6365
*/
6466
function replaceAccents($str)
6567
{
66-
if (function_exists('mb_convert_encoding')) {
67-
return mb_convert_encoding($str, 'ASCII', 'UTF-8');
68-
}
69-
return iconv('UTF-8', 'ASCII//TRANSLIT', $str);
68+
return Str::ascii($str);
7069
}
7170
}
7271

@@ -77,7 +76,7 @@ function replaceAccents($str)
7776
*/
7877
function sanitizeFilename($filename)
7978
{
80-
$sanitizedFilename = replaceAccents($filename);
79+
$sanitizedFilename = Str::ascii($filename);
8180

8281
$invalid = array(
8382
' ' => '-',
@@ -87,8 +86,8 @@ function sanitizeFilename($filename)
8786

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

90-
$sanitizedFilename = preg_replace('#[^A-Za-z0-9-\. ]#', '', $sanitizedFilename); // Remove all non-alphanumeric except .
91-
$sanitizedFilename = preg_replace('#\.(?=.*\.)#', '', $sanitizedFilename); // Remove all but last .
89+
$sanitizedFilename = preg_replace('#[^A-Za-z0-9-. ]#', '', $sanitizedFilename); // Remove all non-alphanumeric except .
90+
$sanitizedFilename = preg_replace('#\.(?=.*\.)#', '-', $sanitizedFilename); // Remove all but last .
9291
$sanitizedFilename = preg_replace('#-+#', '-', $sanitizedFilename); // Replace any more than one - in a row
9392
$sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end
9493
$sanitizedFilename = strtolower($sanitizedFilename); // Lowercase

src/Models/Media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function altTextFrom($filename)
4949
{
5050
$filename = pathinfo($filename, PATHINFO_FILENAME);
5151
if (Str::endsWith($filename, '@2x')) {
52-
$filename = substr($filename, 0, -2);
52+
$filename = substr($filename, 0, -3);
5353
}
5454

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

5858
public function canDeleteSafely()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace A17\Twill\Tests\Unit\Helpers;
4+
5+
use A17\Twill\Tests\Unit\TestCase;
6+
7+
class MediaLibraryHelpersTest extends TestCase
8+
{
9+
public function testReplaceAccents()
10+
{
11+
$this->assertEquals('aeeiou', replaceAccents('àéèïôû'));
12+
}
13+
14+
public function testSanitizeFilename()
15+
{
16+
$this->assertEquals('happy-paques-xo-png.jpg', sanitizeFilename('Happy_Pâques - XO.png.jpg'));
17+
}
18+
19+
}

tests/unit/Models/MediaTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace A17\Twill\Tests\Unit\Models;
4+
5+
use A17\Twill\Models\Media;
6+
use A17\Twill\Tests\Unit\TestCase;
7+
8+
class MediaTest extends TestCase
9+
{
10+
public function testAltText()
11+
{
12+
$m = new Media();
13+
14+
$this->assertEquals("Happy Holidays", $m->altTextFrom('Happy_Holidays.jpg'));
15+
$this->assertEquals("Happy Holidays", $m->altTextFrom('Happy_Holidays@2x.jpg'));
16+
$this->assertEquals("J'aime la pièce", $m->altTextFrom('J\'aime-la-pièce.jpg'));
17+
}
18+
}

0 commit comments

Comments
 (0)