Skip to content

Commit 4847a08

Browse files
Use isImplied
1 parent 2c53321 commit 4847a08

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"posthtmltree"
4040
],
4141
"dependencies": {
42-
"htmlparser2": "^7.1.0"
42+
"htmlparser2": "^7.1.1"
4343
},
4444
"devDependencies": {
4545
"@antfu/eslint-config-ts": "^0.4.3",

src/index.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/location-tracker.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ export class LocationTracker {
2525

2626
getPosition(index: number): Position {
2727
if (index < this.lastIndex) {
28-
this.lastPosition = {
29-
line: 1,
30-
column: 1
31-
};
32-
33-
this.lastIndex = 0;
28+
throw new Error('Source indices must be monotonic');
3429
}
3530

3631
while (this.lastIndex < index) {

0 commit comments

Comments
 (0)