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

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

Merged
merged 1 commit into from
Mar 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
4 changes: 2 additions & 2 deletions application/bookmark/LinkUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ function is_note($linkUrl)
function tags_str2array(?string $tags, string $separator): array
{
// 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) ?: [];
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/bookmark/LinkUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,25 @@ public function testTagsStr2ArrayWithCharSeparator(): void
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.
*/
Expand Down