@@ -450,16 +450,15 @@ def handle_starttag(self, tag, attrs):
450450 return
451451
452452 # Render opening tag if kept
453- if not self .skip .intersection (self .context ):
454- if tag in self .keep :
453+ if not self .skip .intersection (self .context ) and tag in self .keep :
455454
456- # Check whether we're inside the section title
457- data = self .section .text
458- if self .section .el in self .context :
459- data = self .section .title
455+ # Check whether we're inside the section title
456+ data = self .section .text
457+ if self .section .el in self .context :
458+ data = self .section .title
460459
461- # Append to section title or text
462- data .append (f"<{ tag } >" )
460+ # Append to section title or text
461+ data .append (f"<{ tag } >" )
463462
464463 # Called at the end of every HTML tag
465464 def handle_endtag (self , tag ):
@@ -488,29 +487,28 @@ def handle_endtag(self, tag):
488487 return
489488
490489 # Render closing tag if kept
491- if not self .skip .intersection (self .context ):
492- if tag in self .keep :
493-
494- # Check whether we're inside the section title
495- data = self .section .text
496- if self .section .el in self .context :
497- data = self .section .title
498-
499- # Search for corresponding opening tag
500- index = data .index (f"<{ tag } >" )
501- for i in range (index + 1 , len (data )):
502- if not data [i ].isspace ():
503- index = len (data )
504- break
505-
506- # Remove element if empty (or only whitespace)
507- if len (data ) > index :
508- while len (data ) > index :
509- data .pop ()
510-
511- # Append to section title or text
512- else :
513- data .append (f"</{ tag } >" )
490+ if not self .skip .intersection (self .context ) and tag in self .keep :
491+
492+ # Check whether we're inside the section title
493+ data = self .section .text
494+ if self .section .el in self .context :
495+ data = self .section .title
496+
497+ # Search for corresponding opening tag
498+ index = data .index (f"<{ tag } >" )
499+ for i in range (index + 1 , len (data )):
500+ if not data [i ].isspace ():
501+ index = len (data )
502+ break
503+
504+ # Remove element if empty (or only whitespace)
505+ if len (data ) > index :
506+ while len (data ) > index :
507+ data .pop ()
508+
509+ # Append to section title or text
510+ else :
511+ data .append (f"</{ tag } >" )
514512
515513 # Called for the text contents of each tag
516514 def handle_data (self , data ):
0 commit comments