From 46209dfecdba7d721dad13d4aa3614fb09b7541d Mon Sep 17 00:00:00 2001 From: Christina Lin <44586776+chrstinalin@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:21:45 -0500 Subject: [PATCH] review --- .../devhub/addons/edit/describe.html | 4 +- .../devhub/addons/edit/technical.html | 4 +- .../devhub/addons/submit/describe.html | 6 +-- .../templates/devhub/includes/macros.html | 2 +- .../devhub/includes/policy_form.html | 6 +-- src/olympia/translations/models.py | 2 + src/olympia/translations/tests/test_models.py | 48 +++++++++++++++++++ 7 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/olympia/devhub/templates/devhub/addons/edit/describe.html b/src/olympia/devhub/templates/devhub/addons/edit/describe.html index 7a795d9b3f96..8a65f2061bbc 100644 --- a/src/olympia/devhub/templates/devhub/addons/edit/describe.html +++ b/src/olympia/devhub/templates/devhub/addons/edit/describe.html @@ -1,4 +1,4 @@ -{% from "devhub/includes/macros.html" import tip, empty_unless, flags, select_cats, markdown_tip, trans_readonly %} +{% from "devhub/includes/macros.html" import tip, empty_unless, flags, select_cats, supported_syntax_tip, trans_readonly %}
- {{ markdown_tip() }} + {{ supported_syntax_tip() }} {% else %} {% call empty_unless(addon.description) %}
diff --git a/src/olympia/devhub/templates/devhub/addons/edit/technical.html b/src/olympia/devhub/templates/devhub/addons/edit/technical.html index 8873ba535a74..9d97c8735d54 100644 --- a/src/olympia/devhub/templates/devhub/addons/edit/technical.html +++ b/src/olympia/devhub/templates/devhub/addons/edit/technical.html @@ -1,4 +1,4 @@ -{% from "devhub/includes/macros.html" import tip, markdown_tip, empty_unless, flags %} +{% from "devhub/includes/macros.html" import tip, supported_syntax_tip, empty_unless, flags %} @@ -32,7 +32,7 @@

{% if editable %} {{ main_form.developer_comments }} {{ main_form.developer_comments.errors }} - {{ markdown_tip() }} + {{ supported_syntax_tip() }} {% else %} {% call empty_unless(addon.developer_comments) %}
{{ addon|all_locales('developer_comments', nl2br=True) }}
diff --git a/src/olympia/devhub/templates/devhub/addons/submit/describe.html b/src/olympia/devhub/templates/devhub/addons/submit/describe.html index ba80a5b51454..e580185aed68 100644 --- a/src/olympia/devhub/templates/devhub/addons/submit/describe.html +++ b/src/olympia/devhub/templates/devhub/addons/submit/describe.html @@ -1,4 +1,4 @@ -{% from "devhub/includes/macros.html" import markdown_tip, select_cats %} +{% from "devhub/includes/macros.html" import supported_syntax_tip, select_cats %} {% from "includes/forms.html" import tip %} {% extends "devhub/addons/submit/base.html" %} @@ -113,7 +113,7 @@

{{ _('Describe Add-on') }}

data-for-startswith="{{ describe_form.description.auto_id }}_" data-minlength="{{ describe_form.description.field.min_length }}">
- {{ markdown_tip() }} + {{ supported_syntax_tip() }} {% endif %} {% if addon.type != amo.ADDON_STATICTHEME %} @@ -180,7 +180,7 @@

{{ _('Describe Add-on') }}

{{ license_form.text.errors }} {{ license_form.text.label_tag() }} {{ license_form.text }} - {{ markdown_tip() }} + {{ supported_syntax_tip() }} {% endif %} diff --git a/src/olympia/devhub/templates/devhub/includes/macros.html b/src/olympia/devhub/templates/devhub/includes/macros.html index 7493132106b7..13c96ca93739 100644 --- a/src/olympia/devhub/templates/devhub/includes/macros.html +++ b/src/olympia/devhub/templates/devhub/includes/macros.html @@ -10,7 +10,7 @@

{% endmacro %} -{% macro markdown_tip(title=None) %} +{% macro supported_syntax_tip(title=None) %}

{# L10n: %s is a list of markdown syntax. #} {{ tip(_('End-User License Agreement'), _('Please note that a EULA is not ' @@ -13,7 +13,7 @@ {{ policy_form.eula.label }} {{ policy_form.eula }} - {{ markdown_tip() }} + {{ supported_syntax_tip() }} @@ -31,7 +31,7 @@ {{ policy_form.privacy_policy.label }} {{ policy_form.privacy_policy }} - {{ markdown_tip() }} + {{ supported_syntax_tip() }} diff --git a/src/olympia/translations/models.py b/src/olympia/translations/models.py index b92747639ceb..2ab86ce28a8d 100644 --- a/src/olympia/translations/models.py +++ b/src/olympia/translations/models.py @@ -239,6 +239,8 @@ def clean_localized_string(self): ) # hack; cleaning breaks blockquotes text_with_brs = cleaned.replace('>', '>') + # the base syntax of markdown library does not provide abbreviations or fenced code. + # see https://python-markdown.github.io/extensions/ markdown = md.markdown(text_with_brs, extensions=['abbr', 'fenced_code']) # Keep only the allowed tags and attributes, strip the rest. diff --git a/src/olympia/translations/tests/test_models.py b/src/olympia/translations/tests/test_models.py index c1e3b314bc57..ab17d0d817f0 100644 --- a/src/olympia/translations/tests/test_models.py +++ b/src/olympia/translations/tests/test_models.py @@ -19,6 +19,7 @@ from olympia.translations.models import ( LinkifiedTranslation, NoURLsTranslation, + PurifiedMarkdownTranslation, PurifiedTranslation, Translation, TranslationSequence, @@ -701,6 +702,53 @@ def test_external_text_link(self, get_outgoing_url_mock): assert doc('b')[0].text == 'markup' +class PurifiedMarkdownTranslationTest(TestCase): + def test_output(self): + assert isinstance(PurifiedMarkdownTranslation().__html__(), str) + + def test_raw_text(self): + s = ' This is some text ' + x = PurifiedMarkdownTranslation(localized_string=s) + assert x.__html__() == 'This is some text' + + def test_markdown(self): + s = '__bold text__ or _italics_ not bold' + x = PurifiedMarkdownTranslation(localized_string=s) + assert x.__html__() == 'bold text or italics <b>not bold</b>' + + def test_html(self): + s = '' + x = PurifiedMarkdownTranslation(localized_string=s) + assert x.__html__() == '<script>some naughty xss</script>' + + def test_internal_link(self): + s = '**markup** [bar](http://addons.mozilla.org/foo)' + x = PurifiedMarkdownTranslation(localized_string=s) + doc = pq(x.__html__()) + links = doc('a[href="http://addons.mozilla.org/foo"][rel="nofollow"]') + assert links[0].text == 'bar' + assert doc('strong')[0].text == 'markup' + + @patch('olympia.amo.urlresolvers.get_outgoing_url') + def test_external_link(self, get_outgoing_url_mock): + get_outgoing_url_mock.return_value = 'http://external.url' + s = '**markup** [bar](http://example.com)' + x = PurifiedMarkdownTranslation(localized_string=s) + doc = pq(x.__html__()) + links = doc('a[href="http://external.url"][rel="nofollow"]') + assert links[0].text == 'bar' + assert doc('strong')[0].text == 'markup' + + @patch('olympia.amo.urlresolvers.get_outgoing_url') + def test_external_text_link(self, get_outgoing_url_mock): + get_outgoing_url_mock.return_value = 'http://external.url' + s = '**markup** http://example.com' + x = PurifiedMarkdownTranslation(localized_string=s) + doc = pq(x.__html__()) + links = doc('a[href="http://external.url"][rel="nofollow"]') + assert links[0].text == 'http://example.com' + assert doc('strong')[0].text == 'markup' + class LinkifiedTranslationTest(TestCase): @patch('olympia.amo.urlresolvers.get_outgoing_url') def test_allowed_tags(self, get_outgoing_url_mock):