diff --git a/package-lock.json b/package-lock.json index b111dc8..e6c8811 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7315,9 +7315,9 @@ "dev": true }, "htmlparser2": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.1.0.tgz", - "integrity": "sha512-C5W4WAg8imoK0gklCyLSaRR4aL8oyBmaMsYDXtQsIEIpTTJq9bv4ewHQpxaTaGK2kqwyZnsu34tqGlRG23LW/Q==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.1.1.tgz", + "integrity": "sha512-hZb0lfG0hbhR/hB879zbBr8Opv0Be9Zp+JYHgqTw5epF++aotu/zmMTPLy/60iJyR1MaD/3pYRp7xYteXsZMEA==", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", @@ -7341,9 +7341,9 @@ "integrity": "sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A==" }, "domhandler": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.0.tgz", - "integrity": "sha512-zk7sgt970kzPks2Bf+dwT/PLzghLnsivb9CcxkvR8Mzr66Olr0Ofd8neSbglHJHaHa2MadfoSdNlKYAaafmWfA==", + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.2.tgz", + "integrity": "sha512-PzE9aBMsdZO8TK4BnuJwH0QT41wgMbRzuZrHUcpYncEjmQazq8QEaBWgLG7ZyC/DAZKEgglpIA6j4Qn/HmxS3w==", "requires": { "domelementtype": "^2.2.0" } diff --git a/package.json b/package.json index 10769e4..20b8423 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "^7.1.0" + "htmlparser2": "^7.1.1" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", diff --git a/src/index.ts b/src/index.ts index a922c78..1f7e689 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,16 +44,12 @@ export const parser = (html: string, options: Options = {}): Node[] => { const locationTracker = new LocationTracker(html); const bufArray: Node[] = []; const results: Node[] = []; - let lastIndices: [number, number]; + let lastOpenTagEndIndex = 0; - function bufferArrayLast(): Node | undefined { + function bufferArrayLast(): Node { return bufArray[bufArray.length - 1]; } - function resultsLast(): Node | undefined { - return results[results.length - 1]; - } - function isDirective(directive: Directive, tag: string): boolean { if (directive.name instanceof RegExp) { const regex = new RegExp(directive.name.source, 'i'); @@ -130,21 +126,11 @@ export const parser = (html: string, options: Options = {}): Node[] => { const buf: NodeTag = { tag }; if (options.sourceLocations) { - if (lastIndices?.[0] === parser.startIndex && lastIndices?.[1] === parser.endIndex) { - // The last closing tag was inferred, so we need to update its end location - const last = bufferArrayLast() || resultsLast(); - - if (typeof last === 'object' && Array.isArray(last.content) && last.location) { - last.location.end = locationTracker.getPosition(parser.startIndex - 1) - } - } - - const start = locationTracker.getPosition(parser.startIndex); - buf.location = { - start, - end: start + start: locationTracker.getPosition(parser.startIndex), + end: locationTracker.getPosition(parser.endIndex), }; + lastOpenTagEndIndex = parser.endIndex; } if (Object.keys(attrs).length > 0) { @@ -154,12 +140,15 @@ export const parser = (html: string, options: Options = {}): Node[] => { bufArray.push(buf); } - function onclosetag() { + function onclosetag(name: string, isImplied: boolean) { const buf: Node | undefined = bufArray.pop(); - if (buf && typeof buf === 'object' && buf.location && buf.location.end === buf.location.start && parser.endIndex !== null) { - lastIndices = [parser.startIndex, parser.endIndex]; - buf.location.end = locationTracker.getPosition(parser.endIndex); + if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) { + if (!isImplied) { + buf.location.end = locationTracker.getPosition(parser.endIndex); + } else if (lastOpenTagEndIndex < parser.startIndex) { + buf.location.end = locationTracker.getPosition(parser.startIndex - 1); + } } if (buf) { @@ -183,7 +172,7 @@ export const parser = (html: string, options: Options = {}): Node[] => { } function ontext(text: string) { - const last = bufferArrayLast(); + const last: Node = bufferArrayLast(); if (last === undefined) { results.push(text); diff --git a/src/location-tracker.ts b/src/location-tracker.ts index bbcc81c..033959c 100644 --- a/src/location-tracker.ts +++ b/src/location-tracker.ts @@ -25,12 +25,7 @@ export class LocationTracker { getPosition(index: number): Position { if (index < this.lastIndex) { - this.lastPosition = { - line: 1, - column: 1 - }; - - this.lastIndex = 0; + throw new Error('Source indices must be monotonic'); } while (this.lastIndex < index) {