From f88ad38294123cc2116fb479afb0556da6fb909f Mon Sep 17 00:00:00 2001 From: TAKAHASHI Shuuji Date: Sat, 27 May 2023 00:02:49 +0900 Subject: [PATCH] Prepend invisible URL protocol prefix (#586) --- core/html.py | 8 ++++++-- tests/api/test_statuses.py | 3 +-- tests/core/test_html.py | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/html.py b/core/html.py index be5fa97..68d66ff 100644 --- a/core/html.py +++ b/core/html.py @@ -167,9 +167,13 @@ class FediverseHtmlParser(HTMLParser): """ looks_like_link = bool(self.URL_REGEX.match(content)) if looks_like_link: - content = content.split("://", 1)[1] + protocol, content = content.split("://", 1) + else: + protocol = "" if (looks_like_link and len(content) > 30) or has_ellipsis: - return f'{html.escape(content[:30])}' + return f'{html.escape(content[:30])}' + elif looks_like_link: + return f'{html.escape(content)}' else: return f'{html.escape(content)}' diff --git a/tests/api/test_statuses.py b/tests/api/test_statuses.py index 50d5ec3..f6ef0e7 100644 --- a/tests/api/test_statuses.py +++ b/tests/api/test_statuses.py @@ -170,8 +170,7 @@ def test_content_link(api_client, identity, remote_identity): }, ).json() - # temp fix assert ( response["content"] - == '

Takahē - return to the wild - www.youtube.com/watch?v=IG423K

' + == '

Takahē - return to the wild - www.youtube.com/watch?v=IG423K

' ) diff --git a/tests/core/test_html.py b/tests/core/test_html.py index da3ff7a..09f8032 100644 --- a/tests/core/test_html.py +++ b/tests/core/test_html.py @@ -37,7 +37,8 @@ def test_parser(identity): assert parser.plain_text == "test.com" parser = FediverseHtmlParser("

https://test.com

") assert ( - parser.html == '

test.com

' + parser.html + == '

test.com

' ) assert parser.plain_text == "https://test.com" @@ -54,7 +55,7 @@ def test_parser(identity): parser = FediverseHtmlParser(f"

{full_url}

") assert ( parser.html - == f'

social.example.com/a-long/path

' + == f'

social.example.com/a-long/path

' ) assert ( parser.plain_text