Ensure that draftail contentstate is never empty

pull/4136/head
Matt Westcott 2017-12-15 16:31:19 +00:00 zatwierdzone przez Thibaud Colas
rodzic 8280c3e56e
commit 96eab7937c
2 zmienionych plików z 9 dodań i 3 usunięć

Wyświetl plik

@ -7,6 +7,7 @@ from draftjs_exporter.defaults import render_children
from draftjs_exporter.dom import DOM
from draftjs_exporter.html import HTML as HTMLExporter
from wagtail.admin.rich_text.converters.contentstate_models import Block
from wagtail.admin.rich_text.converters.html_to_contentstate import HtmlToContentStateHandler
from wagtail.core.rich_text import features as feature_registry
@ -87,6 +88,11 @@ class ContentstateConverter():
def from_database_format(self, html):
self.html_to_contentstate_handler.reset()
self.html_to_contentstate_handler.feed(html)
if not self.html_to_contentstate_handler.contentstate.blocks:
# add an empty paragraph block to make contentstate valid
self.html_to_contentstate_handler.add_block(Block('unstyled', depth=0))
return self.html_to_contentstate_handler.contentstate.as_json(indent=4, separators=(',', ': '))
def to_database_format(self, contentstate_json):

Wyświetl plik

@ -35,9 +35,9 @@ class DraftailRichTextArea(WidgetWithScript, widgets.HiddenInput):
def render(self, name, value, attrs=None):
if value is None:
translated_value = None
else:
translated_value = self.converter.from_database_format(value)
value = ''
translated_value = self.converter.from_database_format(value)
return super().render(name, translated_value, attrs)
def render_js_init(self, id_, name, value):