From cc62044e6f524519cb5fdaf14b463c5aa98fd77c Mon Sep 17 00:00:00 2001 From: SychO9 Date: Mon, 24 May 2021 21:49:31 +0100 Subject: [PATCH] Support smart quotes in mentions format --- .../js/src/forum/addComposerAutocomplete.js | 2 +- extensions/mentions/src/ConfigureMentions.php | 4 +-- .../integration/api/UserMentionsTest.php | 31 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/extensions/mentions/js/src/forum/addComposerAutocomplete.js b/extensions/mentions/js/src/forum/addComposerAutocomplete.js index e96298e94..18a7229ad 100644 --- a/extensions/mentions/js/src/forum/addComposerAutocomplete.js +++ b/extensions/mentions/js/src/forum/addComposerAutocomplete.js @@ -76,7 +76,7 @@ export default function addComposerAutocomplete() { if (absMentionStart) { typed = lastChunk.substring(relMentionStart).toLowerCase(); - matchTyped = typed.match(/^"((?:(?!"#).)+)$/); + matchTyped = typed.match(/^["|“]((?:(?!"#).)+)$/); typed = (matchTyped && matchTyped[1]) || typed; const makeSuggestion = function(user, replacement, content, className = '') { diff --git a/extensions/mentions/src/ConfigureMentions.php b/extensions/mentions/src/ConfigureMentions.php index f1d50d46e..444e5415c 100644 --- a/extensions/mentions/src/ConfigureMentions.php +++ b/extensions/mentions/src/ConfigureMentions.php @@ -58,7 +58,7 @@ class ConfigureMentions $tag->filterChain->prepend([static::class, 'addUserId']) ->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterUserMentions(tag); }'); - $config->Preg->match('/\B@"(?((?!"#[a-z]{0,3}[0-9]+).)+)"#(?[0-9]+)\b/', $tagName); + $config->Preg->match('/\B@["|“](?((?!"#[a-z]{0,3}[0-9]+).)+)["|”]#(?[0-9]+)\b/', $tagName); $config->Preg->match('/\B@(?[a-z0-9_-]+)(?!#)/i', $tagName); } @@ -114,7 +114,7 @@ class ConfigureMentions ->prepend([static::class, 'addPostId']) ->setJS('function(tag) { return flarum.extensions["flarum-mentions"].filterPostMentions(tag); }'); - $config->Preg->match('/\B@"(?((?!"#[a-z]{0,3}[0-9]+).)+)"#p(?[0-9]+)\b/', $tagName); + $config->Preg->match('/\B@["|“](?((?!"#[a-z]{0,3}[0-9]+).)+)["|”]#p(?[0-9]+)\b/', $tagName); } /** diff --git a/extensions/mentions/tests/integration/api/UserMentionsTest.php b/extensions/mentions/tests/integration/api/UserMentionsTest.php index 7321e52d5..c0443c0c9 100644 --- a/extensions/mentions/tests/integration/api/UserMentionsTest.php +++ b/extensions/mentions/tests/integration/api/UserMentionsTest.php @@ -156,6 +156,37 @@ class UserMentionsTest extends TestCase $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsUsers->find(3)); } + /** + * @test + */ + public function mentioning_a_valid_user_with_new_format_with_smart_quotes_works() + { + $response = $this->send( + $this->request('POST', '/api/posts', [ + 'authenticatedAs' => 1, + 'json' => [ + 'data' => [ + 'attributes' => [ + 'content' => '@“POTATO$”#3', + ], + 'relationships' => [ + 'discussion' => ['data' => ['id' => 2]], + ], + ], + ], + ]) + ); + + $this->assertEquals(201, $response->getStatusCode()); + + $response = json_decode($response->getBody(), true); + + $this->assertStringContainsString('@POTATO$', $response['data']['attributes']['contentHtml']); + $this->assertStringContainsString('@"POTATO$"#3', $response['data']['attributes']['content']); + $this->assertStringContainsString('UserMention', $response['data']['attributes']['contentHtml']); + $this->assertNotNull(CommentPost::find($response['data']['id'])->mentionsUsers->find(3)); + } + /** * @test */