diff --git a/wagtail/tests/wagtail_hooks.py b/wagtail/tests/wagtail_hooks.py index 91c76c9f4..5dccca666 100644 --- a/wagtail/tests/wagtail_hooks.py +++ b/wagtail/tests/wagtail_hooks.py @@ -1,4 +1,5 @@ from wagtail.wagtailadmin import hooks +from wagtail.wagtailcore.whitelist import attribute_rule, check_url, allow_without_attributes def editor_css(): return """""" @@ -8,3 +9,11 @@ hooks.register('insert_editor_css', editor_css) def editor_js(): return """""" hooks.register('insert_editor_js', editor_js) + + +def whitelister_element_rules(): + return { + 'blockquote': allow_without_attributes, + 'a': attribute_rule({'href': check_url, 'target': True}), + } +hooks.register('construct_whitelister_element_rules', whitelister_element_rules) diff --git a/wagtail/wagtailcore/tests/test_dbwhitelister.py b/wagtail/wagtailcore/tests/test_dbwhitelister.py new file mode 100644 index 000000000..db5b931ee --- /dev/null +++ b/wagtail/wagtailcore/tests/test_dbwhitelister.py @@ -0,0 +1,43 @@ +from django.test import TestCase +from wagtail.wagtailcore.rich_text import DbWhitelister + +from bs4 import BeautifulSoup + +class TestDbWhitelister(TestCase): + def assertHtmlEqual(self, str1, str2): + """ + Assert that two HTML strings are equal at the DOM level + (necessary because we can't guarantee the order that attributes are output in) + """ + self.assertEqual(BeautifulSoup(str1), BeautifulSoup(str2)) + + def test_page_link_is_rewritten(self): + input_html = '

Look at the lovely homepage of my Wagtail site

' + output_html = DbWhitelister.clean(input_html) + expected = '

Look at the lovely homepage of my Wagtail site

' + self.assertHtmlEqual(expected, output_html) + + def test_document_link_is_rewritten(self): + input_html = '

Look at our horribly oversized brochure

' + output_html = DbWhitelister.clean(input_html) + expected = '

Look at our horribly oversized brochure

' + self.assertHtmlEqual(expected, output_html) + + def test_image_embed_is_rewritten(self): + input_html = '

OMG look at this picture of a kitten:

A cute kitten
A kitten, yesterday.

' + output_html = DbWhitelister.clean(input_html) + expected = '

OMG look at this picture of a kitten:

' + self.assertHtmlEqual(expected, output_html) + + def test_media_embed_is_rewritten(self): + input_html = '

OMG look at this video of a kitten:

' + output_html = DbWhitelister.clean(input_html) + expected = '

OMG look at this video of a kitten:

' + self.assertHtmlEqual(expected, output_html) + + def test_whitelist_hooks(self): + # wagtail.tests.wagtail_hooks overrides the whitelist to permit
and + input_html = '
I would put a tax on all people who stand in water.

- Gumby' + output_html = DbWhitelister.clean(input_html) + expected = '

I would put a tax on all people who stand in water.

- Gumby' + self.assertHtmlEqual(expected, output_html)