@@ -45,7 +45,7 @@ def __init__(self, *args, **kwargs):
4545 self .root = Element ("root" , attrs = {}, location = (- 1 , - 1 ))
4646 self .stack = [self .root ]
4747
48- def handle_starttag (self , tag : str , attrs : list [tuple [str , str | None ]]):
48+ def _handle_tag (self , tag : str , attrs : list [tuple [str , str | None ]]) -> Element :
4949 # The tag parameter is lower-cased by the HTMLParser.
5050 # In order to figure out whether the tag indicates
5151 # an imported class, we need the original casing for
@@ -68,6 +68,11 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]):
6868 parent = self .stack [- 1 ]
6969 parent .children .append (node )
7070 node .parent = ref (parent )
71+
72+ return node
73+
74+ def handle_starttag (self , tag : str , attrs : list [tuple [str , str | None ]]):
75+ node = self ._handle_tag (tag , attrs )
7176 # Make the new node the last on the stack
7277 self .stack .append (node )
7378
@@ -81,6 +86,20 @@ def handle_endtag(self, tag: str):
8186 if node .tag .lower () == tag :
8287 break
8388
89+ def handle_startendtag (self , tag : str , attrs ):
90+ """Handle self-closing tags"""
91+ node = self ._handle_tag (tag , attrs )
92+ # Get the full text and split it
93+ text = self .get_starttag_text ()
94+ lines = text .splitlines ()
95+ # Find the location of where the tag is actually closed
96+ column = lines [- 1 ].index ("/>" )
97+ # Figure out how many lines the tag spans
98+ line_span_count = len (lines ) - 1
99+ line , _ = self .getpos ()
100+ # Set the end to be exactly where the '/>' is located
101+ node .end = (line + line_span_count , column )
102+
84103 def handle_data (self , data : str ):
85104 if data .strip ():
86105 # Add item as child to the last on the stack
0 commit comments