Skip to content

Commit

Permalink
Replace raw invisible characters in regex expressions with counterpar…
Browse files Browse the repository at this point in the history
…t Unicode regex notations (#45680)
  • Loading branch information
restuff authored Jan 18, 2023
1 parent 7906436 commit e4f05eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
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);
});
}
}

0 comments on commit e4f05eb

Please sign in to comment.