Skip to content

Commit f8ae42d

Browse files
committedJul 28, 2021
refactor: parse now export not default
1 parent 9ed9bd0 commit f8ae42d

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed
 

‎src/index.ts

+34-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Parser, ParserOptions } from 'htmlparser2';
2-
import { LocationTracker } from './location-tracker';
2+
import { LocationTracker, SourceLocation } from './location-tracker';
33

44
export type Directive = {
55
name: string | RegExp;
@@ -14,8 +14,7 @@ export type Options = {
1414

1515
export type Tag = string | boolean;
1616
export type Attributes = Record<string, string | number | boolean>;
17-
// export type Content = NodeText | Node[] | Node[][];
18-
export type Content = NodeText | (Node | Node[])[];
17+
export type Content = NodeText | Array<Node | Node[]>;
1918

2019
export type NodeText = string | number;
2120
export type NodeTag = {
@@ -27,16 +26,6 @@ export type NodeTag = {
2726

2827
export type Node = NodeText | NodeTag;
2928

30-
export type SourceLocation = {
31-
start: Position;
32-
end: Position;
33-
};
34-
35-
export type Position = {
36-
line: number;
37-
column: number;
38-
};
39-
4029
const defaultOptions: ParserOptions = {
4130
lowerCaseTags: false,
4231
lowerCaseAttributeNames: false,
@@ -51,7 +40,7 @@ const defaultDirectives: Directive[] = [
5140
}
5241
];
5342

54-
const parser = (html: string, options: Options = {}): Node[] => {
43+
export const parser = (html: string, options: Options = {}): Node[] => {
5544
const locationTracker = new LocationTracker(html);
5645
const bufArray: Node[] = [];
5746
const results: Node[] = [];
@@ -60,25 +49,6 @@ const parser = (html: string, options: Options = {}): Node[] => {
6049
return bufArray[bufArray.length - 1];
6150
}
6251

63-
function resolveContent(text: NodeText): void {
64-
const last = bufferArrayLast();
65-
66-
if (last === undefined) {
67-
results.push(text);
68-
return;
69-
}
70-
71-
if (typeof last === 'object') {
72-
if (last.content === undefined) {
73-
last.content = [];
74-
}
75-
76-
if (Array.isArray(last.content)) {
77-
last.content.push(text);
78-
}
79-
}
80-
}
81-
8252
function isDirective(directive: Directive, tag: string): boolean {
8353
if (directive.name instanceof RegExp) {
8454
const regex = new RegExp(directive.name.source, 'i');
@@ -107,20 +77,48 @@ const parser = (html: string, options: Options = {}): Node[] => {
10777

10878
function onprocessinginstruction(name: string, data: string) {
10979
const directives = defaultDirectives.concat(options.directives ?? []);
80+
const last = bufferArrayLast();
11081

11182
for (const directive of directives) {
11283
const directiveText = directive.start + data + directive.end;
11384

11485
if (isDirective(directive, name.toLowerCase())) {
115-
resolveContent(directiveText);
86+
if (last === undefined) {
87+
results.push(directiveText);
88+
return;
89+
}
90+
91+
if (typeof last === 'object') {
92+
if (last.content === undefined) {
93+
last.content = [];
94+
}
95+
96+
if (Array.isArray(last.content)) {
97+
last.content.push(directiveText);
98+
}
99+
}
116100
}
117101
}
118102
}
119103

120104
function oncomment(data: string) {
105+
const last = bufferArrayLast();
121106
const comment = `<!--${data}-->`;
122107

123-
resolveContent(comment);
108+
if (last === undefined) {
109+
results.push(comment);
110+
return;
111+
}
112+
113+
if (typeof last === 'object') {
114+
if (last.content === undefined) {
115+
last.content = [];
116+
}
117+
118+
if (Array.isArray(last.content)) {
119+
last.content.push(comment);
120+
}
121+
}
124122
}
125123

126124
function onopentag(tag: string, attrs: Attributes) {
@@ -188,6 +186,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
188186
if (last.content === undefined) {
189187
last.content = [];
190188
}
189+
191190
if (Array.isArray(last.content)) {
192191
last.content.push(text);
193192
}
@@ -207,5 +206,3 @@ const parser = (html: string, options: Options = {}): Node[] => {
207206

208207
return results;
209208
};
210-
211-
export default parser;

‎src/location-tracker.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Position } from '../types/index.d';
1+
export type SourceLocation = {
2+
start: Position;
3+
end: Position;
4+
};
5+
6+
export type Position = {
7+
line: number;
8+
column: number;
9+
};
210

311
export class LocationTracker {
412
private readonly source: string;

‎test/test-core.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import parser from '../src';
2+
import { parser } from '../src';
33

44
test('should be parse doctype in uppercase', t => {
55
const tree = parser('<!DOCTYPE html>');

0 commit comments

Comments
 (0)
Please sign in to comment.