Explorar o código

Update lexer regex, fix mention regex and add more tests

Daniel Supernault hai 1 ano
pai
achega
778e83d398
Modificáronse 2 ficheiros con 64 adicións e 1 borrados
  1. 1 1
      app/Util/Lexer/Regex.php
  2. 63 0
      tests/Unit/Lexer/UsernameTest.php

+ 1 - 1
app/Util/Lexer/Regex.php

@@ -162,7 +162,7 @@ abstract class Regex
         //      look-ahead capture here and don't append $after when we return.
         $tmp['valid_mention_preceding_chars'] = '([^a-zA-Z0-9_!#\$%&*@@\/]|^|(?:^|[^a-z0-9_+~.-])RT:?)';
 
-        $re['valid_mentions_or_lists'] = '/'.$tmp['valid_mention_preceding_chars'].'(['.$tmp['at_signs'].'])([a-z0-9_\-.]{1,20})((\/[a-z][a-z0-9_\-]{0,24})?(?=(.*|$))(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i';
+        $re['valid_mentions_or_lists'] = '/'.$tmp['valid_mention_preceding_chars'].'(['.$tmp['at_signs'].'])([\p{L}0-9_\-.]{1,20})((\/[a-z][a-z0-9_\-]{0,24})?(?=(.*|$))(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/iu';
 
         $re['valid_reply'] = '/^(?:['.$tmp['spaces'].'])*['.$tmp['at_signs'].']([a-z0-9_\-.]{1,20})(?=(.*|$))/iu';
         $re['end_mention_match'] = '/\A(?:['.$tmp['at_signs'].']|['.$tmp['latin_accents'].']|:\/\/)/iu';

+ 63 - 0
tests/Unit/Lexer/UsernameTest.php

@@ -175,4 +175,67 @@ class UsernameTest extends TestCase
         $this->assertEquals($expectedEntity, $entities);
     }
 
+    /** @test * */
+    public function germanUmlatsAutolink()
+    {
+        $mentions = "@März and @königin and @Glück";
+        $autolink = Autolink::create()->autolink($mentions);
+
+        $expectedAutolink = '<a class="u-url mention" href="https://pixelfed.dev/März" rel="external nofollow noopener" target="_blank">@März</a> and <a class="u-url mention" href="https://pixelfed.dev/königin" rel="external nofollow noopener" target="_blank">@königin</a> and <a class="u-url mention" href="https://pixelfed.dev/Glück" rel="external nofollow noopener" target="_blank">@Glück</a>';
+        $this->assertEquals($expectedAutolink, $autolink);
+    }
+
+    /** @test * */
+    public function germanUmlatsExtractor()
+    {
+        $mentions = "@März and @königin and @Glück";
+        $entities = Extractor::create()->extract($mentions);
+
+        $expectedEntity = [
+            "hashtags" => [],
+            "urls" => [],
+            "mentions" => [
+              "märz",
+              "königin",
+              "glück",
+            ],
+            "replyto" => null,
+            "hashtags_with_indices" => [],
+            "urls_with_indices" => [],
+            "mentions_with_indices" => [
+              [
+                "screen_name" => "März",
+                "indices" => [
+                  0,
+                  5,
+                ],
+              ],
+              [
+                "screen_name" => "königin",
+                "indices" => [
+                  10,
+                  18,
+                ],
+              ],
+              [
+                "screen_name" => "Glück",
+                "indices" => [
+                  23,
+                  29,
+                ],
+              ],
+            ],
+        ];
+        $this->assertEquals($expectedEntity, $entities);
+    }
+
+    /** @test * */
+    public function germanUmlatsWebfingerAutolink()
+    {
+        $mentions = "hello @märz@example.org!";
+        $autolink = Autolink::create()->autolink($mentions);
+
+        $expectedAutolink = 'hello <a class="u-url list-slug" href="https://pixelfed.dev/@märz@example.org" rel="external nofollow noopener" target="_blank">@märz@example.org</a>!';
+        $this->assertEquals($expectedAutolink, $autolink);
+    }
 }