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

remove invisible Unicode special characters from search keywords #16483

Merged
merged 5 commits into from
Jan 22, 2025
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Release Notes for Craft CMS 4

## Fixed a bug where all multi-byte characters were getting stripped out of search indexes. ([#16457](https://github.com/craftcms/cms/issues/16457))

## 4.14.0.2 - 2025-01-21

- Fixed an error that occurred when creating a new Structure section. ([#16476](https://github.com/craftcms/cms/issues/16476))
Expand Down
8 changes: 2 additions & 6 deletions src/helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,9 @@ public static function sanitizeFilename(string $filename, array $options = []):
// Replace any control characters in the name with a space.
$filename = preg_replace("/\\x{00a0}/iu", ' ', $filename);

// Remove invisible chars from the filename
// https://github.com/craftcms/cms/issues/12741
// Remove soft hyphens (00ad), no break (0083),
// zero width space (200b), zero width non-joiner (200c), zero width joiner (200d),
// LTR character (200e), RTL character (200f),
// invisible times (2062), invisible comma (2063), invisible plus (2064),
// zero width non-break space (feff) in the filename
$filename = preg_replace('/\\x{00ad}|\\x{0083}|\\x{200b}|\\x{200c}|\\x{200d}|\\x{200e}|\\x{200f}|\\x{2062}|\\x{2063}|\\x{2064}|\\x{feff}/iu', '', $filename);
$filename = preg_replace(StringHelper::invisibleCharsRegex(), '', $filename);

// Strip any characters not allowed.
$filename = str_replace($disallowedChars, '', strip_tags($filename));
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static function normalizeKeywords(array|string $str, array $ignore = [],
}
}

// Get rid of Unicode special characters
// Get rid of invisible Unicode special characters
// (see https://github.com/craftcms/cms/issues/16430)
$str = preg_replace('/[\x{80}-\x{10FFFF}]/u', '', $str);
$str = preg_replace(StringHelper::invisibleCharsRegex(), '', $str);

// Strip out new lines and superfluous spaces
return trim(preg_replace(['/[\n\r]+/u', '/\s{2,}/u'], ' ', $str));
Expand Down
30 changes: 30 additions & 0 deletions src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2039,4 +2039,34 @@ public static function indent(string $str, string $indent = ' '): string
{
return implode("\n", array_map(fn(string $line) => $indent . $line, static::lines($str)));
}

/**
* Returns a regex pattern for invisible characters.
*
* @return string
* @since 4.14.1
*/
public static function invisibleCharsRegex(): string
{
$invisibleCharCodes = [
'00ad', // soft hyphen
'0083', // no break
'200b', // zero width space
'200c', // zero width non-joiner
'200d', // zero width joiner
'200e', // LTR character
'200f', // RTL character
'2062', // invisible times
'2063', // invisible comma
'2064', // invisible plus
'feff', //zero width non-break space
];

array_walk(
$invisibleCharCodes,
fn(&$charCode) => $charCode = sprintf('\\x{%s}', $charCode)
);

return sprintf('/%s/iu', implode('|', $invisibleCharCodes));
}
}
5 changes: 4 additions & 1 deletion tests/unit/helpers/SearchHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function normalizeKeywordsDataProviders(): array
['', '&#11aa;'],
['test test', 'TEST TEST'],
['', ['♠', '♣', '♥', '♦']],
['', ['♠', '♣', '♥', '♦'], [], false],
['♠ ♣ ♥ ♦', ['♠', '♣', '♥', '♦'], [], false],
['test', 'test '],
['', 'test', ['test']],
['test', 'test👍'],
Expand All @@ -66,6 +66,9 @@ public function normalizeKeywordsDataProviders(): array
['this accord', 'this?D’accord!'],
['womens', "women's"],
['womens', 'women’s'],
['toolbox', 'tool­box'],
['toolbox', 'tool‍box'],
['tool box', 'tool box'],
];
}
}