UNPKG

3.92 kBTypeScriptView Raw
1/// <reference types="node" />
2
3export const EVENTS: string[];
4
5export interface SAXOptions {
6 trim?: boolean | undefined;
7 normalize?: boolean | undefined;
8 lowercase?: boolean | undefined;
9 xmlns?: boolean | undefined;
10 noscript?: boolean | undefined;
11 position?: boolean | undefined;
12}
13
14export interface QualifiedName {
15 name: string;
16 prefix: string;
17 local: string;
18 uri: string;
19}
20
21export interface QualifiedAttribute extends QualifiedName {
22 value: string;
23}
24
25export interface BaseTag {
26 name: string;
27 isSelfClosing: boolean;
28}
29
30// Interface used when the xmlns option is set
31export interface QualifiedTag extends QualifiedName, BaseTag {
32 ns: { [key: string]: string };
33 attributes: { [key: string]: QualifiedAttribute };
34}
35
36export interface Tag extends BaseTag {
37 attributes: { [key: string]: string };
38}
39
40export function parser(strict?: boolean, opt?: SAXOptions): SAXParser;
41export class SAXParser {
42 constructor(strict?: boolean, opt?: SAXOptions);
43
44 // Methods
45 end(): void;
46 write(s: string): SAXParser;
47 resume(): SAXParser;
48 close(): SAXParser;
49 flush(): void;
50
51 // Members
52 line: number;
53 column: number;
54 error: Error;
55 position: number;
56 startTagPosition: number;
57 closed: boolean;
58 strict: boolean;
59 opt: SAXOptions;
60 tag: Tag;
61 ENTITIES: { [key: string]: string };
62
63 // Events
64 onerror(e: Error): void;
65 ontext(t: string): void;
66 ondoctype(doctype: string): void;
67 onprocessinginstruction(node: { name: string; body: string }): void;
68 onsgmldeclaration(sgmlDecl: string): void;
69 onopentag(tag: Tag | QualifiedTag): void;
70 onopentagstart(tag: Tag | QualifiedTag): void;
71 onclosetag(tagName: string): void;
72 onattribute(attr: { name: string; value: string }): void;
73 oncomment(comment: string): void;
74 onopencdata(): void;
75 oncdata(cdata: string): void;
76 onclosecdata(): void;
77 onopennamespace(ns: { prefix: string; uri: string }): void;
78 onclosenamespace(ns: { prefix: string; uri: string }): void;
79 onend(): void;
80 onready(): void;
81 onscript(script: string): void;
82}
83
84import stream = require("stream");
85export function createStream(strict?: boolean, opt?: SAXOptions): SAXStream;
86export class SAXStream extends stream.Duplex {
87 constructor(strict?: boolean, opt?: SAXOptions);
88 _parser: SAXParser;
89 on(event: "text", listener: (this: this, text: string) => void): this;
90 on(event: "doctype", listener: (this: this, doctype: string) => void): this;
91 on(event: "processinginstruction", listener: (this: this, node: { name: string; body: string }) => void): this;
92 on(event: "sgmldeclaration", listener: (this: this, sgmlDecl: string) => void): this;
93 on(event: "opentag" | "opentagstart", listener: (this: this, tag: Tag | QualifiedTag) => void): this;
94 on(event: "closetag", listener: (this: this, tagName: string) => void): this;
95 on(event: "attribute", listener: (this: this, attr: { name: string; value: string }) => void): this;
96 on(event: "comment", listener: (this: this, comment: string) => void): this;
97 on(
98 event: "opencdata" | "closecdata" | "end" | "ready" | "close" | "readable" | "drain" | "finish",
99 listener: (this: this) => void,
100 ): this;
101 on(event: "cdata", listener: (this: this, cdata: string) => void): this;
102 on(
103 event: "opennamespace" | "closenamespace",
104 listener: (this: this, ns: { prefix: string; uri: string }) => void,
105 ): this;
106 on(event: "script", listener: (this: this, script: string) => void): this;
107 on(event: "data", listener: (this: this, chunk: any) => void): this;
108 on(event: "error", listener: (this: this, err: Error) => void): this;
109 on(event: "pipe" | "unpipe", listener: (this: this, src: stream.Readable) => void): this;
110 on(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;
111}