@@ -44,16 +44,12 @@ export const parser = (html: string, options: Options = {}): Node[] => {
4444 const locationTracker = new LocationTracker ( html ) ;
4545 const bufArray : Node [ ] = [ ] ;
4646 const results : Node [ ] = [ ] ;
47- let lastIndices : [ number , number ] ;
47+ let lastOpenTagEndIndex = 0 ;
4848
49- function bufferArrayLast ( ) : Node | undefined {
49+ function bufferArrayLast ( ) : Node {
5050 return bufArray [ bufArray . length - 1 ] ;
5151 }
5252
53- function resultsLast ( ) : Node | undefined {
54- return results [ results . length - 1 ] ;
55- }
56-
5753 function isDirective ( directive : Directive , tag : string ) : boolean {
5854 if ( directive . name instanceof RegExp ) {
5955 const regex = new RegExp ( directive . name . source , 'i' ) ;
@@ -130,21 +126,11 @@ export const parser = (html: string, options: Options = {}): Node[] => {
130126 const buf : NodeTag = { tag } ;
131127
132128 if ( options . sourceLocations ) {
133- if ( lastIndices ?. [ 0 ] === parser . startIndex && lastIndices ?. [ 1 ] === parser . endIndex ) {
134- // The last closing tag was inferred, so we need to update its end location
135- const last = bufferArrayLast ( ) || resultsLast ( ) ;
136-
137- if ( typeof last === 'object' && Array . isArray ( last . content ) && last . location ) {
138- last . location . end = locationTracker . getPosition ( parser . startIndex - 1 )
139- }
140- }
141-
142- const start = locationTracker . getPosition ( parser . startIndex ) ;
143-
144129 buf . location = {
145- start,
146- end : start
130+ start : locationTracker . getPosition ( parser . startIndex ) ,
131+ end : locationTracker . getPosition ( parser . endIndex ) ,
147132 } ;
133+ lastOpenTagEndIndex = parser . endIndex ;
148134 }
149135
150136 if ( Object . keys ( attrs ) . length > 0 ) {
@@ -154,12 +140,15 @@ export const parser = (html: string, options: Options = {}): Node[] => {
154140 bufArray . push ( buf ) ;
155141 }
156142
157- function onclosetag ( ) {
143+ function onclosetag ( name : string , isImplied : boolean ) {
158144 const buf : Node | undefined = bufArray . pop ( ) ;
159145
160- if ( buf && typeof buf === 'object' && buf . location && buf . location . end === buf . location . start && parser . endIndex !== null ) {
161- lastIndices = [ parser . startIndex , parser . endIndex ] ;
162- buf . location . end = locationTracker . getPosition ( parser . endIndex ) ;
146+ if ( buf && typeof buf === 'object' && buf . location && parser . endIndex !== null ) {
147+ if ( ! isImplied ) {
148+ buf . location . end = locationTracker . getPosition ( parser . endIndex ) ;
149+ } else if ( lastOpenTagEndIndex < parser . startIndex ) {
150+ buf . location . end = locationTracker . getPosition ( parser . startIndex - 1 ) ;
151+ }
163152 }
164153
165154 if ( buf ) {
@@ -183,7 +172,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
183172 }
184173
185174 function ontext ( text : string ) {
186- const last = bufferArrayLast ( ) ;
175+ const last : Node = bufferArrayLast ( ) ;
187176
188177 if ( last === undefined ) {
189178 results . push ( text ) ;
0 commit comments