Recover from block-level entities being (illegally) contained in other blocks

This commit is contained in:
Matt Westcott 2017-12-12 12:19:17 +00:00 committed by Thibaud Colas
parent e107812393
commit 3d435327b6
2 changed files with 45 additions and 1 deletions

View file

@ -177,7 +177,8 @@ class AtomicBlockEntityElementHandler(object):
Handler for elements like <img> that exist as a single immutable item at the block level
"""
def handle_starttag(self, name, attrs, state, contentstate):
assert state.current_block is None, "%s element found nested inside another block" % name
# forcibly close any block that illegally contains this one
state.current_block = None
entity = self.create_entity(name, dict(attrs), state, contentstate)
key = contentstate.add_entity(entity)

View file

@ -391,3 +391,46 @@ class TestHtmlToContentState(TestCase):
}
}
})
def test_block_element_in_paragraph(self):
converter = ContentstateConverter(features=['hr'])
result = json.loads(converter.from_database_format(
'''
<p>before<hr />after</p>
'''
))
self.assertContentStateEqual(result, {
'blocks': [
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'before', 'type': 'unstyled'},
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'after', 'type': 'unstyled'}
],
'entityMap': {
'0': {
'data': {},
'mutability': 'IMMUTABLE', 'type': 'HORIZONTAL_RULE'
}
}
})
def test_block_element_in_empty_paragraph(self):
converter = ContentstateConverter(features=['hr'])
result = json.loads(converter.from_database_format(
'''
<p><hr /></p>
'''
))
# ignoring the paragraph completely would probably be better,
# but we'll settle for an empty preceding paragraph and not crashing as the next best thing...
self.assertContentStateEqual(result, {
'blocks': [
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': '', 'type': 'unstyled'},
{'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
],
'entityMap': {
'0': {
'data': {},
'mutability': 'IMMUTABLE', 'type': 'HORIZONTAL_RULE'
}
}
})