123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- import json
- from django.test import TestCase
- from mock import patch
- from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
- from wagtail.embeds.models import Embed
- def content_state_equal(v1, v2):
- "Test whether two contentState structures are equal, ignoring 'key' properties"
- if type(v1) != type(v2):
- return False
- if isinstance(v1, dict):
- if set(v1.keys()) != set(v2.keys()):
- return False
- return all(
- k == 'key' or content_state_equal(v, v2[k])
- for k, v in v1.items()
- )
- elif isinstance(v1, list):
- if len(v1) != len(v2):
- return False
- return all(
- content_state_equal(a, b) for a, b in zip(v1, v2)
- )
- else:
- return v1 == v2
- class TestHtmlToContentState(TestCase):
- fixtures = ['test.json']
- def assertContentStateEqual(self, v1, v2):
- "Assert that two contentState structures are equal, ignoring 'key' properties"
- self.assertTrue(content_state_equal(v1, v2), "%r does not match %r" % (v1, v2))
- def test_paragraphs(self):
- converter = ContentstateConverter(features=[])
- result = json.loads(converter.from_database_format(
- '''
- <p>Hello world!</p>
- <p>Goodbye world!</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {'inlineStyleRanges': [], 'text': 'Hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'Goodbye world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
- ]
- })
- def test_unknown_block_becomes_paragraph(self):
- converter = ContentstateConverter(features=[])
- result = json.loads(converter.from_database_format(
- '''
- <foo>Hello world!</foo>
- <foo>I said hello world!</foo>
- <p>Goodbye world!</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- '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': []},
- ]
- })
- def test_ignore_unrecognised_tags_in_blocks(self):
- converter = ContentstateConverter(features=[])
- result = json.loads(converter.from_database_format(
- '''
- <p>Hello <foo>frabjuous</foo> world!</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {'inlineStyleRanges': [], 'text': 'Hello frabjuous world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
- ]
- })
- def test_inline_styles(self):
- converter = ContentstateConverter(features=['bold', 'italic'])
- result = json.loads(converter.from_database_format(
- '''
- <p>You <b>do <em>not</em> talk</b> about Fight Club.</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {
- 'inlineStyleRanges': [
- {'offset': 4, 'length': 11, 'style': 'BOLD'}, {'offset': 7, 'length': 3, 'style': 'ITALIC'}
- ],
- 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []
- },
- ]
- })
- def test_inline_styles_at_top_level(self):
- converter = ContentstateConverter(features=['bold', 'italic'])
- result = json.loads(converter.from_database_format(
- '''
- You <b>do <em>not</em> talk</b> about Fight Club.
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {
- 'inlineStyleRanges': [
- {'offset': 4, 'length': 11, 'style': 'BOLD'}, {'offset': 7, 'length': 3, 'style': 'ITALIC'}
- ],
- 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []
- },
- ]
- })
- def test_inline_styles_depend_on_features(self):
- converter = ContentstateConverter(features=['italic', 'just-made-it-up'])
- result = json.loads(converter.from_database_format(
- '''
- <p>You <b>do <em>not</em> talk</b> about Fight Club.</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {
- 'inlineStyleRanges': [
- {'offset': 7, 'length': 3, 'style': 'ITALIC'}
- ],
- 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []
- },
- ]
- })
- def test_ordered_list(self):
- converter = ContentstateConverter(features=['h1', 'ol', 'bold', 'italic'])
- result = json.loads(converter.from_database_format(
- '''
- <h1>The rules of Fight Club</h1>
- <ol>
- <li>You do not talk about Fight Club.</li>
- <li>You <b>do <em>not</em> talk</b> about Fight Club.</li>
- </ol>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {'inlineStyleRanges': [], 'text': 'The rules of Fight Club', 'depth': 0, 'type': 'header-one', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'ordered-list-item', 'key': '00000', 'entityRanges': []},
- {
- 'inlineStyleRanges': [
- {'offset': 4, 'length': 11, 'style': 'BOLD'}, {'offset': 7, 'length': 3, 'style': 'ITALIC'}
- ],
- 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'ordered-list-item', 'key': '00000', 'entityRanges': []
- },
- ]
- })
- def test_nested_list(self):
- converter = ContentstateConverter(features=['h1', 'ul'])
- result = json.loads(converter.from_database_format(
- '''
- <h1>Shopping list</h1>
- <ul>
- <li>Milk</li>
- <li>
- Flour
- <ul>
- <li>Plain</li>
- <li>Self-raising</li>
- </ul>
- </li>
- <li>Eggs</li>
- </ul>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {},
- 'blocks': [
- {'inlineStyleRanges': [], 'text': 'Shopping list', 'depth': 0, 'type': 'header-one', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'Milk', 'depth': 0, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'Flour', 'depth': 0, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'Plain', 'depth': 1, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'Self-raising', 'depth': 1, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
- {'inlineStyleRanges': [], 'text': 'Eggs', 'depth': 0, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
- ]
- })
- def test_external_link(self):
- converter = ContentstateConverter(features=['link'])
- result = json.loads(converter.from_database_format(
- '''
- <p>an <a href="http://wagtail.io">external</a> link</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {
- '0': {'mutability': 'MUTABLE', 'type': 'LINK', 'data': {'url': 'http://wagtail.io'}}
- },
- 'blocks': [
- {
- 'inlineStyleRanges': [], 'text': 'an external link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
- 'entityRanges': [{'offset': 3, 'length': 8, 'key': 0}]
- },
- ]
- })
- def test_page_link(self):
- converter = ContentstateConverter(features=['link'])
- result = json.loads(converter.from_database_format(
- '''
- <p>an <a linktype="page" id="3">internal</a> link</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {
- '0': {
- 'mutability': 'MUTABLE', 'type': 'LINK',
- 'data': {'id': 3, 'url': '/events/', 'parentId': 2}
- }
- },
- 'blocks': [
- {
- 'inlineStyleRanges': [], 'text': 'an internal link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
- 'entityRanges': [{'offset': 3, 'length': 8, 'key': 0}]
- },
- ]
- })
- def test_broken_page_link(self):
- converter = ContentstateConverter(features=['link'])
- result = json.loads(converter.from_database_format(
- '''
- <p>an <a linktype="page" id="9999">internal</a> link</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {
- '0': {
- 'mutability': 'MUTABLE', 'type': 'LINK',
- 'data': {}
- }
- },
- 'blocks': [
- {
- 'inlineStyleRanges': [], 'text': 'an internal link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
- 'entityRanges': [{'offset': 3, 'length': 8, 'key': 0}]
- },
- ]
- })
- def test_document_link(self):
- converter = ContentstateConverter(features=['document-link'])
- result = json.loads(converter.from_database_format(
- '''
- <p>a <a linktype="document" id="1">document</a> link</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {
- '0': {
- 'mutability': 'MUTABLE', 'type': 'DOCUMENT',
- 'data': {'id': 1, 'url': '/documents/1/test.pdf', 'filename': 'test.pdf'}
- }
- },
- 'blocks': [
- {
- 'inlineStyleRanges': [], 'text': 'a document link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
- 'entityRanges': [{'offset': 2, 'length': 8, 'key': 0}]
- },
- ]
- })
- def test_broken_document_link(self):
- converter = ContentstateConverter(features=['document-link'])
- result = json.loads(converter.from_database_format(
- '''
- <p>a <a linktype="document" id="9999">document</a> link</p>
- '''
- ))
- self.assertContentStateEqual(result, {
- 'entityMap': {
- '0': {
- 'mutability': 'MUTABLE', 'type': 'DOCUMENT',
- 'data': {}
- }
- },
- 'blocks': [
- {
- 'inlineStyleRanges': [], 'text': 'a document link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
- 'entityRanges': [{'offset': 2, 'length': 8, 'key': 0}]
- },
- ]
- })
- def test_image_embed(self):
- converter = ContentstateConverter(features=['image'])
- result = json.loads(converter.from_database_format(
- '''
- <p>before</p>
- <embed embedtype="image" alt="an image" id="1" format="left" />
- <p>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': {'format': 'left', 'alt': 'an image', 'id': '1', 'src': '/media/not-found'},
- 'mutability': 'IMMUTABLE', 'type': 'IMAGE'
- }
- }
- })
- @patch('wagtail.embeds.embeds.get_embed')
- def test_media_embed(self, get_embed):
- get_embed.return_value = Embed(
- url='https://www.youtube.com/watch?v=Kh0Y2hVe_bw',
- max_width=None,
- type='video',
- html='test html',
- title='what are birds',
- author_name='look around you',
- provider_name='YouTube',
- thumbnail_url='http://test/thumbnail.url',
- width=1000,
- height=1000,
- )
- converter = ContentstateConverter(features=['embed'])
- result = json.loads(converter.from_database_format(
- '''
- <p>before</p>
- <embed embedtype="media" url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw" />
- <p>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': {
- 'thumbnail': 'http://test/thumbnail.url',
- 'embedType': 'video',
- 'providerName': 'YouTube',
- 'title': 'what are birds',
- 'authorName': 'look around you',
- 'url': 'https://www.youtube.com/watch?v=Kh0Y2hVe_bw'
- },
- 'mutability': 'IMMUTABLE', 'type': 'EMBED'
- }
- }
- })
- def test_hr(self):
- converter = ContentstateConverter(features=['hr'])
- result = json.loads(converter.from_database_format(
- '''
- <p>before</p>
- <hr />
- <p>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_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'
- }
- }
- })
|