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
Test 1
\n21136101110_1dde1c1a7e_o.jpg
Next Image
\nIMG_20161116_023910.jpg
Another Screenshot
\nScreenshot-from-2016-06-01-16-22-42.png