Repository: python273/telegraph

Issue:
IndexErrortelegraph.utils in handle_endtag error pop from empty list
os:linux
version: "==1.3.2"
https://sentry.io/share/issue/f0d73c02680a4f0ea637437b236e9ec9/

Discussion:

Patch:
diff --git a/telegraph/utils.py b/telegraph/utils.py
index c4b9f8b..41b6df9 100644
--- a/telegraph/utils.py
+++ b/telegraph/utils.py
@@ -83,6 +83,11 @@ class HtmlToNodesParser(HTMLParser):
         if tag in VOID_ELEMENTS:
             return
 
+        if not len(self.parent_nodes):
+            raise InvalidHTML('"{}" missing start tag'.format(
+                tag
+            ))
+
         self.current_nodes = self.parent_nodes.pop()
 
         last_node = self.current_nodes[-1]


Test patch:
diff --git a/tests/test_html_converter.py b/tests/test_html_converter.py
index 42af23a..86fecb1 100644
--- a/tests/test_html_converter.py
+++ b/tests/test_html_converter.py
@@ -19,7 +19,7 @@ NODES_TEST_LIST = [
         'attrs': {'href': 'https://telegra.ph/'},
         'children': ['Test link</a>']
         }]
-    },
+     },
     {'tag': 'figure', 'children': [
         {'tag': 'img', 'attrs': {'src': '/file/6c2ecfdfd6881d37913fa.png'}},
         {'tag': 'figcaption'}
@@ -45,6 +45,8 @@ HTML_MULTI_LINES_NODES_LIST = [
     ]},
 ]
 
+HTML_NO_STARTTAG = "</a><h1></h1>"
+
 
 class TestHTMLConverter(TestCase):
     def test_html_to_nodes(self):
@@ -130,3 +132,7 @@ class TestHTMLConverter(TestCase):
         ]
 
         self.assertEqual(clear_whitespace_nodes(nodes)[0], expected)
+
+    def test_no_starttag_node(self):
+        with self.assertRaises(InvalidHTML):
+             html_to_nodes(HTML_NO_STARTTAG)