3535
3636# Included for versions which do not have current comment fix
3737commentclose = re .compile (r'--!?>' )
38- commentabruptclose = re .compile (r'-?>' )
3938
4039# Import a copy of the html.parser lib as `htmlparser` so we can monkeypatch it.
4140# Users can still do `from html import parser` and get the default behavior.
4847# throwing it away. When we see it, we will process it as data.
4948htmlparser .starttagopen = re .compile ('<[a-zA-Z]|</>' )
5049
50+ htmlparser .endtagopen = re .compile ('</[a-zA-Z]|</' )
51+
5152# Monkeypatch `HTMLParser` to only accept `?>` to close Processing Instructions.
5253htmlparser .piclose = re .compile (r'\?>' )
5354# Monkeypatch `HTMLParser` to only recognize entity references with a closing semicolon.
@@ -110,9 +111,6 @@ def __init__(self, md: Markdown, *args, **kwargs):
110111
111112 self .lineno_start_cache = [0 ]
112113
113- self .override_comment_update = False
114- self .override_comment_start = 0
115-
116114 # This calls self.reset
117115 super ().__init__ (* args , ** kwargs )
118116 self .md = md
@@ -125,8 +123,6 @@ def reset(self):
125123 self ._cache : list [str ] = []
126124 self .cleandoc : list [str ] = []
127125 self .lineno_start_cache = [0 ]
128- self .override_comment_start = 0
129- self .override_comment_update = False
130126
131127 super ().reset ()
132128
@@ -207,6 +203,14 @@ def handle_starttag(self, tag: str, attrs: Sequence[tuple[str, str]]):
207203 # This is presumably a standalone tag in a code span (see #1036).
208204 self .clear_cdata_mode ()
209205
206+ def parse_endtag (self , i ):
207+ start = self .rawdata [i :i + 3 ]
208+ c = ord (start [- 1 ])
209+ if len (start ) < 3 or not (65 <= c <= 90 or 97 <= c <= 122 ):
210+ self .handle_data (self .rawdata [i :i + 2 ])
211+ return i + 2
212+ return super ().parse_endtag (i )
213+
210214 def handle_endtag (self , tag : str ):
211215 text = self .get_endtag_text (tag )
212216
@@ -276,22 +280,8 @@ def handle_entityref(self, name: str):
276280
277281 def handle_comment (self , data : str ):
278282 # Check if the comment is unclosed, if so, we need to override position
279- j = self .rawdata .find (data )
280- i = j - 2
281- if self .rawdata [i :j ] == '</' :
282- self .handle_data ('<' )
283- self .override_comment_start = i
284- self .override_comment_update = True
285- return
286283 self .handle_empty_tag ('<!--{}-->' .format (data ), is_block = True )
287284
288- def updatepos (self , i : int , j : int ) -> int :
289- if self .override_comment_update :
290- self .override_comment_update = False
291- i = self .override_comment_start
292- j = self .override_comment_start + 1
293- return super ().updatepos (i , j )
294-
295285 def handle_decl (self , data : str ):
296286 self .handle_empty_tag ('<!{}>' .format (data ), is_block = True )
297287
0 commit comments