0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Make RawHTMLBlock consistently use SafeText as its native value

This commit is contained in:
Matt Westcott 2015-06-01 18:18:56 +01:00
parent 7fafc63ce0
commit ce590531be
2 changed files with 89 additions and 2 deletions

View File

@ -261,8 +261,23 @@ class RawHTMLBlock(FieldBlock):
widget=forms.Textarea)
super(RawHTMLBlock, self).__init__(**kwargs)
def render_basic(self, value):
return mark_safe(value) # if it isn't safe, that's the site admin's problem for allowing raw HTML blocks in the first place...
def get_default(self):
return mark_safe(self.meta.default or '')
def to_python(self, value):
return mark_safe(value)
def get_prep_value(self, value):
# explicitly convert to a plain string, just in case we're using some serialisation method
# that doesn't cope with SafeText values correctly
return str(value)
def value_for_form(self, value):
# need to explicitly mark as unsafe, or it'll output unescaped HTML in the textarea
return str(value)
def value_from_form(self, value):
return mark_safe(value)
class Meta:
icon = 'code'

View File

@ -4,6 +4,7 @@ from django import forms
from django.forms.utils import ErrorList
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils.safestring import mark_safe, SafeText
from wagtail.wagtailcore import blocks
from wagtail.wagtailcore.models import Page
@ -267,6 +268,77 @@ class TestChoiceBlock(unittest.TestCase):
)
class TestRawHTMLBlock(unittest.TestCase):
def test_get_default_with_fallback_value(self):
default_value = blocks.RawHTMLBlock().get_default()
self.assertEqual(default_value, '')
self.assertTrue(isinstance(default_value, SafeText))
def test_get_default_with_none(self):
default_value = blocks.RawHTMLBlock(default=None).get_default()
self.assertEqual(default_value, '')
self.assertTrue(isinstance(default_value, SafeText))
def test_get_default_with_empty_string(self):
default_value = blocks.RawHTMLBlock(default='').get_default()
self.assertEqual(default_value, '')
self.assertTrue(isinstance(default_value, SafeText))
def test_get_default_with_nonempty_string(self):
default_value = blocks.RawHTMLBlock(default='<blink>BOOM</blink>').get_default()
self.assertEqual(default_value, '<blink>BOOM</blink>')
self.assertTrue(isinstance(default_value, SafeText))
def test_serialize(self):
block = blocks.RawHTMLBlock()
result = block.get_prep_value(mark_safe('<blink>BOOM</blink>'))
self.assertEqual(result, '<blink>BOOM</blink>')
def test_deserialize(self):
block = blocks.RawHTMLBlock()
result = block.to_python('<blink>BOOM</blink>')
self.assertEqual(result, '<blink>BOOM</blink>')
self.assertTrue(isinstance(result, SafeText))
def test_render(self):
block = blocks.RawHTMLBlock()
result = block.render(mark_safe('<blink>BOOM</blink>'))
self.assertEqual(result, '<blink>BOOM</blink>')
self.assertTrue(isinstance(result, SafeText))
def test_render_form(self):
block = blocks.RawHTMLBlock()
result = block.render_form(mark_safe('<blink>BOOM</blink>'), prefix='rawhtml')
self.assertIn('<textarea ', result)
self.assertIn('name="rawhtml"', result)
self.assertIn('&lt;blink&gt;BOOM&lt;/blink&gt;', result)
def test_form_response(self):
block = blocks.RawHTMLBlock()
result = block.value_from_datadict({'rawhtml': '<blink>BOOM</blink>'}, {}, prefix='rawhtml')
self.assertEqual(result, '<blink>BOOM</blink>')
self.assertTrue(isinstance(result, SafeText))
def test_clean_required_field(self):
block = blocks.RawHTMLBlock()
result = block.clean(mark_safe('<blink>BOOM</blink>'))
self.assertEqual(result, '<blink>BOOM</blink>')
self.assertTrue(isinstance(result, SafeText))
with self.assertRaises(ValidationError):
block.clean(mark_safe(''))
def test_clean_nonrequired_field(self):
block = blocks.RawHTMLBlock(required=False)
result = block.clean(mark_safe('<blink>BOOM</blink>'))
self.assertEqual(result, '<blink>BOOM</blink>')
self.assertTrue(isinstance(result, SafeText))
result = block.clean(mark_safe(''))
self.assertEqual(result, '')
self.assertTrue(isinstance(result, SafeText))
class TestMeta(unittest.TestCase):
def test_set_template_with_meta(self):
class HeadingBlock(blocks.CharBlock):