Bug 2444 links in rich content editor

remotes/origin/enhancement/email-actions
Alejandro Alonso 2015-03-13 13:03:40 +01:00 committed by David Barragán Merino
parent 5271e1c043
commit 4d4b52075e
4 changed files with 53 additions and 7 deletions

View File

@ -36,7 +36,6 @@ class AutolinkExtension(markdown.Extension):
* GitHub only accepts URLs with protocols or "www.", whereas Gruber's regex
accepts things like "foo.com/bar".
"""
def extendMarkdown(self, md, md_globals):
url_re = r'(?i)\b((?:(?:ftp|https?)://|www\d{0,3}[.])([^\s<>]+))'
autolink = AutolinkPattern(url_re, md)

View File

@ -0,0 +1,46 @@
# Copyright (C) 2015 Andrey Antukh <niwi@niwi.be>
# Copyright (C) 2015 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2015 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2015 Alejandro Alonso <alejandro.alonso@kaleidos.net>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import markdown
from markdown.treeprocessors import Treeprocessor
from taiga.front import resolve
class TargetBlankLinkExtension(markdown.Extension):
"""An extension that add target="_blank" to all external links."""
def extendMarkdown(self, md, md_globals):
md.treeprocessors.add("target_blank_links",
TargetBlankLinksTreeprocessor(md),
"<prettify")
class TargetBlankLinksTreeprocessor(Treeprocessor):
def run(self, root):
home_url = resolve("home")
links = root.getiterator("a")
for a in links:
href = a.get("href", "")
url = a.get("href", "")
if url.endswith("/"):
url = url[:-1]
if not url.startswith(home_url):
a.set("target", "_blank")

View File

@ -49,7 +49,7 @@ from .extensions.wikilinks import WikiLinkExtension
from .extensions.emojify import EmojifyExtension
from .extensions.mentions import MentionsExtension
from .extensions.references import TaigaReferencesExtension
from .extensions.target_link import TargetBlankLinkExtension
# Bleach configuration
bleach.ALLOWED_TAGS += ["p", "table", "thead", "tbody", "th", "tr", "td", "h1",
@ -59,7 +59,7 @@ bleach.ALLOWED_TAGS += ["p", "table", "thead", "tbody", "th", "tr", "td", "h1",
bleach.ALLOWED_STYLES.append("background")
bleach.ALLOWED_ATTRIBUTES["a"] = ["href", "title", "alt"]
bleach.ALLOWED_ATTRIBUTES["a"] = ["href", "title", "alt", "target"]
bleach.ALLOWED_ATTRIBUTES["img"] = ["alt", "src"]
bleach.ALLOWED_ATTRIBUTES["*"] = ["class", "style"]
@ -74,6 +74,7 @@ def _make_extensions_list(project=None):
EmojifyExtension(),
MentionsExtension(),
TaigaReferencesExtension(project),
TargetBlankLinkExtension(),
"extra",
"codehilite",
"sane_lists",

View File

@ -133,25 +133,25 @@ def test_render_wikilink_relative_to_absolute():
def test_render_reference_links():
expected_result = "<p>An <a href=\"http://example.com/\" title=\"Title\">example</a> of reference link</p>"
expected_result = "<p>An <a href=\"http://example.com/\" target=\"_blank\" title=\"Title\">example</a> of reference link</p>"
source = "An [example][id] of reference link\n [id]: http://example.com/ \"Title\""
assert render(dummy_project, source) == expected_result
def test_render_url_autolinks():
expected_result = "<p>Test the <a href=\"http://example.com/\">http://example.com/</a> autolink</p>"
expected_result = "<p>Test the <a href=\"http://example.com/\" target=\"_blank\">http://example.com/</a> autolink</p>"
source = "Test the http://example.com/ autolink"
assert render(dummy_project, source) == expected_result
def test_render_url_autolinks_without_http():
expected_result = "<p>Test the <a href=\"http://www.example.com\">www.example.com</a> autolink</p>"
expected_result = "<p>Test the <a href=\"http://www.example.com\" target=\"_blank\">www.example.com</a> autolink</p>"
source = "Test the www.example.com autolink"
assert render(dummy_project, source) == expected_result
def test_render_url_automail():
expected_result = "<p>Test the <a href=\"mailto:example@example.com\">example@example.com</a> automail</p>"
expected_result = "<p>Test the <a href=\"mailto:example@example.com\" target=\"_blank\">example@example.com</a> automail</p>"
source = "Test the example@example.com automail"
assert render(dummy_project, source) == expected_result