From 4ba5c8f94efc5468628a62581a587b6ffb94aa3e Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Thu, 26 Mar 2020 11:49:45 +0100
Subject: [PATCH] Set target blanks on all external links in application forms
 with js. Make target blank default for all link in TineMCE editor.

---
 hypha/apply/funds/templates/funds/application_base.html   | 1 +
 hypha/apply/utils/options.py                              | 1 +
 hypha/settings/base.py                                    | 2 +-
 .../src/javascript/apply/external-link-new-window.js      | 8 ++++++++
 4 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 hypha/static_src/src/javascript/apply/external-link-new-window.js

diff --git a/hypha/apply/funds/templates/funds/application_base.html b/hypha/apply/funds/templates/funds/application_base.html
index c4ecb9976..0ccb5ec08 100644
--- a/hypha/apply/funds/templates/funds/application_base.html
+++ b/hypha/apply/funds/templates/funds/application_base.html
@@ -60,6 +60,7 @@
     <script src="{% static 'js/apply/file-uploads.js' %}"></script>
     <script src="{% static 'js/apply/tinymce-word-count.js' %}"></script>
     <script src="{% static 'js/apply/submission-form-copy.js' %}"></script>
+    <script src="{% static 'js/apply/external-link-new-window.js' %}"></script>
     {% if not show_all_group_fields %}
     <script src="{% static 'js/apply/form-group-toggle.js' %}"></script>
     {% endif %}
diff --git a/hypha/apply/utils/options.py b/hypha/apply/utils/options.py
index fed0ebf0e..9f9b45d4d 100644
--- a/hypha/apply/utils/options.py
+++ b/hypha/apply/utils/options.py
@@ -20,6 +20,7 @@ MCE_ATTRIBUTES = {
     ],
     'relative_urls': False,
     'browser_spellcheck': True,
+    'default_link_target': '_blank',
 }
 MCE_ATTRIBUTES_SHORT = {**MCE_ATTRIBUTES, **{'height': 180}}
 
diff --git a/hypha/settings/base.py b/hypha/settings/base.py
index 7f557cc11..e17118970 100644
--- a/hypha/settings/base.py
+++ b/hypha/settings/base.py
@@ -494,7 +494,7 @@ SOCIAL_AUTH_PIPELINE = (
 # Bleach Settings
 BLEACH_ALLOWED_TAGS = ['a', 'b', 'big', 'blockquote', 'br', 'cite', 'code', 'col', 'colgroup', 'dd', 'del', 'dl', 'dt', 'em', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'ins', 'li', 'ol', 'p', 'pre', 'small', 'span', 'strong', 'sub', 'sup', 'table', 'tbody', 'td', 'th', 'thead', 'tr', 'ul']
 
-BLEACH_ALLOWED_ATTRIBUTES = ['class', 'colspan', 'href', 'rowspan', 'title', 'width']
+BLEACH_ALLOWED_ATTRIBUTES = ['class', 'colspan', 'href', 'rowspan', 'target', 'title', 'width']
 
 BLEACH_ALLOWED_STYLES = []
 
diff --git a/hypha/static_src/src/javascript/apply/external-link-new-window.js b/hypha/static_src/src/javascript/apply/external-link-new-window.js
new file mode 100644
index 000000000..81b9d22fe
--- /dev/null
+++ b/hypha/static_src/src/javascript/apply/external-link-new-window.js
@@ -0,0 +1,8 @@
+(function ($) {
+
+    'use strict';
+
+    // Make external links on application forms open in a new window/tab.
+    $('.application-form').find("a[href^='http']").attr('target', '_blank');
+
+})(jQuery);
-- 
GitLab