diff --git a/zerver/lib/bugdown/__init__.py b/zerver/lib/bugdown/__init__.py index 486f3a0a3d..0605296dc7 100644 --- a/zerver/lib/bugdown/__init__.py +++ b/zerver/lib/bugdown/__init__.py @@ -22,7 +22,7 @@ from six.moves import urllib import xml.etree.cElementTree as etree from xml.etree.cElementTree import Element, SubElement -from collections import defaultdict +from collections import defaultdict, deque import requests @@ -113,13 +113,13 @@ def list_of_tlds(): def walk_tree(root, processor, stop_after_first=False): # type: (Element, Callable[[Element], Optional[_T]], bool) -> List[_T] results = [] - stack = [root] + queue = deque([root]) - while stack: - currElement = stack.pop() + while queue: + currElement = queue.popleft() for child in currElement.getchildren(): if child.getchildren(): - stack.append(child) + queue.append(child) result = processor(child) if result is not None: diff --git a/zerver/tests/test_bugdown.py b/zerver/tests/test_bugdown.py index 2bdac9389d..657290f08c 100644 --- a/zerver/tests/test_bugdown.py +++ b/zerver/tests/test_bugdown.py @@ -249,6 +249,24 @@ class BugdownTest(TestCase): converted = render_markdown(msg, content) self.assertEqual(converted, without_preview) + @override_settings(INLINE_IMAGE_PREVIEW=True) + def test_inline_image_preview_order(self): + # type: () -> None + content = 'http://imaging.nikon.com/lineup/dslr/df/img/sample/img_01.jpg\nhttp://imaging.nikon.com/lineup/dslr/df/img/sample/img_02.jpg\nhttp://imaging.nikon.com/lineup/dslr/df/img/sample/img_03.jpg' + expected = '

http://imaging.nikon.com/lineup/dslr/df/img/sample/img_01.jpg
\nhttp://imaging.nikon.com/lineup/dslr/df/img/sample/img_02.jpg
\nhttp://imaging.nikon.com/lineup/dslr/df/img/sample/img_03.jpg

\n
' + + sender_user_profile = get_user_profile_by_email("othello@zulip.com") + msg = Message(sender=sender_user_profile, sending_client=get_client("test")) + converted = render_markdown(msg, content) + self.assertEqual(converted, expected) + + content = 'Test 1\n[21136101110_1dde1c1a7e_o.jpg](/user_uploads/1/6d/F1PX6u16JA2P-nK45PyxHIYZ/21136101110_1dde1c1a7e_o.jpg) \n\nNext Image\n[IMG_20161116_023910.jpg](/user_uploads/1/69/sh7L06e7uH7NaX6d5WFfVYQp/IMG_20161116_023910.jpg) \n\nAnother Screenshot\n[Screenshot-from-2016-06-01-16-22-42.png](/user_uploads/1/70/_aZmIEWaN1iUaxwkDjkO7bpj/Screenshot-from-2016-06-01-16-22-42.png)' + expected = '

Test 1
\n21136101110_1dde1c1a7e_o.jpg

\n

Next Image
\nIMG_20161116_023910.jpg

\n

Another Screenshot
\nScreenshot-from-2016-06-01-16-22-42.png

\n
' + + msg = Message(sender=sender_user_profile, sending_client=get_client("test")) + converted = render_markdown(msg, content) + self.assertEqual(converted, expected) + @override_settings(INLINE_IMAGE_PREVIEW=False) def test_image_preview_enabled_for_realm(self): # type: () -> None