Skip to content

Commit 1790a35

Browse files
authored
Merge pull request #80 from thewilkybarkid/source-indices
sourceLocations is broken when tags are implied
2 parents b193583 + 5166475 commit 1790a35

File tree

4 files changed

+105
-12
lines changed

4 files changed

+105
-12
lines changed

package-lock.json

Lines changed: 38 additions & 5 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": "^6.0.0"
42+
"htmlparser2": "^7.1.1"
4343
},
4444
"devDependencies": {
4545
"@antfu/eslint-config-ts": "^0.4.3",

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
4444
const locationTracker = new LocationTracker(html);
4545
const bufArray: Node[] = [];
4646
const results: Node[] = [];
47+
let lastOpenTagEndIndex = 0;
4748

4849
function bufferArrayLast(): Node {
4950
return bufArray[bufArray.length - 1];
@@ -122,14 +123,14 @@ export const parser = (html: string, options: Options = {}): Node[] => {
122123
}
123124

124125
function onopentag(tag: string, attrs: Attributes) {
125-
const start = locationTracker.getPosition(parser.startIndex);
126126
const buf: NodeTag = { tag };
127127

128128
if (options.sourceLocations) {
129129
buf.location = {
130-
start,
131-
end: start
130+
start: locationTracker.getPosition(parser.startIndex),
131+
end: locationTracker.getPosition(parser.endIndex)
132132
};
133+
lastOpenTagEndIndex = parser.endIndex;
133134
}
134135

135136
if (Object.keys(attrs).length > 0) {
@@ -139,11 +140,15 @@ export const parser = (html: string, options: Options = {}): Node[] => {
139140
bufArray.push(buf);
140141
}
141142

142-
function onclosetag() {
143+
function onclosetag(name: string, isImplied: boolean) {
143144
const buf: Node | undefined = bufArray.pop();
144145

145146
if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) {
146-
buf.location.end = locationTracker.getPosition(parser.endIndex);
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+
}
147152
}
148153

149154
if (buf) {

test/test-core.spec.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ test('should be not converting html entity name', t => {
243243
});
244244

245245
test('should parse with source locations', t => {
246-
const html = '<h1>Test</h1>\n<p><b>Foo</b></p>';
246+
const html = '<h1>Test</h1>\n<p><b>Foo</b><hr></p><p>Bar\n<hr>';
247247
const tree = parser(html, { sourceLocations: true });
248248
const expected = [
249249
{
@@ -284,11 +284,66 @@ test('should parse with source locations', t => {
284284
line: 2,
285285
column: 1
286286
},
287+
end: {
288+
line: 2,
289+
column: 13
290+
}
291+
}
292+
},
293+
{
294+
tag: 'hr',
295+
location: {
296+
start: {
297+
line: 2,
298+
column: 14
299+
},
287300
end: {
288301
line: 2,
289302
column: 17
290303
}
291304
}
305+
},
306+
{
307+
tag: 'p',
308+
location: {
309+
start: {
310+
line: 2,
311+
column: 18
312+
},
313+
end: {
314+
line: 2,
315+
column: 21
316+
}
317+
}
318+
},
319+
{
320+
tag: 'p',
321+
content: [
322+
'Bar\n'
323+
],
324+
location: {
325+
start: {
326+
line: 2,
327+
column: 22
328+
},
329+
end: {
330+
line: 2,
331+
column: 28
332+
}
333+
}
334+
},
335+
{
336+
tag: 'hr',
337+
location: {
338+
start: {
339+
line: 3,
340+
column: 1
341+
},
342+
end: {
343+
line: 3,
344+
column: 4
345+
}
346+
}
292347
}
293348
];
294349
t.deepEqual(tree, expected);

0 commit comments

Comments
 (0)