Gracefully handle bare content at the top level

pull/4136/head
Matt Westcott 2017-12-05 16:40:52 +00:00 zatwierdzone przez Thibaud Colas
rodzic 808ad56e0f
commit aa3b588cf4
2 zmienionych plików z 34 dodań i 4 usunięć

Wyświetl plik

@ -58,7 +58,6 @@ class BlockElementHandler(object):
self.block_type = block_type
def create_block(self, name, attrs, state, contentstate):
assert state.current_block is None, "%s element found nested inside another block" % name
return Block(self.block_type, depth=state.list_depth)
def handle_starttag(self, name, attrs, state, contentstate):
@ -248,7 +247,14 @@ class HtmlToContentStateHandler(HTMLParser):
def handle_data(self, content):
if self.state.current_block is None:
assert not content.strip(), "Bare text content found at the top level: %r" % content
content = content.strip()
if content:
# create a new paragraph block for this content
block = Block('unstyled', depth=self.state.list_depth)
self.contentstate.blocks.append(block)
self.state.current_block = block
else:
# ignore top-level whitespace
return
else:
self.state.current_block.text += content
self.state.current_block.text += content

Wyświetl plik

@ -53,6 +53,7 @@ class TestHtmlToContentState(TestCase):
result = json.loads(converter.from_database_format(
'''
<foo>Hello world!</foo>
<foo>I said hello world!</foo>
<p>Goodbye world!</p>
'''
))
@ -60,6 +61,29 @@ class TestHtmlToContentState(TestCase):
'entityMap': {},
'blocks': [
{'inlineStyleRanges': [], 'text': 'Hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'I said hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'Goodbye world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
]
})
def test_bare_text_becomes_paragraph(self):
converter = ContentstateConverter(features=[])
result = json.loads(converter.from_database_format(
'''
before
<p>paragraph</p>
between
<p>paragraph</p>
after
'''
))
self.assertContentStateEqual(result, {
'entityMap': {},
'blocks': [
{'inlineStyleRanges': [], 'text': 'before', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'paragraph', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'between', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'paragraph', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
{'inlineStyleRanges': [], 'text': 'after', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
]
})