Fix a bug when using '/' as a tag separator (#1953)

This commit is contained in:
ArthurHoaro 2023-03-18 11:59:00 -04:00 committed by GitHub
parent 8457001294
commit b858332f9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -217,9 +217,9 @@ function is_note($linkUrl)
function tags_str2array(?string $tags, string $separator): array function tags_str2array(?string $tags, string $separator): array
{ {
// For whitespaces, we use the special \s regex character // For whitespaces, we use the special \s regex character
$separator = $separator === ' ' ? '\s' : $separator; $separator = str_replace([' ', '/'], ['\s', '\/'], $separator);
return preg_split('/\s*' . $separator . '+\s*/', trim($tags ?? ''), -1, PREG_SPLIT_NO_EMPTY); return preg_split('/\s*' . $separator . '+\s*/', trim($tags ?? ''), -1, PREG_SPLIT_NO_EMPTY) ?: [];
} }
/** /**

View file

@ -652,6 +652,25 @@ public function testTagsStr2ArrayWithCharSeparator(): void
static::assertSame([], tags_str2array(null, $separator)); static::assertSame([], tags_str2array(null, $separator));
} }
/**
* Test tags_str2array with / separator.
*/
public function testTagsStr2ArrayWithRegexDelimiterSeparator(): void
{
$separator = '/';
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1/tag2/tag3', $separator));
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1////tag2////tag3', $separator));
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('///tag1///tag2////tag3//', $separator));
static::assertSame(
['tag1#', 'tag2, and other', '.tag3'],
tags_str2array('/// tag1# /// tag2, and other ////.tag3//', $separator)
);
static::assertSame([], tags_str2array('', $separator));
static::assertSame([], tags_str2array(' ', $separator));
static::assertSame([], tags_str2array(null, $separator));
}
/** /**
* Test tags_array2str with ' ' separator. * Test tags_array2str with ' ' separator.
*/ */