From 2d9f4a3681af33e942b7e6c5aa0c920bf4e7e7e1 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 19 Feb 2023 11:36:35 +0000 Subject: [PATCH] Remove css style from post content This could otherwise mess up the UI --- tests.py | 18 ++++++++++++++++++ utils.py | 20 ++++++++++++++++++++ webapp_post.py | 5 +++++ 3 files changed, 43 insertions(+) diff --git a/tests.py b/tests.py index 026f1a11a..ce6281887 100644 --- a/tests.py +++ b/tests.py @@ -54,6 +54,7 @@ from follow import clear_followers from follow import send_follow_request_via_server from follow import send_unfollow_request_via_server from siteactive import site_is_active +from utils import remove_style_within_html from utils import html_tag_has_closing from utils import remove_inverted_text from utils import remove_square_capitals @@ -7865,6 +7866,22 @@ def _test_html_closing_tag() -> None: assert not html_tag_has_closing('code', content) +def _test_remove_style() -> None: + print('remove_style') + html_str = '

this is a test

' + result = remove_style_within_html(html_str) + assert result == html_str + + html_str = \ + 'something' + result = remove_style_within_html(html_str) + expected = \ + 'something' + if result != expected: + print(expected + '\n\n' + result) + assert result == expected + + def run_all_tests(): base_dir = os.getcwd() print('Running tests...') @@ -7882,6 +7899,7 @@ def run_all_tests(): _test_checkbox_names() _test_thread_functions() _test_functions() + _test_remove_style() _test_html_closing_tag() _test_replace_remote_tags() _test_replace_variable() diff --git a/utils.py b/utils.py index e8324fda2..d5fa9c724 100644 --- a/utils.py +++ b/utils.py @@ -655,6 +655,26 @@ def remove_html(content: str) -> str: return result +def remove_style_within_html(content: str) -> str: + """Removes style="something" within html post content. + Used to ensure that styles + """ + if '<' not in content: + return content + if ' style="' not in content: + return content + sections = content.split(' style="') + result = '' + ctr = 0 + for section_text in sections: + if ctr > 0: + result += section_text.split('"', 1)[1] + else: + result = section_text + ctr = 1 + return result + + def first_paragraph_from_string(content: str) -> str: """Get the first paragraph from a blog post to be used as a summary in the newswire feed diff --git a/webapp_post.py b/webapp_post.py index 0bb9592bf..54e15755d 100644 --- a/webapp_post.py +++ b/webapp_post.py @@ -25,6 +25,7 @@ from posts import post_is_muted from posts import get_person_box from posts import download_announce from posts import populate_replies_json +from utils import remove_style_within_html from utils import license_link_from_name from utils import dont_speak_hashtags from utils import remove_eol @@ -2423,6 +2424,10 @@ def individual_post_as_html(signing_priv_key_pem: str, languages_understood = get_actor_languages_list(actor_json) content_str = get_content_from_post(post_json_object, system_language, languages_understood) + + # remove any css styling within the post itself + content_str = remove_style_within_html(content_str) + content_language = \ get_language_from_post(post_json_object, system_language, languages_understood)