Skip to content

Commit 477faa9

Browse files
committed
Better organization
1 parent fb36669 commit 477faa9

File tree

7 files changed

+40
-54
lines changed

7 files changed

+40
-54
lines changed

src/ast.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import {getTagId} from './htmltagenum.js';
17-
18-
/**
19-
* @file Provides interfaces and helper functions for handling a JSON representation of HTML.
20-
*/
21-
22-
export interface TreeProto {
23-
tree: [DocumentNodeProto];
24-
quirks_mode: undefined | boolean;
25-
root: number;
26-
}
27-
28-
export interface TextNodeProto {
29-
value: string;
30-
num_terms: number;
31-
}
32-
export interface ElementNodeProto {
33-
tagid: number;
34-
value: string;
35-
attributes: Array<AttributeProto>;
36-
children: Array<NodeProto>;
37-
}
38-
39-
export interface DocumentNodeProto {
40-
tagid: 92; // See htmltagenum.ts
41-
children: Array<NodeProto>;
42-
}
43-
44-
export type NodeProto = TextNodeProto | ElementNodeProto;
45-
export interface AttributeProto {
46-
name: string;
47-
value?: string;
48-
}
17+
import {NodeProto, ElementNodeProto, TreeProto} from './protos.js';
4918

5019
export function isElementNode(node: NodeProto): node is ElementNodeProto {
5120
return (node as any).tagid !== undefined;

src/dom.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {isElementNode, NodeProto, TreeProto} from './ast.js';
16+
import {isElementNode} from './ast.js';
17+
import {TreeProto, NodeProto} from './protos.js';
1718

1819
// TypeScript doesn't understand importing a file with a .mjs extension, but it is perfectly valid once transpiled
1920
// See: https://github.com/microsoft/TypeScript/issues/27957

src/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,9 @@
1515
*/
1616
import * as ast from './ast.js';
1717
import * as dom from './dom.js';
18+
import {TreeProto} from './protos.js';
1819

19-
// Reexport all types from ast
20-
export type {
21-
TreeProto,
22-
DocumentNodeProto,
23-
NodeProto,
24-
ElementNodeProto,
25-
TextNodeProto,
26-
AttributeProto,
27-
} from './ast.js';
28-
20+
export * as protos from './protos.js';
2921
export interface InstructionMap {
3022
[key: string]: (element: Element) => void;
3123
}
@@ -35,10 +27,10 @@ function defaultHandleError(tagName, e: Error) {
3527
}
3628

3729
export function renderAst(
38-
tree: ast.TreeProto,
30+
tree: TreeProto,
3931
instructions: InstructionMap,
4032
{handleError = defaultHandleError} = {}
41-
): ast.TreeProto {
33+
): TreeProto {
4234
const doc = dom.fromTreeProto(tree);
4335

4436
// TODO: Optimization opportunity by writing a custom walk instead of N querySelectorAll.

src/protos.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export interface TreeProto {
2+
tree: [DocumentNodeProto];
3+
quirks_mode: undefined | boolean;
4+
root: number;
5+
}
6+
7+
export interface DocumentNodeProto {
8+
tagid: 92; // See htmltagenum.ts
9+
children: Array<NodeProto>;
10+
}
11+
12+
export type NodeProto = TextNodeProto | ElementNodeProto;
13+
14+
export interface TextNodeProto {
15+
value: string;
16+
num_terms: number;
17+
}
18+
19+
export interface ElementNodeProto {
20+
tagid: number;
21+
value: string;
22+
attributes: Array<AttributeProto>;
23+
children: Array<NodeProto>;
24+
}
25+
26+
export interface AttributeProto {
27+
name: string;
28+
value?: string;
29+
}

test/html-utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
* limitations under the License.
1515
*/
1616
import * as parse5 from 'parse5';
17-
import {
18-
DocumentNodeProto,
19-
getNumTerms,
20-
isElementNode,
21-
NodeProto,
22-
TreeProto,
23-
} from '../src/ast.js';
17+
import {getNumTerms, isElementNode} from '../src/ast.js';
18+
import {DocumentNodeProto, NodeProto, TreeProto} from '../src/protos.js';
2419
import {getTagId} from '../src/htmltagenum.js';
2520
import {renderAst, InstructionMap} from '../src/index.js';
2621

test/test-dom.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {DocumentNodeProto, NodeProto, TreeProto} from '../src/ast.js';
16+
import {DocumentNodeProto, NodeProto, TreeProto} from '../src/protos.js';
1717

1818
import test from 'ava';
1919
import {fromTreeProto} from '../src/dom.js';

test/test-index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import test from 'ava';
17-
import {DocumentNodeProto, NodeProto, TreeProto} from '../src/ast.js';
17+
import {DocumentNodeProto, NodeProto, TreeProto} from '../src/protos.js';
1818
import {getTagId} from '../src/htmltagenum.js';
1919
import {renderAst} from '../src/index.js';
2020

0 commit comments

Comments
 (0)