Skip to content

Commit f41ae1e

Browse files
committed
Media: Normalize all space characters to a space in sanitize_file_name().
The `sanitize_file_name()` function normalizes the no-break space to a normal space (`U+0020`) in order to prevent issues saving files with the no-break space in it. This patch expands the replacement to all space characters, since it’s known that macOS stores a `NARROW NO-BREAK SPACE` (`U+202F`) in screenshot filenames between the time and the am/pm indicator. There are deeper issues with the way this function works, but this patch resolves a known and common problem without raising any of the deeper refactoring questions. Reviewed by audrasjb, youknowriad. Merges [60399] to the 6.8 branch. Props audrasjb, desrosj, dmsnell, jonsurrell, matt, room34, siliconforks, zieladam, annezazu. Fixes #62995. git-svn-id: https://develop.svn.wordpress.org/branches/6.8@60411 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6edaddb commit f41ae1e

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/wp-includes/formatting.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,17 @@ function sanitize_file_name( $filename ) {
20352035
}
20362036

20372037
if ( $utf8_pcre ) {
2038-
$filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
2038+
/**
2039+
* Replace all whitespace characters with a basic space (U+0020).
2040+
*
2041+
* The “Zs” in the pattern selects characters in the `Space_Separator`
2042+
* category, which is what Unicode considers space characters.
2043+
*
2044+
* @see https://www.unicode.org/reports/tr44/#General_Category_Values
2045+
* @see https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-6/#G17548
2046+
* @see https://www.php.net/manual/en/regexp.reference.unicode.php
2047+
*/
2048+
$filename = preg_replace( '#\p{Zs}#siu', ' ', $filename );
20392049
}
20402050

20412051
/**

tests/phpunit/tests/formatting/sanitizeFileName.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ public function test_removes_accents() {
3535
* Test that spaces are correctly replaced with dashes.
3636
*
3737
* @ticket 16330
38+
* @ticket 62995
3839
*/
3940
public function test_replaces_spaces() {
4041
$urls = array(
41-
'unencoded space.png' => 'unencoded-space.png',
42-
'encoded-space.jpg' => 'encoded-space.jpg',
43-
'plus+space.jpg' => 'plusspace.jpg',
44-
'multi %20 +space.png' => 'multi-20-space.png',
42+
'unencoded space.png' => 'unencoded-space.png',
43+
'encoded-space.jpg' => 'encoded-space.jpg',
44+
'plus+space.jpg' => 'plusspace.jpg',
45+
'multi %20 +space.png' => 'multi-20-space.png',
46+
"Screenshot 2025-02-19 at 2.17.33\u{202F}PM.png" => 'Screenshot-2025-02-19-at-2.17.33-PM.png',
47+
"Filename with non-breaking\u{00A0}space.txt" => 'Filename-with-non-breaking-space.txt',
48+
"Filename with thin\u{2009}space.txt" => 'Filename-with-thin-space.txt',
4549
);
4650

4751
foreach ( $urls as $test => $expected ) {

0 commit comments

Comments
 (0)