mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-04-22 23:54:46 +00:00
Recover from block-level entities being (illegally) contained in other blocks
This commit is contained in:
parent
e107812393
commit
3d435327b6
2 changed files with 45 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in a new issue