Skip to content

sourceLocations is broken when tags are implied #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"posthtmltree"
],
"dependencies": {
"htmlparser2": "^6.0.0"
"htmlparser2": "^7.1.1"
},
"devDependencies": {
"@antfu/eslint-config-ts": "^0.4.3",
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const parser = (html: string, options: Options = {}): Node[] => {
const locationTracker = new LocationTracker(html);
const bufArray: Node[] = [];
const results: Node[] = [];
let lastOpenTagEndIndex = 0;

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

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

if (options.sourceLocations) {
buf.location = {
start,
end: start
start: locationTracker.getPosition(parser.startIndex),
end: locationTracker.getPosition(parser.endIndex)
};
lastOpenTagEndIndex = parser.endIndex;
}

if (Object.keys(attrs).length > 0) {
Expand All @@ -139,11 +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 && parser.endIndex !== null) {
buf.location.end = locationTracker.getPosition(parser.endIndex);
if (!isImplied) {
buf.location.end = locationTracker.getPosition(parser.endIndex);
} else if (lastOpenTagEndIndex < parser.startIndex) {
buf.location.end = locationTracker.getPosition(parser.startIndex - 1);
}
}

if (buf) {
Expand Down
57 changes: 56 additions & 1 deletion test/test-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ test('should be not converting html entity name', t => {
});

test('should parse with source locations', t => {
const html = '<h1>Test</h1>\n<p><b>Foo</b></p>';
const html = '<h1>Test</h1>\n<p><b>Foo</b><hr></p><p>Bar\n<hr>';
const tree = parser(html, { sourceLocations: true });
const expected = [
{
Expand Down Expand Up @@ -284,11 +284,66 @@ test('should parse with source locations', t => {
line: 2,
column: 1
},
end: {
line: 2,
column: 13
}
}
},
{
tag: 'hr',
location: {
start: {
line: 2,
column: 14
},
end: {
line: 2,
column: 17
}
}
},
{
tag: 'p',
location: {
start: {
line: 2,
column: 18
},
end: {
line: 2,
column: 21
}
}
},
{
tag: 'p',
content: [
'Bar\n'
],
location: {
start: {
line: 2,
column: 22
},
end: {
line: 2,
column: 28
}
}
},
{
tag: 'hr',
location: {
start: {
line: 3,
column: 1
},
end: {
line: 3,
column: 4
}
}
}
];
t.deepEqual(tree, expected);
Expand Down