Skip to content

Commit

Permalink
Use isImplied
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Aug 29, 2021
1 parent 2c53321 commit 4847a08
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
12 changes: 6 additions & 6 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": "^7.1.0"
"htmlparser2": "^7.1.1"
},
"devDependencies": {
"@antfu/eslint-config-ts": "^0.4.3",
Expand Down
37 changes: 13 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions src/location-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 4847a08

Please sign in to comment.