test_contentstate.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import json
  2. from django.test import TestCase
  3. from mock import patch
  4. from wagtail.admin.rich_text.converters.contentstate import ContentstateConverter
  5. from wagtail.embeds.models import Embed
  6. def content_state_equal(v1, v2):
  7. "Test whether two contentState structures are equal, ignoring 'key' properties"
  8. if type(v1) != type(v2):
  9. return False
  10. if isinstance(v1, dict):
  11. if set(v1.keys()) != set(v2.keys()):
  12. return False
  13. return all(
  14. k == 'key' or content_state_equal(v, v2[k])
  15. for k, v in v1.items()
  16. )
  17. elif isinstance(v1, list):
  18. if len(v1) != len(v2):
  19. return False
  20. return all(
  21. content_state_equal(a, b) for a, b in zip(v1, v2)
  22. )
  23. else:
  24. return v1 == v2
  25. class TestHtmlToContentState(TestCase):
  26. fixtures = ['test.json']
  27. def assertContentStateEqual(self, v1, v2):
  28. "Assert that two contentState structures are equal, ignoring 'key' properties"
  29. self.assertTrue(content_state_equal(v1, v2), "%r does not match %r" % (v1, v2))
  30. def test_paragraphs(self):
  31. converter = ContentstateConverter(features=[])
  32. result = json.loads(converter.from_database_format(
  33. '''
  34. <p>Hello world!</p>
  35. <p>Goodbye world!</p>
  36. '''
  37. ))
  38. self.assertContentStateEqual(result, {
  39. 'entityMap': {},
  40. 'blocks': [
  41. {'inlineStyleRanges': [], 'text': 'Hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  42. {'inlineStyleRanges': [], 'text': 'Goodbye world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  43. ]
  44. })
  45. def test_unknown_block_becomes_paragraph(self):
  46. converter = ContentstateConverter(features=[])
  47. result = json.loads(converter.from_database_format(
  48. '''
  49. <foo>Hello world!</foo>
  50. <foo>I said hello world!</foo>
  51. <p>Goodbye world!</p>
  52. '''
  53. ))
  54. self.assertContentStateEqual(result, {
  55. 'entityMap': {},
  56. 'blocks': [
  57. {'inlineStyleRanges': [], 'text': 'Hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  58. {'inlineStyleRanges': [], 'text': 'I said hello world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  59. {'inlineStyleRanges': [], 'text': 'Goodbye world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  60. ]
  61. })
  62. def test_bare_text_becomes_paragraph(self):
  63. converter = ContentstateConverter(features=[])
  64. result = json.loads(converter.from_database_format(
  65. '''
  66. before
  67. <p>paragraph</p>
  68. between
  69. <p>paragraph</p>
  70. after
  71. '''
  72. ))
  73. self.assertContentStateEqual(result, {
  74. 'entityMap': {},
  75. 'blocks': [
  76. {'inlineStyleRanges': [], 'text': 'before', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  77. {'inlineStyleRanges': [], 'text': 'paragraph', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  78. {'inlineStyleRanges': [], 'text': 'between', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  79. {'inlineStyleRanges': [], 'text': 'paragraph', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  80. {'inlineStyleRanges': [], 'text': 'after', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  81. ]
  82. })
  83. def test_ignore_unrecognised_tags_in_blocks(self):
  84. converter = ContentstateConverter(features=[])
  85. result = json.loads(converter.from_database_format(
  86. '''
  87. <p>Hello <foo>frabjuous</foo> world!</p>
  88. '''
  89. ))
  90. self.assertContentStateEqual(result, {
  91. 'entityMap': {},
  92. 'blocks': [
  93. {'inlineStyleRanges': [], 'text': 'Hello frabjuous world!', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []},
  94. ]
  95. })
  96. def test_inline_styles(self):
  97. converter = ContentstateConverter(features=['bold', 'italic'])
  98. result = json.loads(converter.from_database_format(
  99. '''
  100. <p>You <b>do <em>not</em> talk</b> about Fight Club.</p>
  101. '''
  102. ))
  103. self.assertContentStateEqual(result, {
  104. 'entityMap': {},
  105. 'blocks': [
  106. {
  107. 'inlineStyleRanges': [
  108. {'offset': 4, 'length': 11, 'style': 'BOLD'}, {'offset': 7, 'length': 3, 'style': 'ITALIC'}
  109. ],
  110. 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []
  111. },
  112. ]
  113. })
  114. def test_inline_styles_at_top_level(self):
  115. converter = ContentstateConverter(features=['bold', 'italic'])
  116. result = json.loads(converter.from_database_format(
  117. '''
  118. You <b>do <em>not</em> talk</b> about Fight Club.
  119. '''
  120. ))
  121. self.assertContentStateEqual(result, {
  122. 'entityMap': {},
  123. 'blocks': [
  124. {
  125. 'inlineStyleRanges': [
  126. {'offset': 4, 'length': 11, 'style': 'BOLD'}, {'offset': 7, 'length': 3, 'style': 'ITALIC'}
  127. ],
  128. 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []
  129. },
  130. ]
  131. })
  132. def test_inline_styles_depend_on_features(self):
  133. converter = ContentstateConverter(features=['italic', 'just-made-it-up'])
  134. result = json.loads(converter.from_database_format(
  135. '''
  136. <p>You <b>do <em>not</em> talk</b> about Fight Club.</p>
  137. '''
  138. ))
  139. self.assertContentStateEqual(result, {
  140. 'entityMap': {},
  141. 'blocks': [
  142. {
  143. 'inlineStyleRanges': [
  144. {'offset': 7, 'length': 3, 'style': 'ITALIC'}
  145. ],
  146. 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'unstyled', 'key': '00000', 'entityRanges': []
  147. },
  148. ]
  149. })
  150. def test_ordered_list(self):
  151. converter = ContentstateConverter(features=['h1', 'ol', 'bold', 'italic'])
  152. result = json.loads(converter.from_database_format(
  153. '''
  154. <h1>The rules of Fight Club</h1>
  155. <ol>
  156. <li>You do not talk about Fight Club.</li>
  157. <li>You <b>do <em>not</em> talk</b> about Fight Club.</li>
  158. </ol>
  159. '''
  160. ))
  161. self.assertContentStateEqual(result, {
  162. 'entityMap': {},
  163. 'blocks': [
  164. {'inlineStyleRanges': [], 'text': 'The rules of Fight Club', 'depth': 0, 'type': 'header-one', 'key': '00000', 'entityRanges': []},
  165. {'inlineStyleRanges': [], 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'ordered-list-item', 'key': '00000', 'entityRanges': []},
  166. {
  167. 'inlineStyleRanges': [
  168. {'offset': 4, 'length': 11, 'style': 'BOLD'}, {'offset': 7, 'length': 3, 'style': 'ITALIC'}
  169. ],
  170. 'text': 'You do not talk about Fight Club.', 'depth': 0, 'type': 'ordered-list-item', 'key': '00000', 'entityRanges': []
  171. },
  172. ]
  173. })
  174. def test_nested_list(self):
  175. converter = ContentstateConverter(features=['h1', 'ul'])
  176. result = json.loads(converter.from_database_format(
  177. '''
  178. <h1>Shopping list</h1>
  179. <ul>
  180. <li>Milk</li>
  181. <li>
  182. Flour
  183. <ul>
  184. <li>Plain</li>
  185. <li>Self-raising</li>
  186. </ul>
  187. </li>
  188. <li>Eggs</li>
  189. </ul>
  190. '''
  191. ))
  192. self.assertContentStateEqual(result, {
  193. 'entityMap': {},
  194. 'blocks': [
  195. {'inlineStyleRanges': [], 'text': 'Shopping list', 'depth': 0, 'type': 'header-one', 'key': '00000', 'entityRanges': []},
  196. {'inlineStyleRanges': [], 'text': 'Milk', 'depth': 0, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
  197. {'inlineStyleRanges': [], 'text': 'Flour', 'depth': 0, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
  198. {'inlineStyleRanges': [], 'text': 'Plain', 'depth': 1, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
  199. {'inlineStyleRanges': [], 'text': 'Self-raising', 'depth': 1, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
  200. {'inlineStyleRanges': [], 'text': 'Eggs', 'depth': 0, 'type': 'unordered-list-item', 'key': '00000', 'entityRanges': []},
  201. ]
  202. })
  203. def test_external_link(self):
  204. converter = ContentstateConverter(features=['link'])
  205. result = json.loads(converter.from_database_format(
  206. '''
  207. <p>an <a href="http://wagtail.io">external</a> link</p>
  208. '''
  209. ))
  210. self.assertContentStateEqual(result, {
  211. 'entityMap': {
  212. '0': {'mutability': 'MUTABLE', 'type': 'LINK', 'data': {'url': 'http://wagtail.io'}}
  213. },
  214. 'blocks': [
  215. {
  216. 'inlineStyleRanges': [], 'text': 'an external link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
  217. 'entityRanges': [{'offset': 3, 'length': 8, 'key': 0}]
  218. },
  219. ]
  220. })
  221. def test_page_link(self):
  222. converter = ContentstateConverter(features=['link'])
  223. result = json.loads(converter.from_database_format(
  224. '''
  225. <p>an <a linktype="page" id="3">internal</a> link</p>
  226. '''
  227. ))
  228. self.assertContentStateEqual(result, {
  229. 'entityMap': {
  230. '0': {
  231. 'mutability': 'MUTABLE', 'type': 'LINK',
  232. 'data': {'id': 3, 'url': '/events/', 'parentId': 2}
  233. }
  234. },
  235. 'blocks': [
  236. {
  237. 'inlineStyleRanges': [], 'text': 'an internal link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
  238. 'entityRanges': [{'offset': 3, 'length': 8, 'key': 0}]
  239. },
  240. ]
  241. })
  242. def test_broken_page_link(self):
  243. converter = ContentstateConverter(features=['link'])
  244. result = json.loads(converter.from_database_format(
  245. '''
  246. <p>an <a linktype="page" id="9999">internal</a> link</p>
  247. '''
  248. ))
  249. self.assertContentStateEqual(result, {
  250. 'entityMap': {
  251. '0': {
  252. 'mutability': 'MUTABLE', 'type': 'LINK',
  253. 'data': {}
  254. }
  255. },
  256. 'blocks': [
  257. {
  258. 'inlineStyleRanges': [], 'text': 'an internal link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
  259. 'entityRanges': [{'offset': 3, 'length': 8, 'key': 0}]
  260. },
  261. ]
  262. })
  263. def test_document_link(self):
  264. converter = ContentstateConverter(features=['document-link'])
  265. result = json.loads(converter.from_database_format(
  266. '''
  267. <p>a <a linktype="document" id="1">document</a> link</p>
  268. '''
  269. ))
  270. self.assertContentStateEqual(result, {
  271. 'entityMap': {
  272. '0': {
  273. 'mutability': 'MUTABLE', 'type': 'DOCUMENT',
  274. 'data': {'id': 1, 'url': '/documents/1/test.pdf', 'filename': 'test.pdf'}
  275. }
  276. },
  277. 'blocks': [
  278. {
  279. 'inlineStyleRanges': [], 'text': 'a document link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
  280. 'entityRanges': [{'offset': 2, 'length': 8, 'key': 0}]
  281. },
  282. ]
  283. })
  284. def test_broken_document_link(self):
  285. converter = ContentstateConverter(features=['document-link'])
  286. result = json.loads(converter.from_database_format(
  287. '''
  288. <p>a <a linktype="document" id="9999">document</a> link</p>
  289. '''
  290. ))
  291. self.assertContentStateEqual(result, {
  292. 'entityMap': {
  293. '0': {
  294. 'mutability': 'MUTABLE', 'type': 'DOCUMENT',
  295. 'data': {}
  296. }
  297. },
  298. 'blocks': [
  299. {
  300. 'inlineStyleRanges': [], 'text': 'a document link', 'depth': 0, 'type': 'unstyled', 'key': '00000',
  301. 'entityRanges': [{'offset': 2, 'length': 8, 'key': 0}]
  302. },
  303. ]
  304. })
  305. def test_image_embed(self):
  306. converter = ContentstateConverter(features=['image'])
  307. result = json.loads(converter.from_database_format(
  308. '''
  309. <p>before</p>
  310. <embed embedtype="image" alt="an image" id="1" format="left" />
  311. <p>after</p>
  312. '''
  313. ))
  314. self.assertContentStateEqual(result, {
  315. 'blocks': [
  316. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'before', 'type': 'unstyled'},
  317. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
  318. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'after', 'type': 'unstyled'}
  319. ],
  320. 'entityMap': {
  321. '0': {
  322. 'data': {'format': 'left', 'alt': 'an image', 'id': '1', 'src': '/media/not-found'},
  323. 'mutability': 'IMMUTABLE', 'type': 'IMAGE'
  324. }
  325. }
  326. })
  327. @patch('wagtail.embeds.embeds.get_embed')
  328. def test_media_embed(self, get_embed):
  329. get_embed.return_value = Embed(
  330. url='https://www.youtube.com/watch?v=Kh0Y2hVe_bw',
  331. max_width=None,
  332. type='video',
  333. html='test html',
  334. title='what are birds',
  335. author_name='look around you',
  336. provider_name='YouTube',
  337. thumbnail_url='http://test/thumbnail.url',
  338. width=1000,
  339. height=1000,
  340. )
  341. converter = ContentstateConverter(features=['embed'])
  342. result = json.loads(converter.from_database_format(
  343. '''
  344. <p>before</p>
  345. <embed embedtype="media" url="https://www.youtube.com/watch?v=Kh0Y2hVe_bw" />
  346. <p>after</p>
  347. '''
  348. ))
  349. self.assertContentStateEqual(result, {
  350. 'blocks': [
  351. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'before', 'type': 'unstyled'},
  352. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
  353. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'after', 'type': 'unstyled'}
  354. ],
  355. 'entityMap': {
  356. '0': {
  357. 'data': {
  358. 'thumbnail': 'http://test/thumbnail.url',
  359. 'embedType': 'video',
  360. 'providerName': 'YouTube',
  361. 'title': 'what are birds',
  362. 'authorName': 'look around you',
  363. 'url': 'https://www.youtube.com/watch?v=Kh0Y2hVe_bw'
  364. },
  365. 'mutability': 'IMMUTABLE', 'type': 'EMBED'
  366. }
  367. }
  368. })
  369. def test_hr(self):
  370. converter = ContentstateConverter(features=['hr'])
  371. result = json.loads(converter.from_database_format(
  372. '''
  373. <p>before</p>
  374. <hr />
  375. <p>after</p>
  376. '''
  377. ))
  378. self.assertContentStateEqual(result, {
  379. 'blocks': [
  380. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'before', 'type': 'unstyled'},
  381. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
  382. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'after', 'type': 'unstyled'}
  383. ],
  384. 'entityMap': {
  385. '0': {
  386. 'data': {},
  387. 'mutability': 'IMMUTABLE', 'type': 'HORIZONTAL_RULE'
  388. }
  389. }
  390. })
  391. def test_block_element_in_paragraph(self):
  392. converter = ContentstateConverter(features=['hr'])
  393. result = json.loads(converter.from_database_format(
  394. '''
  395. <p>before<hr />after</p>
  396. '''
  397. ))
  398. self.assertContentStateEqual(result, {
  399. 'blocks': [
  400. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'before', 'type': 'unstyled'},
  401. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
  402. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': 'after', 'type': 'unstyled'}
  403. ],
  404. 'entityMap': {
  405. '0': {
  406. 'data': {},
  407. 'mutability': 'IMMUTABLE', 'type': 'HORIZONTAL_RULE'
  408. }
  409. }
  410. })
  411. def test_block_element_in_empty_paragraph(self):
  412. converter = ContentstateConverter(features=['hr'])
  413. result = json.loads(converter.from_database_format(
  414. '''
  415. <p><hr /></p>
  416. '''
  417. ))
  418. # ignoring the paragraph completely would probably be better,
  419. # but we'll settle for an empty preceding paragraph and not crashing as the next best thing...
  420. self.assertContentStateEqual(result, {
  421. 'blocks': [
  422. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [], 'depth': 0, 'text': '', 'type': 'unstyled'},
  423. {'key': '00000', 'inlineStyleRanges': [], 'entityRanges': [{'key': 0, 'offset': 0, 'length': 1}], 'depth': 0, 'text': ' ', 'type': 'atomic'},
  424. ],
  425. 'entityMap': {
  426. '0': {
  427. 'data': {},
  428. 'mutability': 'IMMUTABLE', 'type': 'HORIZONTAL_RULE'
  429. }
  430. }
  431. })