diff --git a/README.md b/README.md index 7984ba1..3222747 100644 --- a/README.md +++ b/README.md @@ -35,18 +35,24 @@ reader.close(); SAX event listener register definitions are below. ```typescript -on(event: 'start_document', listener: () => void): this; -on(event: 'processing_instruction', listener: (procInst: string) => void): this; -on(event: 'sgml_declaration', listener: (sgmlDecl: string) => void): this; -on(event: 'text', listener: (text: string, element: ElementInfo, cdata: boolean) => void): this; -on(event: 'doctype', listener: (doctype: string) => void): this; -on(event: 'start_prefix_mapping', listener: (ns: string, uri: string) => void): this; -on(event: 'start_element', listener: (element: ElementInfo) => void): this; -on(event: 'comment', listener: (comment: string) => void): this; -on(event: 'end_element', listener: (element: ElementInfo) => void): this; -on(event: 'end_prefix_mapping', listener: (ns: string, uri: string) => void): this; -on(event: 'end_document', listener: () => void): this; -on(event: 'error', listener: (error: XMLParseError) => void): this; +interface SAXEvent { + start_document: () => void; + processing_instruction: (procInst: string) => void; + sgml_declaration: (sgmlDecl: string) => void; + text: (text: string, element: ElementInfo, cdata: boolean) => void; + doctype: (doctype: string) => void; + start_prefix_mapping: (ns: string, uri: string) => void; + start_element: (element: ElementInfo) => void; + comment: (comment: string) => void; + end_element: (element: ElementInfo) => void; + end_prefix_mapping: (ns: string, uri: string) => void; + end_document: () => void; + error: (error: XMLParseError) => void; +} + +class SAXParser { + on(event: K, listener: SAXEvent[K]): this {} +} ``` You can use "SAXParser" on Deno's stream i/o because this is a simple "UnderlyingSink" impl. diff --git a/mod.ts b/mod.ts index 0f2c305..4c61358 100644 --- a/mod.ts +++ b/mod.ts @@ -7,6 +7,7 @@ export { export type { PullResult, + SAXEvent, } from './parser.ts'; export { diff --git a/parser.ts b/parser.ts index 968268c..5d96cc4 100644 --- a/parser.ts +++ b/parser.ts @@ -141,6 +141,24 @@ export abstract class ParserBase implements XMLLocator { } } +/** + * A catalog of events. + */ +export interface SAXEvent { + start_document: () => void; + processing_instruction: (procInst: string) => void; + sgml_declaration: (sgmlDecl: string) => void; + text: (text: string, element: ElementInfo, cdata: boolean) => void; + doctype: (doctype: string) => void; + start_prefix_mapping: (ns: string, uri: string) => void; + start_element: (element: ElementInfo) => void; + comment: (comment: string) => void; + end_element: (element: ElementInfo) => void; + end_prefix_mapping: (ns: string, uri: string) => void; + end_document: () => void; + error: (error: XMLParseError) => void; +} + /** * SAX-style XML parser. */ @@ -232,20 +250,7 @@ export class SAXParser extends ParserBase implements UnderlyingSink } } - on(event: 'start_document', listener: () => void): this; - on(event: 'processing_instruction', listener: (procInst: string) => void): this; - on(event: 'sgml_declaration', listener: (sgmlDecl: string) => void): this; - on(event: 'text', listener: (text: string, element: ElementInfo, cdata: boolean) => void): this; - on(event: 'doctype', listener: (doctype: string) => void): this; - on(event: 'start_prefix_mapping', listener: (ns: string, uri: string) => void): this; - on(event: 'start_element', listener: (element: ElementInfo) => void): this; - on(event: 'comment', listener: (comment: string) => void): this; - on(event: 'end_element', listener: (element: ElementInfo) => void): this; - on(event: 'end_prefix_mapping', listener: (ns: string, uri: string) => void): this; - on(event: 'end_document', listener: () => void): this; - on(event: 'error', listener: (error: XMLParseError) => void): this; - // deno-lint-ignore no-explicit-any - on(event: string, listener: (...arg: any[]) => void): this { + on(event: K, listener: SAXEvent[K]): this { const list = this._listeners[event] || []; list.push(listener); this._listeners[event] = list;