Skip to content

Commit

Permalink
refactor: parse now export not default
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed Jul 28, 2021
1 parent 9ed9bd0 commit f8ae42d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
71 changes: 34 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Parser, ParserOptions } from 'htmlparser2';
import { LocationTracker } from './location-tracker';
import { LocationTracker, SourceLocation } from './location-tracker';

export type Directive = {
name: string | RegExp;
Expand All @@ -14,8 +14,7 @@ export type Options = {

export type Tag = string | boolean;
export type Attributes = Record<string, string | number | boolean>;
// export type Content = NodeText | Node[] | Node[][];
export type Content = NodeText | (Node | Node[])[];
export type Content = NodeText | Array<Node | Node[]>;

export type NodeText = string | number;
export type NodeTag = {
Expand All @@ -27,16 +26,6 @@ export type NodeTag = {

export type Node = NodeText | NodeTag;

export type SourceLocation = {
start: Position;
end: Position;
};

export type Position = {
line: number;
column: number;
};

const defaultOptions: ParserOptions = {
lowerCaseTags: false,
lowerCaseAttributeNames: false,
Expand All @@ -51,7 +40,7 @@ const defaultDirectives: Directive[] = [
}
];

const parser = (html: string, options: Options = {}): Node[] => {
export const parser = (html: string, options: Options = {}): Node[] => {
const locationTracker = new LocationTracker(html);
const bufArray: Node[] = [];
const results: Node[] = [];
Expand All @@ -60,25 +49,6 @@ const parser = (html: string, options: Options = {}): Node[] => {
return bufArray[bufArray.length - 1];
}

function resolveContent(text: NodeText): void {
const last = bufferArrayLast();

if (last === undefined) {
results.push(text);
return;
}

if (typeof last === 'object') {
if (last.content === undefined) {
last.content = [];
}

if (Array.isArray(last.content)) {
last.content.push(text);
}
}
}

function isDirective(directive: Directive, tag: string): boolean {
if (directive.name instanceof RegExp) {
const regex = new RegExp(directive.name.source, 'i');
Expand Down Expand Up @@ -107,20 +77,48 @@ const parser = (html: string, options: Options = {}): Node[] => {

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

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

if (isDirective(directive, name.toLowerCase())) {
resolveContent(directiveText);
if (last === undefined) {
results.push(directiveText);
return;
}

if (typeof last === 'object') {
if (last.content === undefined) {
last.content = [];
}

if (Array.isArray(last.content)) {
last.content.push(directiveText);
}
}
}
}
}

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

resolveContent(comment);
if (last === undefined) {
results.push(comment);
return;
}

if (typeof last === 'object') {
if (last.content === undefined) {
last.content = [];
}

if (Array.isArray(last.content)) {
last.content.push(comment);
}
}
}

function onopentag(tag: string, attrs: Attributes) {
Expand Down Expand Up @@ -188,6 +186,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
if (last.content === undefined) {
last.content = [];
}

if (Array.isArray(last.content)) {
last.content.push(text);
}
Expand All @@ -207,5 +206,3 @@ const parser = (html: string, options: Options = {}): Node[] => {

return results;
};

export default parser;
10 changes: 9 additions & 1 deletion src/location-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Position } from '../types/index.d';
export type SourceLocation = {
start: Position;
end: Position;
};

export type Position = {
line: number;
column: number;
};

export class LocationTracker {
private readonly source: string;
Expand Down
2 changes: 1 addition & 1 deletion test/test-core.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import parser from '../src';
import { parser } from '../src';

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

0 comments on commit f8ae42d

Please sign in to comment.