kopia lustrzana https://github.com/wagtail/wagtail
Gracefully handle bare content at the top level
rodzic
808ad56e0f
commit
aa3b588cf4
|
@ -58,7 +58,6 @@ class BlockElementHandler(object):
|
||||||
self.block_type = block_type
|
self.block_type = block_type
|
||||||
|
|
||||||
def create_block(self, name, attrs, state, contentstate):
|
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)
|
return Block(self.block_type, depth=state.list_depth)
|
||||||
|
|
||||||
def handle_starttag(self, name, attrs, state, contentstate):
|
def handle_starttag(self, name, attrs, state, contentstate):
|
||||||
|
@ -248,7 +247,14 @@ class HtmlToContentStateHandler(HTMLParser):
|
||||||
|
|
||||||
def handle_data(self, content):
|
def handle_data(self, content):
|
||||||
if self.state.current_block is None:
|
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
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ class TestHtmlToContentState(TestCase):
|
||||||
result = json.loads(converter.from_database_format(
|
result = json.loads(converter.from_database_format(
|
||||||
'''
|
'''
|
||||||
<foo>Hello world!</foo>
|
<foo>Hello world!</foo>
|
||||||
|
<foo>I said hello world!</foo>
|
||||||
<p>Goodbye world!</p>
|
<p>Goodbye world!</p>
|
||||||
'''
|
'''
|
||||||
))
|
))
|
||||||
|
@ -60,6 +61,29 @@ class TestHtmlToContentState(TestCase):
|
||||||
'entityMap': {},
|
'entityMap': {},
|
||||||
'blocks': [
|
'blocks': [
|
||||||
{'inlineStyleRanges': [], 'text': 'Hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
|
{'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': []},
|
{'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': []},
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
Ładowanie…
Reference in New Issue