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

[9.x] Replace raw invisible characters in regex expressions with counterpart Unicode regex notations #45680

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Http/Middleware/TrimStrings.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function transform($key, $value)
return $value;
}

return preg_replace('~^[\s​]+|[\s​]+$~u', '', $value) ?? trim($value);
return preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ public static function snake($value, $delimiter = '_')
*/
public static function squish($value)
{
return preg_replace('~(\s|\x{3164})+~u', ' ', preg_replace('~^[\s]+|[\s]+$~u', '', $value));
return preg_replace('~(\s|\x{3164})+~u', ' ', preg_replace('~^[\s\x{FEFF}]+|[\s\x{FEFF}]+$~u', '', $value));
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/Http/Middleware/TrimStringsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public function test_no_zero_width_space_character_returns_the_same_string()
$request = new Request;

$request->merge([
'title' => 'This title does not contains any zero-width space',
'title' => 'This title does not contain any zero-width space',
]);

$middleware = new TrimStrings;

$middleware->handle($request, function ($req) {
$this->assertEquals('This title does not contains any zero-width space', $req->title);
$this->assertEquals('This title does not contain any zero-width space', $req->title);
});
}

Expand All @@ -34,13 +34,13 @@ public function test_leading_zero_width_space_character_is_trimmed()
$request = new Request;

$request->merge([
'title' => '​This title contains a zero-width space at the begining',
'title' => '​This title contains a zero-width space at the beginning',
]);

$middleware = new TrimStrings;

$middleware->handle($request, function ($req) {
$this->assertEquals('This title contains a zero-width space at the begining', $req->title);
$this->assertEquals('This title contains a zero-width space at the beginning', $req->title);
});
}

Expand Down Expand Up @@ -70,13 +70,13 @@ public function test_leading_zero_width_non_breakable_space_character_is_trimmed
$request = new Request;

$request->merge([
'title' => 'This title contains a zero-width non-breakable space at the begining',
'title' => 'This title contains a zero-width non-breakable space at the beginning',
]);

$middleware = new TrimStrings;

$middleware->handle($request, function ($req) {
$this->assertEquals('This title contains a zero-width non-breakable space at the begining', $req->title);
$this->assertEquals('This title contains a zero-width non-breakable space at the beginning', $req->title);
});
}

Expand All @@ -88,13 +88,13 @@ public function test_leading_multiple_zero_width_non_breakable_space_characters_
$request = new Request;

$request->merge([
'title' => 'This title contains a zero-width non-breakable space at the begining',
'title' => 'This title contains a zero-width non-breakable space at the beginning',
]);

$middleware = new TrimStrings;

$middleware->handle($request, function ($req) {
$this->assertEquals('This title contains a zero-width non-breakable space at the begining', $req->title);
$this->assertEquals('This title contains a zero-width non-breakable space at the beginning', $req->title);
});
}

Expand All @@ -106,13 +106,13 @@ public function test_combination_of_leading_and_trailing_zero_width_non_breakabl
$request = new Request;

$request->merge([
'title' => '​This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the begining and the end​',
'title' => '​This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the beginning and the end​',
]);

$middleware = new TrimStrings;

$middleware->handle($request, function ($req) {
$this->assertEquals('This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the begining and the end', $req->title);
$this->assertEquals('This title contains a combination of zero-width non-breakable space and zero-width spaces characters at the beginning and the end', $req->title);
});
}
}