@@ -44,16 +44,12 @@ export const parser = (html: string, options: Options = {}): Node[] => {
44
44
const locationTracker = new LocationTracker ( html ) ;
45
45
const bufArray : Node [ ] = [ ] ;
46
46
const results : Node [ ] = [ ] ;
47
- let lastIndices : [ number , number ] ;
47
+ let lastOpenTagEndIndex = 0 ;
48
48
49
- function bufferArrayLast ( ) : Node | undefined {
49
+ function bufferArrayLast ( ) : Node {
50
50
return bufArray [ bufArray . length - 1 ] ;
51
51
}
52
52
53
- function resultsLast ( ) : Node | undefined {
54
- return results [ results . length - 1 ] ;
55
- }
56
-
57
53
function isDirective ( directive : Directive , tag : string ) : boolean {
58
54
if ( directive . name instanceof RegExp ) {
59
55
const regex = new RegExp ( directive . name . source , 'i' ) ;
@@ -130,21 +126,11 @@ export const parser = (html: string, options: Options = {}): Node[] => {
130
126
const buf : NodeTag = { tag } ;
131
127
132
128
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
-
144
129
buf . location = {
145
- start,
146
- end : start
130
+ start : locationTracker . getPosition ( parser . startIndex ) ,
131
+ end : locationTracker . getPosition ( parser . endIndex ) ,
147
132
} ;
133
+ lastOpenTagEndIndex = parser . endIndex ;
148
134
}
149
135
150
136
if ( Object . keys ( attrs ) . length > 0 ) {
@@ -154,12 +140,15 @@ export const parser = (html: string, options: Options = {}): Node[] => {
154
140
bufArray . push ( buf ) ;
155
141
}
156
142
157
- function onclosetag ( ) {
143
+ function onclosetag ( name : string , isImplied : boolean ) {
158
144
const buf : Node | undefined = bufArray . pop ( ) ;
159
145
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
+ }
163
152
}
164
153
165
154
if ( buf ) {
@@ -183,7 +172,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
183
172
}
184
173
185
174
function ontext ( text : string ) {
186
- const last = bufferArrayLast ( ) ;
175
+ const last : Node = bufferArrayLast ( ) ;
187
176
188
177
if ( last === undefined ) {
189
178
results . push ( text ) ;
0 commit comments