UNPKG

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