Skip to content

Commit

Permalink
improved SAXParser#on
Browse files Browse the repository at this point in the history
  • Loading branch information
masataka committed Dec 19, 2020
1 parent 29d2c2c commit 300747b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<K extends keyof SAXEvent>(event: K, listener: SAXEvent[K]): this {}
}
```

You can use "SAXParser" on Deno's stream i/o because this is a simple "UnderlyingSink<Uint8Array>" impl.
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {

export type {
PullResult,
SAXEvent,
} from './parser.ts';

export {
Expand Down
33 changes: 19 additions & 14 deletions parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -232,20 +250,7 @@ export class SAXParser extends ParserBase implements UnderlyingSink<Uint8Array>
}
}

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<K extends keyof SAXEvent>(event: K, listener: SAXEvent[K]): this {
const list = this._listeners[event] || [];
list.push(listener);
this._listeners[event] = list;
Expand Down

0 comments on commit 300747b

Please sign in to comment.