From 99e0859f77d4aa320206b82df99cc0b4bd5e368d Mon Sep 17 00:00:00 2001 From: puklipo Date: Wed, 20 Nov 2024 10:38:33 +0900 Subject: [PATCH] Update DetectFacets.php (#6) * Update TextBuilderTest.php * Update DetectFacets.php --- src/RichText/DetectFacets.php | 20 ++++++++++---------- tests/Feature/RichText/TextBuilderTest.php | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/RichText/DetectFacets.php b/src/RichText/DetectFacets.php index ff6f5765..62c42f2e 100644 --- a/src/RichText/DetectFacets.php +++ b/src/RichText/DetectFacets.php @@ -88,19 +88,19 @@ protected function link(): void $start = data_get($match, 1); $end = $start + strlen($uri); - if (! Str::startsWith($uri, 'http')) { - return; + if (! Str::startsWith($uri, 'http') && Str::isUrl('https://'.$uri, ['https'])) { + $uri = 'https://'.$uri; } - $uri = Str::of($uri)->whenTest('/[.,;:!?]$/', function (Stringable $string) use (&$end) { - $end--; - return $string->rtrim('.,;:!?'); - })->toString(); + if (Str::of($uri)->test('/[.,;:!?]$/')) { + $uri = Str::rtrim($uri, '.,;:!?'); + $end = $start + strlen($uri); + } - $uri = Str::of($uri)->whenTest('/[)]$/', function (Stringable $string) use (&$end) { - $end--; - return $string->rtrim(')'); - })->toString(); + if (Str::of($uri)->test('/[)]$/') && Str::doesntContain($uri, '(')) { + $uri = Str::rtrim($uri, ')'); + $end = $start + strlen($uri); + } $this->facets[] = [ '$type' => self::TYPE, diff --git a/tests/Feature/RichText/TextBuilderTest.php b/tests/Feature/RichText/TextBuilderTest.php index f9aecb73..cdb9e01c 100644 --- a/tests/Feature/RichText/TextBuilderTest.php +++ b/tests/Feature/RichText/TextBuilderTest.php @@ -30,15 +30,15 @@ public function test_detect_facets_mention() public function test_detect_facets_link() { - $builder = TextBuilder::make('https://localhost https://localhost? test test (https://localhost) example.com https://')->detectFacets(); + $builder = TextBuilder::make('https://localhost https://localhost/?test=a test test (https://localhost) example.com https://localhost/.,;:!? https://localhost/#hash https://')->detectFacets(); $this->assertIsArray($builder->facets); $this->assertSame(0, data_get($builder->facets, '0.index.byteStart')); $this->assertSame(17, data_get($builder->facets, '0.index.byteEnd')); - $this->assertSame(48, data_get($builder->facets, '2.index.byteStart')); - $this->assertSame(65, data_get($builder->facets, '2.index.byteEnd')); + $this->assertSame(55, data_get($builder->facets, '2.index.byteStart')); + $this->assertSame(72, data_get($builder->facets, '2.index.byteEnd')); - $this->assertSame(['https://localhost', 'https://localhost', 'https://localhost'], collect($builder->facets)->pluck('features.0.uri')->toArray()); + $this->assertSame(['https://localhost', 'https://localhost/?test=a', 'https://localhost','https://example.com', 'https://localhost/', 'https://localhost/#hash'], collect($builder->facets)->pluck('features.0.uri')->toArray()); } public function test_detect_facets_tag()