From 651ef209acf00c619a7990a3154e46cfe3ad0ff8 Mon Sep 17 00:00:00 2001 From: Jason Bau Date: Tue, 22 Oct 2013 12:04:37 -0700 Subject: [PATCH] add autolinking --- .../openendedchild.py | 9 +++-- .../xmodule/tests/test_combined_open_ended.py | 33 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/common/lib/xmodule/xmodule/open_ended_grading_classes/openendedchild.py b/common/lib/xmodule/xmodule/open_ended_grading_classes/openendedchild.py index 4804ffbf81..49c66ed0af 100644 --- a/common/lib/xmodule/xmodule/open_ended_grading_classes/openendedchild.py +++ b/common/lib/xmodule/xmodule/open_ended_grading_classes/openendedchild.py @@ -2,6 +2,7 @@ import json import logging import re import bleach +from html5lib.tokenizer import HTMLTokenizer from xmodule.progress import Progress import capa.xqueue_interface as xqueue_interface from capa.util import * @@ -53,7 +54,7 @@ def upload_to_s3(file_to_upload, keyname, s3_interface): # Used by sanitize_html ALLOWED_HTML_ATTRS = { '*': ['id', 'class', 'height', 'width', 'alt'], - 'a': ['href', 'title', 'rel'], + 'a': ['href', 'title', 'rel', 'target'], 'embed': ['src'], 'iframe': ['src'], 'img': ['src'], @@ -222,7 +223,11 @@ class OpenEndedChild(object): tags=['embed', 'iframe', 'a', 'img', 'br'], attributes=ALLOWED_HTML_ATTRS, strip=True) - return OpenEndedChild.replace_newlines(clean_html) + autolinked = bleach.linkify(clean_html, + callbacks=[bleach.callbacks.target_blank], + skip_pre=True, + tokenizer=HTMLTokenizer) + return OpenEndedChild.replace_newlines(autolinked) @staticmethod def replace_newlines(html): diff --git a/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py b/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py index aaebd03821..47e6d9bdb7 100644 --- a/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py +++ b/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py @@ -1011,10 +1011,10 @@ class OpenEndedModuleUtilTest(unittest.TestCase): script_clean = u'alert("xss!")' img_dirty = u'cats' img_clean = u'cats' - embed_dirty = u'' + embed_dirty = u'' embed_clean = u'' - iframe_dirty = u'' - iframe_clean = u'' + iframe_dirty = u'' text = u'I am a \u201c\xfcber student\u201d' text_lessthan_noencd = u'This used to be broken < by the other parser. 3>5' @@ -1022,6 +1022,9 @@ class OpenEndedModuleUtilTest(unittest.TestCase): text_linebreaks = u"St\xfcdent submission:\nI like lamp." text_brs = u"St\xfcdent submission:
I like lamp." + link_text = u'I love going to www.lolcatz.com' + link_atag = u'I love going to www.lolcatz.com' + def test_script(self): """ Basic test for stripping